Skip to content

Commit 71d6316

Browse files
author
deepayan
committed
fix nesting bug in HTML toc (partly reported in PR#18961)
git-svn-id: https://svn.r-project.org/R/trunk@89104 00db46b3-68df-0310-9c12-caf00c1e9a41
1 parent 679f14e commit 71d6316

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

src/library/tools/R/Rd2HTML.R

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,8 @@ Rd2HTML <-
611611
list(argitem =
612612
list(id = item_id,
613613
value = sprintf("<code>%s</code>",
614-
item_value))))
614+
item_value),
615+
sectionLevel = NULL)))
615616
sprintf('<code id="%s">%s</code>', item_id, item_value)
616617
}
617618
else sprintf('<code>%s</code>', item_value)
@@ -1180,7 +1181,8 @@ Rd2HTML <-
11801181
sec_value <- paste0("<p>", sectionTitles[tag], "</p>")
11811182
sec_id <- tag2id(tag = tag, name = if (standalone) NULL else name)
11821183
}
1183-
toc_entry <- list(id = trimws(sec_id), value = trimws(sec_value))
1184+
toc_entry <- list(id = trimws(sec_id), value = trimws(sec_value),
1185+
sectionLevel = sectionLevel)
11841186
toc_entries <<-
11851187
c(toc_entries,
11861188
if (tag == "\\subsection") list(subsection = toc_entry)
@@ -1236,19 +1238,40 @@ Rd2HTML <-
12361238
'<h1>Contents</h1>\n',
12371239
'<ul class="menu">\n')
12381240

1239-
currentLevel <- 1L # entry_types = argitem, subsection are level 2
1240-
## toc_entries <- list( section|subsection|argitem = list(id, value) )
12411241
entry_types <- names(toc_entries)
1242+
1243+
previous_level <- 1 # initial value, beginning of TOC
1244+
previous_entry <- NULL
1245+
last_section_level <- NULL
1246+
## toc_entries <- list( section|subsection|argitem = list(id, value, sectionLevel) )
12421247
for (i in seq_along(toc_entries)) {
1243-
newLevel <-
1244-
if (entry_types[[i]] %in% c("argitem", "subsection")) 2L
1245-
else 1L
1246-
if (newLevel > currentLevel) of1(" <ul>")
1247-
else if (newLevel < currentLevel) of1(" </ul>")
1248-
currentLevel <- newLevel
12491248
e <- toc_entries[[i]] # id, value can be vectors
1249+
## section-level is recorded in e$sectionLevel for
1250+
## sections and subsections. argitems will have
1251+
## sectionLevel = NULL, which need to be interpreted as
1252+
## one level more than the section in which it is nested.
1253+
if (!is.null(e$sectionLevel)) { # section|subsection
1254+
current_level <- e$sectionLevel
1255+
last_section_level <- current_level
1256+
}
1257+
else if (!is.null(last_section_level)) { # argitem
1258+
current_level <- last_section_level + 1
1259+
}
1260+
else stop("Invalid value of 'toc_entries'")
1261+
jump_level <- current_level - previous_level
1262+
## Positive jump values should be exactly 1
1263+
if (jump_level > 1) warning("Unexpected jump in section level")
1264+
if (jump_level > 0) replicate(jump_level, of1("<li><ul>\n")) # see NOTE below
1265+
else if (jump_level < 0) replicate(-jump_level, of1("</ul></li>\n"))
12501266
of0(sprintf("<li><a href='#%s'>%s</a></li>\n", e$id, e$value))
1267+
previous_level <- current_level
12511268
}
1269+
## We may end up at currentLevel > 1. Add closing tags in that case.
1270+
if (current_level > 1) replicate(current_level - 1, of1("</ul></li>\n"))
1271+
## NOTE: Ideally the nested second-level <ul>-s should start
1272+
## _within_ the parent <li>, but that will require us to look
1273+
## forward. We will not do this (to keep the code simple), but
1274+
## this may be something to revisit at some point.
12521275

12531276
of0('</ul>\n',
12541277
'</div>\n',

0 commit comments

Comments
 (0)