Skip to content

Commit

Permalink
update and adapt to new pulldown cmark
Browse files Browse the repository at this point in the history
  • Loading branch information
JR-1991 committed Dec 19, 2024
1 parent 7196fd0 commit 9116d13
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
13 changes: 10 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ crate-type = ["cdylib", "rlib"]

[dependencies]
serde = { version = "1.0.198", features = ["derive"] }
pulldown-cmark = "0.8.0"
pulldown-cmark = "0.12.2"
serde_json = { "version" = "1.0.116", features = ["preserve_order"] }
regex = "1.10.4"
serde_with = "3.8.0"
Expand Down
36 changes: 29 additions & 7 deletions src/markdown/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use log::error;
use std::collections::BTreeMap;
use std::error::Error;

use pulldown_cmark::{CowStr, Event, Parser, Tag};
use pulldown_cmark::{CowStr, Event, HeadingLevel, Parser, Tag, TagEnd};
use regex::Regex;

use crate::attribute;
Expand All @@ -53,6 +53,28 @@ lazy_static! {
};
}

// Heading levels for re-use
const H1: Tag = Tag::Heading {
level: HeadingLevel::H1,
id: None,
classes: Vec::new(),
attrs: Vec::new(),
};
const H2: Tag = Tag::Heading {
level: HeadingLevel::H2,
id: None,
classes: Vec::new(),
attrs: Vec::new(),
};
const H3: Tag = Tag::Heading {
level: HeadingLevel::H3,
id: None,
classes: Vec::new(),
attrs: Vec::new(),
};

const H3_END: TagEnd = TagEnd::Heading(HeadingLevel::H3);

#[derive(Debug, PartialEq, Eq)]
enum ParserState {
InDefinition,
Expand Down Expand Up @@ -151,18 +173,18 @@ fn process_object_event(
state: &mut ParserState,
) {
match event {
Event::Start(Tag::Heading(1)) => {
Event::Start(tag) if tag == H1 => {
model.name = Some(extract_name(iterator));
}
Event::Start(Tag::Heading(2)) => {
Event::Start(tag) if tag == H2 => {
*state = ParserState::OutsideDefinition;
}
Event::Start(Tag::Heading(3)) => {
Event::Start(tag) if tag == H3 => {
*state = ParserState::InHeading;
let object = process_object_heading(iterator);
objects.push(object);
}
Event::End(Tag::Heading(3)) => {
Event::End(tag) if tag == H3_END => {
*state = ParserState::InDefinition;
}
Event::Text(CowStr::Borrowed("[")) => {
Expand Down Expand Up @@ -324,7 +346,7 @@ fn extract_attribute_options(iterator: &mut Parser) -> Vec<String> {
let name = extract_name(iterator);
options.push(name);
}
Event::End(Tag::List(None)) => {
Event::End(TagEnd::List(false)) => {
break;
}
Event::Text(text) if text.to_string() == "[" => {
Expand Down Expand Up @@ -415,7 +437,7 @@ fn process_option(option: &String) -> (String, String) {
/// * `event` - The current Markdown event.
pub fn process_enum_event(iterator: &mut Parser, enums: &mut Vec<Enumeration>, event: Event) {
match event {
Event::Start(Tag::Heading(3)) => {
Event::Start(tag) if tag == H3 => {
let enum_name = extract_name(iterator);
let enum_obj = Enumeration {
name: enum_name,
Expand Down

0 comments on commit 9116d13

Please sign in to comment.