Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Replace markdown with pulldown-cmark #133

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 34 additions & 25 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ clap = "2.33.3"
error-chain = "0.12.4"
itertools = "0.10.0"
libc = "0.2.82"
markdown = "0.3.0"
pulldown-cmark = { version = "0.11", default-features = false }
rand = "0.8.2"
regex = "1.4.3"
remove_dir_all = "0.8.0"
Expand Down
2 changes: 1 addition & 1 deletion src/elan-cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extern crate elan;
extern crate flate2;
extern crate itertools;
extern crate json;
extern crate markdown;
extern crate pulldown_cmark;
extern crate rand;
extern crate regex;
extern crate same_file;
Expand Down
153 changes: 98 additions & 55 deletions src/elan-cli/term2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
//! if TERM isn't defined.

use elan_utils::tty;
use markdown::tokenize;
use markdown::{Block, ListItem, Span};
use std::io;
use term;
use pulldown_cmark::{Event, Tag, TagEnd};

pub use term::color;
pub use term::Attr;
Expand Down Expand Up @@ -137,13 +136,15 @@ impl<'a, T: io::Write + 'a> LineWrapper<'a, T> {

// Handles the formatting of text
struct LineFormatter<'a, T: Instantiable + Isatty + io::Write + 'a> {
is_code_block: bool,
wrapper: LineWrapper<'a, Terminal<T>>,
attrs: Vec<Attr>,
}

impl<'a, T: Instantiable + Isatty + io::Write + 'a> LineFormatter<'a, T> {
fn new(w: &'a mut Terminal<T>, indent: u32, margin: u32) -> Self {
LineFormatter {
is_code_block: false,
wrapper: LineWrapper::new(w, indent, margin),
attrs: Vec::new(),
}
Expand All @@ -159,70 +160,112 @@ impl<'a, T: Instantiable + Isatty + io::Write + 'a> LineFormatter<'a, T> {
let _ = self.wrapper.w.attr(*attr);
}
}
fn do_spans(&mut self, spans: Vec<Span>) {
for span in spans {
match span {
Span::Break => {}
Span::Text(text) => {
self.wrapper.write_span(&text);
}
Span::Code(code) => {
self.push_attr(Attr::Bold);
self.wrapper.write_word(&code);
self.pop_attr();
}
Span::Emphasis(spans) => {
self.push_attr(Attr::ForegroundColor(color::BRIGHT_RED));
self.do_spans(spans);
self.pop_attr();
}
_ => {}

fn start_tag(&mut self, tag: Tag<'a>) {
match tag {
Tag::Paragraph => {
self.wrapper.write_line();
}
}
}
fn do_block(&mut self, b: Block) {
match b {
Block::Header(spans, _) => {

Tag::Heading { .. } => {
self.push_attr(Attr::Bold);
self.wrapper.write_line();
self.do_spans(spans);
}
Tag::MetadataBlock(_) => {}
Tag::Table(_alignments) => {}
Tag::TableHead => {}
Tag::TableRow => {}
Tag::TableCell => {}
Tag::BlockQuote(_) => {}
Tag::CodeBlock(_) | Tag::HtmlBlock { .. } => {
self.wrapper.write_line();
self.pop_attr();
self.wrapper.indent += 2;
self.is_code_block = true;
}
Block::CodeBlock(_, code) => {
Tag::List(_) => {
self.wrapper.write_line();
self.wrapper.indent += 2;
for line in code.lines() {
// Don't word-wrap code lines
self.wrapper.write_word(line);
self.wrapper.write_line();
}
self.wrapper.indent -= 2;
}
Block::Paragraph(spans) => {
Tag::Item => {
self.wrapper.write_line();
self.do_spans(spans);
}
Tag::Emphasis => {
self.push_attr(Attr::ForegroundColor(color::BRIGHT_RED));
}
Tag::Strong => {}
Tag::Strikethrough => {}
Tag::Link { .. } => {}
Tag::Image { .. } => {}
Tag::FootnoteDefinition(_name) => {}
}
}

fn end_tag(&mut self, tag: TagEnd) {
match tag {
TagEnd::Paragraph => {
self.wrapper.write_line();
}
Block::UnorderedList(items) => {
TagEnd::Heading { .. } => {
self.wrapper.write_line();
for item in items {
self.wrapper.indent += 2;
match item {
ListItem::Simple(spans) => {
self.do_spans(spans);
}
ListItem::Paragraph(blocks) => {
for block in blocks {
self.do_block(block);
}
}
}
self.wrapper.write_line();
self.wrapper.indent -= 2;
self.pop_attr();
}
TagEnd::Table => {}
TagEnd::TableHead => {}
TagEnd::TableRow => {}
TagEnd::TableCell => {}
TagEnd::BlockQuote => {}
TagEnd::CodeBlock | TagEnd::HtmlBlock => {
self.is_code_block = false;
self.wrapper.indent -= 2;
}
TagEnd::List(_) => {
self.wrapper.indent -= 2;
self.wrapper.write_line();
}
TagEnd::Item => {}
TagEnd::Emphasis => {
self.pop_attr();
}
TagEnd::Strong => {}
TagEnd::Strikethrough => {}
TagEnd::Link { .. } => {}
TagEnd::Image { .. } => {} // shouldn't happen, handled in start
TagEnd::FootnoteDefinition => {}
TagEnd::MetadataBlock(_) => {}
}
}

fn process_event(&mut self, event: Event<'a>) {
use self::Event::*;
match event {
Start(tag) => self.start_tag(tag),
End(tag) => self.end_tag(tag),
Text(text) => {
if self.is_code_block {
self.wrapper.write_word(&text);
} else {
self.wrapper.write_span(&text);
}
}
_ => {}
Code(code) => {
self.push_attr(Attr::Bold);
self.wrapper.write_word(&code);
self.pop_attr();
}
Html(_html) => {}
SoftBreak => {
self.wrapper.write_line();
}
HardBreak => {
self.wrapper.write_line();
}
Rule => {}
FootnoteReference(_name) => {}
TaskListMarker(true) => {}
TaskListMarker(false) => {}
InlineHtml(_) => {}
InlineMath(_) => {}
DisplayMath(_) => {}
}
}
}
Expand Down Expand Up @@ -294,9 +337,9 @@ impl<T: Instantiable + Isatty + io::Write> Terminal<T> {

pub fn md<S: AsRef<str>>(&mut self, content: S) {
let mut f = LineFormatter::new(self, 0, 79);
let blocks = tokenize(content.as_ref());
for b in blocks {
f.do_block(b);
let parser = pulldown_cmark::Parser::new(content.as_ref());
for event in parser {
f.process_event(event);
}
}
}
Loading