Skip to content

Commit 7b1f19f

Browse files
authored
Merge pull request #192 from vojtechkral/code-num-backticks
Add the NodeCode struct to expose number of backticks from inline code spans
2 parents 38beddb + 7284d7f commit 7b1f19f

File tree

7 files changed

+74
-14
lines changed

7 files changed

+74
-14
lines changed

examples/headers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
extern crate comrak;
44

55
use comrak::{
6-
nodes::{AstNode, NodeValue},
6+
nodes::{AstNode, NodeCode, NodeValue},
77
parse_document, Arena, ComrakOptions,
88
};
99

@@ -40,7 +40,7 @@ fn get_document_title(document: &str) -> String {
4040

4141
fn collect_text<'a>(node: &'a AstNode<'a>, output: &mut Vec<u8>) {
4242
match node.data.borrow().value {
43-
NodeValue::Text(ref literal) | NodeValue::Code(ref literal) => {
43+
NodeValue::Text(ref literal) | NodeValue::Code(NodeCode { ref literal, .. }) => {
4444
output.extend_from_slice(literal)
4545
}
4646
NodeValue::LineBreak | NodeValue::SoftBreak => output.push(b' '),

examples/s-expr.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,19 @@ fn iter_nodes<'a, W: Write>(
4444
match &node.data.borrow().value {
4545
Text(t) => write!(writer, "{:?}", String::from_utf8_lossy(&t))?,
4646
value => {
47-
try_node_inline!(value, Code);
4847
try_node_inline!(value, FootnoteDefinition);
4948
try_node_inline!(value, FootnoteReference);
5049
try_node_inline!(value, HtmlInline);
5150

51+
if let Code(code) = value {
52+
return write!(
53+
writer,
54+
"Code({:?}, {})",
55+
String::from_utf8_lossy(&code.literal),
56+
code.num_backticks
57+
);
58+
}
59+
5260
let has_blocks = node.children().any(|c| c.data.borrow().value.block());
5361

5462
write!(writer, "({:?}", value)?;

src/cm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ impl<'a, 'o> CommonMarkFormatter<'a, 'o> {
313313
NodeValue::Text(ref literal) => self.format_text(literal, allow_wrap, entering),
314314
NodeValue::LineBreak => self.format_line_break(entering),
315315
NodeValue::SoftBreak => self.format_soft_break(allow_wrap, entering),
316-
NodeValue::Code(ref literal) => self.format_code(literal, allow_wrap, entering),
316+
NodeValue::Code(ref code) => self.format_code(&code.literal, allow_wrap, entering),
317317
NodeValue::HtmlInline(ref literal) => self.format_html_inline(literal, entering),
318318
NodeValue::Strong => self.format_strong(),
319319
NodeValue::Emph => self.format_emph(node),

src/html.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use ctype::isspace;
2-
use nodes::{AstNode, ListType, NodeValue, TableAlignment};
2+
use nodes::{AstNode, ListType, NodeCode, NodeValue, TableAlignment};
33
use parser::ComrakOptions;
44
use regex::Regex;
55
use scanners;
@@ -338,7 +338,7 @@ impl<'o> HtmlFormatter<'o> {
338338
if plain {
339339
match node.data.borrow().value {
340340
NodeValue::Text(ref literal)
341-
| NodeValue::Code(ref literal)
341+
| NodeValue::Code(NodeCode { ref literal, .. })
342342
| NodeValue::HtmlInline(ref literal) => {
343343
self.escape(literal)?;
344344
}
@@ -369,7 +369,7 @@ impl<'o> HtmlFormatter<'o> {
369369

370370
fn collect_text<'a>(&self, node: &'a AstNode<'a>, output: &mut Vec<u8>) {
371371
match node.data.borrow().value {
372-
NodeValue::Text(ref literal) | NodeValue::Code(ref literal) => {
372+
NodeValue::Text(ref literal) | NodeValue::Code(NodeCode { ref literal, .. }) => {
373373
output.extend_from_slice(literal)
374374
}
375375
NodeValue::LineBreak | NodeValue::SoftBreak => output.push(b' '),
@@ -563,7 +563,7 @@ impl<'o> HtmlFormatter<'o> {
563563
}
564564
}
565565
}
566-
NodeValue::Code(ref literal) => {
566+
NodeValue::Code(NodeCode { ref literal, .. }) => {
567567
if entering {
568568
self.output.write_all(b"<code>")?;
569569
self.escape(literal)?;

src/nodes.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ pub enum NodeValue {
114114
LineBreak,
115115

116116
/// **Inline**. A [code span](https://github.github.com/gfm/#code-spans).
117-
Code(Vec<u8>),
117+
Code(NodeCode),
118118

119119
/// **Inline**. [Raw HTML](https://github.github.com/gfm/#raw-html) contained inline.
120120
HtmlInline(Vec<u8>),
@@ -160,6 +160,19 @@ pub enum TableAlignment {
160160
Right,
161161
}
162162

163+
/// An inline [code span](https://github.github.com/gfm/#code-spans).
164+
#[derive(Debug, Clone)]
165+
pub struct NodeCode {
166+
/// The URL for the link destination or image source.
167+
pub num_backticks: usize,
168+
169+
/// The content of the inline code span.
170+
/// As the contents are not interpreted as Markdown at all,
171+
/// they are contained within this structure,
172+
/// rather than inserted into a child inline of any kind.
173+
pub literal: Vec<u8>,
174+
}
175+
163176
/// The details of a link's destination, or an image's source.
164177
#[derive(Debug, Clone)]
165178
pub struct NodeLink {

src/parser/inlines.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use arena_tree::Node;
22
use ctype::{ispunct, isspace};
33
use entity;
4-
use nodes::{Ast, AstNode, NodeLink, NodeValue};
4+
use nodes::{Ast, AstNode, NodeCode, NodeLink, NodeValue};
55
use parser::{unwrap_into_2, unwrap_into_copy, AutolinkType, Callback, ComrakOptions, Reference};
66
use scanners;
77
use std::cell::{Cell, RefCell};
@@ -518,7 +518,11 @@ impl<'a, 'r, 'o, 'd, 'i, 'c, 'subj> Subject<'a, 'r, 'o, 'd, 'i, 'c, 'subj> {
518518
Some(endpos) => {
519519
let buf = &self.input[startpos..endpos - openticks];
520520
let buf = strings::normalize_code(buf);
521-
make_inline(self.arena, NodeValue::Code(buf))
521+
let code = NodeCode {
522+
num_backticks: openticks,
523+
literal: buf,
524+
};
525+
make_inline(self.arena, NodeValue::Code(code))
522526
}
523527
}
524528
}

src/tests.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::nodes::{AstNode, NodeCode, NodeValue};
12
use cm;
23
use html;
34
use propfuzz::prelude::*;
@@ -95,6 +96,18 @@ macro_rules! html_opts {
9596
};
9697
}
9798

99+
fn asssert_node_eq<'a>(node: &'a AstNode<'a>, location: &[usize], expected: &NodeValue) {
100+
let node = location
101+
.iter()
102+
.fold(node, |node, &n| node.children().nth(n).unwrap());
103+
104+
let data = node.data.borrow();
105+
let actual = format!("{:?}", data.value);
106+
let expected = format!("{:?}", expected);
107+
108+
compare_strs(&actual, &expected, "ast comparison");
109+
}
110+
98111
#[test]
99112
fn basic() {
100113
html(
@@ -310,6 +323,27 @@ fn backticks() {
310323
);
311324
}
312325

326+
#[test]
327+
fn backticks_num() {
328+
let input = "Some `code1`. More ``` code2 ```.\n";
329+
330+
let arena = Arena::new();
331+
let options = ComrakOptions::default();
332+
let root = parse_document(&arena, input, &options);
333+
334+
let code1 = NodeValue::Code(NodeCode {
335+
num_backticks: 1,
336+
literal: b"code1".to_vec(),
337+
});
338+
asssert_node_eq(root, &[0, 1], &code1);
339+
340+
let code2 = NodeValue::Code(NodeCode {
341+
num_backticks: 3,
342+
literal: b"code2".to_vec(),
343+
});
344+
asssert_node_eq(root, &[0, 3], &code2);
345+
}
346+
313347
#[test]
314348
fn backslashes() {
315349
html(
@@ -1065,9 +1099,9 @@ fn exercise_full_api() {
10651099

10661100
let _: String = ::Anchorizer::new().anchorize("header".to_string());
10671101

1068-
let _: &::nodes::AstNode = ::parse_document(&arena, "document", &default_options);
1102+
let _: &AstNode = ::parse_document(&arena, "document", &default_options);
10691103

1070-
let _: &::nodes::AstNode = ::parse_document_with_broken_link_callback(
1104+
let _: &AstNode = ::parse_document_with_broken_link_callback(
10711105
&arena,
10721106
"document",
10731107
&default_options,
@@ -1168,7 +1202,8 @@ fn exercise_full_api() {
11681202
::nodes::NodeValue::SoftBreak => {}
11691203
::nodes::NodeValue::LineBreak => {}
11701204
::nodes::NodeValue::Code(code) => {
1171-
let _: &Vec<u8> = code;
1205+
let _: usize = code.num_backticks;
1206+
let _: Vec<u8> = code.literal;
11721207
}
11731208
::nodes::NodeValue::HtmlInline(html) => {
11741209
let _: &Vec<u8> = html;

0 commit comments

Comments
 (0)