Skip to content

Commit

Permalink
Merge pull request #17 from Trivernis/develop
Browse files Browse the repository at this point in the history
Changes and fixes to whitespace behaviour
  • Loading branch information
Trivernis authored Jan 23, 2021
2 parents 673a752 + e50d73a commit ccad154
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 33 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "snekdown"
version = "0.32.2"
version = "0.33.0"
authors = ["trivernis <trivernis@protonmail.com>"]
edition = "2018"
license-file = "LICENSE"
Expand All @@ -21,7 +21,7 @@ path = "src/main.rs"
pdf = ["headless_chrome", "failure"]

[dependencies]
charred = "0.3.5"
charred = "0.3.6"
asciimath-rs = "0.5.7"
bibliographix = "0.6.0"
crossbeam-utils = "0.7.2"
Expand Down
9 changes: 9 additions & 0 deletions src/elements/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,15 @@ impl Quote {
pub fn add_text(&mut self, text: TextLine) {
self.text.push(text)
}

/// Strips a single linebreak from the end of the quote
pub fn strip_linebreak(&mut self) {
if let Some(last) = self.text.last_mut() {
if let Some(Inline::LineBreak) = last.subtext.last() {
last.subtext.pop();
}
}
}
}

impl ImportAnchor {
Expand Down
2 changes: 2 additions & 0 deletions src/format/assets/base.scss
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ table tr td:first-child, table tr th:first-child {

blockquote {
margin-left: 0;
padding-top: 0.2em;
padding-bottom: 0.2em;
background-color: rgba(0, 0, 0, 0);
}

Expand Down
2 changes: 1 addition & 1 deletion src/format/html/to_html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ impl ToHtml for Paragraph {
}
if self.elements.len() > 1 {
for element in &self.elements[1..] {
writer.write("<br/>".to_string())?;
writer.write(" ".to_string())?;
element.to_html(writer)?;
}
}
Expand Down
43 changes: 28 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use snekdown::settings::Settings;
use snekdown::utils::caching::CacheStorage;
use snekdown::Parser;
use std::fs::{File, OpenOptions};
use std::io::{BufWriter, Write};
use std::io::{stdout, BufWriter, Write};
use std::path::PathBuf;
use std::process::exit;
use std::sync::mpsc::channel;
Expand Down Expand Up @@ -48,7 +48,11 @@ struct RenderOptions {

/// Path for the output file
#[structopt(parse(from_os_str))]
output: PathBuf,
output: Option<PathBuf>,

/// If the output should be written to stdout instead of the output file
#[structopt(long = "stdout")]
stdout: bool,

/// the output format
#[structopt(short, long, default_value = "html")]
Expand Down Expand Up @@ -165,6 +169,7 @@ fn render(opt: &RenderOptions) -> Parser {

exit(1)
}

let start = Instant::now();

let mut parser = Parser::with_defaults(ParserOptions::default().add_path(opt.input.clone()));
Expand All @@ -173,47 +178,55 @@ fn render(opt: &RenderOptions) -> Parser {
log::info!("Parsing + Processing took: {:?}", start.elapsed());
let start_render = Instant::now();

let file = OpenOptions::new()
.read(true)
.write(true)
.truncate(true)
.create(true)
.open(&opt.output)
.unwrap();
let writer = BufWriter::new(file);
if let Some(output) = &opt.output {
let file = OpenOptions::new()
.read(true)
.write(true)
.truncate(true)
.create(true)
.open(output)
.unwrap();

render_format(opt, document, BufWriter::new(file));
} else {
if !opt.stdout {
log::error!("No output file specified");
exit(1)
}
render_format(opt, document, BufWriter::new(stdout()));
}

render_format(opt, document, writer);
log::info!("Rendering took: {:?}", start_render.elapsed());
log::info!("Total: {:?}", start.elapsed());

parser
}

#[cfg(not(feature = "pdf"))]
fn render_format(opt: &RenderOptions, document: Document, writer: BufWriter<File>) {
fn render_format<W: Write + 'static>(opt: &RenderOptions, document: Document, writer: W) {
match opt.format.as_str() {
"html" => render_html(document, writer),
_ => log::error!("Unknown format {}", opt.format),
}
}

#[cfg(feature = "pdf")]
fn render_format(opt: &RenderOptions, document: Document, writer: BufWriter<File>) {
fn render_format<W: Write + 'static>(opt: &RenderOptions, document: Document, writer: W) {
match opt.format.as_str() {
"html" => render_html(document, writer),
"pdf" => render_pdf(document, writer),
_ => log::error!("Unknown format {}", opt.format),
}
}

fn render_html(document: Document, writer: BufWriter<File>) {
fn render_html<W: Write + 'static>(document: Document, writer: W) {
let mut writer = HTMLWriter::new(Box::new(writer), document.config.lock().style.theme.clone());
document.to_html(&mut writer).unwrap();
writer.flush().unwrap();
}

#[cfg(feature = "pdf")]
fn render_pdf(document: Document, mut writer: BufWriter<File>) {
fn render_pdf<W: Write + 'static>(document: Document, mut writer: W) {
use snekdown::format::chromium_pdf::render_to_pdf;

let result = render_to_pdf(document).expect("Failed to render pdf!");
Expand Down
10 changes: 7 additions & 3 deletions src/parser/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ impl ParseBlock for Parser {
fn parse_quote(&mut self) -> ParseResult<Quote> {
let start_index = self.ctm.get_index();
self.ctm.seek_whitespace();

let metadata = if let Ok(meta) = self.parse_inline_metadata() {
Some(meta)
} else {
Expand All @@ -190,6 +191,8 @@ impl ParseBlock for Parser {
break;
}
}

quote.strip_linebreak();
if quote.text.len() == 0 {
return Err(self.ctm.rewind_with_error(start_index).into());
}
Expand All @@ -199,11 +202,12 @@ impl ParseBlock for Parser {

/// Parses a paragraph
fn parse_paragraph(&mut self) -> ParseResult<Paragraph> {
self.ctm.seek_whitespace();
let mut paragraph = Paragraph::new();
while let Ok(token) = self.parse_line() {
paragraph.add_element(token);

while let Ok(element) = self.parse_line() {
paragraph.add_element(element);
let start_index = self.ctm.get_index();

if self.ctm.check_any_sequence(&BLOCK_SPECIAL_CHARS)
|| self.ctm.check_any(&self.block_break_at)
{
Expand Down
35 changes: 29 additions & 6 deletions src/parser/line.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use super::ParseResult;
use crate::elements::tokens::*;
use crate::elements::Inline::LineBreak;
use crate::elements::{BibEntry, Metadata};
use crate::elements::{Cell, Centered, Header, Inline, Line, ListItem, Row, Ruler, TextLine};
use crate::elements::{Cell, Centered, Header, Line, ListItem, Row, Ruler, TextLine};
use crate::parser::inline::ParseInline;
use crate::Parser;
use bibliographix::bibliography::bibliography_entry::BibliographyEntry;
Expand All @@ -16,6 +17,7 @@ pub(crate) trait ParseLine {
fn parse_row(&mut self) -> ParseResult<Row>;
fn parse_centered(&mut self) -> ParseResult<Centered>;
fn parse_ruler(&mut self) -> ParseResult<Ruler>;
fn parse_paragraph_break(&mut self) -> ParseResult<TextLine>;
fn parse_text_line(&mut self) -> ParseResult<TextLine>;
fn parse_bib_entry(&mut self) -> ParseResult<BibEntry>;
}
Expand All @@ -36,6 +38,9 @@ impl ParseLine for Parser {
} else if let Ok(bib) = self.parse_bib_entry() {
log::trace!("Line::BibEntry");
Ok(Line::BibEntry(bib))
} else if let Ok(text) = self.parse_paragraph_break() {
log::trace!("Line::LineBreak");
Ok(Line::Text(text))
} else if let Ok(text) = self.parse_text_line() {
log::trace!("Line::Text");
Ok(Line::Text(text))
Expand Down Expand Up @@ -166,28 +171,45 @@ impl ParseLine for Parser {
/// Parses a line of text
fn parse_text_line(&mut self) -> ParseResult<TextLine> {
let mut text = TextLine::new();
let start_index = self.ctm.get_index();

while let Ok(subtext) = self.parse_inline() {
text.add_subtext(subtext);
if self.ctm.check_eof() || self.ctm.check_any(&self.inline_break_at) {
break;
}
}

// add a linebreak when encountering \n\n
if self.ctm.check_char(&LB) {
if let Ok(_) = self.ctm.seek_one() {
if self.ctm.check_char(&LB) {
text.add_subtext(Inline::LineBreak)
}
self.ctm.try_seek();

if self.ctm.check_char(&LB) {
text.add_subtext(LineBreak);

self.ctm.try_seek();
}
}

if text.subtext.len() > 0 {
Ok(text)
} else {
Err(self.ctm.err().into())
Err(self.ctm.rewind_with_error(start_index).into())
}
}

/// Parses a paragraph break
fn parse_paragraph_break(&mut self) -> ParseResult<TextLine> {
let start_index = self.ctm.get_index();
self.ctm.assert_char(&LB, Some(start_index))?;
self.ctm.seek_one()?;

let mut line = TextLine::new();
line.subtext.push(LineBreak);

Ok(line)
}

fn parse_bib_entry(&mut self) -> ParseResult<BibEntry> {
let start_index = self.ctm.get_index();
self.ctm.seek_any(&INLINE_WHITESPACE)?;
Expand Down Expand Up @@ -239,6 +261,7 @@ impl ParseLine for Parser {
}
}
};
self.ctm.seek_whitespace();

self.options
.document
Expand Down
22 changes: 19 additions & 3 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ impl Parser {
/// Returns a string of the current position in the file
pub(crate) fn get_position_string(&self) -> String {
let char_index = self.ctm.get_index();
self.get_position_string_for_index(char_index)
}

/// Returns a string of the given index position in the file
fn get_position_string_for_index(&self, char_index: usize) -> String {
let text = self.ctm.get_text();
let mut text_unil = text[..char_index].to_vec();
let line_number = text_unil.iter().filter(|c| c == &&LB).count();
Expand Down Expand Up @@ -180,10 +185,10 @@ impl Parser {
let anchor = Arc::new(RwLock::new(ImportAnchor::new()));
let anchor_clone = Arc::clone(&anchor);
let wg = self.wg.clone();
let mut chid_parser = self.create_child(path.clone());
let mut child_parser = self.create_child(path.clone());

let _ = thread::spawn(move || {
let document = chid_parser.parse();
let document = child_parser.parse();
anchor_clone.write().unwrap().set_document(document);

drop(wg);
Expand Down Expand Up @@ -332,7 +337,18 @@ impl Parser {
if self.ctm.check_eof() {
break;
}
eprintln!("{}", err);
match err {
ParseError::TapeError(t) => {
log::error!(
"Parse Error: {}\n\t--> {}\n",
t,
self.get_position_string_for_index(t.get_index())
)
}
_ => {
log::error!("{}", err)
}
}
break;
}
}
Expand Down

0 comments on commit ccad154

Please sign in to comment.