use std::path::Path;
use std::str;
use errors;
-use html::markdown::Markdown;
+use syntax::feature_gate::UnstableFeatures;
+use html::markdown::{ErrorCodes, Markdown};
#[derive(Clone)]
pub struct ExternalHtml {
pub fn load(in_header: &[String], before_content: &[String], after_content: &[String],
md_before_content: &[String], md_after_content: &[String], diag: &errors::Handler)
-> Option<ExternalHtml> {
+ let codes = ErrorCodes::from(UnstableFeatures::from_environment().is_nightly_build());
load_external_files(in_header, diag)
.and_then(|ih|
load_external_files(before_content, diag)
)
.and_then(|(ih, bc)|
load_external_files(md_before_content, diag)
- .map(|m_bc| (ih, format!("{}{}", bc, Markdown(&m_bc, &[]))))
+ .map(|m_bc| (ih, format!("{}{}", bc, Markdown(&m_bc, &[], codes))))
)
.and_then(|(ih, bc)|
load_external_files(after_content, diag)
)
.and_then(|(ih, bc, ac)|
load_external_files(md_after_content, diag)
- .map(|m_ac| (ih, bc, format!("{}{}", ac, Markdown(&m_ac, &[]))))
+ .map(|m_ac| (ih, bc, format!("{}{}", ac, Markdown(&m_ac, &[], codes))))
)
.map(|(ih, bc, ac)|
ExternalHtml {
//! ```
//! #![feature(rustc_private)]
//!
-//! use rustdoc::html::markdown::Markdown;
+//! use rustdoc::html::markdown::{Markdown, ErrorCodes};
//!
//! let s = "My *markdown* _text_";
-//! let html = format!("{}", Markdown(s, &[]));
+//! let html = format!("{}", Markdown(s, &[], ErrorCodes::Yes));
//! // ... something using html
//! ```
use std::ops::Range;
use std::str;
-use syntax::feature_gate::UnstableFeatures;
use html::render::derive_id;
use html::toc::TocBuilder;
use html::highlight;
/// formatted, this struct will emit the HTML corresponding to the rendered
/// version of the contained markdown string.
/// The second parameter is a list of link replacements
-pub struct Markdown<'a>(pub &'a str, pub &'a [(String, String)]);
+pub struct Markdown<'a>(pub &'a str, pub &'a [(String, String)], pub ErrorCodes);
/// A unit struct like `Markdown`, that renders the markdown with a
/// table of contents.
-pub struct MarkdownWithToc<'a>(pub &'a str);
+pub struct MarkdownWithToc<'a>(pub &'a str, pub ErrorCodes);
/// A unit struct like `Markdown`, that renders the markdown escaping HTML tags.
-pub struct MarkdownHtml<'a>(pub &'a str);
+pub struct MarkdownHtml<'a>(pub &'a str, pub ErrorCodes);
/// A unit struct like `Markdown`, that renders only the first paragraph.
pub struct MarkdownSummaryLine<'a>(pub &'a str, pub &'a [(String, String)]);
+#[derive(Copy, Clone, PartialEq, Debug)]
+pub enum ErrorCodes {
+ Yes,
+ No,
+}
+
+impl ErrorCodes {
+ pub fn from(b: bool) -> Self {
+ match b {
+ true => ErrorCodes::Yes,
+ false => ErrorCodes::No,
+ }
+ }
+
+ pub fn as_bool(self) -> bool {
+ match self {
+ ErrorCodes::Yes => true,
+ ErrorCodes::No => false,
+ }
+ }
+}
+
/// Controls whether a line will be hidden or shown in HTML output.
///
/// All lines are used in documentation tests.
/// Adds syntax highlighting and playground Run buttons to rust code blocks.
struct CodeBlocks<'a, I: Iterator<Item = Event<'a>>> {
inner: I,
- check_error_codes: bool,
+ check_error_codes: ErrorCodes,
}
impl<'a, I: Iterator<Item = Event<'a>>> CodeBlocks<'a, I> {
- fn new(iter: I) -> Self {
+ fn new(iter: I, error_codes: ErrorCodes) -> Self {
CodeBlocks {
inner: iter,
- check_error_codes: UnstableFeatures::from_environment().is_nightly_build(),
+ check_error_codes: error_codes,
}
}
}
}
pub fn find_testable_code(
- doc: &str, tests: &mut test::Collector
+ doc: &str, tests: &mut test::Collector, error_codes: ErrorCodes,
) -> Result<(), TestableCodeError> {
- let is_nightly = UnstableFeatures::from_environment().is_nightly_build();
let mut parser = Parser::new(doc);
let mut prev_offset = 0;
let mut nb_lines = 0;
let block_info = if s.is_empty() {
LangString::all_false()
} else {
- LangString::parse(&*s, is_nightly)
+ LangString::parse(&*s, error_codes)
};
if !block_info.rust {
continue
}
}
- fn parse(string: &str, allow_error_code_check: bool) -> LangString {
+ fn parse(string: &str, allow_error_code_check: ErrorCodes) -> LangString {
+ let allow_error_code_check = allow_error_code_check.as_bool();
let mut seen_rust_tags = false;
let mut seen_other_tags = false;
let mut data = LangString::all_false();
impl<'a> fmt::Display for Markdown<'a> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
- let Markdown(md, links) = *self;
+ let Markdown(md, links, codes) = *self;
// This is actually common enough to special-case
if md.is_empty() { return Ok(()) }
CodeBlocks::new(
LinkReplacer::new(
HeadingLinks::new(p, None),
- links))));
+ links), codes)));
fmt.write_str(&s)
}
impl<'a> fmt::Display for MarkdownWithToc<'a> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
- let MarkdownWithToc(md) = *self;
+ let MarkdownWithToc(md, codes) = *self;
let mut opts = Options::empty();
opts.insert(OPTION_ENABLE_TABLES);
let mut toc = TocBuilder::new();
- html::push_html(&mut s,
- Footnotes::new(CodeBlocks::new(HeadingLinks::new(p, Some(&mut toc)))));
+ {
+ let p = HeadingLinks::new(p, Some(&mut toc));
+ let p = CodeBlocks::new(p, codes);
+ let p = Footnotes::new(p);
+ html::push_html(&mut s, p);
+ }
write!(fmt, "<nav id=\"TOC\">{}</nav>", toc.into_toc())?;
impl<'a> fmt::Display for MarkdownHtml<'a> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
- let MarkdownHtml(md) = *self;
+ let MarkdownHtml(md, codes) = *self;
// This is actually common enough to special-case
if md.is_empty() { return Ok(()) }
let mut s = String::with_capacity(md.len() * 3 / 2);
- html::push_html(&mut s,
- Footnotes::new(CodeBlocks::new(HeadingLinks::new(p, None))));
+ let p = HeadingLinks::new(p, None);
+ let p = CodeBlocks::new(p, codes);
+ let p = Footnotes::new(p);
+ html::push_html(&mut s, p);
fmt.write_str(&s)
}
#[cfg(test)]
mod tests {
- use super::{LangString, Markdown, MarkdownHtml};
+ use super::{ErrorCodes, LangString, Markdown, MarkdownHtml};
use super::plain_summary_line;
use html::render::reset_ids;
fn t(s: &str,
should_panic: bool, no_run: bool, ignore: bool, rust: bool, test_harness: bool,
compile_fail: bool, allow_fail: bool, error_codes: Vec<String>) {
- assert_eq!(LangString::parse(s, true), LangString {
+ assert_eq!(LangString::parse(s, ErrorCodes::Yes), LangString {
should_panic,
no_run,
ignore,
#[test]
fn issue_17736() {
let markdown = "# title";
- Markdown(markdown, &[]).to_string();
+ Markdown(markdown, &[], ErrorCodes::Yes).to_string();
reset_ids(true);
}
#[test]
fn test_header() {
fn t(input: &str, expect: &str) {
- let output = Markdown(input, &[]).to_string();
+ let output = Markdown(input, &[], ErrorCodes::Yes).to_string();
assert_eq!(output, expect, "original: {}", input);
reset_ids(true);
}
#[test]
fn test_header_ids_multiple_blocks() {
fn t(input: &str, expect: &str) {
- let output = Markdown(input, &[]).to_string();
+ let output = Markdown(input, &[], ErrorCodes::Yes).to_string();
assert_eq!(output, expect, "original: {}", input);
}
#[test]
fn test_markdown_html_escape() {
fn t(input: &str, expect: &str) {
- let output = MarkdownHtml(input).to_string();
+ let output = MarkdownHtml(input, ErrorCodes::Yes).to_string();
assert_eq!(output, expect, "original: {}", input);
}
use serialize::json::{ToJson, Json, as_json};
use syntax::ast;
use syntax::codemap::FileName;
+use syntax::feature_gate::UnstableFeatures;
use rustc::hir::def_id::{CrateNum, CRATE_DEF_INDEX, DefId};
use rustc::middle::privacy::AccessLevels;
use rustc::middle::stability;
use html::format::{VisSpace, Method, UnsafetySpace, MutableSpace};
use html::format::fmt_impl_for_trait_page;
use html::item_type::ItemType;
-use html::markdown::{self, Markdown, MarkdownHtml, MarkdownSummaryLine};
+use html::markdown::{self, Markdown, MarkdownHtml, MarkdownSummaryLine, ErrorCodes};
use html::{highlight, layout};
use minifier;
/// real location of an item. This is used to allow external links to
/// publicly reused items to redirect to the right location.
pub render_redirect_pages: bool,
+ pub codes: ErrorCodes,
pub shared: Arc<SharedContext>,
}
current: Vec::new(),
dst,
render_redirect_pages: false,
+ codes: ErrorCodes::from(UnstableFeatures::from_environment().is_nightly_build()),
shared: Arc::new(scx),
};
fn render_markdown(w: &mut fmt::Formatter,
md_text: &str,
links: Vec<(String, String)>,
- prefix: &str,)
+ prefix: &str,
+ codes: ErrorCodes)
-> fmt::Result {
- write!(w, "<div class='docblock'>{}{}</div>", prefix, Markdown(md_text, &links))
+ write!(w, "<div class='docblock'>{}{}</div>", prefix, Markdown(md_text, &links, codes))
}
fn document_short(w: &mut fmt::Formatter, item: &clean::Item, link: AssocItemLink,
- prefix: &str) -> fmt::Result {
+ prefix: &str, codes: ErrorCodes) -> fmt::Result {
if let Some(s) = item.doc_value() {
let markdown = if s.contains('\n') {
format!("{} [Read more]({})",
} else {
plain_summary_line(Some(s)).to_string()
};
- render_markdown(w, &markdown, item.links(), prefix)?;
+ render_markdown(w, &markdown, item.links(), prefix, codes)?;
} else if !prefix.is_empty() {
write!(w, "<div class='docblock'>{}</div>", prefix)?;
}
cx: &Context, prefix: &str) -> fmt::Result {
if let Some(s) = cx.shared.maybe_collapsed_doc_value(item) {
debug!("Doc block: =====\n{}\n=====", s);
- render_markdown(w, &*s, item.links(), prefix)?;
+ render_markdown(w, &*s, item.links(), prefix, cx.codes)?;
} else if !prefix.is_empty() {
write!(w, "<div class='docblock'>{}</div>", prefix)?;
}
fn short_stability(item: &clean::Item, cx: &Context, show_reason: bool) -> Vec<String> {
let mut stability = vec![];
+ let error_codes = ErrorCodes::from(UnstableFeatures::from_environment().is_nightly_build());
if let Some(stab) = item.stability.as_ref() {
let deprecated_reason = if show_reason && !stab.deprecated_reason.is_empty() {
} else {
String::new()
};
+ let html = MarkdownHtml(&deprecated_reason, error_codes);
let text = if stability::deprecation_in_effect(&stab.deprecated_since) {
- format!("Deprecated{}{}",
- since,
- MarkdownHtml(&deprecated_reason))
+ format!("Deprecated{}{}", since, html)
} else {
- format!("Deprecating in {}{}",
- Escape(&stab.deprecated_since),
- MarkdownHtml(&deprecated_reason))
+ format!("Deprecating in {}{}", Escape(&stab.deprecated_since), html)
};
stability.push(format!("<div class='stab deprecated'>{}</div>", text))
};
This is a nightly-only experimental API. {}\
</summary>{}",
unstable_extra,
- MarkdownHtml(&stab.unstable_reason));
+ MarkdownHtml(
+ &stab.unstable_reason,
+ error_codes));
stability.push(format!("<div class='stab unstable'><details>{}</details></div>",
text));
}
let text = if stability::deprecation_in_effect(&depr.since) {
format!("Deprecated{}{}",
since,
- MarkdownHtml(¬e))
+ MarkdownHtml(¬e, error_codes))
} else {
format!("Deprecating in {}{}",
Escape(&depr.since),
- MarkdownHtml(¬e))
+ MarkdownHtml(¬e, error_codes))
};
stability.push(format!("<div class='stab deprecated'>{}</div>", text))
}
write!(w, "</span></td></tr></tbody></table></h3>")?;
if let Some(ref dox) = cx.shared.maybe_collapsed_doc_value(&i.impl_item) {
write!(w, "<div class='docblock'>{}</div>",
- Markdown(&*dox, &i.impl_item.links()))?;
+ Markdown(&*dox, &i.impl_item.links(), cx.codes))?;
}
}
} else if show_def_docs {
// In case the item isn't documented,
// provide short documentation from the trait.
- document_short(w, it, link, &prefix)?;
+ document_short(w, it, link, &prefix, cx.codes)?;
}
}
} else {
} else {
document_stability(w, cx, item)?;
if show_def_docs {
- document_short(w, item, link, &prefix)?;
+ document_short(w, item, link, &prefix, cx.codes)?;
}
}
}
use rustc::session::search_paths::SearchPaths;
use rustc::session::config::{Externs, CodegenOptions};
use syntax::codemap::DUMMY_SP;
+use syntax::feature_gate::UnstableFeatures;
use syntax::edition::Edition;
use externalfiles::{ExternalHtml, LoadStringError, load_string};
use html::render::reset_ids;
use html::escape::Escape;
use html::markdown;
-use html::markdown::{Markdown, MarkdownWithToc, find_testable_code};
+use html::markdown::{ErrorCodes, Markdown, MarkdownWithToc, find_testable_code};
use test::{TestOptions, Collector};
/// Separate any lines at the start of the file that begin with `# ` or `%`.
reset_ids(false);
+ let error_codes = ErrorCodes::from(UnstableFeatures::from_environment().is_nightly_build());
let text = if include_toc {
- MarkdownWithToc(text).to_string()
+ MarkdownWithToc(text, error_codes).to_string()
} else {
- Markdown(text, &[]).to_string()
+ Markdown(text, &[], error_codes).to_string()
};
let err = write!(
Some(PathBuf::from(input)),
linker, edition);
collector.set_position(DUMMY_SP);
- let res = find_testable_code(&input_str, &mut collector);
+ let codes = ErrorCodes::from(UnstableFeatures::from_environment().is_nightly_build());
+ let res = find_testable_code(&input_str, &mut collector, codes);
if let Err(err) = res {
diag.span_warn(DUMMY_SP, &err.to_string());
}
use errors::emitter::ColorConfig;
use clean::Attributes;
-use html::markdown::{self, LangString};
+use html::markdown::{self, ErrorCodes, LangString};
#[derive(Clone, Default)]
pub struct TestOptions {
let mut hir_collector = HirCollector {
sess: &sess,
collector: &mut collector,
- map: &map
+ map: &map,
+ codes: ErrorCodes::from(sess.opts.unstable_features.is_nightly_build()),
};
hir_collector.visit_testable("".to_string(), &krate.attrs, |this| {
intravisit::walk_crate(this, krate);
struct HirCollector<'a, 'hir: 'a> {
sess: &'a session::Session,
collector: &'a mut Collector,
- map: &'a hir::map::Map<'hir>
+ map: &'a hir::map::Map<'hir>,
+ codes: ErrorCodes,
}
impl<'a, 'hir> HirCollector<'a, 'hir> {
// anything else, this will combine them for us
if let Some(doc) = attrs.collapsed_doc_value() {
self.collector.set_position(attrs.span.unwrap_or(DUMMY_SP));
- let res = markdown::find_testable_code(&doc, self.collector);
+ let res = markdown::find_testable_code(&doc, self.collector, self.codes);
if let Err(err) = res {
self.sess.diagnostic().span_warn(attrs.span.unwrap_or(DUMMY_SP),
&err.to_string());
use syntax::diagnostics::metadata::{get_metadata_dir, ErrorMetadataMap, ErrorMetadata};
-use rustdoc::html::markdown::{Markdown, PLAYGROUND};
+use rustdoc::html::markdown::{Markdown, ErrorCodes, PLAYGROUND};
use rustc_serialize::json;
enum OutputFormat {
// Description rendered as markdown.
match info.description {
- Some(ref desc) => write!(output, "{}", Markdown(desc, &[]))?,
+ Some(ref desc) => write!(output, "{}", Markdown(desc, &[], ErrorCodes::Yes))?,
None => write!(output, "<p>No description.</p>\n")?,
}