Skip to content

Commit

Permalink
Fix-up nested lists (#154)
Browse files Browse the repository at this point in the history
* Add failing tests

* Handle nesting of lists

* Cleanup list prefix handling
  • Loading branch information
CosmicHorrorDev authored Jul 22, 2023
1 parent 4995a4b commit b3a7f62
Show file tree
Hide file tree
Showing 5 changed files with 247 additions and 14 deletions.
24 changes: 10 additions & 14 deletions src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,24 +504,20 @@ impl TokenSink for HtmlInterpreter {
for element in self.state.element_stack.iter_mut().rev() {
if let html::Element::List(html_list) = element {
list = Some(html_list);
break;
}
}
let list = list.expect("List ended unexpectedly");
if self.current_textbox.texts.is_empty() {
if let html::List {
list_type: html::ListType::Ordered(index),
..
} = list
{
self.state.pending_list_prefix = Some(format!("{}. ", index));
*index += 1;
} else if let html::List {
list_type: html::ListType::Unordered,
..
} = list
{
self.state.pending_list_prefix = Some("· ".to_owned());
}
let prefix = match &mut list.list_type {
html::ListType::Ordered(index) => {
*index += 1;
format!("{}. ", *index - 1)
}
html::ListType::Unordered => "· ".to_owned(),
};

self.state.pending_list_prefix = Some(prefix);
}
}
"ul" => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
source: src/interpreter/tests.rs
description: " --- md\n\n1. 1st outer\n 1. 1st inner\n2. 2nd outer\n\n\n --- html\n\n<ol>\n<li>1st outer\n<ol>\n<li>1st inner</li>\n</ol>\n</li>\n<li>2nd outer</li>\n</ol>\n"
expression: interpret_md(text)
---
[
TextBox(
TextBox {
indent: 50.0,
texts: [
Text {
text: "1. ",
default_color: Color(BLACK),
style: BOLD ,
..
},
Text {
text: "1st outer",
default_color: Color(BLACK),
..
},
Text {
text: " ",
default_color: Color(BLACK),
..
},
],
..
},
),
TextBox(
TextBox {
indent: 100.0,
texts: [
Text {
text: "1. ",
default_color: Color(BLACK),
style: BOLD ,
..
},
Text {
text: "1st inner",
default_color: Color(BLACK),
..
},
],
..
},
),
TextBox(
TextBox {
indent: 50.0,
texts: [
Text {
text: "2. ",
default_color: Color(BLACK),
style: BOLD ,
..
},
Text {
text: "2nd outer",
default_color: Color(BLACK),
..
},
],
..
},
),
Spacer(
InvisibleSpacer(5),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
source: src/interpreter/tests.rs
description: " --- md\n\n- bullet\n 1. 1st inner\n- bullet\n\n\n --- html\n\n<ul>\n<li>bullet\n<ol>\n<li>1st inner</li>\n</ol>\n</li>\n<li>bullet</li>\n</ul>\n"
expression: interpret_md(text)
---
[
TextBox(
TextBox {
indent: 50.0,
texts: [
Text {
text: "· ",
default_color: Color(BLACK),
style: BOLD ,
..
},
Text {
text: "bullet",
default_color: Color(BLACK),
..
},
Text {
text: " ",
default_color: Color(BLACK),
..
},
],
..
},
),
TextBox(
TextBox {
indent: 100.0,
texts: [
Text {
text: "1. ",
default_color: Color(BLACK),
style: BOLD ,
..
},
Text {
text: "1st inner",
default_color: Color(BLACK),
..
},
],
..
},
),
TextBox(
TextBox {
indent: 50.0,
texts: [
Text {
text: "· ",
default_color: Color(BLACK),
style: BOLD ,
..
},
Text {
text: "bullet",
default_color: Color(BLACK),
..
},
],
..
},
),
Spacer(
InvisibleSpacer(5),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
source: src/interpreter/tests.rs
description: " --- md\n\n1. 1st outer\n - bullet\n2. 2nd outer\n\n\n --- html\n\n<ol>\n<li>1st outer\n<ul>\n<li>bullet</li>\n</ul>\n</li>\n<li>2nd outer</li>\n</ol>\n"
expression: interpret_md(text)
---
[
TextBox(
TextBox {
indent: 50.0,
texts: [
Text {
text: "1. ",
default_color: Color(BLACK),
style: BOLD ,
..
},
Text {
text: "1st outer",
default_color: Color(BLACK),
..
},
Text {
text: " ",
default_color: Color(BLACK),
..
},
],
..
},
),
TextBox(
TextBox {
indent: 100.0,
texts: [
Text {
text: "· ",
default_color: Color(BLACK),
style: BOLD ,
..
},
Text {
text: "bullet",
default_color: Color(BLACK),
..
},
],
..
},
),
TextBox(
TextBox {
indent: 50.0,
texts: [
Text {
text: "2. ",
default_color: Color(BLACK),
style: BOLD ,
..
},
Text {
text: "2nd outer",
default_color: Color(BLACK),
..
},
],
..
},
),
Spacer(
InvisibleSpacer(5),
),
]
21 changes: 21 additions & 0 deletions src/interpreter/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,34 @@ let v = 1;
```
";

const UNORDERED_LIST_IN_ORDERED: &str = "\
1. 1st outer
- bullet
2. 2nd outer
";

const NESTED_ORDERED_LIST: &str = "\
1. 1st outer
1. 1st inner
2. 2nd outer
";

const ORDERED_LIST_IN_UNORDERED: &str = "\
- bullet
1. 1st inner
- bullet
";

snapshot_interpreted_elements!(
(footnotes_list_prefix, FOOTNOTES_LIST_PREFIX),
(checklist_has_no_text_prefix, CHECKLIST_HAS_NO_TEXT_PREFIX),
(code_block_bg_color, CODE_BLOCK_BG_COLOR),
(bare_link_gets_autolinked, BARE_LINK_GETS_AUTOLINKED),
(toml_gets_highlighted, TOML_GETS_HIGHLIGHTED),
(handles_comma_in_info_str, HANDLES_COMMA_IN_INFO_STR),
(unordered_list_in_ordered, UNORDERED_LIST_IN_ORDERED),
(nested_ordered_list, NESTED_ORDERED_LIST),
(ordered_list_in_unordered, ORDERED_LIST_IN_UNORDERED),
);

/// Spin up a server, so we can test network requests without external services
Expand Down

0 comments on commit b3a7f62

Please sign in to comment.