Skip to content
Merged
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
15 changes: 8 additions & 7 deletions examples/s-expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const INDENT: usize = 4;
/// If true, the close parenthesis is printed in its own line.
const CLOSE_NEWLINE: bool = false;

use comrak::{parse_document, Arena, ComrakOptions};
use comrak::nodes::{AstNode, NodeValue};
use comrak::{parse_document, Arena, ComrakOptions};
use std::env;
use std::error::Error;
use std::fs::File;
Expand All @@ -27,15 +27,18 @@ fn iter_nodes<'a, W: Write>(
writer: &mut W,
indent: usize,
) -> io::Result<()> {

use NodeValue::*;

macro_rules! try_node_inline {
($node:expr, $name:ident) => ({
($node:expr, $name:ident) => {{
if let $name(t) = $node {
return write!(writer, concat!(stringify!($name), "({:?})"), String::from_utf8_lossy(&t));
return write!(
writer,
concat!(stringify!($name), "({:?})"),
String::from_utf8_lossy(&t)
);
}
})
}};
}

match &node.data.borrow().value {
Expand Down Expand Up @@ -72,7 +75,6 @@ fn iter_nodes<'a, W: Write>(
}

fn dump(source: &str) -> io::Result<()> {

let arena = Arena::new();

let opts = ComrakOptions {
Expand All @@ -94,7 +96,6 @@ fn dump(source: &str) -> io::Result<()> {
}

fn main() -> Result<(), Box<Error>> {

let mut args = env::args_os().skip(1).peekable();
let mut body = String::new();

Expand Down
3 changes: 2 additions & 1 deletion script/cibuild
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#!/bin/bash

set -ev
set -evx

sudo apt-get install python3
cargo build --verbose

if [ x"$SPEC" = "xtrue" ]; then
cd vendor/cmark-gfm/test
python3 spec_tests.py --program=../../../target/debug/comrak
python3 spec_tests.py --spec extensions.txt --program=../../../target/debug/comrak --extensions "table strikethrough autolink tagfilter footnotes"
python3 roundtrip_tests.py --program=../../../target/debug/comrak
python3 spec_tests.py --no-normalize --spec regression.txt --program=../../../target/debug/comrak
python3 entity_tests.py --program=../../../target/debug/comrak
Expand Down
31 changes: 16 additions & 15 deletions src/arena_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ pub struct Node<'a, T: 'a> {

/// A simple Debug implementation that prints the children as a tree, without
/// ilooping through the various interior pointer cycles.
impl<'a, T: 'a> fmt::Debug for Node<'a, T> where T: fmt::Debug {
impl<'a, T: 'a> fmt::Debug for Node<'a, T>
where
T: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
// FIXME: would be better not to build a vector for the children but I
// can't presently figure out the borrowing to use the debug_list API
Expand Down Expand Up @@ -243,7 +246,7 @@ impl<'a, T> Node<'a, T> {
}

macro_rules! axis_iterator {
(#[$attr:meta] $name: ident: $next: ident) => {
(#[$attr:meta] $name:ident : $next:ident) => {
#[$attr]
#[derive(Debug)]
pub struct $name<'a, T: 'a>(Option<&'a Node<'a, T>>);
Expand All @@ -257,11 +260,11 @@ macro_rules! axis_iterator {
self.0 = node.$next.get();
Some(node)
}
None => None
None => None,
}
}
}
}
};
}

axis_iterator! {
Expand Down Expand Up @@ -321,7 +324,7 @@ pub enum NodeEdge<T> {
}

macro_rules! traverse_iterator {
(#[$attr:meta] $name: ident: $first_child: ident, $next_sibling: ident) => {
(#[$attr:meta] $name:ident : $first_child:ident, $next_sibling:ident) => {
#[$attr]
#[derive(Debug)]
pub struct $name<'a, T: 'a> {
Expand All @@ -336,12 +339,10 @@ macro_rules! traverse_iterator {
match self.next.take() {
Some(item) => {
self.next = match item {
NodeEdge::Start(node) => {
match node.$first_child.get() {
Some(child) => Some(NodeEdge::Start(child)),
None => Some(NodeEdge::End(node))
}
}
NodeEdge::Start(node) => match node.$first_child.get() {
Some(child) => Some(NodeEdge::Start(child)),
None => Some(NodeEdge::End(node)),
},
NodeEdge::End(node) => {
if node.same_node(self.root) {
None
Expand All @@ -355,19 +356,19 @@ macro_rules! traverse_iterator {
// if the tree has been modified during iteration,
// but silently stoping iteration
// seems a more sensible behavior than panicking.
None => None
}
None => None,
},
}
}
}
};
Some(item)
}
None => None
None => None,
}
}
}
}
};
}

traverse_iterator! {
Expand Down
46 changes: 34 additions & 12 deletions src/cm.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ctype::{isalpha, isdigit, isspace};
use nodes;
use nodes::{AstNode, ListDelimType, ListType, NodeLink, NodeValue};
use nodes::TableAlignment;
use nodes::{AstNode, ListDelimType, ListType, NodeLink, NodeValue};
use parser::ComrakOptions;
use scanners;
use std;
Expand All @@ -19,7 +19,7 @@ pub fn format_document<'a>(
if !f.v.is_empty() && f.v[f.v.len() - 1] != b'\n' {
f.v.push(b'\n');
}
try!(output.write_all(&f.v));
output.write_all(&f.v)?;
Ok(())
}

Expand Down Expand Up @@ -143,7 +143,9 @@ impl<'a, 'o> CommonMarkFormatter<'a, 'o> {
self.begin_content = self.begin_content && isdigit(buf[i]);
}

if self.options.width > 0 && self.column > self.options.width && !self.begin_line
if self.options.width > 0
&& self.column > self.options.width
&& !self.begin_line
&& self.last_breakable > 0
{
let remainder = self.v[self.last_breakable + 1..].to_vec();
Expand All @@ -168,16 +170,32 @@ impl<'a, 'o> CommonMarkFormatter<'a, 'o> {

let needs_escaping = c < 0x80 && escaping != Escaping::Literal
&& ((escaping == Escaping::Normal
&& (c == b'*' || c == b'_' || c == b'[' || c == b']' || c == b'#' || c == b'<'
|| c == b'>' || c == b'\\' || c == b'`' || c == b'!'
&& (c == b'*'
|| c == b'_'
|| c == b'['
|| c == b']'
|| c == b'#'
|| c == b'<'
|| c == b'>'
|| c == b'\\'
|| c == b'`'
|| c == b'!'
|| (c == b'&' && isalpha(nextc))
|| (c == b'!' && nextc == 0x5b)
|| (self.begin_content && (c == b'-' || c == b'+' || c == b'=')
|| (self.begin_content
&& (c == b'-' || c == b'+' || c == b'=')
&& !follows_digit)
|| (self.begin_content && (c == b'.' || c == b')') && follows_digit
|| (self.begin_content
&& (c == b'.' || c == b')')
&& follows_digit
&& (nextc == 0 || isspace(nextc)))))
|| (escaping == Escaping::URL
&& (c == b'`' || c == b'<' || c == b'>' || isspace(c) || c == b'\\' || c == b')'
&& (c == b'`'
|| c == b'<'
|| c == b'>'
|| isspace(c)
|| c == b'\\'
|| c == b')'
|| c == b'('))
|| (escaping == Escaping::Title
&& (c == b'`' || c == b'<' || c == b'>' || c == b'"' || c == b'\\')));
Expand Down Expand Up @@ -205,8 +223,10 @@ impl<'a, 'o> CommonMarkFormatter<'a, 'o> {
}

fn format(&mut self, node: &'a AstNode<'a>) {

enum Phase { Pre, Post }
enum Phase {
Pre,
Post,
}
let mut stack = vec![(node, Phase::Pre)];

while let Some((node, phase)) = stack.pop() {
Expand Down Expand Up @@ -338,7 +358,9 @@ impl<'a, 'o> CommonMarkFormatter<'a, 'o> {
NodeValue::DescriptionList => (),
NodeValue::DescriptionItem(..) => (),
NodeValue::DescriptionTerm => (),
NodeValue::DescriptionDetails => if entering { write!(self, ": ").unwrap() },
NodeValue::DescriptionDetails => if entering {
write!(self, ": ").unwrap()
},
NodeValue::Heading(ref nch) => if entering {
for _ in 0..nch.level {
write!(self, "#").unwrap();
Expand Down Expand Up @@ -636,7 +658,7 @@ fn is_autolink<'a>(node: &'a AstNode<'a>, nl: &NodeLink) -> bool {
};

let mut real_url: &[u8] = &nl.url;
if real_url.len() >=7 && &real_url[..7] == b"mailto:" {
if real_url.len() >= 7 && &real_url[..7] == b"mailto:" {
real_url = &real_url[7..];
}

Expand Down
3 changes: 2 additions & 1 deletion src/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ pub fn unescape(text: &[u8]) -> Option<(Vec<u8>, usize)> {
};

if num_digits >= 1 && num_digits <= 8 && i < text.len() && text[i] == b';' {
if codepoint == 0 || (codepoint >= 0xD800 && codepoint <= 0xE000)
if codepoint == 0
|| (codepoint >= 0xD800 && codepoint <= 0xE000)
|| codepoint >= 0x110000
{
codepoint = 0xFFFD;
Expand Down
Loading