Skip to content

Use pulldown_cmark instead of hoedown #75

New issue

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

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

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit 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
115 changes: 73 additions & 42 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 @@ -24,7 +24,7 @@ suppress-cargo-output = ["chan"]
chan = { version = "0.1.19", optional = true }
clap = "2.23.2"
env_logger = "0.4.2"
hoedown = "6.0.0"
pulldown-cmark = "0.5.0"
itertools = "0.5.10" # version 0.6 incompatible with Rust < 1.12
lazy_static = "0.2.6"
log = "0.3.7"
Expand Down
55 changes: 21 additions & 34 deletions src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ or distributed except according to those terms.
/*!
This module is concerned with how `cargo-script` extracts the manfiest from a script file.
*/
extern crate hoedown;
extern crate pulldown_cmark;
extern crate regex;

use std::collections::HashMap;
Expand Down Expand Up @@ -621,47 +621,34 @@ fn find_code_block_manifest(s: &str) -> Option<(Manifest, &str)> {
Extracts the first `Cargo` fenced code block from a chunk of Markdown.
*/
fn scrape_markdown_manifest(content: &str) -> Result<Option<String>> {
use self::hoedown::{Buffer, Markdown, Render};
use self::pulldown_cmark::{Parser, Options, Event, Tag};

// To match librustdoc/html/markdown.rs, HOEDOWN_EXTENSIONS.
// To match librustdoc/html/markdown.rs, opts.
let exts
= hoedown::NO_INTRA_EMPHASIS
| hoedown::TABLES
| hoedown::FENCED_CODE
| hoedown::AUTOLINK
| hoedown::STRIKETHROUGH
| hoedown::SUPERSCRIPT
| hoedown::FOOTNOTES;

let md = Markdown::new(&content).extensions(exts);

struct ManifestScraper {
seen_manifest: bool,
}
= Options::ENABLE_TABLES | Options::ENABLE_FOOTNOTES;

impl Render for ManifestScraper {
fn code_block(&mut self, output: &mut Buffer, text: Option<&Buffer>, lang: Option<&Buffer>) {
use std::ascii::AsciiExt;
let md = Parser::new_ext(&content, exts);

let lang = lang.map(|b| b.to_str().unwrap()).unwrap_or("");
let mut found = false;
let mut output = None;

if !self.seen_manifest && lang.eq_ignore_ascii_case("cargo") {
// Pass it through.
info!("found code block manifest");
if let Some(text) = text {
output.pipe(text);
}
self.seen_manifest = true;
}
for item in md {
match item {
Event::Start(Tag::CodeBlock(ref info)) if info.to_lowercase() == "cargo" && output.is_none() => {
found = true;
},
Event::Text(ref text) if found => {
let mut s = output.get_or_insert(String::new());
s.push_str(&*text);
},
Event::End(Tag::CodeBlock(_)) if found => {
found = false;
},
_ => ()
}
}

let mut ms = ManifestScraper { seen_manifest: false };
let mani_buf = ms.render(&md);

if !ms.seen_manifest { return Ok(None) }
mani_buf.to_str().map(|s| Some(s.into()))
.map_err(|_| "error decoding manifest as UTF-8".into())
Ok(output)
}

#[test]
Expand Down