2014-04-04 06:03:01 +00:00
|
|
|
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
|
2013-09-19 05:18:38 +00:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-10-03 17:24:40 +00:00
|
|
|
//! Markdown formatting for rustdoc
|
|
|
|
//!
|
2017-03-28 17:54:11 +00:00
|
|
|
//! This module implements markdown formatting through the pulldown-cmark
|
|
|
|
//! rust-library. This module exposes all of the
|
2013-10-03 17:24:40 +00:00
|
|
|
//! functionality through a unit-struct, `Markdown`, which has an implementation
|
2015-12-02 23:06:26 +00:00
|
|
|
//! of `fmt::Display`. Example usage:
|
2013-10-03 17:24:40 +00:00
|
|
|
//!
|
2017-06-20 07:15:16 +00:00
|
|
|
//! ```
|
2017-04-06 12:09:20 +00:00
|
|
|
//! use rustdoc::html::markdown::Markdown;
|
2014-01-25 05:00:31 +00:00
|
|
|
//!
|
2013-10-03 17:24:40 +00:00
|
|
|
//! let s = "My *markdown* _text_";
|
2017-04-06 12:09:20 +00:00
|
|
|
//! let html = format!("{}", Markdown(s));
|
2013-10-03 17:24:40 +00:00
|
|
|
//! // ... something using html
|
|
|
|
//! ```
|
|
|
|
|
2014-03-22 01:05:05 +00:00
|
|
|
#![allow(non_camel_case_types)]
|
2014-02-10 14:36:31 +00:00
|
|
|
|
2017-04-13 23:23:14 +00:00
|
|
|
use libc;
|
|
|
|
use std::slice;
|
|
|
|
|
2014-11-21 20:00:05 +00:00
|
|
|
use std::ascii::AsciiExt;
|
2015-01-12 09:37:01 +00:00
|
|
|
use std::cell::RefCell;
|
2017-04-06 12:09:20 +00:00
|
|
|
use std::collections::{HashMap, VecDeque};
|
2015-04-07 01:39:39 +00:00
|
|
|
use std::default::Default;
|
2016-10-11 08:56:30 +00:00
|
|
|
use std::fmt::{self, Write};
|
2014-04-29 03:36:08 +00:00
|
|
|
use std::str;
|
2016-02-11 10:09:01 +00:00
|
|
|
use syntax::feature_gate::UnstableFeatures;
|
2017-02-06 21:11:03 +00:00
|
|
|
use syntax::codemap::Span;
|
2013-09-19 05:18:38 +00:00
|
|
|
|
2015-12-03 23:48:59 +00:00
|
|
|
use html::render::derive_id;
|
2014-03-07 14:13:17 +00:00
|
|
|
use html::toc::TocBuilder;
|
2014-02-20 09:14:51 +00:00
|
|
|
use html::highlight;
|
2017-04-20 22:32:23 +00:00
|
|
|
use html::escape::Escape;
|
2014-06-06 16:12:18 +00:00
|
|
|
use test;
|
2014-02-20 09:14:51 +00:00
|
|
|
|
2017-04-06 12:09:20 +00:00
|
|
|
use pulldown_cmark::{html, Event, Tag, Parser};
|
|
|
|
use pulldown_cmark::{Options, OPTION_ENABLE_FOOTNOTES, OPTION_ENABLE_TABLES};
|
2017-03-11 00:43:36 +00:00
|
|
|
|
2017-04-20 22:32:23 +00:00
|
|
|
#[derive(PartialEq, Debug, Clone, Copy)]
|
|
|
|
pub enum RenderType {
|
|
|
|
Hoedown,
|
|
|
|
Pulldown,
|
|
|
|
}
|
|
|
|
|
2015-12-02 23:06:26 +00:00
|
|
|
/// A unit struct which has the `fmt::Display` trait implemented. When
|
2013-10-03 17:24:40 +00:00
|
|
|
/// formatted, this struct will emit the HTML corresponding to the rendered
|
|
|
|
/// version of the contained markdown string.
|
2017-03-10 13:06:24 +00:00
|
|
|
// The second parameter is whether we need a shorter version or not.
|
2017-04-20 22:32:23 +00:00
|
|
|
pub struct Markdown<'a>(pub &'a str, pub RenderType);
|
2014-03-07 14:13:17 +00:00
|
|
|
/// A unit struct like `Markdown`, that renders the markdown with a
|
|
|
|
/// table of contents.
|
2017-04-20 22:32:23 +00:00
|
|
|
pub struct MarkdownWithToc<'a>(pub &'a str, pub RenderType);
|
2016-12-12 23:18:22 +00:00
|
|
|
/// A unit struct like `Markdown`, that renders the markdown escaping HTML tags.
|
2017-04-20 22:32:23 +00:00
|
|
|
pub struct MarkdownHtml<'a>(pub &'a str, pub RenderType);
|
2017-04-06 12:09:20 +00:00
|
|
|
/// A unit struct like `Markdown`, that renders only the first paragraph.
|
|
|
|
pub struct MarkdownSummaryLine<'a>(pub &'a str);
|
2013-09-19 05:18:38 +00:00
|
|
|
|
2017-05-06 13:08:41 +00:00
|
|
|
/// Controls whether a line will be hidden or shown in HTML output.
|
|
|
|
///
|
|
|
|
/// All lines are used in documentation tests.
|
|
|
|
enum Line<'a> {
|
|
|
|
Hidden(&'a str),
|
|
|
|
Shown(&'a str),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Line<'a> {
|
|
|
|
fn for_html(self) -> Option<&'a str> {
|
|
|
|
match self {
|
|
|
|
Line::Shown(l) => Some(l),
|
|
|
|
Line::Hidden(_) => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn for_code(self) -> &'a str {
|
|
|
|
match self {
|
|
|
|
Line::Shown(l) |
|
|
|
|
Line::Hidden(l) => l,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: There is a minor inconsistency here. For lines that start with ##, we
|
|
|
|
// have no easy way of removing a potential single space after the hashes, which
|
|
|
|
// is done in the single # case. This inconsistency seems okay, if non-ideal. In
|
|
|
|
// order to fix it we'd have to iterate to find the first non-# character, and
|
|
|
|
// then reallocate to remove it; which would make us return a String.
|
|
|
|
fn map_line(s: &str) -> Line {
|
2013-12-30 05:31:24 +00:00
|
|
|
let trimmed = s.trim();
|
2017-05-06 13:08:41 +00:00
|
|
|
if trimmed.starts_with("##") {
|
|
|
|
Line::Shown(&trimmed[1..])
|
2015-04-07 22:30:05 +00:00
|
|
|
} else if trimmed.starts_with("# ") {
|
2017-05-06 13:08:41 +00:00
|
|
|
// # text
|
|
|
|
Line::Hidden(&trimmed[2..])
|
|
|
|
} else if trimmed == "#" {
|
|
|
|
// We cannot handle '#text' because it could be #[attr].
|
|
|
|
Line::Hidden("")
|
2013-12-30 05:31:24 +00:00
|
|
|
} else {
|
2017-05-06 13:08:41 +00:00
|
|
|
Line::Shown(s)
|
2013-12-30 05:31:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 22:32:23 +00:00
|
|
|
/// Returns a new string with all consecutive whitespace collapsed into
|
|
|
|
/// single spaces.
|
|
|
|
///
|
|
|
|
/// Any leading or trailing whitespace will be trimmed.
|
|
|
|
fn collapse_whitespace(s: &str) -> String {
|
|
|
|
s.split_whitespace().collect::<Vec<_>>().join(" ")
|
|
|
|
}
|
|
|
|
|
2017-04-06 12:09:20 +00:00
|
|
|
/// Convert chars from a title for an id.
|
2015-04-06 17:06:39 +00:00
|
|
|
///
|
2017-04-06 12:09:20 +00:00
|
|
|
/// "Hello, world!" -> "hello-world"
|
|
|
|
fn slugify(c: char) -> Option<char> {
|
|
|
|
if c.is_alphanumeric() || c == '-' || c == '_' {
|
|
|
|
if c.is_ascii() {
|
|
|
|
Some(c.to_ascii_lowercase())
|
|
|
|
} else {
|
|
|
|
Some(c)
|
|
|
|
}
|
|
|
|
} else if c.is_whitespace() && c.is_ascii() {
|
|
|
|
Some('-')
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2015-04-06 17:06:39 +00:00
|
|
|
}
|
|
|
|
|
2016-10-11 08:56:30 +00:00
|
|
|
// Information about the playground if a URL has been specified, containing an
|
|
|
|
// optional crate name and the URL.
|
|
|
|
thread_local!(pub static PLAYGROUND: RefCell<Option<(Option<String>, String)>> = {
|
2014-11-14 22:20:57 +00:00
|
|
|
RefCell::new(None)
|
2014-11-14 17:18:10 +00:00
|
|
|
});
|
2014-03-04 19:24:20 +00:00
|
|
|
|
2017-04-06 12:09:20 +00:00
|
|
|
/// Adds syntax highlighting and playground Run buttons to rust code blocks.
|
|
|
|
struct CodeBlocks<'a, I: Iterator<Item = Event<'a>>> {
|
|
|
|
inner: I,
|
2017-03-30 01:48:06 +00:00
|
|
|
}
|
|
|
|
|
2017-04-06 12:09:20 +00:00
|
|
|
impl<'a, I: Iterator<Item = Event<'a>>> CodeBlocks<'a, I> {
|
|
|
|
fn new(iter: I) -> Self {
|
|
|
|
CodeBlocks {
|
|
|
|
inner: iter,
|
2017-03-30 01:48:06 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-06 12:09:20 +00:00
|
|
|
}
|
2017-03-30 23:29:54 +00:00
|
|
|
|
2017-04-06 12:09:20 +00:00
|
|
|
impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'a, I> {
|
|
|
|
type Item = Event<'a>;
|
2017-03-30 01:48:06 +00:00
|
|
|
|
2017-04-06 12:09:20 +00:00
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
let event = self.inner.next();
|
|
|
|
if let Some(Event::Start(Tag::CodeBlock(lang))) = event {
|
|
|
|
if !LangString::parse(&lang).rust {
|
|
|
|
return Some(Event::Start(Tag::CodeBlock(lang)));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return event;
|
|
|
|
}
|
2017-03-30 01:48:06 +00:00
|
|
|
|
2017-03-08 00:01:23 +00:00
|
|
|
let mut origtext = String::new();
|
2017-04-06 12:09:20 +00:00
|
|
|
for event in &mut self.inner {
|
2017-03-11 00:43:36 +00:00
|
|
|
match event {
|
2017-04-06 12:09:20 +00:00
|
|
|
Event::End(Tag::CodeBlock(..)) => break,
|
2017-03-11 00:43:36 +00:00
|
|
|
Event::Text(ref s) => {
|
|
|
|
origtext.push_str(s);
|
2017-03-08 00:01:23 +00:00
|
|
|
}
|
2017-03-11 00:43:36 +00:00
|
|
|
_ => {}
|
2017-03-08 00:01:23 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-06 13:08:41 +00:00
|
|
|
let lines = origtext.lines().filter_map(|l| map_line(l).for_html());
|
2017-03-08 00:01:23 +00:00
|
|
|
let text = lines.collect::<Vec<&str>>().join("\n");
|
|
|
|
PLAYGROUND.with(|play| {
|
|
|
|
// insert newline to clearly separate it from the
|
|
|
|
// previous block so we can shorten the html output
|
2017-04-06 12:09:20 +00:00
|
|
|
let mut s = String::from("\n");
|
2017-03-08 00:01:23 +00:00
|
|
|
let playground_button = play.borrow().as_ref().and_then(|&(ref krate, ref url)| {
|
|
|
|
if url.is_empty() {
|
|
|
|
return None;
|
|
|
|
}
|
2017-05-06 13:08:41 +00:00
|
|
|
let test = origtext.lines()
|
|
|
|
.map(|l| map_line(l).for_code())
|
|
|
|
.collect::<Vec<&str>>().join("\n");
|
2017-03-08 00:01:23 +00:00
|
|
|
let krate = krate.as_ref().map(|s| &**s);
|
|
|
|
let test = test::maketest(&test, krate, false,
|
2017-04-06 12:09:20 +00:00
|
|
|
&Default::default());
|
2017-03-08 00:01:23 +00:00
|
|
|
let channel = if test.contains("#![feature(") {
|
|
|
|
"&version=nightly"
|
|
|
|
} else {
|
|
|
|
""
|
|
|
|
};
|
|
|
|
// These characters don't need to be escaped in a URI.
|
|
|
|
// FIXME: use a library function for percent encoding.
|
|
|
|
fn dont_escape(c: u8) -> bool {
|
|
|
|
(b'a' <= c && c <= b'z') ||
|
|
|
|
(b'A' <= c && c <= b'Z') ||
|
|
|
|
(b'0' <= c && c <= b'9') ||
|
|
|
|
c == b'-' || c == b'_' || c == b'.' ||
|
|
|
|
c == b'~' || c == b'!' || c == b'\'' ||
|
|
|
|
c == b'(' || c == b')' || c == b'*'
|
|
|
|
}
|
|
|
|
let mut test_escaped = String::new();
|
|
|
|
for b in test.bytes() {
|
|
|
|
if dont_escape(b) {
|
|
|
|
test_escaped.push(char::from(b));
|
|
|
|
} else {
|
|
|
|
write!(test_escaped, "%{:02X}", b).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(format!(
|
|
|
|
r#"<a class="test-arrow" target="_blank" href="{}?code={}{}">Run</a>"#,
|
|
|
|
url, test_escaped, channel
|
|
|
|
))
|
|
|
|
});
|
2017-04-06 12:09:20 +00:00
|
|
|
s.push_str(&highlight::render_with_highlighting(
|
|
|
|
&text,
|
|
|
|
Some("rust-example-rendered"),
|
|
|
|
None,
|
|
|
|
playground_button.as_ref().map(String::as_str)));
|
|
|
|
Some(Event::Html(s.into()))
|
|
|
|
})
|
2013-12-22 19:23:04 +00:00
|
|
|
}
|
2017-04-06 12:09:20 +00:00
|
|
|
}
|
2014-05-03 00:56:35 +00:00
|
|
|
|
2017-04-06 12:09:20 +00:00
|
|
|
/// Make headings links with anchor ids and build up TOC.
|
|
|
|
struct HeadingLinks<'a, 'b, I: Iterator<Item = Event<'a>>> {
|
|
|
|
inner: I,
|
|
|
|
toc: Option<&'b mut TocBuilder>,
|
|
|
|
buf: VecDeque<Event<'a>>,
|
|
|
|
}
|
2013-12-22 19:23:04 +00:00
|
|
|
|
2017-04-06 12:09:20 +00:00
|
|
|
impl<'a, 'b, I: Iterator<Item = Event<'a>>> HeadingLinks<'a, 'b, I> {
|
|
|
|
fn new(iter: I, toc: Option<&'b mut TocBuilder>) -> Self {
|
|
|
|
HeadingLinks {
|
|
|
|
inner: iter,
|
|
|
|
toc: toc,
|
|
|
|
buf: VecDeque::new(),
|
2017-03-30 01:48:06 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-06 12:09:20 +00:00
|
|
|
}
|
2017-03-30 01:48:06 +00:00
|
|
|
|
2017-04-06 12:09:20 +00:00
|
|
|
impl<'a, 'b, I: Iterator<Item = Event<'a>>> Iterator for HeadingLinks<'a, 'b, I> {
|
|
|
|
type Item = Event<'a>;
|
2017-03-10 13:06:24 +00:00
|
|
|
|
2017-04-06 12:09:20 +00:00
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
if let Some(e) = self.buf.pop_front() {
|
|
|
|
return Some(e);
|
2017-03-10 13:06:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-06 12:09:20 +00:00
|
|
|
let event = self.inner.next();
|
|
|
|
if let Some(Event::Start(Tag::Header(level))) = event {
|
|
|
|
let mut id = String::new();
|
|
|
|
for event in &mut self.inner {
|
|
|
|
match event {
|
|
|
|
Event::End(Tag::Header(..)) => break,
|
|
|
|
Event::Text(ref text) => id.extend(text.chars().filter_map(slugify)),
|
|
|
|
_ => {},
|
2017-03-10 13:06:24 +00:00
|
|
|
}
|
2017-04-06 12:09:20 +00:00
|
|
|
self.buf.push_back(event);
|
2017-03-10 13:06:24 +00:00
|
|
|
}
|
2017-04-06 12:09:20 +00:00
|
|
|
let id = derive_id(id);
|
2017-03-10 13:06:24 +00:00
|
|
|
|
2017-04-06 12:09:20 +00:00
|
|
|
if let Some(ref mut builder) = self.toc {
|
|
|
|
let mut html_header = String::new();
|
|
|
|
html::push_html(&mut html_header, self.buf.iter().cloned());
|
|
|
|
let sec = builder.push(level as u32, html_header, id.clone());
|
|
|
|
self.buf.push_front(Event::InlineHtml(format!("{} ", sec).into()));
|
2017-03-10 13:06:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-06 12:09:20 +00:00
|
|
|
self.buf.push_back(Event::InlineHtml(format!("</a></h{}>", level).into()));
|
2017-03-11 00:43:36 +00:00
|
|
|
|
2017-04-06 12:09:20 +00:00
|
|
|
let start_tags = format!("<h{level} id=\"{id}\" class=\"section-header\">\
|
|
|
|
<a href=\"#{id}\">",
|
|
|
|
id = id,
|
|
|
|
level = level);
|
|
|
|
return Some(Event::InlineHtml(start_tags.into()));
|
2017-03-11 00:43:36 +00:00
|
|
|
}
|
2017-04-06 12:09:20 +00:00
|
|
|
event
|
2017-03-11 00:43:36 +00:00
|
|
|
}
|
2017-04-06 12:09:20 +00:00
|
|
|
}
|
2017-03-11 00:43:36 +00:00
|
|
|
|
2017-04-06 12:09:20 +00:00
|
|
|
/// Extracts just the first paragraph.
|
|
|
|
struct SummaryLine<'a, I: Iterator<Item = Event<'a>>> {
|
|
|
|
inner: I,
|
|
|
|
started: bool,
|
|
|
|
depth: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, I: Iterator<Item = Event<'a>>> SummaryLine<'a, I> {
|
|
|
|
fn new(iter: I) -> Self {
|
|
|
|
SummaryLine {
|
|
|
|
inner: iter,
|
|
|
|
started: false,
|
|
|
|
depth: 0,
|
2017-03-11 00:43:36 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-06 12:09:20 +00:00
|
|
|
}
|
2017-03-11 00:43:36 +00:00
|
|
|
|
2017-04-06 12:09:20 +00:00
|
|
|
impl<'a, I: Iterator<Item = Event<'a>>> Iterator for SummaryLine<'a, I> {
|
|
|
|
type Item = Event<'a>;
|
2017-03-11 00:43:36 +00:00
|
|
|
|
2017-04-06 12:09:20 +00:00
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
if self.started && self.depth == 0 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
if !self.started {
|
|
|
|
self.started = true;
|
|
|
|
}
|
|
|
|
let event = self.inner.next();
|
|
|
|
match event {
|
|
|
|
Some(Event::Start(..)) => self.depth += 1,
|
|
|
|
Some(Event::End(..)) => self.depth -= 1,
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
event
|
2017-03-11 00:43:36 +00:00
|
|
|
}
|
2017-04-06 12:09:20 +00:00
|
|
|
}
|
2017-03-11 00:43:36 +00:00
|
|
|
|
2017-04-06 12:09:20 +00:00
|
|
|
/// Moves all footnote definitions to the end and add back links to the
|
|
|
|
/// references.
|
|
|
|
struct Footnotes<'a, I: Iterator<Item = Event<'a>>> {
|
|
|
|
inner: I,
|
|
|
|
footnotes: HashMap<String, (Vec<Event<'a>>, u16)>,
|
|
|
|
}
|
2017-03-30 01:48:06 +00:00
|
|
|
|
2017-04-06 12:09:20 +00:00
|
|
|
impl<'a, I: Iterator<Item = Event<'a>>> Footnotes<'a, I> {
|
|
|
|
fn new(iter: I) -> Self {
|
|
|
|
Footnotes {
|
|
|
|
inner: iter,
|
|
|
|
footnotes: HashMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn get_entry(&mut self, key: &str) -> &mut (Vec<Event<'a>>, u16) {
|
|
|
|
let new_id = self.footnotes.keys().count() + 1;
|
|
|
|
let key = key.to_owned();
|
|
|
|
self.footnotes.entry(key).or_insert((Vec::new(), new_id as u16))
|
2017-03-30 01:48:06 +00:00
|
|
|
}
|
2017-04-06 12:09:20 +00:00
|
|
|
}
|
2017-03-30 01:48:06 +00:00
|
|
|
|
2017-04-06 12:09:20 +00:00
|
|
|
impl<'a, I: Iterator<Item = Event<'a>>> Iterator for Footnotes<'a, I> {
|
|
|
|
type Item = Event<'a>;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
loop {
|
|
|
|
match self.inner.next() {
|
|
|
|
Some(Event::FootnoteReference(ref reference)) => {
|
|
|
|
let entry = self.get_entry(&reference);
|
|
|
|
let reference = format!("<sup id=\"supref{0}\"><a href=\"#ref{0}\">{0}\
|
|
|
|
</a></sup>",
|
|
|
|
(*entry).1);
|
|
|
|
return Some(Event::Html(reference.into()));
|
|
|
|
}
|
|
|
|
Some(Event::Start(Tag::FootnoteDefinition(def))) => {
|
|
|
|
let mut content = Vec::new();
|
|
|
|
for event in &mut self.inner {
|
|
|
|
if let Event::End(Tag::FootnoteDefinition(..)) = event {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
content.push(event);
|
|
|
|
}
|
|
|
|
let entry = self.get_entry(&def);
|
|
|
|
(*entry).0 = content;
|
|
|
|
}
|
|
|
|
Some(e) => return Some(e),
|
|
|
|
None => {
|
|
|
|
if !self.footnotes.is_empty() {
|
|
|
|
let mut v: Vec<_> = self.footnotes.drain().map(|(_, x)| x).collect();
|
|
|
|
v.sort_by(|a, b| a.1.cmp(&b.1));
|
|
|
|
let mut ret = String::from("<div class=\"footnotes\"><hr><ol>");
|
|
|
|
for (mut content, id) in v {
|
|
|
|
write!(ret, "<li id=\"ref{}\">", id).unwrap();
|
|
|
|
let mut is_paragraph = false;
|
|
|
|
if let Some(&Event::End(Tag::Paragraph)) = content.last() {
|
|
|
|
content.pop();
|
|
|
|
is_paragraph = true;
|
|
|
|
}
|
|
|
|
html::push_html(&mut ret, content.into_iter());
|
|
|
|
write!(ret,
|
|
|
|
" <a href=\"#supref{}\" rev=\"footnote\">↩</a>",
|
|
|
|
id).unwrap();
|
|
|
|
if is_paragraph {
|
|
|
|
ret.push_str("</p>");
|
|
|
|
}
|
|
|
|
ret.push_str("</li>");
|
|
|
|
}
|
|
|
|
ret.push_str("</ol></div>");
|
|
|
|
return Some(Event::Html(ret.into()));
|
|
|
|
} else {
|
|
|
|
return None;
|
2017-03-31 17:02:46 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-08 00:01:23 +00:00
|
|
|
}
|
2017-03-08 22:56:00 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-08 00:01:23 +00:00
|
|
|
}
|
2014-05-03 00:56:35 +00:00
|
|
|
|
2017-04-13 23:23:14 +00:00
|
|
|
const DEF_OUNIT: libc::size_t = 64;
|
|
|
|
const HOEDOWN_EXT_NO_INTRA_EMPHASIS: libc::c_uint = 1 << 11;
|
|
|
|
const HOEDOWN_EXT_TABLES: libc::c_uint = 1 << 0;
|
|
|
|
const HOEDOWN_EXT_FENCED_CODE: libc::c_uint = 1 << 1;
|
|
|
|
const HOEDOWN_EXT_AUTOLINK: libc::c_uint = 1 << 3;
|
|
|
|
const HOEDOWN_EXT_STRIKETHROUGH: libc::c_uint = 1 << 4;
|
|
|
|
const HOEDOWN_EXT_SUPERSCRIPT: libc::c_uint = 1 << 8;
|
|
|
|
const HOEDOWN_EXT_FOOTNOTES: libc::c_uint = 1 << 2;
|
2017-04-20 22:32:23 +00:00
|
|
|
const HOEDOWN_HTML_ESCAPE: libc::c_uint = 1 << 1;
|
2017-04-13 23:23:14 +00:00
|
|
|
|
|
|
|
const HOEDOWN_EXTENSIONS: libc::c_uint =
|
|
|
|
HOEDOWN_EXT_NO_INTRA_EMPHASIS | HOEDOWN_EXT_TABLES |
|
|
|
|
HOEDOWN_EXT_FENCED_CODE | HOEDOWN_EXT_AUTOLINK |
|
|
|
|
HOEDOWN_EXT_STRIKETHROUGH | HOEDOWN_EXT_SUPERSCRIPT |
|
|
|
|
HOEDOWN_EXT_FOOTNOTES;
|
|
|
|
|
|
|
|
enum hoedown_document {}
|
|
|
|
|
|
|
|
type blockcodefn = extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer,
|
|
|
|
*const hoedown_buffer, *const hoedown_renderer_data,
|
|
|
|
libc::size_t);
|
|
|
|
|
|
|
|
type blockquotefn = extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer,
|
|
|
|
*const hoedown_renderer_data, libc::size_t);
|
|
|
|
|
|
|
|
type headerfn = extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer,
|
|
|
|
libc::c_int, *const hoedown_renderer_data,
|
|
|
|
libc::size_t);
|
|
|
|
|
|
|
|
type blockhtmlfn = extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer,
|
|
|
|
*const hoedown_renderer_data, libc::size_t);
|
|
|
|
|
|
|
|
type codespanfn = extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer,
|
|
|
|
*const hoedown_renderer_data, libc::size_t) -> libc::c_int;
|
|
|
|
|
|
|
|
type linkfn = extern "C" fn (*mut hoedown_buffer, *const hoedown_buffer,
|
|
|
|
*const hoedown_buffer, *const hoedown_buffer,
|
|
|
|
*const hoedown_renderer_data, libc::size_t) -> libc::c_int;
|
|
|
|
|
|
|
|
type entityfn = extern "C" fn (*mut hoedown_buffer, *const hoedown_buffer,
|
|
|
|
*const hoedown_renderer_data, libc::size_t);
|
|
|
|
|
|
|
|
type normaltextfn = extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer,
|
|
|
|
*const hoedown_renderer_data, libc::size_t);
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
struct hoedown_renderer_data {
|
|
|
|
opaque: *mut libc::c_void,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
struct hoedown_renderer {
|
|
|
|
opaque: *mut libc::c_void,
|
|
|
|
|
|
|
|
blockcode: Option<blockcodefn>,
|
|
|
|
blockquote: Option<blockquotefn>,
|
|
|
|
header: Option<headerfn>,
|
|
|
|
|
|
|
|
other_block_level_callbacks: [libc::size_t; 11],
|
|
|
|
|
|
|
|
blockhtml: Option<blockhtmlfn>,
|
|
|
|
|
|
|
|
/* span level callbacks - NULL or return 0 prints the span verbatim */
|
|
|
|
autolink: libc::size_t, // unused
|
|
|
|
codespan: Option<codespanfn>,
|
|
|
|
other_span_level_callbacks_1: [libc::size_t; 7],
|
|
|
|
link: Option<linkfn>,
|
|
|
|
other_span_level_callbacks_2: [libc::size_t; 6],
|
|
|
|
|
|
|
|
/* low level callbacks - NULL copies input directly into the output */
|
|
|
|
entity: Option<entityfn>,
|
|
|
|
normal_text: Option<normaltextfn>,
|
|
|
|
|
|
|
|
/* header and footer */
|
|
|
|
other_callbacks: [libc::size_t; 2],
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
struct hoedown_html_renderer_state {
|
|
|
|
opaque: *mut libc::c_void,
|
|
|
|
toc_data: html_toc_data,
|
|
|
|
flags: libc::c_uint,
|
|
|
|
link_attributes: Option<extern "C" fn(*mut hoedown_buffer,
|
|
|
|
*const hoedown_buffer,
|
|
|
|
*const hoedown_renderer_data)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
struct html_toc_data {
|
|
|
|
header_count: libc::c_int,
|
|
|
|
current_level: libc::c_int,
|
|
|
|
level_offset: libc::c_int,
|
|
|
|
nesting_level: libc::c_int,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
struct hoedown_buffer {
|
|
|
|
data: *const u8,
|
|
|
|
size: libc::size_t,
|
|
|
|
asize: libc::size_t,
|
|
|
|
unit: libc::size_t,
|
|
|
|
}
|
|
|
|
|
2017-04-20 22:32:23 +00:00
|
|
|
struct MyOpaque {
|
|
|
|
dfltblk: extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer,
|
|
|
|
*const hoedown_buffer, *const hoedown_renderer_data,
|
|
|
|
libc::size_t),
|
|
|
|
toc_builder: Option<TocBuilder>,
|
|
|
|
}
|
|
|
|
|
2017-04-13 23:23:14 +00:00
|
|
|
extern {
|
|
|
|
fn hoedown_html_renderer_new(render_flags: libc::c_uint,
|
|
|
|
nesting_level: libc::c_int)
|
|
|
|
-> *mut hoedown_renderer;
|
|
|
|
fn hoedown_html_renderer_free(renderer: *mut hoedown_renderer);
|
|
|
|
|
|
|
|
fn hoedown_document_new(rndr: *const hoedown_renderer,
|
|
|
|
extensions: libc::c_uint,
|
|
|
|
max_nesting: libc::size_t) -> *mut hoedown_document;
|
|
|
|
fn hoedown_document_render(doc: *mut hoedown_document,
|
|
|
|
ob: *mut hoedown_buffer,
|
|
|
|
document: *const u8,
|
|
|
|
doc_size: libc::size_t);
|
|
|
|
fn hoedown_document_free(md: *mut hoedown_document);
|
|
|
|
|
|
|
|
fn hoedown_buffer_new(unit: libc::size_t) -> *mut hoedown_buffer;
|
2017-04-20 22:32:23 +00:00
|
|
|
fn hoedown_buffer_puts(b: *mut hoedown_buffer, c: *const libc::c_char);
|
2017-04-13 23:23:14 +00:00
|
|
|
fn hoedown_buffer_free(b: *mut hoedown_buffer);
|
2017-08-05 21:54:48 +00:00
|
|
|
fn hoedown_buffer_put(b: *mut hoedown_buffer, c: *const libc::c_char, len: libc::size_t);
|
2017-04-13 23:23:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl hoedown_buffer {
|
|
|
|
fn as_bytes(&self) -> &[u8] {
|
|
|
|
unsafe { slice::from_raw_parts(self.data, self.size as usize) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 22:32:23 +00:00
|
|
|
pub fn render(w: &mut fmt::Formatter,
|
|
|
|
s: &str,
|
|
|
|
print_toc: bool,
|
|
|
|
html_flags: libc::c_uint) -> fmt::Result {
|
|
|
|
extern fn block(ob: *mut hoedown_buffer, orig_text: *const hoedown_buffer,
|
|
|
|
lang: *const hoedown_buffer, data: *const hoedown_renderer_data,
|
|
|
|
line: libc::size_t) {
|
|
|
|
unsafe {
|
|
|
|
if orig_text.is_null() { return }
|
|
|
|
|
|
|
|
let opaque = (*data).opaque as *mut hoedown_html_renderer_state;
|
|
|
|
let my_opaque: &MyOpaque = &*((*opaque).opaque as *const MyOpaque);
|
|
|
|
let text = (*orig_text).as_bytes();
|
|
|
|
let origtext = str::from_utf8(text).unwrap();
|
|
|
|
let origtext = origtext.trim_left();
|
|
|
|
debug!("docblock: ==============\n{:?}\n=======", text);
|
|
|
|
let rendered = if lang.is_null() || origtext.is_empty() {
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
let rlang = (*lang).as_bytes();
|
|
|
|
let rlang = str::from_utf8(rlang).unwrap();
|
|
|
|
if !LangString::parse(rlang).rust {
|
|
|
|
(my_opaque.dfltblk)(ob, orig_text, lang,
|
|
|
|
opaque as *const hoedown_renderer_data,
|
|
|
|
line);
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-05-06 13:08:41 +00:00
|
|
|
let lines = origtext.lines().filter_map(|l| map_line(l).for_html());
|
2017-04-20 22:32:23 +00:00
|
|
|
let text = lines.collect::<Vec<&str>>().join("\n");
|
|
|
|
if rendered { return }
|
|
|
|
PLAYGROUND.with(|play| {
|
|
|
|
// insert newline to clearly separate it from the
|
|
|
|
// previous block so we can shorten the html output
|
|
|
|
let mut s = String::from("\n");
|
|
|
|
let playground_button = play.borrow().as_ref().and_then(|&(ref krate, ref url)| {
|
|
|
|
if url.is_empty() {
|
|
|
|
return None;
|
|
|
|
}
|
2017-05-06 13:08:41 +00:00
|
|
|
let test = origtext.lines()
|
|
|
|
.map(|l| map_line(l).for_code())
|
|
|
|
.collect::<Vec<&str>>().join("\n");
|
2017-04-20 22:32:23 +00:00
|
|
|
let krate = krate.as_ref().map(|s| &**s);
|
|
|
|
let test = test::maketest(&test, krate, false,
|
|
|
|
&Default::default());
|
|
|
|
let channel = if test.contains("#![feature(") {
|
|
|
|
"&version=nightly"
|
|
|
|
} else {
|
|
|
|
""
|
|
|
|
};
|
|
|
|
// These characters don't need to be escaped in a URI.
|
|
|
|
// FIXME: use a library function for percent encoding.
|
|
|
|
fn dont_escape(c: u8) -> bool {
|
|
|
|
(b'a' <= c && c <= b'z') ||
|
|
|
|
(b'A' <= c && c <= b'Z') ||
|
|
|
|
(b'0' <= c && c <= b'9') ||
|
|
|
|
c == b'-' || c == b'_' || c == b'.' ||
|
|
|
|
c == b'~' || c == b'!' || c == b'\'' ||
|
|
|
|
c == b'(' || c == b')' || c == b'*'
|
|
|
|
}
|
|
|
|
let mut test_escaped = String::new();
|
|
|
|
for b in test.bytes() {
|
|
|
|
if dont_escape(b) {
|
|
|
|
test_escaped.push(char::from(b));
|
|
|
|
} else {
|
|
|
|
write!(test_escaped, "%{:02X}", b).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(format!(
|
|
|
|
r#"<a class="test-arrow" target="_blank" href="{}?code={}{}">Run</a>"#,
|
|
|
|
url, test_escaped, channel
|
|
|
|
))
|
|
|
|
});
|
|
|
|
s.push_str(&highlight::render_with_highlighting(
|
|
|
|
&text,
|
|
|
|
Some("rust-example-rendered"),
|
|
|
|
None,
|
|
|
|
playground_button.as_ref().map(String::as_str)));
|
2017-08-05 21:54:48 +00:00
|
|
|
hoedown_buffer_put(ob, s.as_ptr() as *const libc::c_char, s.len());
|
2017-04-20 22:32:23 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern fn header(ob: *mut hoedown_buffer, text: *const hoedown_buffer,
|
|
|
|
level: libc::c_int, data: *const hoedown_renderer_data,
|
|
|
|
_: libc::size_t) {
|
|
|
|
// hoedown does this, we may as well too
|
|
|
|
unsafe { hoedown_buffer_puts(ob, "\n\0".as_ptr() as *const _); }
|
|
|
|
|
|
|
|
// Extract the text provided
|
|
|
|
let s = if text.is_null() {
|
|
|
|
"".to_owned()
|
|
|
|
} else {
|
|
|
|
let s = unsafe { (*text).as_bytes() };
|
|
|
|
str::from_utf8(&s).unwrap().to_owned()
|
|
|
|
};
|
|
|
|
|
|
|
|
// Discard '<em>', '<code>' tags and some escaped characters,
|
|
|
|
// transform the contents of the header into a hyphenated string
|
|
|
|
// without non-alphanumeric characters other than '-' and '_'.
|
|
|
|
//
|
|
|
|
// This is a terrible hack working around how hoedown gives us rendered
|
|
|
|
// html for text rather than the raw text.
|
|
|
|
let mut id = s.clone();
|
|
|
|
let repl_sub = vec!["<em>", "</em>", "<code>", "</code>",
|
|
|
|
"<strong>", "</strong>",
|
|
|
|
"<", ">", "&", "'", """];
|
|
|
|
for sub in repl_sub {
|
|
|
|
id = id.replace(sub, "");
|
|
|
|
}
|
|
|
|
let id = id.chars().filter_map(|c| {
|
|
|
|
if c.is_alphanumeric() || c == '-' || c == '_' {
|
|
|
|
if c.is_ascii() {
|
|
|
|
Some(c.to_ascii_lowercase())
|
|
|
|
} else {
|
|
|
|
Some(c)
|
|
|
|
}
|
|
|
|
} else if c.is_whitespace() && c.is_ascii() {
|
|
|
|
Some('-')
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}).collect::<String>();
|
|
|
|
|
|
|
|
let opaque = unsafe { (*data).opaque as *mut hoedown_html_renderer_state };
|
|
|
|
let opaque = unsafe { &mut *((*opaque).opaque as *mut MyOpaque) };
|
|
|
|
|
|
|
|
let id = derive_id(id);
|
|
|
|
|
|
|
|
let sec = opaque.toc_builder.as_mut().map_or("".to_owned(), |builder| {
|
|
|
|
format!("{} ", builder.push(level as u32, s.clone(), id.clone()))
|
|
|
|
});
|
|
|
|
|
|
|
|
// Render the HTML
|
|
|
|
let text = format!("<h{lvl} id='{id}' class='section-header'>\
|
|
|
|
<a href='#{id}'>{sec}{}</a></h{lvl}>",
|
|
|
|
s, lvl = level, id = id, sec = sec);
|
|
|
|
|
2017-08-05 21:54:48 +00:00
|
|
|
unsafe { hoedown_buffer_put(ob, text.as_ptr() as *const libc::c_char, text.len()); }
|
2017-04-20 22:32:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extern fn codespan(
|
|
|
|
ob: *mut hoedown_buffer,
|
|
|
|
text: *const hoedown_buffer,
|
|
|
|
_: *const hoedown_renderer_data,
|
|
|
|
_: libc::size_t
|
|
|
|
) -> libc::c_int {
|
|
|
|
let content = if text.is_null() {
|
|
|
|
"".to_owned()
|
|
|
|
} else {
|
|
|
|
let bytes = unsafe { (*text).as_bytes() };
|
|
|
|
let s = str::from_utf8(bytes).unwrap();
|
|
|
|
collapse_whitespace(s)
|
|
|
|
};
|
|
|
|
|
2017-08-05 21:54:48 +00:00
|
|
|
let content = format!("<code>{}</code>", Escape(&content)).replace("\0", "\\0");
|
|
|
|
unsafe {
|
|
|
|
hoedown_buffer_put(ob, content.as_ptr() as *const libc::c_char, content.len());
|
|
|
|
}
|
2017-04-20 22:32:23 +00:00
|
|
|
// Return anything except 0, which would mean "also print the code span verbatim".
|
|
|
|
1
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let ob = hoedown_buffer_new(DEF_OUNIT);
|
|
|
|
let renderer = hoedown_html_renderer_new(html_flags, 0);
|
|
|
|
let mut opaque = MyOpaque {
|
|
|
|
dfltblk: (*renderer).blockcode.unwrap(),
|
|
|
|
toc_builder: if print_toc {Some(TocBuilder::new())} else {None}
|
|
|
|
};
|
|
|
|
(*((*renderer).opaque as *mut hoedown_html_renderer_state)).opaque
|
|
|
|
= &mut opaque as *mut _ as *mut libc::c_void;
|
|
|
|
(*renderer).blockcode = Some(block);
|
|
|
|
(*renderer).header = Some(header);
|
|
|
|
(*renderer).codespan = Some(codespan);
|
|
|
|
|
|
|
|
let document = hoedown_document_new(renderer, HOEDOWN_EXTENSIONS, 16);
|
|
|
|
hoedown_document_render(document, ob, s.as_ptr(),
|
|
|
|
s.len() as libc::size_t);
|
|
|
|
hoedown_document_free(document);
|
|
|
|
|
|
|
|
hoedown_html_renderer_free(renderer);
|
|
|
|
|
|
|
|
let mut ret = opaque.toc_builder.map_or(Ok(()), |builder| {
|
|
|
|
write!(w, "<nav id=\"TOC\">{}</nav>", builder.into_toc())
|
|
|
|
});
|
|
|
|
|
|
|
|
if ret.is_ok() {
|
|
|
|
let buf = (*ob).as_bytes();
|
|
|
|
ret = w.write_str(str::from_utf8(buf).unwrap());
|
|
|
|
}
|
|
|
|
hoedown_buffer_free(ob);
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-13 23:23:14 +00:00
|
|
|
pub fn old_find_testable_code(doc: &str, tests: &mut ::test::Collector, position: Span) {
|
|
|
|
extern fn block(_ob: *mut hoedown_buffer,
|
|
|
|
text: *const hoedown_buffer,
|
|
|
|
lang: *const hoedown_buffer,
|
|
|
|
data: *const hoedown_renderer_data,
|
2017-04-22 12:56:36 +00:00
|
|
|
line: libc::size_t) {
|
2017-04-13 23:23:14 +00:00
|
|
|
unsafe {
|
|
|
|
if text.is_null() { return }
|
|
|
|
let block_info = if lang.is_null() {
|
|
|
|
LangString::all_false()
|
|
|
|
} else {
|
|
|
|
let lang = (*lang).as_bytes();
|
|
|
|
let s = str::from_utf8(lang).unwrap();
|
|
|
|
LangString::parse(s)
|
|
|
|
};
|
|
|
|
if !block_info.rust { return }
|
2017-04-19 22:31:34 +00:00
|
|
|
let text = (*text).as_bytes();
|
2017-04-13 23:23:14 +00:00
|
|
|
let opaque = (*data).opaque as *mut hoedown_html_renderer_state;
|
|
|
|
let tests = &mut *((*opaque).opaque as *mut ::test::Collector);
|
2017-04-19 22:31:34 +00:00
|
|
|
let text = str::from_utf8(text).unwrap();
|
2017-05-06 13:08:41 +00:00
|
|
|
let lines = text.lines().map(|l| map_line(l).for_code());
|
2017-04-22 12:56:36 +00:00
|
|
|
let text = lines.collect::<Vec<&str>>().join("\n");
|
2017-04-13 23:23:14 +00:00
|
|
|
let filename = tests.get_filename();
|
2017-04-20 22:32:23 +00:00
|
|
|
|
|
|
|
if tests.render_type == RenderType::Hoedown {
|
2017-04-22 12:56:36 +00:00
|
|
|
let line = tests.get_line() + line;
|
2017-04-20 22:32:23 +00:00
|
|
|
tests.add_test(text.to_owned(),
|
|
|
|
block_info.should_panic, block_info.no_run,
|
|
|
|
block_info.ignore, block_info.test_harness,
|
|
|
|
block_info.compile_fail, block_info.error_codes,
|
2017-05-24 17:58:37 +00:00
|
|
|
line, filename, block_info.allow_fail);
|
2017-04-20 22:32:23 +00:00
|
|
|
} else {
|
2017-04-22 12:56:36 +00:00
|
|
|
tests.add_old_test(text, filename);
|
2017-04-20 22:32:23 +00:00
|
|
|
}
|
2017-04-13 23:23:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern fn header(_ob: *mut hoedown_buffer,
|
|
|
|
text: *const hoedown_buffer,
|
|
|
|
level: libc::c_int, data: *const hoedown_renderer_data,
|
|
|
|
_: libc::size_t) {
|
|
|
|
unsafe {
|
|
|
|
let opaque = (*data).opaque as *mut hoedown_html_renderer_state;
|
|
|
|
let tests = &mut *((*opaque).opaque as *mut ::test::Collector);
|
|
|
|
if text.is_null() {
|
|
|
|
tests.register_header("", level as u32);
|
|
|
|
} else {
|
|
|
|
let text = (*text).as_bytes();
|
|
|
|
let text = str::from_utf8(text).unwrap();
|
|
|
|
tests.register_header(text, level as u32);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tests.set_position(position);
|
|
|
|
unsafe {
|
|
|
|
let ob = hoedown_buffer_new(DEF_OUNIT);
|
|
|
|
let renderer = hoedown_html_renderer_new(0, 0);
|
|
|
|
(*renderer).blockcode = Some(block);
|
|
|
|
(*renderer).header = Some(header);
|
|
|
|
(*((*renderer).opaque as *mut hoedown_html_renderer_state)).opaque
|
|
|
|
= tests as *mut _ as *mut libc::c_void;
|
|
|
|
|
|
|
|
let document = hoedown_document_new(renderer, HOEDOWN_EXTENSIONS, 16);
|
|
|
|
hoedown_document_render(document, ob, doc.as_ptr(),
|
|
|
|
doc.len() as libc::size_t);
|
|
|
|
hoedown_document_free(document);
|
|
|
|
|
|
|
|
hoedown_html_renderer_free(renderer);
|
|
|
|
hoedown_buffer_free(ob);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-08 00:01:23 +00:00
|
|
|
pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector, position: Span) {
|
|
|
|
tests.set_position(position);
|
|
|
|
|
|
|
|
let mut parser = Parser::new(doc);
|
|
|
|
let mut prev_offset = 0;
|
|
|
|
let mut nb_lines = 0;
|
|
|
|
let mut register_header = None;
|
2017-03-10 13:06:24 +00:00
|
|
|
'main: while let Some(event) = parser.next() {
|
|
|
|
match event {
|
|
|
|
Event::Start(Tag::CodeBlock(s)) => {
|
|
|
|
let block_info = if s.is_empty() {
|
|
|
|
LangString::all_false()
|
|
|
|
} else {
|
|
|
|
LangString::parse(&*s)
|
|
|
|
};
|
|
|
|
if !block_info.rust {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
let mut test_s = String::new();
|
|
|
|
let mut offset = None;
|
|
|
|
loop {
|
|
|
|
let event = parser.next();
|
|
|
|
if let Some(event) = event {
|
|
|
|
match event {
|
|
|
|
Event::End(Tag::CodeBlock(_)) => break,
|
|
|
|
Event::Text(ref s) => {
|
|
|
|
test_s.push_str(s);
|
|
|
|
if offset.is_none() {
|
|
|
|
offset = Some(parser.get_offset());
|
2017-03-08 00:01:23 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-10 13:06:24 +00:00
|
|
|
_ => {}
|
2017-03-08 00:01:23 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-03-10 13:06:24 +00:00
|
|
|
break 'main;
|
2017-03-08 00:01:23 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-10 13:06:24 +00:00
|
|
|
let offset = offset.unwrap_or(0);
|
2017-05-06 13:08:41 +00:00
|
|
|
let lines = test_s.lines().map(|l| map_line(l).for_code());
|
2017-03-10 13:06:24 +00:00
|
|
|
let text = lines.collect::<Vec<&str>>().join("\n");
|
|
|
|
nb_lines += doc[prev_offset..offset].lines().count();
|
|
|
|
let line = tests.get_line() + (nb_lines - 1);
|
|
|
|
let filename = tests.get_filename();
|
|
|
|
tests.add_test(text.to_owned(),
|
|
|
|
block_info.should_panic, block_info.no_run,
|
|
|
|
block_info.ignore, block_info.test_harness,
|
|
|
|
block_info.compile_fail, block_info.error_codes,
|
2017-05-24 17:58:37 +00:00
|
|
|
line, filename, block_info.allow_fail);
|
2017-03-10 13:06:24 +00:00
|
|
|
prev_offset = offset;
|
2017-03-08 00:01:23 +00:00
|
|
|
}
|
2017-03-10 13:06:24 +00:00
|
|
|
Event::Start(Tag::Header(level)) => {
|
|
|
|
register_header = Some(level as u32);
|
|
|
|
}
|
|
|
|
Event::Text(ref s) if register_header.is_some() => {
|
|
|
|
let level = register_header.unwrap();
|
|
|
|
if s.is_empty() {
|
|
|
|
tests.register_header("", level);
|
|
|
|
} else {
|
|
|
|
tests.register_header(s, level);
|
|
|
|
}
|
|
|
|
register_header = None;
|
|
|
|
}
|
|
|
|
_ => {}
|
2017-03-08 00:01:23 +00:00
|
|
|
}
|
2013-12-22 19:23:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-28 13:34:18 +00:00
|
|
|
#[derive(Eq, PartialEq, Clone, Debug)]
|
2014-06-19 12:38:01 +00:00
|
|
|
struct LangString {
|
2016-09-07 14:43:18 +00:00
|
|
|
original: String,
|
2015-03-26 20:30:33 +00:00
|
|
|
should_panic: bool,
|
2014-06-19 12:38:01 +00:00
|
|
|
no_run: bool,
|
|
|
|
ignore: bool,
|
2014-12-10 11:17:14 +00:00
|
|
|
rust: bool,
|
2014-06-19 13:11:18 +00:00
|
|
|
test_harness: bool,
|
2016-01-05 22:38:11 +00:00
|
|
|
compile_fail: bool,
|
2016-06-09 21:50:52 +00:00
|
|
|
error_codes: Vec<String>,
|
2017-05-24 17:58:37 +00:00
|
|
|
allow_fail: bool,
|
2014-06-19 12:38:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LangString {
|
|
|
|
fn all_false() -> LangString {
|
|
|
|
LangString {
|
2016-09-07 14:43:18 +00:00
|
|
|
original: String::new(),
|
2015-03-26 20:30:33 +00:00
|
|
|
should_panic: false,
|
2014-06-19 12:38:01 +00:00
|
|
|
no_run: false,
|
|
|
|
ignore: false,
|
2014-12-24 02:47:32 +00:00
|
|
|
rust: true, // NB This used to be `notrust = false`
|
2014-06-19 13:11:18 +00:00
|
|
|
test_harness: false,
|
2016-01-05 22:38:11 +00:00
|
|
|
compile_fail: false,
|
2016-06-09 21:50:52 +00:00
|
|
|
error_codes: Vec::new(),
|
2017-05-24 17:58:37 +00:00
|
|
|
allow_fail: false,
|
2014-05-31 22:33:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-19 12:38:01 +00:00
|
|
|
fn parse(string: &str) -> LangString {
|
|
|
|
let mut seen_rust_tags = false;
|
|
|
|
let mut seen_other_tags = false;
|
|
|
|
let mut data = LangString::all_false();
|
2016-06-09 21:50:52 +00:00
|
|
|
let mut allow_compile_fail = false;
|
|
|
|
let mut allow_error_code_check = false;
|
2016-09-24 17:26:18 +00:00
|
|
|
if UnstableFeatures::from_environment().is_nightly_build() {
|
|
|
|
allow_compile_fail = true;
|
|
|
|
allow_error_code_check = true;
|
|
|
|
}
|
2014-06-19 12:38:01 +00:00
|
|
|
|
2016-09-07 14:43:18 +00:00
|
|
|
data.original = string.to_owned();
|
2015-02-01 17:44:15 +00:00
|
|
|
let tokens = string.split(|c: char|
|
2014-06-19 12:38:01 +00:00
|
|
|
!(c == '_' || c == '-' || c.is_alphanumeric())
|
|
|
|
);
|
|
|
|
|
|
|
|
for token in tokens {
|
2017-04-10 12:35:28 +00:00
|
|
|
match token.trim() {
|
2014-06-19 12:38:01 +00:00
|
|
|
"" => {},
|
2017-04-09 16:31:59 +00:00
|
|
|
"should_panic" => {
|
|
|
|
data.should_panic = true;
|
|
|
|
seen_rust_tags = seen_other_tags == false;
|
|
|
|
}
|
2017-04-10 12:35:28 +00:00
|
|
|
"no_run" => { data.no_run = true; seen_rust_tags = !seen_other_tags; }
|
|
|
|
"ignore" => { data.ignore = true; seen_rust_tags = !seen_other_tags; }
|
2017-05-24 17:58:37 +00:00
|
|
|
"allow_fail" => { data.allow_fail = true; seen_rust_tags = !seen_other_tags; }
|
2017-04-09 16:31:59 +00:00
|
|
|
"rust" => { data.rust = true; seen_rust_tags = true; }
|
|
|
|
"test_harness" => {
|
|
|
|
data.test_harness = true;
|
2017-04-10 12:35:28 +00:00
|
|
|
seen_rust_tags = !seen_other_tags || seen_rust_tags;
|
2017-04-09 16:31:59 +00:00
|
|
|
}
|
2016-02-09 06:18:35 +00:00
|
|
|
"compile_fail" if allow_compile_fail => {
|
|
|
|
data.compile_fail = true;
|
2017-04-10 12:35:28 +00:00
|
|
|
seen_rust_tags = !seen_other_tags || seen_rust_tags;
|
2016-02-09 06:18:35 +00:00
|
|
|
data.no_run = true;
|
2016-06-09 21:50:52 +00:00
|
|
|
}
|
|
|
|
x if allow_error_code_check && x.starts_with("E") && x.len() == 5 => {
|
|
|
|
if let Ok(_) = x[1..].parse::<u32>() {
|
|
|
|
data.error_codes.push(x.to_owned());
|
2017-04-10 12:35:28 +00:00
|
|
|
seen_rust_tags = !seen_other_tags || seen_rust_tags;
|
2016-06-09 21:50:52 +00:00
|
|
|
} else {
|
|
|
|
seen_other_tags = true;
|
|
|
|
}
|
|
|
|
}
|
2014-06-19 12:38:01 +00:00
|
|
|
_ => { seen_other_tags = true }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-24 02:47:32 +00:00
|
|
|
data.rust &= !seen_other_tags || seen_rust_tags;
|
2014-05-31 22:33:32 +00:00
|
|
|
|
2014-06-19 12:38:01 +00:00
|
|
|
data
|
|
|
|
}
|
2014-05-31 22:33:32 +00:00
|
|
|
}
|
|
|
|
|
2015-01-20 23:45:07 +00:00
|
|
|
impl<'a> fmt::Display for Markdown<'a> {
|
2014-02-05 12:55:13 +00:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
2017-04-20 22:32:23 +00:00
|
|
|
let Markdown(md, render_type) = *self;
|
|
|
|
|
2013-09-23 23:55:48 +00:00
|
|
|
// This is actually common enough to special-case
|
2015-03-24 23:53:34 +00:00
|
|
|
if md.is_empty() { return Ok(()) }
|
2017-04-20 22:32:23 +00:00
|
|
|
if render_type == RenderType::Hoedown {
|
|
|
|
render(fmt, md, false, 0)
|
|
|
|
} else {
|
|
|
|
let mut opts = Options::empty();
|
|
|
|
opts.insert(OPTION_ENABLE_TABLES);
|
|
|
|
opts.insert(OPTION_ENABLE_FOOTNOTES);
|
2017-04-06 12:09:20 +00:00
|
|
|
|
2017-04-20 22:32:23 +00:00
|
|
|
let p = Parser::new_ext(md, opts);
|
2017-04-06 12:09:20 +00:00
|
|
|
|
2017-04-20 22:32:23 +00:00
|
|
|
let mut s = String::with_capacity(md.len() * 3 / 2);
|
2017-04-06 12:09:20 +00:00
|
|
|
|
2017-04-20 22:32:23 +00:00
|
|
|
html::push_html(&mut s,
|
|
|
|
Footnotes::new(CodeBlocks::new(HeadingLinks::new(p, None))));
|
2017-04-06 12:09:20 +00:00
|
|
|
|
2017-04-20 22:32:23 +00:00
|
|
|
fmt.write_str(&s)
|
|
|
|
}
|
2014-03-07 14:13:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-20 23:45:07 +00:00
|
|
|
impl<'a> fmt::Display for MarkdownWithToc<'a> {
|
2014-03-07 14:13:17 +00:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
2017-04-20 22:32:23 +00:00
|
|
|
let MarkdownWithToc(md, render_type) = *self;
|
2017-04-06 12:09:20 +00:00
|
|
|
|
2017-04-20 22:32:23 +00:00
|
|
|
if render_type == RenderType::Hoedown {
|
|
|
|
render(fmt, md, true, 0)
|
|
|
|
} else {
|
|
|
|
let mut opts = Options::empty();
|
|
|
|
opts.insert(OPTION_ENABLE_TABLES);
|
|
|
|
opts.insert(OPTION_ENABLE_FOOTNOTES);
|
2017-04-06 12:09:20 +00:00
|
|
|
|
2017-04-20 22:32:23 +00:00
|
|
|
let p = Parser::new_ext(md, opts);
|
2017-04-06 12:09:20 +00:00
|
|
|
|
2017-04-20 22:32:23 +00:00
|
|
|
let mut s = String::with_capacity(md.len() * 3 / 2);
|
2017-04-06 12:09:20 +00:00
|
|
|
|
2017-04-20 22:32:23 +00:00
|
|
|
let mut toc = TocBuilder::new();
|
2017-04-06 12:09:20 +00:00
|
|
|
|
2017-04-20 22:32:23 +00:00
|
|
|
html::push_html(&mut s,
|
|
|
|
Footnotes::new(CodeBlocks::new(HeadingLinks::new(p, Some(&mut toc)))));
|
2017-04-06 12:09:20 +00:00
|
|
|
|
2017-04-20 22:32:23 +00:00
|
|
|
write!(fmt, "<nav id=\"TOC\">{}</nav>", toc.into_toc())?;
|
2017-04-06 12:09:20 +00:00
|
|
|
|
2017-04-20 22:32:23 +00:00
|
|
|
fmt.write_str(&s)
|
|
|
|
}
|
2016-12-12 23:18:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> fmt::Display for MarkdownHtml<'a> {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
2017-04-20 22:32:23 +00:00
|
|
|
let MarkdownHtml(md, render_type) = *self;
|
|
|
|
|
2016-12-12 23:18:22 +00:00
|
|
|
// This is actually common enough to special-case
|
|
|
|
if md.is_empty() { return Ok(()) }
|
2017-04-20 22:32:23 +00:00
|
|
|
if render_type == RenderType::Hoedown {
|
|
|
|
render(fmt, md, false, HOEDOWN_HTML_ESCAPE)
|
|
|
|
} else {
|
|
|
|
let mut opts = Options::empty();
|
|
|
|
opts.insert(OPTION_ENABLE_TABLES);
|
|
|
|
opts.insert(OPTION_ENABLE_FOOTNOTES);
|
2017-04-06 12:09:20 +00:00
|
|
|
|
2017-04-20 22:32:23 +00:00
|
|
|
let p = Parser::new_ext(md, opts);
|
2017-04-06 12:09:20 +00:00
|
|
|
|
2017-04-20 22:32:23 +00:00
|
|
|
// Treat inline HTML as plain text.
|
|
|
|
let p = p.map(|event| match event {
|
|
|
|
Event::Html(text) | Event::InlineHtml(text) => Event::Text(text),
|
|
|
|
_ => event
|
|
|
|
});
|
2017-04-06 12:09:20 +00:00
|
|
|
|
2017-04-20 22:32:23 +00:00
|
|
|
let mut s = String::with_capacity(md.len() * 3 / 2);
|
2017-04-06 12:09:20 +00:00
|
|
|
|
2017-04-20 22:32:23 +00:00
|
|
|
html::push_html(&mut s,
|
|
|
|
Footnotes::new(CodeBlocks::new(HeadingLinks::new(p, None))));
|
2017-04-06 12:09:20 +00:00
|
|
|
|
2017-04-20 22:32:23 +00:00
|
|
|
fmt.write_str(&s)
|
|
|
|
}
|
2017-04-06 12:09:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> fmt::Display for MarkdownSummaryLine<'a> {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
let MarkdownSummaryLine(md) = *self;
|
|
|
|
// This is actually common enough to special-case
|
|
|
|
if md.is_empty() { return Ok(()) }
|
|
|
|
|
|
|
|
let p = Parser::new(md);
|
|
|
|
|
|
|
|
let mut s = String::new();
|
|
|
|
|
|
|
|
html::push_html(&mut s, SummaryLine::new(p));
|
|
|
|
|
|
|
|
fmt.write_str(&s)
|
2013-09-19 05:18:38 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-31 22:33:32 +00:00
|
|
|
|
2014-12-25 05:39:29 +00:00
|
|
|
pub fn plain_summary_line(md: &str) -> String {
|
2017-03-08 00:01:23 +00:00
|
|
|
struct ParserWrapper<'a> {
|
|
|
|
inner: Parser<'a>,
|
|
|
|
is_in: isize,
|
|
|
|
is_first: bool,
|
2014-12-25 05:39:29 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 00:01:23 +00:00
|
|
|
impl<'a> Iterator for ParserWrapper<'a> {
|
|
|
|
type Item = String;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<String> {
|
|
|
|
let next_event = self.inner.next();
|
|
|
|
if next_event.is_none() {
|
|
|
|
return None
|
|
|
|
}
|
|
|
|
let next_event = next_event.unwrap();
|
|
|
|
let (ret, is_in) = match next_event {
|
2017-03-10 13:06:24 +00:00
|
|
|
Event::Start(Tag::Paragraph) => (None, 1),
|
2017-03-25 00:48:33 +00:00
|
|
|
Event::Start(Tag::Code) => (Some("`".to_owned()), 1),
|
|
|
|
Event::End(Tag::Code) => (Some("`".to_owned()), -1),
|
|
|
|
Event::Start(Tag::Header(_)) => (None, 1),
|
2017-03-10 13:06:24 +00:00
|
|
|
Event::Text(ref s) if self.is_in > 0 => (Some(s.as_ref().to_owned()), 0),
|
2017-03-25 00:48:33 +00:00
|
|
|
Event::End(Tag::Paragraph) | Event::End(Tag::Header(_)) => (None, -1),
|
2017-03-08 00:01:23 +00:00
|
|
|
_ => (None, 0),
|
|
|
|
};
|
|
|
|
if is_in > 0 || (is_in < 0 && self.is_in > 0) {
|
|
|
|
self.is_in += is_in;
|
|
|
|
}
|
|
|
|
if ret.is_some() {
|
|
|
|
self.is_first = false;
|
|
|
|
ret
|
|
|
|
} else {
|
|
|
|
Some(String::new())
|
|
|
|
}
|
2014-12-25 05:39:29 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-08 00:01:23 +00:00
|
|
|
let mut s = String::with_capacity(md.len() * 3 / 2);
|
|
|
|
let mut p = ParserWrapper {
|
|
|
|
inner: Parser::new(md),
|
|
|
|
is_in: 0,
|
|
|
|
is_first: true,
|
|
|
|
};
|
|
|
|
while let Some(t) = p.next() {
|
|
|
|
if !t.is_empty() {
|
|
|
|
s.push_str(&t);
|
|
|
|
}
|
2014-12-25 05:39:29 +00:00
|
|
|
}
|
2017-03-08 00:01:23 +00:00
|
|
|
s
|
2014-12-25 05:39:29 +00:00
|
|
|
}
|
|
|
|
|
2014-05-31 22:33:32 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2017-04-06 12:09:20 +00:00
|
|
|
use super::{LangString, Markdown, MarkdownHtml};
|
2015-09-18 18:39:05 +00:00
|
|
|
use super::plain_summary_line;
|
2017-04-21 16:41:47 +00:00
|
|
|
use super::RenderType;
|
2015-12-02 22:49:18 +00:00
|
|
|
use html::render::reset_ids;
|
2014-05-31 22:33:32 +00:00
|
|
|
|
|
|
|
#[test]
|
2014-06-19 12:38:01 +00:00
|
|
|
fn test_lang_string_parse() {
|
2014-06-19 13:11:18 +00:00
|
|
|
fn t(s: &str,
|
2016-01-05 22:38:11 +00:00
|
|
|
should_panic: bool, no_run: bool, ignore: bool, rust: bool, test_harness: bool,
|
2017-05-24 17:58:37 +00:00
|
|
|
compile_fail: bool, allow_fail: bool, error_codes: Vec<String>) {
|
2014-06-19 12:38:01 +00:00
|
|
|
assert_eq!(LangString::parse(s), LangString {
|
2015-03-26 20:30:33 +00:00
|
|
|
should_panic: should_panic,
|
2014-06-19 12:38:01 +00:00
|
|
|
no_run: no_run,
|
|
|
|
ignore: ignore,
|
2014-12-10 11:17:14 +00:00
|
|
|
rust: rust,
|
2014-06-19 13:11:18 +00:00
|
|
|
test_harness: test_harness,
|
2016-01-05 22:38:11 +00:00
|
|
|
compile_fail: compile_fail,
|
2016-06-09 21:50:52 +00:00
|
|
|
error_codes: error_codes,
|
2016-09-07 14:43:18 +00:00
|
|
|
original: s.to_owned(),
|
2017-05-24 17:58:37 +00:00
|
|
|
allow_fail: allow_fail,
|
2014-06-19 12:38:01 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-05-25 15:36:18 +00:00
|
|
|
fn v() -> Vec<String> {
|
|
|
|
Vec::new()
|
|
|
|
}
|
|
|
|
|
2016-01-05 22:38:11 +00:00
|
|
|
// marker | should_panic| no_run| ignore| rust | test_harness| compile_fail
|
2017-05-24 17:58:37 +00:00
|
|
|
// | allow_fail | error_codes
|
2017-05-25 15:36:18 +00:00
|
|
|
t("", false, false, false, true, false, false, false, v());
|
|
|
|
t("rust", false, false, false, true, false, false, false, v());
|
|
|
|
t("sh", false, false, false, false, false, false, false, v());
|
|
|
|
t("ignore", false, false, true, true, false, false, false, v());
|
|
|
|
t("should_panic", true, false, false, true, false, false, false, v());
|
|
|
|
t("no_run", false, true, false, true, false, false, false, v());
|
|
|
|
t("test_harness", false, false, false, true, true, false, false, v());
|
|
|
|
t("compile_fail", false, true, false, true, false, true, false, v());
|
|
|
|
t("allow_fail", false, false, false, true, false, false, true, v());
|
|
|
|
t("{.no_run .example}", false, true, false, true, false, false, false, v());
|
|
|
|
t("{.sh .should_panic}", true, false, false, false, false, false, false, v());
|
|
|
|
t("{.example .rust}", false, false, false, true, false, false, false, v());
|
|
|
|
t("{.test_harness .rust}", false, false, false, true, true, false, false, v());
|
|
|
|
t("text, no_run", false, true, false, false, false, false, false, v());
|
|
|
|
t("text,no_run", false, true, false, false, false, false, false, v());
|
2014-05-31 22:33:32 +00:00
|
|
|
}
|
2014-10-05 13:00:50 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_17736() {
|
|
|
|
let markdown = "# title";
|
2017-04-21 16:41:47 +00:00
|
|
|
format!("{}", Markdown(markdown, RenderType::Pulldown));
|
2016-03-26 15:42:38 +00:00
|
|
|
reset_ids(true);
|
2014-10-05 13:00:50 +00:00
|
|
|
}
|
2014-12-25 05:39:29 +00:00
|
|
|
|
2015-09-19 01:43:59 +00:00
|
|
|
#[test]
|
|
|
|
fn test_header() {
|
|
|
|
fn t(input: &str, expect: &str) {
|
2017-04-21 16:41:47 +00:00
|
|
|
let output = format!("{}", Markdown(input, RenderType::Pulldown));
|
2017-03-25 18:07:52 +00:00
|
|
|
assert_eq!(output, expect, "original: {}", input);
|
2016-03-26 15:42:38 +00:00
|
|
|
reset_ids(true);
|
2015-09-19 01:43:59 +00:00
|
|
|
}
|
|
|
|
|
2017-03-25 00:48:33 +00:00
|
|
|
t("# Foo bar", "<h1 id=\"foo-bar\" class=\"section-header\">\
|
|
|
|
<a href=\"#foo-bar\">Foo bar</a></h1>");
|
|
|
|
t("## Foo-bar_baz qux", "<h2 id=\"foo-bar_baz-qux\" class=\"section-\
|
|
|
|
header\"><a href=\"#foo-bar_baz-qux\">Foo-bar_baz qux</a></h2>");
|
2015-09-19 01:43:59 +00:00
|
|
|
t("### **Foo** *bar* baz!?!& -_qux_-%",
|
2017-03-25 18:07:52 +00:00
|
|
|
"<h3 id=\"foo-bar-baz--qux-\" class=\"section-header\">\
|
|
|
|
<a href=\"#foo-bar-baz--qux-\"><strong>Foo</strong> \
|
|
|
|
<em>bar</em> baz!?!& -<em>qux</em>-%</a></h3>");
|
|
|
|
t("#### **Foo?** & \\*bar?!* _`baz`_ ❤ #qux",
|
2017-03-25 00:48:33 +00:00
|
|
|
"<h4 id=\"foo--bar--baz--qux\" class=\"section-header\">\
|
|
|
|
<a href=\"#foo--bar--baz--qux\"><strong>Foo?</strong> & *bar?!* \
|
2015-09-19 01:43:59 +00:00
|
|
|
<em><code>baz</code></em> ❤ #qux</a></h4>");
|
|
|
|
}
|
|
|
|
|
2015-12-05 22:09:20 +00:00
|
|
|
#[test]
|
|
|
|
fn test_header_ids_multiple_blocks() {
|
|
|
|
fn t(input: &str, expect: &str) {
|
2017-04-21 16:41:47 +00:00
|
|
|
let output = format!("{}", Markdown(input, RenderType::Pulldown));
|
2017-03-25 18:07:52 +00:00
|
|
|
assert_eq!(output, expect, "original: {}", input);
|
2015-12-05 22:09:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let test = || {
|
2017-03-25 00:48:33 +00:00
|
|
|
t("# Example", "<h1 id=\"example\" class=\"section-header\">\
|
|
|
|
<a href=\"#example\">Example</a></h1>");
|
|
|
|
t("# Panics", "<h1 id=\"panics\" class=\"section-header\">\
|
|
|
|
<a href=\"#panics\">Panics</a></h1>");
|
|
|
|
t("# Example", "<h1 id=\"example-1\" class=\"section-header\">\
|
|
|
|
<a href=\"#example-1\">Example</a></h1>");
|
|
|
|
t("# Main", "<h1 id=\"main-1\" class=\"section-header\">\
|
|
|
|
<a href=\"#main-1\">Main</a></h1>");
|
|
|
|
t("# Example", "<h1 id=\"example-2\" class=\"section-header\">\
|
|
|
|
<a href=\"#example-2\">Example</a></h1>");
|
|
|
|
t("# Panics", "<h1 id=\"panics-1\" class=\"section-header\">\
|
|
|
|
<a href=\"#panics-1\">Panics</a></h1>");
|
2015-12-05 22:09:20 +00:00
|
|
|
};
|
|
|
|
test();
|
2016-03-26 15:42:38 +00:00
|
|
|
reset_ids(true);
|
2015-12-05 22:09:20 +00:00
|
|
|
test();
|
|
|
|
}
|
|
|
|
|
2014-12-25 05:39:29 +00:00
|
|
|
#[test]
|
|
|
|
fn test_plain_summary_line() {
|
|
|
|
fn t(input: &str, expect: &str) {
|
|
|
|
let output = plain_summary_line(input);
|
2017-03-25 18:07:52 +00:00
|
|
|
assert_eq!(output, expect, "original: {}", input);
|
2014-12-25 05:39:29 +00:00
|
|
|
}
|
|
|
|
|
2015-08-09 21:15:05 +00:00
|
|
|
t("hello [Rust](https://www.rust-lang.org) :)", "hello Rust :)");
|
2017-04-06 12:09:20 +00:00
|
|
|
t("hello [Rust](https://www.rust-lang.org \"Rust\") :)", "hello Rust :)");
|
2014-12-25 05:39:29 +00:00
|
|
|
t("code `let x = i32;` ...", "code `let x = i32;` ...");
|
|
|
|
t("type `Type<'static>` ...", "type `Type<'static>` ...");
|
|
|
|
t("# top header", "top header");
|
|
|
|
t("## header", "header");
|
|
|
|
}
|
2016-12-23 07:12:56 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_markdown_html_escape() {
|
|
|
|
fn t(input: &str, expect: &str) {
|
2017-04-21 16:41:47 +00:00
|
|
|
let output = format!("{}", MarkdownHtml(input, RenderType::Pulldown));
|
2017-03-25 18:07:52 +00:00
|
|
|
assert_eq!(output, expect, "original: {}", input);
|
2016-12-23 07:12:56 +00:00
|
|
|
}
|
|
|
|
|
2017-04-06 12:09:20 +00:00
|
|
|
t("`Struct<'a, T>`", "<p><code>Struct<'a, T></code></p>\n");
|
|
|
|
t("Struct<'a, T>", "<p>Struct<'a, T></p>\n");
|
|
|
|
t("Struct<br>", "<p>Struct<br></p>\n");
|
2016-12-23 07:12:56 +00:00
|
|
|
}
|
2014-05-30 02:03:06 +00:00
|
|
|
}
|