Skip to content

Commit

Permalink
Only keep the syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Nov 27, 2019
1 parent 1de6cb8 commit 1cd6631
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 31 deletions.
25 changes: 15 additions & 10 deletions src/html.rs
Expand Up @@ -26,7 +26,7 @@ use std::io::{self, ErrorKind, Write};

use crate::escape::{escape_href, escape_html};
use crate::parse::Event::*;
use crate::parse::{Alignment, Event, LinkType, Tag};
use crate::parse::{Alignment, CodeBlockKind, Event, LinkType, Tag};
use crate::strings::CowStr;

enum TableState {
Expand Down Expand Up @@ -250,17 +250,22 @@ where
self.write("\n<blockquote>\n")
}
}
Tag::CodeBlock(_, info) => {
Tag::CodeBlock(info) => {
if !self.end_newline {
self.write_newline()?;
}
let lang = info.split(' ').next().unwrap();
if lang.is_empty() {
self.write("<pre><code>")
} else {
self.write("<pre><code class=\"language-")?;
escape_html(&mut self.writer, lang)?;
self.write("\">")
match info {
CodeBlockKind::Fenced(info) => {
let lang = info.split(' ').next().unwrap();
if lang.is_empty() {
self.write("<pre><code>")
} else {
self.write("<pre><code class=\"language-")?;
escape_html(&mut self.writer, lang)?;
self.write("\">")
}
}
CodeBlockKind::Indented => self.write("<pre><code>"),
}
}
Tag::List(Some(1)) => {
Expand Down Expand Up @@ -375,7 +380,7 @@ where
Tag::BlockQuote => {
self.write("</blockquote>\n")?;
}
Tag::CodeBlock(_, _) => {
Tag::CodeBlock(_) => {
self.write("</code></pre>\n")?;
}
Tag::List(Some(_)) => {
Expand Down
31 changes: 10 additions & 21 deletions src/parse.rs
Expand Up @@ -42,6 +42,7 @@ const LINK_MAX_NESTED_PARENS: usize = 5;
#[derive(Clone, Debug, PartialEq)]
pub enum CodeBlockKind<'a> {
Indented,
/// The value contained in the tag describes the language of the code, which may be empty.
Fenced(CowStr<'a>),
}

Expand Down Expand Up @@ -79,11 +80,7 @@ pub enum Tag<'a> {

BlockQuote,
/// A code block.
///
/// The boolean is `true` is this is an indented code block (not starting with "\`\`\`").
///
/// The value contained in the tag describes the language of the code, which may be empty.
CodeBlock(CodeBlockKind<'a>, CowStr<'a>),
CodeBlock(CodeBlockKind<'a>),

/// A list. If the list is ordered the field indicates the number of the first item.
/// Contains only list items.
Expand Down Expand Up @@ -2712,13 +2709,10 @@ fn item_to_tag<'a>(item: &Item, allocs: &Allocations<'a>) -> Tag<'a> {
Tag::Image(*link_type, url.clone(), title.clone())
}
ItemBody::Heading(level) => Tag::Heading(level),
ItemBody::FencedCodeBlock(fences_ix, cow_ix) => {
Tag::CodeBlock(
CodeBlockKind::Fenced(allocs[fences_ix].clone()),
allocs[cow_ix].clone(),
)
ItemBody::FencedCodeBlock(_, cow_ix) => {
Tag::CodeBlock(CodeBlockKind::Fenced(allocs[cow_ix].clone()))
}
ItemBody::IndentCodeBlock => Tag::CodeBlock(CodeBlockKind::Indented, "".into()),
ItemBody::IndentCodeBlock => Tag::CodeBlock(CodeBlockKind::Indented),
ItemBody::BlockQuote => Tag::BlockQuote,
ItemBody::List(_, c, listitem_start) => {
if c == b'.' || c == b')' {
Expand Down Expand Up @@ -2764,13 +2758,10 @@ fn item_to_event<'a>(item: Item, text: &'a str, allocs: &Allocations<'a>) -> Eve
Tag::Image(*link_type, url.clone(), title.clone())
}
ItemBody::Heading(level) => Tag::Heading(level),
ItemBody::FencedCodeBlock(fences_ix, cow_ix) => {
Tag::CodeBlock(
CodeBlockKind::Fenced(allocs[fences_ix].clone()),
allocs[cow_ix].clone(),
)
ItemBody::FencedCodeBlock(_, cow_ix) => {
Tag::CodeBlock(CodeBlockKind::Fenced(allocs[cow_ix].clone()))
}
ItemBody::IndentCodeBlock => Tag::CodeBlock(CodeBlockKind::Indented, "".into()),
ItemBody::IndentCodeBlock => Tag::CodeBlock(CodeBlockKind::Indented),
ItemBody::BlockQuote => Tag::BlockQuote,
ItemBody::List(_, c, listitem_start) => {
if c == b'.' || c == b')' {
Expand Down Expand Up @@ -3084,8 +3075,7 @@ mod test {
let mut found = 0;
for (ev, _range) in parser.into_offset_iter() {
match ev {
Event::Start(Tag::CodeBlock(CodeBlockKind::Fenced(fences), syntax)) => {
assert_eq!(fences.as_ref(), "```");
Event::Start(Tag::CodeBlock(CodeBlockKind::Fenced(syntax))) => {
assert_eq!(syntax.as_ref(), "test");
found += 1;
}
Expand All @@ -3101,8 +3091,7 @@ mod test {
let mut found = 0;
for (ev, _range) in parser.into_offset_iter() {
match ev {
Event::Start(Tag::CodeBlock(CodeBlockKind::Indented, syntax)) => {
assert_eq!(syntax.as_ref(), "");
Event::Start(Tag::CodeBlock(CodeBlockKind::Indented)) => {
found += 1;
}
_ => {}
Expand Down

0 comments on commit 1cd6631

Please sign in to comment.