changeEncoding <- function(File, # 編集すべき *.html ファイル encoding = "euc-jp", # *.html のエンコーディング fleqn = TRUE, # LaTeX と同じように数式を若干左に寄せる skip = TRUE # ~ タグを有効にする ) { stopifnot(grepl(".html", File)) grafile <- sub(".html", "", File) con <- file(File, open="r", encoding="utf-8") str <- readLines(con) close(con) con <- file(File, open="w", encoding=encoding) n <- length(str) header <- TRUE no <- 0 section <- subsection <- subsubsection <- subsubsubsection <- fig.no <- tab.no <- eq.no <- 0 i <- 0 repeat { i <- i+1 if (i > n) break s <- str[i] if (grepl("^

", s) && header) { # title s <- sub("^

", "

", s) s <- sub("

$", "

", s) mod <- sprintf('
Last modified: %s
', sub(" ..:..:..", ",", sub("^....", "", date()))) s <- sprintf("%s\n%s\n", s, mod) } else if (grepl("<(skip|SKIP|Skip)>", s) && skip) { repeat { i <- i+1 if (i > n) { stop(" is missing.") } else { s <- str[i] if (grepl("", s)) break } } next } else if (grepl("^", s)) { #

section,

subsection,

subsubsection,

subsubsubsection ...? if (grepl("^

", s)) { section <- section+1 subsection <- subsubsection <- fig.no <- tab.no <- eq.no <- 0 } else if (grepl("^

", s)) { subsection <- subsection+1 subsubsection <- 0 } else if (grepl("^

", s)) { subsubsection <- subsubsection+1 subsubsubsection <- 0 } else if (grepl("^

", s)) { subsubsubsection <- subsubsubsection+1 } if (subsection == 0) { num <- sprintf(">%i ", section) } else if (subsubsection == 0) { num <- sprintf(">%i.%i ", section, subsection) } else if (subsubsubsection == 0) { num <- sprintf(">%i.%i.%i ", section, subsection ,subsubsection) } else { num <- sprintf(">%i.%i.%i.%i ", section, subsection, subsubsection, subsubsubsection) } s <- sub(">", num, s) s <- sub("^") { # tab s <- "" } else if (grepl("font-family: sans-serif;", s)) { # font s <- sub("sans-", "", s) } else if (grepl("padding-left: 1em;", s)) { # blockquote s <- sub("1em", "3em", s) } else if (grepl("page-break-before: always;", s)) { # hr s <- "" } else if (grepl("height: 0px;", s)) { # hr s <- sub("0px", "3px", s) } else if (grepl("^

", grafile, no), s) } else if (grepl("図 %i ", fig.no), s) } else { s <- sub('図 %i.%i ", section, fig.no), s) } s <- sub('">', "



", s) } # else if (grepl("^", s)) { # break after end of table s <- sub("", "

", s) } else if (grepl("^<(CAPTION ALIGN|caption align)=(\"top\"|'top')>", s)) { # caption of table tab.no <- tab.no+1 if (section == 0) { s <- sub("^<(CAPTION ALIGN|caption align)=(\"top\"|'top')>", sprintf('表 %i ', tab.no), s) } else { s <- sub("^<(CAPTION ALIGN|caption align)=(\"top\"|'top')>", sprintf('表 %i.%i ', section, tab.no), s) } } else if (grepl("^(

)*%", s)) { # remarks s <- sub("^(

)*", "", s) } else if (grepl("\\\\tag\\{[0-9]+\\}", s)) { # \tag{n} as eqation number eq.no <- eq.no+1 if (section == 0) { s <- sub("\\\\tag\\{[0-9]+\\}", sprintf("\\\\tag{%i}", eq.no), s) } else { s <- sub("\\\\tag\\{[0-9]+\\}", sprintf("\\\\tag{%i.%i}", section, eq.no), s) } } else if (grepl("MathJax scripts", s) && fleqn == TRUE) { # equations to align left s <- paste(s, '', sep="\n") } # if (grepl("^

\n", s, "\n", file=con)
         }
         else if (s == "
") { cat("\n\n", file=con) } else if (s == " font-size: 12px;") { # body. td cat(s, "\n line-height: 150%;\n", file=con) } else if (s != " border-left: 0.5em #EEE solid;" && # blockquote s != " border-bottom: none;" && # hr s != " border-top-width: thin;" && # hr s != " border-top-style: dotted;" && # hr s != " border-top-color: #999999;" # hr ) { cat(s, "\n", file=con) } } close(con) }