Skip to content

Commit e8ab71f

Browse files
committed
Rollup merge of #32715 - nrc:rustdoc-highlight, r=cmr
rustdoc: factor out function for getting inner html of highlighted source
2 parents 7ed71c2 + a4e2933 commit e8ab71f

File tree

3 files changed

+49
-26
lines changed

3 files changed

+49
-26
lines changed

src/librustdoc/html/highlight.rs

+42-19
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,36 @@ use html::escape::Escape;
1717

1818
use std::io;
1919
use std::io::prelude::*;
20-
use syntax::parse::lexer;
20+
use syntax::parse::lexer::{self, Reader};
2121
use syntax::parse::token;
2222
use syntax::parse;
2323

24-
/// Highlights some source code, returning the HTML output.
25-
pub fn highlight(src: &str, class: Option<&str>, id: Option<&str>) -> String {
24+
/// Highlights `src`, returning the HTML output.
25+
pub fn render_with_highlighting(src: &str, class: Option<&str>, id: Option<&str>) -> String {
2626
debug!("highlighting: ================\n{}\n==============", src);
2727
let sess = parse::ParseSess::new();
2828
let fm = sess.codemap().new_filemap("<stdin>".to_string(), src.to_string());
2929

3030
let mut out = Vec::new();
31-
doit(&sess,
32-
lexer::StringReader::new(&sess.span_diagnostic, fm),
33-
class,
34-
id,
35-
&mut out).unwrap();
31+
write_header(class, id, &mut out).unwrap();
32+
write_source(&sess,
33+
lexer::StringReader::new(&sess.span_diagnostic, fm),
34+
&mut out).unwrap();
35+
write_footer(&mut out).unwrap();
36+
String::from_utf8_lossy(&out[..]).into_owned()
37+
}
38+
39+
/// Highlights `src`, returning the HTML output. Returns only the inner html to
40+
/// be inserted into an element. C.f., `render_with_highlighting` which includes
41+
/// an enclosing `<pre>` block.
42+
pub fn render_inner_with_highlighting(src: &str) -> String {
43+
let sess = parse::ParseSess::new();
44+
let fm = sess.codemap().new_filemap("<stdin>".to_string(), src.to_string());
45+
46+
let mut out = Vec::new();
47+
write_source(&sess,
48+
lexer::StringReader::new(&sess.span_diagnostic, fm),
49+
&mut out).unwrap();
3650
String::from_utf8_lossy(&out[..]).into_owned()
3751
}
3852

@@ -43,17 +57,10 @@ pub fn highlight(src: &str, class: Option<&str>, id: Option<&str>) -> String {
4357
/// it's used. All source code emission is done as slices from the source map,
4458
/// not from the tokens themselves, in order to stay true to the original
4559
/// source.
46-
fn doit(sess: &parse::ParseSess, mut lexer: lexer::StringReader,
47-
class: Option<&str>, id: Option<&str>,
48-
out: &mut Write) -> io::Result<()> {
49-
use syntax::parse::lexer::Reader;
50-
51-
write!(out, "<pre ")?;
52-
match id {
53-
Some(id) => write!(out, "id='{}' ", id)?,
54-
None => {}
55-
}
56-
write!(out, "class='rust {}'>\n", class.unwrap_or(""))?;
60+
fn write_source(sess: &parse::ParseSess,
61+
mut lexer: lexer::StringReader,
62+
out: &mut Write)
63+
-> io::Result<()> {
5764
let mut is_attribute = false;
5865
let mut is_macro = false;
5966
let mut is_macro_nonterminal = false;
@@ -184,5 +191,21 @@ fn doit(sess: &parse::ParseSess, mut lexer: lexer::StringReader,
184191
}
185192
}
186193

194+
Ok(())
195+
}
196+
197+
fn write_header(class: Option<&str>,
198+
id: Option<&str>,
199+
out: &mut Write)
200+
-> io::Result<()> {
201+
write!(out, "<pre ")?;
202+
match id {
203+
Some(id) => write!(out, "id='{}' ", id)?,
204+
None => {}
205+
}
206+
write!(out, "class='rust {}'>\n", class.unwrap_or(""))
207+
}
208+
209+
fn write_footer(out: &mut Write) -> io::Result<()> {
187210
write!(out, "</pre>\n")
188211
}

src/librustdoc/html/markdown.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,9 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
262262
&Default::default());
263263
s.push_str(&format!("<span class='rusttest'>{}</span>", Escape(&test)));
264264
});
265-
s.push_str(&highlight::highlight(&text,
266-
Some("rust-example-rendered"),
267-
None));
265+
s.push_str(&highlight::render_with_highlighting(&text,
266+
Some("rust-example-rendered"),
267+
None));
268268
let output = CString::new(s).unwrap();
269269
hoedown_buffer_puts(ob, output.as_ptr());
270270
})

src/librustdoc/html/render.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2640,16 +2640,16 @@ impl<'a> fmt::Display for Source<'a> {
26402640
write!(fmt, "<span id=\"{0}\">{0:1$}</span>\n", i, cols)?;
26412641
}
26422642
write!(fmt, "</pre>")?;
2643-
write!(fmt, "{}", highlight::highlight(s, None, None))?;
2643+
write!(fmt, "{}", highlight::render_with_highlighting(s, None, None))?;
26442644
Ok(())
26452645
}
26462646
}
26472647

26482648
fn item_macro(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
26492649
t: &clean::Macro) -> fmt::Result {
2650-
w.write_str(&highlight::highlight(&t.source,
2651-
Some("macro"),
2652-
None))?;
2650+
w.write_str(&highlight::render_with_highlighting(&t.source,
2651+
Some("macro"),
2652+
None))?;
26532653
render_stability_since_raw(w, it.stable_since(), None)?;
26542654
document(w, cx, it)
26552655
}

0 commit comments

Comments
 (0)