diff options
| author | bors <bors@rust-lang.org> | 2014-03-02 01:41:23 -0800 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-03-02 01:41:23 -0800 |
| commit | baf79083aedb8ae64efddbcf28b358841cfd1157 (patch) | |
| tree | b94d3a9b36571f71b3713f1b1826da58227fbc48 | |
| parent | b5ad3022da9d4cea11ce3ddbce953edcc6e0eae9 (diff) | |
| parent | c602c814ff7220b9afb228270a53f8dcf16c910a (diff) | |
| download | rust-baf79083aedb8ae64efddbcf28b358841cfd1157.tar.gz rust-baf79083aedb8ae64efddbcf28b358841cfd1157.zip | |
auto merge of #12647 : huonw/rust/rustdoc-highlight-macros, r=alexcrichton
Macro definitions are just their raw source code, and so should be highlighted where possible. Also, $ident non-terminal substitutions are special, and so are worthy of a little special treatment.
| -rw-r--r-- | src/librustdoc/html/highlight.rs | 25 | ||||
| -rw-r--r-- | src/librustdoc/html/markdown.rs | 2 | ||||
| -rw-r--r-- | src/librustdoc/html/render.rs | 4 | ||||
| -rw-r--r-- | src/librustdoc/html/static/main.css | 1 |
4 files changed, 22 insertions, 10 deletions
diff --git a/src/librustdoc/html/highlight.rs b/src/librustdoc/html/highlight.rs index 8f90a39539a..07c4903585b 100644 --- a/src/librustdoc/html/highlight.rs +++ b/src/librustdoc/html/highlight.rs @@ -26,7 +26,7 @@ use html::escape::Escape; use t = syntax::parse::token; /// Highlights some source code, returning the HTML output. -pub fn highlight(src: &str) -> ~str { +pub fn highlight(src: &str, class: Option<&str>) -> ~str { let sess = parse::new_parse_sess(); let handler = diagnostic::default_handler(); let span_handler = diagnostic::mk_span_handler(handler, sess.cm); @@ -35,6 +35,7 @@ pub fn highlight(src: &str) -> ~str { let mut out = io::MemWriter::new(); doit(sess, lexer::new_string_reader(span_handler, fm), + class, &mut out).unwrap(); str::from_utf8_lossy(out.unwrap()).into_owned() } @@ -46,14 +47,15 @@ pub fn highlight(src: &str) -> ~str { /// it's used. All source code emission is done as slices from the source map, /// not from the tokens themselves, in order to stay true to the original /// source. -fn doit(sess: @parse::ParseSess, lexer: lexer::StringReader, +fn doit(sess: @parse::ParseSess, lexer: lexer::StringReader, class: Option<&str>, out: &mut Writer) -> io::IoResult<()> { use syntax::parse::lexer::Reader; - try!(write!(out, "<pre class='rust'>\n")); + try!(write!(out, "<pre class='rust {}'>\n", class.unwrap_or(""))); let mut last = BytePos(0); let mut is_attribute = false; let mut is_macro = false; + let mut is_macro_nonterminal = false; loop { let next = lexer.next_token(); let test = if next.tok == t::EOF {lexer.pos.get()} else {next.sp.lo}; @@ -101,8 +103,15 @@ fn doit(sess: @parse::ParseSess, lexer: lexer::StringReader, // miscellaneous, no highlighting t::DOT | t::DOTDOT | t::DOTDOTDOT | t::COMMA | t::SEMI | t::COLON | t::MOD_SEP | t::LARROW | t::DARROW | t::LPAREN | - t::RPAREN | t::LBRACKET | t::LBRACE | t::RBRACE | - t::DOLLAR => "", + t::RPAREN | t::LBRACKET | t::LBRACE | t::RBRACE => "", + t::DOLLAR => { + if t::is_ident(&lexer.peek().tok) { + is_macro_nonterminal = true; + "macro-nonterminal" + } else { + "" + } + } // This is the start of an attribute. We're going to want to // continue highlighting it as an attribute until the ending ']' is @@ -143,7 +152,10 @@ fn doit(sess: @parse::ParseSess, lexer: lexer::StringReader, _ if t::is_any_keyword(&next.tok) => "kw", _ => { - if lexer.peek().tok == t::NOT { + if is_macro_nonterminal { + is_macro_nonterminal = false; + "macro-nonterminal" + } else if lexer.peek().tok == t::NOT { is_macro = true; "macro" } else { @@ -171,4 +183,3 @@ fn doit(sess: @parse::ParseSess, lexer: lexer::StringReader, write!(out, "</pre>\n") } - diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index 2ad5bfb6e44..bc59f9e657a 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -146,7 +146,7 @@ pub fn render(w: &mut io::Writer, s: &str) -> fmt::Result { }; if !rendered { - let output = highlight::highlight(text).to_c_str(); + let output = highlight::highlight(text, None).to_c_str(); output.with_ref(|r| { bufputs(ob, r) }) diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index cf488fa53d3..0f5d01e1895 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -1630,13 +1630,13 @@ impl<'a> fmt::Show for Source<'a> { try!(write!(fmt.buf, "<span id='{0:u}'>{0:1$u}</span>\n", i, cols)); } try!(write!(fmt.buf, "</pre>")); - try!(write!(fmt.buf, "{}", highlight::highlight(s.as_slice()))); + try!(write!(fmt.buf, "{}", highlight::highlight(s.as_slice(), None))); Ok(()) } } fn item_macro(w: &mut Writer, it: &clean::Item, t: &clean::Macro) -> fmt::Result { - try!(write!(w, "<pre class='macro'>{}</pre>", t.source)); + try!(w.write_str(highlight::highlight(t.source, Some("macro")))); document(w, it) } diff --git a/src/librustdoc/html/static/main.css b/src/librustdoc/html/static/main.css index 6e5cdbafcd6..20c94d00711 100644 --- a/src/librustdoc/html/static/main.css +++ b/src/librustdoc/html/static/main.css @@ -315,6 +315,7 @@ pre.rust .op { color: #cc782f; } pre.rust .comment { color: #533add; } pre.rust .doccomment { color: #d343d0; } pre.rust .macro { color: #d343d0; } +pre.rust .macro-nonterminal { color: #d343d0; } pre.rust .string { color: #c13928; } pre.rust .lifetime { color: #d343d0; } pre.rust .attribute { color: #d343d0 !important; } |
