Skip to content

Commit d89a6ce

Browse files
committed
rustdoc: properly nest markup within enum variant lists (fixes #6605)
This indents all but the first line of multi-line annotations for individual enum variants with four spaces so that pandoc will recognize everything as belonging to the same list item. Since that introduces `<p>` tags for some list items, I've gone ahead and inserted blank lines after each list item so that consistently get `<p>` tags for all `<li>`s documenting variants. It's a bit less compact now but still tolerable, I think.
1 parent 510d0f2 commit d89a6ce

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

src/librustdoc/markdown_pass.rs

+38-1
Original file line numberDiff line numberDiff line change
@@ -451,16 +451,32 @@ fn write_variants(
451451
fn write_variant(ctxt: &Ctxt, doc: doc::VariantDoc) {
452452
assert!(doc.sig.is_some());
453453
let sig = (&doc.sig).get();
454+
455+
// space out list items so they all end up within paragraph elements
456+
ctxt.w.put_line(~"");
457+
454458
match copy doc.desc {
455459
Some(desc) => {
456-
ctxt.w.put_line(fmt!("* `%s` - %s", sig, desc));
460+
ctxt.w.put_line(list_item_indent(fmt!("* `%s` - %s", sig, desc)));
457461
}
458462
None => {
459463
ctxt.w.put_line(fmt!("* `%s`", sig));
460464
}
461465
}
462466
}
463467

468+
fn list_item_indent(item: &str) -> ~str {
469+
let mut indented = ~[];
470+
for str::each_line_any(item) |line| {
471+
indented.push(line);
472+
}
473+
474+
// separate markdown elements within `*` lists must be indented by four
475+
// spaces, or they will escape the list context. indenting everything
476+
// seems fine though.
477+
str::connect_slices(indented, "\n ")
478+
}
479+
464480
fn write_trait(ctxt: &Ctxt, doc: doc::TraitDoc) {
465481
write_common(ctxt, doc.desc(), doc.sections());
466482
write_methods(ctxt, doc.methods);
@@ -807,7 +823,9 @@ mod test {
807823
assert!(str::contains(
808824
markdown,
809825
"\n\n#### Variants\n\
826+
\n\
810827
\n* `b` - test\
828+
\n\
811829
\n* `c` - test\n\n"));
812830
}
813831

@@ -817,7 +835,24 @@ mod test {
817835
assert!(str::contains(
818836
markdown,
819837
"\n\n#### Variants\n\
838+
\n\
820839
\n* `b`\
840+
\n\
841+
\n* `c`\n\n"));
842+
}
843+
844+
#[test]
845+
fn should_write_variant_list_with_indent() {
846+
let markdown = render(
847+
~"enum a { #[doc = \"line 1\\n\\nline 2\"] b, c }");
848+
assert!(str::contains(
849+
markdown,
850+
"\n\n#### Variants\n\
851+
\n\
852+
\n* `b` - line 1\
853+
\n \
854+
\n line 2\
855+
\n\
821856
\n* `c`\n\n"));
822857
}
823858

@@ -827,7 +862,9 @@ mod test {
827862
assert!(str::contains(
828863
markdown,
829864
"\n\n#### Variants\n\
865+
\n\
830866
\n* `b(int)`\
867+
\n\
831868
\n* `c(int)` - a\n\n"));
832869
}
833870

0 commit comments

Comments
 (0)