Skip to content

Commit

Permalink
rustdoc: make langstring parsing more robust
Browse files Browse the repository at this point in the history
This changes the parsing of the language string
in code examples so that unrecognized examples
are not considered Rust code. This was, for example,
the case when a code example was marked `sh` for shell
code.

This relieves authors of having to mark those samples
as `notrust`.

Also adds recognition of the positive marker `rust`.

By default, unmarked examples are still considered rust.

If any rust-specific tags are seen, code is considered
rust unless marked as "notrust".

Adds test cases for the detection logic.
  • Loading branch information
skade committed Jun 1, 2014
1 parent 1527dab commit 3fef7a7
Showing 1 changed file with 49 additions and 4 deletions.
53 changes: 49 additions & 4 deletions src/librustdoc/html/markdown.rs
Expand Up @@ -287,10 +287,7 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector) {
slice::raw::buf_as_slice((*lang).data,
(*lang).size as uint, |lang| {
let s = str::from_utf8(lang).unwrap();
(s.contains("should_fail"),
s.contains("no_run"),
s.contains("ignore"),
s.contains("notrust"))
parse_lang_string(s)
})
};
if notrust { return }
Expand Down Expand Up @@ -340,6 +337,35 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector) {
}
}

fn parse_lang_string(string: &str) -> (bool,bool,bool,bool) {
let mut seen_rust_tags = false;
let mut seen_other_tags = false;
let mut should_fail = false;
let mut no_run = false;
let mut ignore = false;
let mut notrust = false;

let mut tokens = string.as_slice().split(|c: char|
!(c == '_' || c == '-' || c.is_alphanumeric())
);

for token in tokens {
match token {
"" => {},
"should_fail" => { should_fail = true; seen_rust_tags = true; },
"no_run" => { no_run = true; seen_rust_tags = true; },
"ignore" => { ignore = true; seen_rust_tags = true; },
"notrust" => { notrust = true; seen_rust_tags = true; },
"rust" => { notrust = false; seen_rust_tags = true; },
_ => { seen_other_tags = true }
}
}

let notrust = notrust || (seen_other_tags && !seen_rust_tags);

(should_fail, no_run, ignore, notrust)
}

/// By default this markdown renderer generates anchors for each header in the
/// rendered document. The anchor name is the contents of the header spearated
/// by hyphens, and a task-local map is used to disambiguate among duplicate
Expand Down Expand Up @@ -367,3 +393,22 @@ impl<'a> fmt::Show for MarkdownWithToc<'a> {
render(fmt, md.as_slice(), true)
}
}

#[cfg(test)]
mod tests {
use super::parse_lang_string;

#[test]
fn test_parse_lang_string() {
assert_eq!(parse_lang_string(""), (false,false,false,false))
assert_eq!(parse_lang_string("rust"), (false,false,false,false))
assert_eq!(parse_lang_string("sh"), (false,false,false,true))
assert_eq!(parse_lang_string("notrust"), (false,false,false,true))
assert_eq!(parse_lang_string("ignore"), (false,false,true,false))
assert_eq!(parse_lang_string("should_fail"), (true,false,false,false))
assert_eq!(parse_lang_string("no_run"), (false,true,false,false))
assert_eq!(parse_lang_string("{.no_run .example}"), (false,true,false,false))
assert_eq!(parse_lang_string("{.sh .should_fail}"), (true,false,false,false))
assert_eq!(parse_lang_string("{.example .rust}"), (false,false,false,false))
}
}

5 comments on commit 3fef7a7

@bors
Copy link
Contributor

@bors bors commented on 3fef7a7 Jun 2, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at skade@3fef7a7

@bors
Copy link
Contributor

@bors bors commented on 3fef7a7 Jun 2, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging skade/rust/rustdoc-robust-langstring-parsing = 3fef7a7 into auto

@bors
Copy link
Contributor

@bors bors commented on 3fef7a7 Jun 2, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

skade/rust/rustdoc-robust-langstring-parsing = 3fef7a7 merged ok, testing candidate = b981add

@bors
Copy link
Contributor

@bors bors commented on 3fef7a7 Jun 2, 2014

@bors
Copy link
Contributor

@bors bors commented on 3fef7a7 Jun 2, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = b981add

Please sign in to comment.