Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 31 additions & 21 deletions src/librustdoc/html/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ struct TokenHandler<'a, 'tcx, F: Write> {
/// We need to keep the `Class` for each element because it could contain a `Span` which is
/// used to generate links.
href_context: Option<HrefContext<'a, 'tcx>>,
write_line_number: fn(u32) -> String,
write_line_number: fn(u32) -> LineNumber,
line: u32,
max_lines: u32,
}
Expand All @@ -355,7 +355,7 @@ impl<F: Write> std::fmt::Debug for TokenHandler<'_, '_, F> {
}

impl<'a, F: Write> TokenHandler<'a, '_, F> {
fn handle_backline(&mut self) -> Option<String> {
fn handle_backline(&mut self) -> Option<LineNumber> {
self.line += 1;
if self.line < self.max_lines {
return Some((self.write_line_number)(self.line));
Expand All @@ -376,8 +376,7 @@ impl<'a, F: Write> TokenHandler<'a, '_, F> {
if text == "\n"
&& let Some(backline) = self.handle_backline()
{
self.out.write_str(&text).unwrap();
self.out.write_str(&backline).unwrap();
write!(self.out, "{text}{backline}").unwrap();
} else {
self.push_token_without_backline_check(class, text, true);
}
Expand Down Expand Up @@ -437,20 +436,27 @@ impl<F: Write> Drop for TokenHandler<'_, '_, F> {
}
}

fn scraped_line_number(line: u32) -> String {
// https://developers.google.com/search/docs/crawling-indexing/robots-meta-tag#data-nosnippet-attr
// Do not show "1 2 3 4 5 ..." in web search results.
format!("<span data-nosnippet>{line}</span>")
/// Represents line numbers to be generated as HTML.
enum LineNumber {
/// Used for scraped code examples.
Scraped(u32),
/// Used for non-scraped code examples, ie source code pages and code examples in documentation.
Normal(u32),
/// Code examples without line numbers.
#[allow(dead_code)]
Empty(u32),
}

fn line_number(line: u32) -> String {
// https://developers.google.com/search/docs/crawling-indexing/robots-meta-tag#data-nosnippet-attr
// Do not show "1 2 3 4 5 ..." in web search results.
format!("<a href=#{line} id={line} data-nosnippet>{line}</a>")
}

fn empty_line_number(_: u32) -> String {
String::new()
impl Display for LineNumber {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
// https://developers.google.com/search/docs/crawling-indexing/robots-meta-tag#data-nosnippet-attr
// Do not show "1 2 3 4 5 ..." in web search results.
Self::Scraped(line) => write!(f, "<span data-nosnippet>{line}</span>"),
Self::Normal(line) => write!(f, "<a href=#{line} id={line} data-nosnippet>{line}</a>"),
Self::Empty(_) => Ok(()),
}
}
}

fn get_next_expansion(
Expand Down Expand Up @@ -537,12 +543,12 @@ pub(super) fn write_code(
write_line_number: match line_info {
Some(line_info) => {
if line_info.is_scraped_example {
scraped_line_number
LineNumber::Scraped
} else {
line_number
LineNumber::Normal
}
}
None => empty_line_number,
None => LineNumber::Empty,
},
line: 0,
max_lines: u32::MAX,
Expand All @@ -552,8 +558,12 @@ pub(super) fn write_code(
if let Some(line_info) = line_info {
token_handler.line = line_info.start_line - 1;
token_handler.max_lines = line_info.max_lines;
if let Some(text) = token_handler.handle_backline() {
token_handler.push_token_without_backline_check(None, Cow::Owned(text), false);
if let Some(backline) = token_handler.handle_backline() {
token_handler.push_token_without_backline_check(
None,
Cow::Owned(backline.to_string()),
false,
);
}
}

Expand Down
Loading