From 6bc0dfc61bfd80b85000664aceda748cf0f77733 Mon Sep 17 00:00:00 2001 From: Kenton Hamaluik Date: Sat, 7 Dec 2019 00:38:42 -0700 Subject: [PATCH] added tables and footnotes, but footnotes not working in latex? --- docs-src/02-markdown/06-tables.md | 21 +++++++ .../{06-task-lists.md => 07-task-lists.md} | 0 docs-src/02-markdown/08-footnotes.md | 7 +++ src/latex.rs | 62 +++++++++++++++---- 4 files changed, 79 insertions(+), 11 deletions(-) create mode 100644 docs-src/02-markdown/06-tables.md rename docs-src/02-markdown/{06-task-lists.md => 07-task-lists.md} (100%) create mode 100644 docs-src/02-markdown/08-footnotes.md diff --git a/docs-src/02-markdown/06-tables.md b/docs-src/02-markdown/06-tables.md new file mode 100644 index 0000000..a20af73 --- /dev/null +++ b/docs-src/02-markdown/06-tables.md @@ -0,0 +1,21 @@ +--- +title = "Tables" +--- + +Tables are created using the [pipe syntax](https://github.github.com/gfm/#tables-extension-), for example the following: + +```md +| Tables | Are | Cool | +| ------------- |:-------------:| -----:| +| col 3 is | right-aligned | $1600 | +| col 2 is | centered | $12 | +| zebra stripes | are neat | $1 | +``` + +renders as: + +| Tables | Are | Cool | +| ------------- |:-------------:| -----:| +| col 3 is | right-aligned | $1600 | +| col 2 is | centered | $12 | +| zebra stripes | are neat | $1 | diff --git a/docs-src/02-markdown/06-task-lists.md b/docs-src/02-markdown/07-task-lists.md similarity index 100% rename from docs-src/02-markdown/06-task-lists.md rename to docs-src/02-markdown/07-task-lists.md diff --git a/docs-src/02-markdown/08-footnotes.md b/docs-src/02-markdown/08-footnotes.md new file mode 100644 index 0000000..78d9fd5 --- /dev/null +++ b/docs-src/02-markdown/08-footnotes.md @@ -0,0 +1,7 @@ +--- +title = "Footnotes" +--- + +Footnotes can be created by inserting a footnote reference (`[^1]`) such as this: [^1] followed by the footnote definition at the bottom of the document. This markdown extension is modelled after the [kramdown](https://kramdown.gettalong.org/syntax.html#tables) syntax. + +[^1]: Here is the footnote. diff --git a/src/latex.rs b/src/latex.rs index 70b5bf2..58d0fc5 100644 --- a/src/latex.rs +++ b/src/latex.rs @@ -184,18 +184,53 @@ fn format_node<'a>(section_offset: u32, node: &'a comrak::nodes::AstNode<'a>, ou NodeValue::ThematicBreak => { output.push_str("\\hline\n"); }, - NodeValue::FootnoteDefinition(text) => { - + NodeValue::FootnoteDefinition(label) => { + let label = std::str::from_utf8(&label).expect("valid utf-8"); + log::debug!("footnote definition: {}", label); + output.push_str("\\footnotetext["); + output.push_str(&escape_text(label)); + output.push_str("]{"); + let mut definition: String = String::default(); + for child in node.children() { format_node(section_offset, child, &mut definition); } + output.push_str(definition.trim()); + output.push_str("}\n"); }, NodeValue::Table(table_alignments) => { - - }, - NodeValue::TableRow(bool) => { - + use comrak::nodes::TableAlignment; + let spec: String = table_alignments.iter().map(|a| match a { + TableAlignment::None => "l", + TableAlignment::Center => "c", + TableAlignment::Left => "l", + TableAlignment::Right => "r", + }).collect::>().join(" "); + + output.push_str("\\begin{tabular}{"); + output.push_str(&spec); + output.push_str("}\n"); + + let mut rows: String = String::default(); + for child in node.children() { format_node(section_offset, child, &mut rows); } + output.push_str(rows.trim()); + output.push_str("\\end{tabular}\n"); }, - NodeValue::TableCell => { - + NodeValue::TableRow(header) => { + let row: String = node.children().map(|child| { + let mut column: String = String::default(); + format_node(section_offset, child, &mut column); + column + }) + .collect::>() + .join(" & "); + output.push_str(row.trim()); + output.push_str(r" \\"); + if *header { + output.push_str("\n\\hline\n"); + } + else { + output.push_str("\n"); + } }, + NodeValue::TableCell => for child in node.children() { format_node(section_offset, child, output); }, NodeValue::Text(text) => { if let Ok(text) = std::str::from_utf8(text) { output.push_str(&escape_text(text)); @@ -221,7 +256,8 @@ fn format_node<'a>(section_offset: u32, node: &'a comrak::nodes::AstNode<'a>, ou NodeValue::Code(text) => { if let Ok(text) = std::str::from_utf8(text) { output.push_str("\\verb|"); - output.push_str(&escape_text(text)); + //output.push_str(&escape_text(text)); + output.push_str(text); output.push_str("|"); } }, @@ -265,8 +301,12 @@ fn format_node<'a>(section_offset: u32, node: &'a comrak::nodes::AstNode<'a>, ou NodeValue::Image(node_link) => { }, - NodeValue::FootnoteReference(text) => { - + NodeValue::FootnoteReference(label) => { + let label = std::str::from_utf8(&label).expect("valid utf-8"); + log::debug!("footnote reference: {}", label); + output.push_str("\\footnotemark["); + output.push_str(&escape_text(label)); + output.push_str("]"); }, } }