diff --git a/src/librustdoc/html/highlight.rs b/src/librustdoc/html/highlight.rs index 8f90a39539a87..07c4903585b81 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, "
\n")); + try!(write!(out, "")); - 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, "\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, "\n") } - diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index 2ad5bfb6e44b2..bc59f9e657aeb 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 cf488fa53d389..0f5d01e18950e 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, "{0:1$u}\n", i, cols)); } try!(write!(fmt.buf, "
{}", 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 6e5cdbafcd6c9..20c94d00711af 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; }