Skip to content

Commit 80be987

Browse files
author
deepayan
committed
ensure that Rd2HTML() output does not have duplicate ids
git-svn-id: https://svn.r-project.org/R/trunk@89118 00db46b3-68df-0310-9c12-caf00c1e9a41
1 parent b40e17d commit 80be987

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/library/tools/R/Rd2HTML.R

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,11 @@ topic2href <- function(x, destpkg = NULL, hooks = list())
270270

271271
## Note that tagid can be a vector (for comma-separated items)
272272

273-
tag2id <- function(tag, name = NULL, tagid = section2id[tag])
273+
tag2id <- function(tag, name = NULL, tagid = section2id[tag], dedup = NULL)
274274
{
275+
## id-s must not be duplicated within a HTML file. If 'dedup' is
276+
## supplied, we ensure that the id returned is not in it, by
277+
## adding a random suffix
275278
section2id <-
276279
c("\\description" = "_sec_description", "\\usage" = "_sec_usage",
277280
"\\arguments" = "_sec_arguments", "\\format" = "_sec_format",
@@ -283,7 +286,12 @@ tag2id <- function(tag, name = NULL, tagid = section2id[tag])
283286
if (anyNA(tagid)) return(NULL) # or "" ?
284287
id <- if (is.null(name)) tagid
285288
else paste(name2id(name), tagid, sep = "_:_")
286-
string2id(gsub("[[:space:]]+", "-", id))
289+
id <- trimws(string2id(gsub("[[:space:]]+", "-", id)))
290+
## make id unique: but note that id can be a vector (for argument items)
291+
if (!is.null(dedup))
292+
while (any(id %in% dedup))
293+
id <- paste0(id, sample(100:999, 1))
294+
id
287295
}
288296

289297
rdfragment2text <- function(rd, html = TRUE)
@@ -485,6 +493,8 @@ Rd2HTML <-
485493
if (!standalone) toc <- FALSE
486494
else toc_entries <- list()
487495
}
496+
## keep global list of all HTML ids used to ensure no duplicates
497+
id_list <- NULL
488498

489499
skipNewline <- FALSE
490500
linestart <- TRUE
@@ -604,7 +614,8 @@ Rd2HTML <-
604614
s <- s[nzchar(s)] # unlikely to matter, but just to be safe
605615
item_value <- vhtmlify(s)
606616
s <- if (addID) {
607-
item_id <- tag2id(name = if (standalone) NULL else name, tagid = s)
617+
item_id <- tag2id(name = if (standalone) NULL else name, tagid = s, dedup = id_list)
618+
id_list <<- c(id_list, item_id)
608619
if (toc)
609620
toc_entries <<-
610621
c(toc_entries,
@@ -1175,13 +1186,16 @@ Rd2HTML <-
11751186
sec_value <- rdfragment2text(section[[1L]])
11761187
sec_id <-
11771188
tag2id(name = if (standalone) NULL else name,
1178-
tagid = rdfragment2text(section[[1L]], html = FALSE))
1189+
tagid = rdfragment2text(section[[1L]], html = FALSE),
1190+
dedup = id_list)
11791191
}
11801192
else {
11811193
sec_value <- paste0("<p>", sectionTitles[tag], "</p>")
1182-
sec_id <- tag2id(tag = tag, name = if (standalone) NULL else name)
1194+
sec_id <- tag2id(tag = tag, name = if (standalone) NULL else name,
1195+
dedup = id_list)
11831196
}
1184-
toc_entry <- list(id = trimws(sec_id), value = trimws(sec_value),
1197+
id_list <<- c(id_list, sec_id)
1198+
toc_entry <- list(id = sec_id, value = trimws(sec_value),
11851199
sectionLevel = sectionLevel)
11861200
toc_entries <<-
11871201
c(toc_entries,

0 commit comments

Comments
 (0)