diff --git a/Cargo.lock b/Cargo.lock index da59c91..a62c42e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1142,7 +1142,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "snekdown" -version = "0.26.0" +version = "0.26.1" dependencies = [ "asciimath-rs 0.5.7 (registry+https://github.com/rust-lang/crates.io-index)", "base64 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 64e0bd9..b5f5306 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "snekdown" -version = "0.26.0" +version = "0.26.1" authors = ["trivernis "] edition = "2018" license-file = "LICENSE" diff --git a/src/elements/mod.rs b/src/elements/mod.rs index aee972d..56346ee 100644 --- a/src/elements/mod.rs +++ b/src/elements/mod.rs @@ -355,6 +355,7 @@ impl Document { self.elements.reverse(); let mut count: usize = 0; let mut last_section: Option<(u8, usize)> = None; + while let Some(element) = self.elements.pop() { match element { Block::Section(sec) => { @@ -733,6 +734,7 @@ impl BibReference { pub(crate) fn get_formatted(&self) -> String { if let Some(entry) = &self.entry_anchor.lock().unwrap().entry { let entry = entry.lock().unwrap(); + if let Some(display) = &self.display { let display = display.read().unwrap(); let mut template = PlaceholderTemplate::new(display.get().as_string()); diff --git a/src/format/html/html_writer.rs b/src/format/html/html_writer.rs index 72abb80..e5ba6e6 100644 --- a/src/format/html/html_writer.rs +++ b/src/format/html/html_writer.rs @@ -25,4 +25,9 @@ impl HTMLWriter { pub fn write_attribute(&mut self, attribute_value: String) -> io::Result<()> { self.write(htmlescape::encode_attribute(attribute_value.as_str())) } + + /// Flushes the writer + pub fn flush(&mut self) -> io::Result<()> { + self.inner.flush() + } } diff --git a/src/format/html/to_html.rs b/src/format/html/to_html.rs index 61d4028..6f33001 100644 --- a/src/format/html/to_html.rs +++ b/src/format/html/to_html.rs @@ -6,7 +6,6 @@ use crate::references::templates::{Template, TemplateVariable}; use asciimath_rs::format::mathml::ToMathML; use htmlescape::encode_attribute; use minify::html::minify; -use std::cell::RefCell; use std::io; use syntect::highlighting::ThemeSet; use syntect::html::highlighted_html_for_string; @@ -97,7 +96,7 @@ impl ToHtml for MetadataValue { impl ToHtml for Document { fn to_html(&self, writer: &mut HTMLWriter) -> io::Result<()> { let path = if let Some(path) = &self.path { - format!("path='{}'", encode_attribute(path.as_str())) + format!("path=\"{}\"", encode_attribute(path.as_str())) } else { "".to_string() }; @@ -108,29 +107,29 @@ impl ToHtml for Document { .map(|e| e.get().as_string()) .unwrap_or("en".to_string()); let style = minify(std::include_str!("assets/style.css")); - writer.write("".to_string())?; writer.write("".to_string())?; - writer.write("".to_string())?; + writer.write("".to_string())?; writer.write("".to_string())?; for stylesheet in &self.stylesheets { writer.write("".to_string())?; } - writer.write("
".to_string())?; + writer.write("
".to_string())?; for element in &self.elements { element.to_html(writer)?; } writer.write("
".to_string())?; } else { - writer.write("
".to_string())?; @@ -165,13 +164,8 @@ impl ToHtml for MathBlock { } impl ToHtml for Import { - fn to_html(&self, writer: &mut HTMLWriter) -> io::Result<()> { - let anchor = self.anchor.read().unwrap(); - if let Some(document) = &anchor.document { - document.to_html(writer) - } else { - Ok(()) - } + fn to_html(&self, _writer: &mut HTMLWriter) -> io::Result<()> { + Ok(()) } } @@ -200,10 +194,16 @@ impl ToHtml for Header { impl ToHtml for Paragraph { fn to_html(&self, writer: &mut HTMLWriter) -> io::Result<()> { - writer.write("
".to_string())?; - for element in &self.elements { - element.to_html(writer)?; - writer.write("
".to_string())?; + writer.write("
".to_string())?; + + if let Some(first) = self.elements.first() { + first.to_html(writer)?; + } + if self.elements.len() > 1 { + for element in &self.elements[1..] { + writer.write("
".to_string())?; + element.to_html(writer)?; + } } writer.write("
".to_string()) @@ -256,7 +256,7 @@ impl ToHtml for ListItem { impl ToHtml for Table { fn to_html(&self, writer: &mut HTMLWriter) -> io::Result<()> { - writer.write("
".to_string())?; + writer.write("
".to_string())?; for cell in &self.header.cells { writer.write("
".to_string())?; @@ -293,9 +293,6 @@ impl ToHtml for Cell { } } -thread_local! {static PS: RefCell = RefCell::new(SyntaxSet::load_defaults_nonewlines());} -thread_local! {static TS: RefCell = RefCell::new(ThemeSet::load_defaults());} - impl ToHtml for CodeBlock { fn to_html(&self, writer: &mut HTMLWriter) -> io::Result<()> { writer.write("
".to_string())?; - PS.with(|ps_cell| { - let ps = ps_cell.borrow(); - if let Some(syntax) = ps.find_syntax_by_token(self.language.as_str()) { - TS.with(|ts_cell| { - let ts = ts_cell.borrow(); - - let _ = writer.write(highlighted_html_for_string( - self.code.as_str(), - &ps, - syntax, - &ts.themes["InspiredGitHub"], - )); - }) - } else { - let _ = writer.write("
".to_string());
-                    let _ = writer.write(self.code.clone());
-                    let _ = writer.write("
".to_string()); - } - }) + lazy_static::lazy_static! { static ref PS: SyntaxSet = SyntaxSet::load_defaults_nonewlines(); } + lazy_static::lazy_static! { static ref TS: ThemeSet = ThemeSet::load_defaults(); } + + if let Some(syntax) = PS.find_syntax_by_token(self.language.as_str()) { + writer.write(highlighted_html_for_string( + self.code.as_str(), + &PS, + syntax, + &TS.themes["InspiredGitHub"], + ))?; + } else { + writer.write("
".to_string())?;
+                writer.write_escaped(self.code.clone())?;
+                writer.write("
".to_string())?; + } } else { writer.write(">
".to_string())?;
             writer.write_escaped(self.code.clone())?;
@@ -334,13 +327,13 @@ impl ToHtml for CodeBlock {
 
 impl ToHtml for Quote {
     fn to_html(&self, writer: &mut HTMLWriter) -> io::Result<()> {
-        writer.write("
".to_string())?; + writer.write("
".to_string())?; for line in &self.text { line.to_html(writer)?; - writer.write("
".to_string())?; + writer.write("
".to_string())?; } if let Some(meta) = self.metadata.clone() { - writer.write("