Skip to content

Commit

Permalink
Add definition lists
Browse files Browse the repository at this point in the history
  • Loading branch information
lampsitter committed Aug 24, 2024
1 parent d3d32de commit 6b86fe9
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 39 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

## Added

- Definition lists

## Changed

- Updated pulldown-cmark to 0.12
Expand Down
8 changes: 6 additions & 2 deletions egui_commonmark/examples/book.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,20 @@ fn main() -> eframe::Result {
name: "Lists".to_owned(),
content: include_str!("markdown/lists.md").to_owned(),
},
Page {
name: "Definition lists".to_owned(),
content: include_str!("markdown/definition_list.md").to_owned(),
},
Page {
name: "Code blocks".to_owned(),
content: include_str!("markdown/code-blocks.md").to_owned(),
},
Page {
name: "Block Quotes ".to_owned(),
name: "Block Quotes".to_owned(),
content: include_str!("markdown/blockquotes.md").to_owned(),
},
Page {
name: "Tables ".to_owned(),
name: "Tables".to_owned(),
content: include_str!("markdown/tables.md").to_owned(),
},
],
Expand Down
9 changes: 9 additions & 0 deletions egui_commonmark/examples/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ impl eframe::App for App {
&mut self.cache,
"egui_commonmark/examples/markdown/tables.md"
);
commonmark!("n12", ui, &mut self.cache, "------------");

commonmark_str!(
"n13",
ui,
&mut self.cache,
"egui_commonmark/examples/markdown/definition_list.md"
);
commonmark!("n14", ui, &mut self.cache, "------------");
});
});
}
Expand Down
17 changes: 17 additions & 0 deletions egui_commonmark/examples/markdown/definition_list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Definition list

**Term 1**

: Definition 1

**Term 2**

: Definition 2

```rs
let x = 3
```

Third paragraph of definition 2.

An easy way to have indentation without the bullet points of lists
64 changes: 38 additions & 26 deletions egui_commonmark/examples/mixing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,56 +16,58 @@ macro_rules! m {
};
}

#[cfg(feature = "macros")]
const WINDOW_NAME: &str = "Mixed egui and markdown (macro version)";
#[cfg(not(feature = "macros"))]
const WINDOW_NAME: &str = "Mixed egui and markdown (normal version)";

// This is more of an test...
// Ensure that there are no newlines that should not be present when mixing markdown
// and egui widgets.
fn main() -> eframe::Result<()> {
let mut cache = egui_commonmark::CommonMarkCache::default();

eframe::run_simple_native(
"Mixed egui and markdown",
Default::default(),
move |ctx, _frame| {
egui::CentralPanel::default().show(ctx, |ui| {
egui::ScrollArea::vertical().show(ui, |ui| {
m!(
ui,
cache,
"Markdown *a*",
"# Markdown (Deliberate space above)",
"--------------------",
r#"
eframe::run_simple_native(WINDOW_NAME, Default::default(), move |ctx, _frame| {
egui::CentralPanel::default().show(ctx, |ui| {
egui::ScrollArea::vertical().show(ui, |ui| {
m!(
ui,
cache,
"Markdown *a*",
"# Markdown (Deliberate space above)",
"--------------------",
r#"
- Simple list 1
- Simple list 2
"#,
r#"
r#"
1. aaa
2. aaa
- abb
- acc
3. bbb
- baa
"#,
r#"
r#"
```rust
let x = 3;
```
"#,
r#"
r#"
A footnote [^F1]
[^F1]: The footnote"#,
r#"
r#"
>
> Test
>
"#,
r#"
r#"
> [!TIP]
>
> Test
"#,
r#"
r#"
Column A | Column B
-----------|----------
Expand All @@ -75,17 +77,27 @@ item a3 | item b3
item a4 | item b4
"#,
r#"
r#"
![Rust logo](egui_commonmark/examples/rust-logo-128x128.png)
"#,
r#"
r#"
[Link to repo](https://github.com/lampsitter/egui_commonmark)
"#,
);
r#"
Term 1
: Definition 1
Term 2
: Definition 2
Paragraph 2
"#,
);

ui.label("Label!");
});
ui.label("Label!");
});
},
)
});
})
}
80 changes: 75 additions & 5 deletions egui_commonmark/src/parsers/pulldown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pub struct CommonMarkViewerInternal {
line: Newline,
fenced_code_block: Option<FencedCodeBlock>,
is_list_item: bool,
is_def_list_def: bool,
is_table: bool,
is_blockquote: bool,
checkbox_events: Vec<CheckboxClickEvent>,
Expand All @@ -89,6 +90,7 @@ impl CommonMarkViewerInternal {
image: None,
line: Newline::default(),
is_list_item: false,
is_def_list_def: false,
fenced_code_block: None,
is_table: false,
is_blockquote: false,
Expand Down Expand Up @@ -265,11 +267,73 @@ impl CommonMarkViewerInternal {
) {
self.event(ui, event, src_span, cache, options, max_width);

self.def_list_def_wrapping(events, max_width, cache, options, ui);
self.item_list_wrapping(events, max_width, cache, options, ui);
self.table(events, cache, options, ui, max_width);
self.blockquote(events, max_width, cache, options, ui);
}

fn def_list_def_wrapping<'e>(
&mut self,
events: &mut Peekable<impl Iterator<Item = EventIteratorItem<'e>>>,
max_width: f32,
cache: &mut CommonMarkCache,
options: &CommonMarkOptions,
ui: &mut Ui,
) {
if self.is_def_list_def {
self.is_def_list_def = false;

let item_events = delayed_events(events, |tag| {
matches!(tag, pulldown_cmark::TagEnd::DefinitionListDefinition)
});

let mut events_iter = item_events.into_iter().enumerate().peekable();

self.line.try_insert_start(ui);

// Proccess a single event separately so that we do not insert spaces where we do not
// want them
self.line.should_start_newline = false;
if let Some((_, (e, src_span))) = events_iter.next() {
self.process_event(ui, &mut events_iter, e, src_span, cache, options, max_width);
}

ui.label(" ".repeat(options.indentation_spaces));
self.line.should_start_newline = true;
self.line.should_end_newline = false;
// Required to ensure that the content is aligned with the identation
ui.horizontal_wrapped(|ui| {
while let Some((_, (e, src_span))) = events_iter.next() {
self.process_event(
ui,
&mut events_iter,
e,
src_span,
cache,
options,
max_width,
);
}
});
self.line.should_end_newline = true;

// Only end the definition items line if it is not the last element in the list
if !matches!(
events.peek(),
Some((
_,
(
pulldown_cmark::Event::End(pulldown_cmark::TagEnd::DefinitionList),
_
)
))
) {
self.line.try_insert_end(ui);
}
}
}

fn item_list_wrapping<'e>(
&mut self,
events: &mut impl Iterator<Item = EventIteratorItem<'e>>,
Expand Down Expand Up @@ -564,9 +628,14 @@ impl CommonMarkViewerInternal {
}
pulldown_cmark::Tag::HtmlBlock => {}
pulldown_cmark::Tag::MetadataBlock(_) => {}
pulldown_cmark::Tag::DefinitionList
| pulldown_cmark::Tag::DefinitionListTitle
| pulldown_cmark::Tag::DefinitionListDefinition => {}

pulldown_cmark::Tag::DefinitionList => {
self.line.try_insert_start(ui);
}
pulldown_cmark::Tag::DefinitionListTitle => {}
pulldown_cmark::Tag::DefinitionListDefinition => {
self.is_def_list_def = true;
}
}
}

Expand Down Expand Up @@ -630,8 +699,9 @@ impl CommonMarkViewerInternal {
}
pulldown_cmark::TagEnd::HtmlBlock => {}
pulldown_cmark::TagEnd::MetadataBlock(_) => {}
pulldown_cmark::TagEnd::DefinitionList
| pulldown_cmark::TagEnd::DefinitionListTitle

pulldown_cmark::TagEnd::DefinitionList => self.line.try_insert_end(ui),
pulldown_cmark::TagEnd::DefinitionListTitle
| pulldown_cmark::TagEnd::DefinitionListDefinition => {}
}
}
Expand Down
1 change: 1 addition & 0 deletions egui_commonmark_backend/src/pulldown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,5 @@ pub fn parser_options() -> Options {
| Options::ENABLE_TASKLISTS
| Options::ENABLE_STRIKETHROUGH
| Options::ENABLE_FOOTNOTES
| Options::ENABLE_DEFINITION_LIST
}
Loading

0 comments on commit 6b86fe9

Please sign in to comment.