@@ -166,12 +166,15 @@ function(package, dir, lib.loc = NULL,
166
166
file <- basename(file )
167
167
name <- vigns $ names [i ]
168
168
engine <- vignetteEngine(vigns $ engines [i ])
169
-
169
+ enc <- vigns $ encodings [i ]
170
+ if (enc == " non-ASCII" )
171
+ stop(gettextf(" Vignette '%s' is non-ASCII but has no declared encoding" , name ),
172
+ domain = NA )
170
173
if (tangle ) {
171
174
message(" Running " , sQuote(file ))
172
175
.eval_with_capture({
173
176
result $ tangle [[file ]] <- tryCatch({
174
- engine $ tangle(file , quiet = TRUE )
177
+ engine $ tangle(file , quiet = TRUE , encoding = enc )
175
178
setwd(startdir ) # in case a vignette changes the working dir
176
179
find_vignette_product(name , by = " tangle" , main = FALSE , engine = engine )
177
180
}, error = function (e ) e )
@@ -181,7 +184,7 @@ function(package, dir, lib.loc = NULL,
181
184
setwd(startdir ) # in case a vignette changes the working dir then errored out
182
185
.eval_with_capture({
183
186
result $ weave [[file ]] <- tryCatch({
184
- engine $ weave(file , quiet = TRUE )
187
+ engine $ weave(file , quiet = TRUE , encoding = enc )
185
188
setwd(startdir )
186
189
find_vignette_product(name , by = " weave" , engine = engine )
187
190
}, error = function (e ) e )
@@ -413,8 +416,11 @@ function(package, dir, subdirs = NULL, lib.loc = NULL, output = FALSE,
413
416
stopifnot(length(names ) == length(docs ),
414
417
length(engines ) == length(docs ),
415
418
length(patterns ) == length(docs ), ! anyDuplicated(docs ))
416
-
417
- z <- list (docs = docs , names = names , engines = engines , patterns = patterns ,
419
+
420
+ defaultEncoding <- .get_package_metadata(dir )[" Encoding" ]
421
+ encodings <- vapply(docs , getVignetteEncoding , " " , default = defaultEncoding )
422
+
423
+ z <- list (docs = docs , names = names , engines = engines , patterns = patterns , encodings = encodings ,
418
424
dir = docdir , pkgdir = dir , msg = msg )
419
425
420
426
if (output ) {
@@ -501,10 +507,14 @@ buildVignettes <-
501
507
file <- basename(vigns $ docs [i ])
502
508
name <- vigns $ names [i ]
503
509
engine <- vignetteEngine(vigns $ engine [i ])
504
-
510
+ enc <- vigns $ encodings [i ]
511
+ if (enc == " non-ASCII" )
512
+ stop(gettextf(" Vignette '%s' is non-ASCII but has no declared encoding" ,
513
+ file ), domain = NA , call. = FALSE )
514
+
505
515
output <- tryCatch({
506
516
# # FIXME: run this in a separate process
507
- engine $ weave(file , quiet = quiet )
517
+ engine $ weave(file , quiet = quiet , encoding = enc )
508
518
setwd(startdir )
509
519
find_vignette_product(name , by = " weave" , engine = engine )
510
520
}, error = function (e ) {
@@ -522,7 +532,7 @@ buildVignettes <-
522
532
if (tangle ) { # This is set for all engines as of 3.0.2
523
533
output <- tryCatch({
524
534
# # FIXME: run this in a separate process
525
- engine $ tangle(file , quiet = quiet )
535
+ engine $ tangle(file , quiet = quiet , encoding = enc )
526
536
setwd(startdir )
527
537
find_vignette_product(name , by = " tangle" , main = FALSE , engine = engine )
528
538
}, error = function (e ) {
@@ -586,7 +596,8 @@ buildVignettes <-
586
596
buildVignette <-
587
597
function (file , dir = " ." , weave = TRUE , latex = TRUE , tangle = TRUE ,
588
598
quiet = TRUE , clean = TRUE , keep = character (),
589
- engine = NULL , buildPkg = NULL , ... )
599
+ engine = NULL , buildPkg = NULL ,
600
+ encoding = getVignetteEncoding(file ), ... )
590
601
{
591
602
if (! file_test(" -f" , file ))
592
603
stop(gettextf(" file '%s' not found" , file ), domain = NA )
@@ -615,6 +626,9 @@ buildVignette <-
615
626
file , paste(engine $ package , engine $ name , sep = " ::" )),
616
627
domain = NA )
617
628
629
+ if (encoding == " non-ASCII" )
630
+ stop(gettextf(" Vignette '%s' is non-ASCII but has no declared encoding" , name ))
631
+
618
632
# Set output directory temporarily
619
633
file <- file_path_as_absolute(file )
620
634
olddir <- setwd(dir )
@@ -631,7 +645,7 @@ buildVignette <-
631
645
632
646
# Weave
633
647
final <- if (weave ) {
634
- engine $ weave(file , quiet = quiet , ... )
648
+ engine $ weave(file , quiet = quiet , encoding = encoding , ... )
635
649
setwd(tdir ) # In case weave/vignette changed it
636
650
output <- find_vignette_product(name , by = " weave" , engine = engine )
637
651
@@ -645,7 +659,7 @@ buildVignette <-
645
659
646
660
# Tangle
647
661
sources <- if (tangle ) {
648
- engine $ tangle(file , quiet = quiet , ... )
662
+ engine $ tangle(file , quiet = quiet , encoding = encoding , ... )
649
663
setwd(tdir ) # In case tangle changed it
650
664
find_vignette_product(name , by = " tangle" , main = FALSE , engine = engine )
651
665
} # else NULL
@@ -675,72 +689,70 @@ buildVignette <-
675
689
unique(keep )
676
690
}
677
691
678
- # ## * . getVignetteEncoding
692
+ # ## * getVignetteEncoding
679
693
680
694
getVignetteEncoding <- function (file , ... )
681
695
{
682
- # # Look for inputen[cx] first, then %\SweaveUTF8. Complain about
683
- # # inconsistencies.
684
-
685
696
lines <- readLines(file , warn = FALSE )
686
- result1 <- .getVignetteEncoding(lines , ... )
687
-
688
- poss <- grep(" ^[[:space:]]*%+[[:space:]]*\\\\ SweaveUTF8[[:space:]]*$" , lines , useBytes = TRUE )
689
- if (length(poss )) {
690
- result <- " UTF-8"
691
- if (! (result1 %in% c(" " , " non-ASCII" , " UTF-8" )))
692
- stop(gettextf(" Inconsistent encoding specifications: %s with %%\\ SweaveUTF8" , result1 ), domain = NA )
693
- } else
694
- result <- result1
695
- result
697
+ .getVignetteEncoding(lines , ... )
696
698
}
697
699
698
- .getVignetteEncoding <- function (lines , convert = FALSE )
700
+ .getVignetteEncoding <- function (lines , default = NA )
699
701
{
700
702
res <- .get_vignette_metadata(lines , " Encoding" )[1L ]
701
703
702
704
if (is.na(res )) {
703
- # # Look for input enc lines using inputenc or inputenx
704
- # # Note, multiple encodings are excluded.
705
- poss <-
706
- grep(" ^[[:space:]]*\\\\ usepackage\\ [([[:alnum:]]+)\\ ]\\ {inputen[cx]\\ }" ,
707
- lines , useBytes = TRUE )
708
- # # Check it is in the preamble
709
- start <- grep(" ^[[:space:]]*\\\\ begin\\ {document\\ }" ,
710
- lines , useBytes = TRUE )
711
- if (length(start )) poss <- poss [poss < start [1L ]]
712
- if (! length(poss )) {
713
- asc <- iconv(lines , " latin1" , " ASCII" )
714
- ind <- is.na(asc ) | asc != lines
715
- if (any(ind )) return (" non-ASCII" )
716
- return (" " ) # or "ASCII"
705
+ poss <- grep(" ^[[:space:]]*%+[[:space:]]*\\\\ SweaveUTF8[[:space:]]*$" , lines , useBytes = TRUE )
706
+ if (length(poss ))
707
+ res <- " UTF-8"
708
+ else {
709
+ # # Look for input enc lines using inputenc or inputenx
710
+ # # Note, multiple encodings are excluded.
711
+ poss <-
712
+ grep(" ^[[:space:]]*\\\\ usepackage\\ [([[:alnum:]]+)\\ ]\\ {inputen[cx]\\ }" ,
713
+ lines , useBytes = TRUE )
714
+ # # Check it is in the preamble
715
+ start <- grep(" ^[[:space:]]*\\\\ begin\\ {document\\ }" ,
716
+ lines , useBytes = TRUE )
717
+ if (length(start ))
718
+ poss <- poss [poss < start [1L ]]
719
+ if (length(poss )) {
720
+ poss <- lines [poss [1L ]]
721
+ res <- gsub(" ^[[:space:]]*\\\\ usepackage\\ [([[:alnum:]]+)\\ ].*" , " \\ 1" ,
722
+ poss ) # This line should be ASCII.
723
+ # # see Rd2latex.R.
724
+ # # Currently utf8, utf8x, latin1, latin9 and ansinew are in use.
725
+ res <- switch (res ,
726
+ " utf8" = , " utf8x" = " UTF-8" ,
727
+ " latin1" = , " iso-8859-1" = " latin1" ,
728
+ " latin2" = , " iso-8859-2" = " latin2" ,
729
+ " latin9" = , " iso-8859-15" = " latin-9" , # only form known to GNU libiconv
730
+ " latin10" = , " iso-8859-16" = " latin10" ,
731
+ " cyrillic" = , " iso-8859-5" = " ISO-8859-5" , # inputenx
732
+ " koi8-r" = " KOI8-R" , # inputenx
733
+ " arabic" = " ISO-8859-6" , # Not clear next 3 are known to latex
734
+ " greek" = , " iso-8859-7" = " ISO-8859-7" ,
735
+ " hebrew" = , " iso-8859-8" = " ISO-8859-8" ,
736
+ " ansinew" = " CP1252" ,
737
+ " applemac" = " macroman" ,
738
+ # # assume these only get used on Windows
739
+ " cp1250" = " CP1250" ,
740
+ " cp1252" = " CP1252" ,
741
+ " cp1257" = " CP1257" ,
742
+ " unknown" )
743
+ } else if (! is.na(default )) {
744
+ res <- default
745
+ } else { # Nothing else has indicated an encoding, maybe it's just ASCII
746
+ asc <- iconv(lines , " latin1" , " ASCII" )
747
+ ind <- is.na(asc ) | asc != lines
748
+ if (any(ind ))
749
+ res <- " non-ASCII"
750
+ else
751
+ res <- " " # or "ASCII"
752
+ }
717
753
}
718
- poss <- lines [poss [1L ]]
719
- res <- gsub(" ^[[:space:]]*\\\\ usepackage\\ [([[:alnum:]]+)\\ ].*" , " \\ 1" ,
720
- poss ) # This line should be ASCII.
721
754
}
722
- if (convert ) {
723
- # # see Rd2latex.R.
724
- # # Currently utf8, utf8x, latin1, latin9 and ansinew are in use.
725
- switch (res ,
726
- " utf8" = , " utf8x" = " UTF-8" ,
727
- " latin1" = , " iso-8859-1" = " latin1" ,
728
- " latin2" = , " iso-8859-2" = " latin2" ,
729
- " latin9" = , " iso-8859-15" = " latin-9" , # only form known to GNU libiconv
730
- " latin10" = , " iso-8859-16" = " latin10" ,
731
- " cyrillic" = , " iso-8859-5" = " ISO-8859-5" , # inputenx
732
- " koi8-r" = " KOI8-R" , # inputenx
733
- " arabic" = " ISO-8859-6" , # Not clear next 3 are known to latex
734
- " greek" = , " iso-8859-7" = " ISO-8859-7" ,
735
- " hebrew" = , " iso-8859-8" = " ISO-8859-8" ,
736
- " ansinew" = " CP1252" ,
737
- " applemac" = " macroman" ,
738
- # # assume these only get used on Windows
739
- " cp1250" = " CP1250" ,
740
- " cp1252" = " CP1252" ,
741
- " cp1257" = " CP1257" ,
742
- " unknown" )
743
- } else res
755
+ res
744
756
}
745
757
746
758
# ## * .build_vignette_index
0 commit comments