summaryrefslogtreecommitdiff
path: root/src/pretty.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-06-09 00:37:13 +0200
committerLaurenz <laurmaedje@gmail.com>2021-06-09 00:37:13 +0200
commit5afb42ad89abb518a01a09051f0f9b6f75bd383e (patch)
treeb12368a287f22de711df8d759c20ee742ed5b4c2 /src/pretty.rs
parentd69dfa84ec957ac4037f60a3335416a9f73b97c8 (diff)
Lists with indent-based parsing
- Unordered lists with indent-based parsing and basic layout using stacks - Headings are now also indent based - Removes syntax functions since they will be superseded by select & transform
Diffstat (limited to 'src/pretty.rs')
-rw-r--r--src/pretty.rs50
1 files changed, 29 insertions, 21 deletions
diff --git a/src/pretty.rs b/src/pretty.rs
index 49f6fd82..9987e506 100644
--- a/src/pretty.rs
+++ b/src/pretty.rs
@@ -17,8 +17,8 @@ where
p.finish()
}
-/// Pretty print an item with a node map and return the resulting string.
-pub fn pretty_with_map<T>(item: &T, map: &NodeMap) -> String
+/// Pretty print an item with a expression map and return the resulting string.
+pub fn pretty_with_map<T>(item: &T, map: &ExprMap) -> String
where
T: PrettyWithMap + ?Sized,
{
@@ -33,10 +33,10 @@ pub trait Pretty {
fn pretty(&self, p: &mut Printer);
}
-/// Pretty print an item with a node map that applies to it.
+/// Pretty print an item with an expression map that applies to it.
pub trait PrettyWithMap {
/// Pretty print this item into the given printer.
- fn pretty_with_map(&self, p: &mut Printer, map: Option<&NodeMap>);
+ fn pretty_with_map(&self, p: &mut Printer, map: Option<&ExprMap>);
}
impl<T> Pretty for T
@@ -104,7 +104,7 @@ impl Write for Printer {
}
impl PrettyWithMap for Tree {
- fn pretty_with_map(&self, p: &mut Printer, map: Option<&NodeMap>) {
+ fn pretty_with_map(&self, p: &mut Printer, map: Option<&ExprMap>) {
for node in self {
node.pretty_with_map(p, map);
}
@@ -112,20 +112,21 @@ impl PrettyWithMap for Tree {
}
impl PrettyWithMap for Node {
- fn pretty_with_map(&self, p: &mut Printer, map: Option<&NodeMap>) {
+ fn pretty_with_map(&self, p: &mut Printer, map: Option<&ExprMap>) {
match self {
// TODO: Handle escaping.
Self::Text(text) => p.push_str(text),
Self::Space => p.push(' '),
- Self::Strong(_) => p.push('*'),
- Self::Emph(_) => p.push('_'),
Self::Linebreak(_) => p.push_str(r"\"),
Self::Parbreak(_) => p.push_str("\n\n"),
- Self::Heading(heading) => heading.pretty_with_map(p, map),
+ Self::Strong(_) => p.push('*'),
+ Self::Emph(_) => p.push('_'),
Self::Raw(raw) => raw.pretty(p),
+ Self::Heading(heading) => heading.pretty_with_map(p, map),
+ Self::List(list) => list.pretty_with_map(p, map),
Self::Expr(expr) => {
if let Some(map) = map {
- let value = &map[&(self as *const _)];
+ let value = &map[&(expr as *const _)];
value.pretty(p);
} else {
if expr.has_short_form() {
@@ -138,15 +139,6 @@ impl PrettyWithMap for Node {
}
}
-impl PrettyWithMap for HeadingNode {
- fn pretty_with_map(&self, p: &mut Printer, map: Option<&NodeMap>) {
- for _ in 0 .. self.level {
- p.push('#');
- }
- self.contents.pretty_with_map(p, map);
- }
-}
-
impl Pretty for RawNode {
fn pretty(&self, p: &mut Printer) {
// Find out how many backticks we need.
@@ -203,6 +195,23 @@ impl Pretty for RawNode {
}
}
+impl PrettyWithMap for HeadingNode {
+ fn pretty_with_map(&self, p: &mut Printer, map: Option<&ExprMap>) {
+ for _ in 0 .. self.level {
+ p.push('#');
+ }
+ p.push(' ');
+ self.body.pretty_with_map(p, map);
+ }
+}
+
+impl PrettyWithMap for ListNode {
+ fn pretty_with_map(&self, p: &mut Printer, map: Option<&ExprMap>) {
+ p.push_str("- ");
+ self.body.pretty_with_map(p, map);
+ }
+}
+
impl Pretty for Expr {
fn pretty(&self, p: &mut Printer) {
match self {
@@ -664,9 +673,8 @@ mod tests {
roundtrip("\\ ");
roundtrip("\n\n");
roundtrip("hi");
-
- // Heading.
roundtrip("# *Ok*");
+ roundtrip("- Ok");
// Raw.
roundtrip("``");