Skip to content

Commit 01d9555

Browse files
Further extract error code switch
Removes dependency on UnstableFeatures from markdown rendering
1 parent 03e34f8 commit 01d9555

File tree

6 files changed

+96
-58
lines changed

6 files changed

+96
-58
lines changed

src/librustdoc/externalfiles.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ use std::fs;
1212
use std::path::Path;
1313
use std::str;
1414
use errors;
15-
use html::markdown::Markdown;
15+
use syntax::feature_gate::UnstableFeatures;
16+
use html::markdown::{ErrorCodes, Markdown};
1617

1718
#[derive(Clone)]
1819
pub struct ExternalHtml {
@@ -31,22 +32,23 @@ impl ExternalHtml {
3132
pub fn load(in_header: &[String], before_content: &[String], after_content: &[String],
3233
md_before_content: &[String], md_after_content: &[String], diag: &errors::Handler)
3334
-> Option<ExternalHtml> {
35+
let codes = ErrorCodes::from(UnstableFeatures::from_environment().is_nightly_build());
3436
load_external_files(in_header, diag)
3537
.and_then(|ih|
3638
load_external_files(before_content, diag)
3739
.map(|bc| (ih, bc))
3840
)
3941
.and_then(|(ih, bc)|
4042
load_external_files(md_before_content, diag)
41-
.map(|m_bc| (ih, format!("{}{}", bc, Markdown(&m_bc, &[]))))
43+
.map(|m_bc| (ih, format!("{}{}", bc, Markdown(&m_bc, &[], codes))))
4244
)
4345
.and_then(|(ih, bc)|
4446
load_external_files(after_content, diag)
4547
.map(|ac| (ih, bc, ac))
4648
)
4749
.and_then(|(ih, bc, ac)|
4850
load_external_files(md_after_content, diag)
49-
.map(|m_ac| (ih, bc, format!("{}{}", ac, Markdown(&m_ac, &[]))))
51+
.map(|m_ac| (ih, bc, format!("{}{}", ac, Markdown(&m_ac, &[], codes))))
5052
)
5153
.map(|(ih, bc, ac)|
5254
ExternalHtml {

src/librustdoc/html/markdown.rs

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
//! ```
1919
//! #![feature(rustc_private)]
2020
//!
21-
//! use rustdoc::html::markdown::Markdown;
21+
//! use rustdoc::html::markdown::{Markdown, ErrorCodes};
2222
//!
2323
//! let s = "My *markdown* _text_";
24-
//! let html = format!("{}", Markdown(s, &[]));
24+
//! let html = format!("{}", Markdown(s, &[], ErrorCodes::Yes));
2525
//! // ... something using html
2626
//! ```
2727
@@ -35,7 +35,6 @@ use std::borrow::Cow;
3535
use std::ops::Range;
3636
use std::str;
3737

38-
use syntax::feature_gate::UnstableFeatures;
3938
use html::render::derive_id;
4039
use html::toc::TocBuilder;
4140
use html::highlight;
@@ -48,15 +47,37 @@ use pulldown_cmark::{Options, OPTION_ENABLE_FOOTNOTES, OPTION_ENABLE_TABLES};
4847
/// formatted, this struct will emit the HTML corresponding to the rendered
4948
/// version of the contained markdown string.
5049
/// The second parameter is a list of link replacements
51-
pub struct Markdown<'a>(pub &'a str, pub &'a [(String, String)]);
50+
pub struct Markdown<'a>(pub &'a str, pub &'a [(String, String)], pub ErrorCodes);
5251
/// A unit struct like `Markdown`, that renders the markdown with a
5352
/// table of contents.
54-
pub struct MarkdownWithToc<'a>(pub &'a str);
53+
pub struct MarkdownWithToc<'a>(pub &'a str, pub ErrorCodes);
5554
/// A unit struct like `Markdown`, that renders the markdown escaping HTML tags.
56-
pub struct MarkdownHtml<'a>(pub &'a str);
55+
pub struct MarkdownHtml<'a>(pub &'a str, pub ErrorCodes);
5756
/// A unit struct like `Markdown`, that renders only the first paragraph.
5857
pub struct MarkdownSummaryLine<'a>(pub &'a str, pub &'a [(String, String)]);
5958

59+
#[derive(Copy, Clone, PartialEq, Debug)]
60+
pub enum ErrorCodes {
61+
Yes,
62+
No,
63+
}
64+
65+
impl ErrorCodes {
66+
pub fn from(b: bool) -> Self {
67+
match b {
68+
true => ErrorCodes::Yes,
69+
false => ErrorCodes::No,
70+
}
71+
}
72+
73+
pub fn as_bool(self) -> bool {
74+
match self {
75+
ErrorCodes::Yes => true,
76+
ErrorCodes::No => false,
77+
}
78+
}
79+
}
80+
6081
/// Controls whether a line will be hidden or shown in HTML output.
6182
///
6283
/// All lines are used in documentation tests.
@@ -127,14 +148,14 @@ thread_local!(pub static PLAYGROUND: RefCell<Option<(Option<String>, String)>> =
127148
/// Adds syntax highlighting and playground Run buttons to rust code blocks.
128149
struct CodeBlocks<'a, I: Iterator<Item = Event<'a>>> {
129150
inner: I,
130-
check_error_codes: bool,
151+
check_error_codes: ErrorCodes,
131152
}
132153

133154
impl<'a, I: Iterator<Item = Event<'a>>> CodeBlocks<'a, I> {
134-
fn new(iter: I) -> Self {
155+
fn new(iter: I, error_codes: ErrorCodes) -> Self {
135156
CodeBlocks {
136157
inner: iter,
137-
check_error_codes: UnstableFeatures::from_environment().is_nightly_build(),
158+
check_error_codes: error_codes,
138159
}
139160
}
140161
}
@@ -476,9 +497,8 @@ impl fmt::Display for TestableCodeError {
476497
}
477498

478499
pub fn find_testable_code(
479-
doc: &str, tests: &mut test::Collector
500+
doc: &str, tests: &mut test::Collector, error_codes: ErrorCodes,
480501
) -> Result<(), TestableCodeError> {
481-
let is_nightly = UnstableFeatures::from_environment().is_nightly_build();
482502
let mut parser = Parser::new(doc);
483503
let mut prev_offset = 0;
484504
let mut nb_lines = 0;
@@ -489,7 +509,7 @@ pub fn find_testable_code(
489509
let block_info = if s.is_empty() {
490510
LangString::all_false()
491511
} else {
492-
LangString::parse(&*s, is_nightly)
512+
LangString::parse(&*s, error_codes)
493513
};
494514
if !block_info.rust {
495515
continue
@@ -570,7 +590,8 @@ impl LangString {
570590
}
571591
}
572592

573-
fn parse(string: &str, allow_error_code_check: bool) -> LangString {
593+
fn parse(string: &str, allow_error_code_check: ErrorCodes) -> LangString {
594+
let allow_error_code_check = allow_error_code_check.as_bool();
574595
let mut seen_rust_tags = false;
575596
let mut seen_other_tags = false;
576597
let mut data = LangString::all_false();
@@ -620,7 +641,7 @@ impl LangString {
620641

621642
impl<'a> fmt::Display for Markdown<'a> {
622643
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
623-
let Markdown(md, links) = *self;
644+
let Markdown(md, links, codes) = *self;
624645

625646
// This is actually common enough to special-case
626647
if md.is_empty() { return Ok(()) }
@@ -645,15 +666,15 @@ impl<'a> fmt::Display for Markdown<'a> {
645666
CodeBlocks::new(
646667
LinkReplacer::new(
647668
HeadingLinks::new(p, None),
648-
links))));
669+
links), codes)));
649670

650671
fmt.write_str(&s)
651672
}
652673
}
653674

654675
impl<'a> fmt::Display for MarkdownWithToc<'a> {
655676
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
656-
let MarkdownWithToc(md) = *self;
677+
let MarkdownWithToc(md, codes) = *self;
657678

658679
let mut opts = Options::empty();
659680
opts.insert(OPTION_ENABLE_TABLES);
@@ -665,8 +686,12 @@ impl<'a> fmt::Display for MarkdownWithToc<'a> {
665686

666687
let mut toc = TocBuilder::new();
667688

668-
html::push_html(&mut s,
669-
Footnotes::new(CodeBlocks::new(HeadingLinks::new(p, Some(&mut toc)))));
689+
{
690+
let p = HeadingLinks::new(p, Some(&mut toc));
691+
let p = CodeBlocks::new(p, codes);
692+
let p = Footnotes::new(p);
693+
html::push_html(&mut s, p);
694+
}
670695

671696
write!(fmt, "<nav id=\"TOC\">{}</nav>", toc.into_toc())?;
672697

@@ -676,7 +701,7 @@ impl<'a> fmt::Display for MarkdownWithToc<'a> {
676701

677702
impl<'a> fmt::Display for MarkdownHtml<'a> {
678703
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
679-
let MarkdownHtml(md) = *self;
704+
let MarkdownHtml(md, codes) = *self;
680705

681706
// This is actually common enough to special-case
682707
if md.is_empty() { return Ok(()) }
@@ -694,8 +719,10 @@ impl<'a> fmt::Display for MarkdownHtml<'a> {
694719

695720
let mut s = String::with_capacity(md.len() * 3 / 2);
696721

697-
html::push_html(&mut s,
698-
Footnotes::new(CodeBlocks::new(HeadingLinks::new(p, None))));
722+
let p = HeadingLinks::new(p, None);
723+
let p = CodeBlocks::new(p, codes);
724+
let p = Footnotes::new(p);
725+
html::push_html(&mut s, p);
699726

700727
fmt.write_str(&s)
701728
}
@@ -830,7 +857,7 @@ pub fn markdown_links(md: &str) -> Vec<(String, Option<Range<usize>>)> {
830857

831858
#[cfg(test)]
832859
mod tests {
833-
use super::{LangString, Markdown, MarkdownHtml};
860+
use super::{ErrorCodes, LangString, Markdown, MarkdownHtml};
834861
use super::plain_summary_line;
835862
use html::render::reset_ids;
836863

@@ -839,7 +866,7 @@ mod tests {
839866
fn t(s: &str,
840867
should_panic: bool, no_run: bool, ignore: bool, rust: bool, test_harness: bool,
841868
compile_fail: bool, allow_fail: bool, error_codes: Vec<String>) {
842-
assert_eq!(LangString::parse(s, true), LangString {
869+
assert_eq!(LangString::parse(s, ErrorCodes::Yes), LangString {
843870
should_panic,
844871
no_run,
845872
ignore,
@@ -878,14 +905,14 @@ mod tests {
878905
#[test]
879906
fn issue_17736() {
880907
let markdown = "# title";
881-
Markdown(markdown, &[]).to_string();
908+
Markdown(markdown, &[], ErrorCodes::Yes).to_string();
882909
reset_ids(true);
883910
}
884911

885912
#[test]
886913
fn test_header() {
887914
fn t(input: &str, expect: &str) {
888-
let output = Markdown(input, &[]).to_string();
915+
let output = Markdown(input, &[], ErrorCodes::Yes).to_string();
889916
assert_eq!(output, expect, "original: {}", input);
890917
reset_ids(true);
891918
}
@@ -907,7 +934,7 @@ mod tests {
907934
#[test]
908935
fn test_header_ids_multiple_blocks() {
909936
fn t(input: &str, expect: &str) {
910-
let output = Markdown(input, &[]).to_string();
937+
let output = Markdown(input, &[], ErrorCodes::Yes).to_string();
911938
assert_eq!(output, expect, "original: {}", input);
912939
}
913940

@@ -948,7 +975,7 @@ mod tests {
948975
#[test]
949976
fn test_markdown_html_escape() {
950977
fn t(input: &str, expect: &str) {
951-
let output = MarkdownHtml(input).to_string();
978+
let output = MarkdownHtml(input, ErrorCodes::Yes).to_string();
952979
assert_eq!(output, expect, "original: {}", input);
953980
}
954981

0 commit comments

Comments
 (0)