-
Notifications
You must be signed in to change notification settings - Fork 0
/
foot.R
30 lines (26 loc) · 978 Bytes
/
foot.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
## Execute as Rscript foot.R <filename>
args <- commandArgs()
extras <- grep("--args", args) + 1
filename <- args[extras[1]]
## Find all footnote labels
library(xml2)
xml <- read_xml(filename)
internalAnchor <- xml_find_all(xml, "//a[starts-with(@href, '#')]")
footCount <- 0
footnotes <- xml_new_root("div")
for (i in seq_along(internalAnchor)) {
label <- gsub("^#", "", xml_attr(internalAnchor[i], "href"))
note <- xml_find_first(xml,
paste0("//p[@class = 'footnote' and a[@name = '",
label, "']]"))
if (length(note)) {
footCount <- footCount + 1
xml_add_child(internalAnchor[i], "sup", footCount)
p <- xml_add_child(footnotes, note)
xml_add_child(p, "sup", footCount, .where=0)
}
}
oldfootnotes <- xml_find_first(xml, "//div[preceding-sibling::h2['Footnotes']]")
xml_replace(oldfootnotes, xml_root(footnotes))
## write out modified XML
write_xml(xml, filename)