diff --git a/Cargo.lock b/Cargo.lock index 2445e43..8af28e4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -358,7 +358,7 @@ dependencies = [ [[package]] name = "snekdown" -version = "0.5.5" +version = "0.6.0" dependencies = [ "chrono 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)", "crossbeam-utils 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 74ead05..54be35d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "snekdown" -version = "0.5.5" +version = "0.6.0" authors = ["trivernis "] edition = "2018" license-file = "LICENSE" diff --git a/README.md b/README.md index 7342e6b..328e3aa 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,21 @@ Header with rows | row | row | row ``` +### Placeholders + +Placeholders can be used to insert special elements in a specific place. + +```md +Insert the table of contents +[[TOC]] + +Insert the current date +[[date]] + +Insert the current time +[[time]] +``` + ### Metadata Additional metadata can be provided for some elements. @@ -120,4 +135,7 @@ Set the size of an image Set the source of a quote [author=Me date=[[date]] display="author - date"]> It's me + +Set options for placeholders +[[toc]][ordered] ``` \ No newline at end of file diff --git a/src/parsing/elements.rs b/src/parsing/elements.rs index dd729ef..3c88c3a 100644 --- a/src/parsing/elements.rs +++ b/src/parsing/elements.rs @@ -211,6 +211,7 @@ pub struct Image { pub struct Placeholder { pub(crate) name: String, pub(crate) value: Option, + pub(crate) metadata: Option, } #[derive(Clone, Debug)] @@ -260,21 +261,21 @@ impl Document { found } - pub fn create_toc(&self) -> List { + pub fn create_toc(&self, ordered: bool) -> List { let mut list = List::new(); - list.ordered = true; + list.ordered = ordered; self.elements.iter().for_each(|e| match e { Block::Section(sec) => { if !sec.get_hide_in_toc() { - let mut item = ListItem::new(Line::Anchor(sec.header.get_anchor()), 1, true); - item.children.append(&mut sec.get_toc_list().items); + let mut item = ListItem::new(Line::Anchor(sec.header.get_anchor()), 1, ordered); + item.children.append(&mut sec.get_toc_list(ordered).items); list.add_item(item); } } Block::Import(imp) => { let anchor = imp.anchor.lock().unwrap(); if let Some(doc) = &anchor.document { - list.items.append(&mut doc.create_toc().items) + list.items.append(&mut doc.create_toc(ordered).items) } } _ => {} @@ -317,13 +318,13 @@ impl Section { found } - pub fn get_toc_list(&self) -> List { + pub fn get_toc_list(&self, ordered: bool) -> List { let mut list = List::new(); self.elements.iter().for_each(|e| { if let Block::Section(sec) = e { if !sec.get_hide_in_toc() { - let mut item = ListItem::new(Line::Anchor(sec.header.get_anchor()), 1, true); - item.children.append(&mut sec.get_toc_list().items); + let mut item = ListItem::new(Line::Anchor(sec.header.get_anchor()), 1, ordered); + item.children.append(&mut sec.get_toc_list(ordered).items); list.add_item(item); } } @@ -469,8 +470,12 @@ impl PartialEq for Import { } impl Placeholder { - pub fn new(name: String) -> Self { - Self { name, value: None } + pub fn new(name: String, metadata: Option) -> Self { + Self { + name, + value: None, + metadata, + } } pub fn set_value(&mut self, value: Element) { diff --git a/src/parsing/parser.rs b/src/parsing/parser.rs index ac5b896..af4451d 100644 --- a/src/parsing/parser.rs +++ b/src/parsing/parser.rs @@ -106,8 +106,6 @@ pub struct Parser { } impl Parser { - /// TODO fn get_until(until: &[char], err_when: &[]) -> String - pub fn new_from_file(path: String) -> Result { let content = read_to_string(path.clone())?; Ok(Self::new(content, Some(path))) @@ -716,7 +714,13 @@ impl Parser { }; self.skip_char(); - let placeholder = Arc::new(Mutex::new(Placeholder::new(name))); + let metadata = if let Ok(meta) = self.parse_inline_metadata() { + Some(meta) + } else { + None + }; + + let placeholder = Arc::new(Mutex::new(Placeholder::new(name, metadata))); self.document.add_placeholder(Arc::clone(&placeholder)); Ok(placeholder) diff --git a/src/parsing/placeholders.rs b/src/parsing/placeholders.rs index 923a833..f7600a5 100644 --- a/src/parsing/placeholders.rs +++ b/src/parsing/placeholders.rs @@ -35,7 +35,14 @@ impl ProcessPlaceholders for Document { self.placeholders.iter().for_each(|p| { let mut pholder = p.lock().unwrap(); match pholder.name.to_ascii_lowercase().as_str() { - P_TOC => pholder.set_value(block!(Block::List(self.create_toc()))), + P_TOC => { + let ordered = if let Some(meta) = &pholder.metadata { + meta.get_bool("ordered") + } else { + false + }; + pholder.set_value(block!(Block::List(self.create_toc(ordered)))) + } P_DATE => pholder.set_value(inline!(Inline::Plain(PlainText { value: get_date_string() }))), @@ -53,7 +60,7 @@ impl ProcessPlaceholders for Document { fn get_time_string() -> String { let now = Local::now(); - format!("{:02}:{:02}:{02}", now.hour(), now.minute(), now.second()) + format!("{:02}:{:02}:{:02}", now.hour(), now.minute(), now.second()) } fn get_date_string() -> String {