Skip to content

Commit 86621a7

Browse files
committed
Correct complex list item support in AsciiDoc-to-Markdown conversion
Support for following cases are added: - newlines between list items - list item with multiline principal text with or without indent - list item immediately after a list continuation paragraph without empty lines in between
1 parent 97ec0ea commit 86621a7

File tree

1 file changed

+57
-11
lines changed

1 file changed

+57
-11
lines changed

xtask/src/publish/notes.rs

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl<'a, 'b, R: BufRead> Converter<'a, 'b, R> {
4242
} else if line.starts_with(VIDEO_BLOCK_PREFIX) {
4343
self.process_video_block(None, 0)?;
4444
} else {
45-
self.process_paragraph(0)?;
45+
self.process_paragraph(0, |line| line.is_empty())?;
4646
}
4747

4848
self.skip_blank_lines()?;
@@ -84,16 +84,19 @@ impl<'a, 'b, R: BufRead> Converter<'a, 'b, R> {
8484

8585
fn process_list(&mut self) -> anyhow::Result<()> {
8686
let mut nesting = ListNesting::new();
87-
while let Some(line) = self.iter.next() {
88-
let line = line?;
89-
if line.is_empty() {
90-
break;
91-
}
87+
while let Some(line) = self.iter.peek() {
88+
let line = line.as_deref().map_err(|e| anyhow!("{e}"))?;
9289

93-
if let Some((marker, item)) = get_list_item(&line) {
90+
if get_list_item(&line).is_some() {
91+
let line = self.iter.next().unwrap()?;
92+
let (marker, item) = get_list_item(&line).unwrap();
9493
nesting.set_current(marker);
9594
self.write_list_item(item, &nesting);
95+
self.process_paragraph(nesting.indent(), |line| {
96+
line.is_empty() || get_list_item(line).is_some() || line == "+"
97+
})?;
9698
} else if line == "+" {
99+
let _ = self.iter.next().unwrap()?;
97100
let line = self
98101
.iter
99102
.peek()
@@ -118,11 +121,17 @@ impl<'a, 'b, R: BufRead> Converter<'a, 'b, R> {
118121
self.process_video_block(None, indent)?;
119122
} else {
120123
self.write_line("", 0);
121-
self.process_paragraph(indent)?;
124+
let current = nesting.current().unwrap();
125+
self.process_paragraph(indent, |line| {
126+
line.is_empty()
127+
|| get_list_item(line).filter(|(m, _)| m == current).is_some()
128+
|| line == "+"
129+
})?;
122130
}
123131
} else {
124-
bail!("not a list block")
132+
break;
125133
}
134+
self.skip_blank_lines()?;
126135
}
127136

128137
Ok(())
@@ -235,15 +244,19 @@ impl<'a, 'b, R: BufRead> Converter<'a, 'b, R> {
235244
bail!("not a video block")
236245
}
237246

238-
fn process_paragraph(&mut self, level: usize) -> anyhow::Result<()> {
247+
fn process_paragraph<P>(&mut self, level: usize, predicate: P) -> anyhow::Result<()>
248+
where
249+
P: Fn(&str) -> bool,
250+
{
239251
while let Some(line) = self.iter.peek() {
240252
let line = line.as_deref().map_err(|e| anyhow!("{e}"))?;
241-
if line.is_empty() || (level > 0 && line == "+") {
253+
if predicate(&line) {
242254
break;
243255
}
244256

245257
self.write_indent(level);
246258
let line = self.iter.next().unwrap()?;
259+
let line = line.trim_start();
247260
if line.ends_with('+') {
248261
let line = &line[..(line.len() - 1)];
249262
self.output.push_str(line);
@@ -373,6 +386,10 @@ impl ListNesting {
373386
Self(Vec::<ListMarker>::with_capacity(6))
374387
}
375388

389+
fn current(&mut self) -> Option<&ListMarker> {
390+
self.0.last()
391+
}
392+
376393
fn set_current(&mut self, marker: ListMarker) {
377394
let Self(markers) = self;
378395
if let Some(index) = markers.iter().position(|m| *m == marker) {
@@ -469,6 +486,22 @@ rustup update nightly
469486
----
470487
This is a plain listing.
471488
----
489+
* single line item followed by empty lines
490+
491+
* multiline list
492+
item followed by empty lines
493+
494+
* multiline list
495+
item with indent
496+
497+
* multiline list
498+
item not followed by empty lines
499+
* multiline list
500+
item followed by different marker
501+
** foo
502+
** bar
503+
* multiline list
504+
item followed by list continuation
472505
+
473506
paragraph
474507
paragraph
@@ -539,6 +572,19 @@ Release: release:2022-01-01[]
539572
```
540573
This is a plain listing.
541574
```
575+
- single line item followed by empty lines
576+
- multiline list
577+
item followed by empty lines
578+
- multiline list
579+
item with indent
580+
- multiline list
581+
item not followed by empty lines
582+
- multiline list
583+
item followed by different marker
584+
- foo
585+
- bar
586+
- multiline list
587+
item followed by list continuation
542588
543589
paragraph
544590
paragraph

0 commit comments

Comments
 (0)