Add options to placeholders

pull/1/head
trivernis 5 years ago
parent ba7fd40f92
commit e80ecea6cf

2
Cargo.lock generated

@ -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)",

@ -1,6 +1,6 @@
[package]
name = "snekdown"
version = "0.5.5"
version = "0.6.0"
authors = ["trivernis <trivernis@protonmail.com>"]
edition = "2018"
license-file = "LICENSE"

@ -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]
```

@ -211,6 +211,7 @@ pub struct Image {
pub struct Placeholder {
pub(crate) name: String,
pub(crate) value: Option<Element>,
pub(crate) metadata: Option<InlineMetadata>,
}
#[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<InlineMetadata>) -> Self {
Self {
name,
value: None,
metadata,
}
}
pub fn set_value(&mut self, value: Element) {

@ -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<Self, io::Error> {
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)

@ -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 {

Loading…
Cancel
Save