Skip to content
Draft
Show file tree
Hide file tree
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
261 changes: 261 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,243 @@ dependencies = [
"object 0.37.3",
]

[[package]]
name = "arborium"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f00a72aec131b90c1c6495c565c4e4842fdbaf137ea4cacec110d353937ab99d"
dependencies = [
"arborium-bash",
"arborium-c",
"arborium-cpp",
"arborium-css",
"arborium-go",
"arborium-highlight",
"arborium-html",
"arborium-java",
"arborium-javascript",
"arborium-json",
"arborium-python",
"arborium-ruby",
"arborium-sql",
"arborium-theme",
"arborium-toml",
"arborium-tree-sitter",
"arborium-typescript",
"arborium-yaml",
"dlmalloc",
]

[[package]]
name = "arborium-bash"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34c6fd5230512b1191480100dd7876e972d1d6fd8e04fc62950a46b2a185405a"
dependencies = [
"arborium-sysroot",
"cc",
"tree-sitter-language",
]

[[package]]
name = "arborium-c"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6eb49d9d4d314d39e3ad41f891f3c46a417e21127c572621d3bb2b8acb0f67d2"
dependencies = [
"arborium-sysroot",
"cc",
"tree-sitter-language",
]

[[package]]
name = "arborium-cpp"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3813b0fd9e61425fe387333eb77ff5a3ea890f66d89e1262a66372ecad1274f"
dependencies = [
"arborium-c",
"arborium-sysroot",
"cc",
"tree-sitter-language",
]

[[package]]
name = "arborium-css"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d95540ee6ea2c33f40b45d9c40283a5c396e0ceb8529c4f2151932e43858a3b"
dependencies = [
"arborium-sysroot",
"cc",
"tree-sitter-language",
]

[[package]]
name = "arborium-go"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d7f8df9adca3da8c9e36889e0f52ab359dd36d168bc677e65fce5f43ca66b0d"
dependencies = [
"arborium-sysroot",
"cc",
"tree-sitter-language",
]

[[package]]
name = "arborium-highlight"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f2df668f0c80bfa2e437f74d45a4922a0e9256c2476560200774be4b60686f3"
dependencies = [
"arborium-theme",
"arborium-tree-sitter",
"streaming-iterator",
]

[[package]]
name = "arborium-html"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8d898910e534cddb0dca62ff3789b94637979b16d5354f153235438f58d29d6"
dependencies = [
"arborium-css",
"arborium-javascript",
"arborium-sysroot",
"cc",
"tree-sitter-language",
]

[[package]]
name = "arborium-java"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b91053783a4c3806cdcf92a7009bfe765ebcf0a13cd49ee361751a68f1f2c10f"
dependencies = [
"arborium-sysroot",
"cc",
"tree-sitter-language",
]

[[package]]
name = "arborium-javascript"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7220c71b1056508a028a4acfa4a10d8ca1713420ac3a36853dfd4a55bb335a4b"
dependencies = [
"arborium-sysroot",
"cc",
"tree-sitter-language",
]

[[package]]
name = "arborium-json"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "775321ffff8b71819c6dfead9e62bcd1efc1616306e59f2e1317ba7bef282e1f"
dependencies = [
"arborium-sysroot",
"cc",
"tree-sitter-language",
]

[[package]]
name = "arborium-python"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "45c5db8f67870cc64b67933a644fb296b3c367e9bfd85aa222a1ff6d49883e25"
dependencies = [
"arborium-sysroot",
"cc",
"tree-sitter-language",
]

[[package]]
name = "arborium-ruby"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7391c3561e43b892241ef216aafa974e1c3a59cd4e29d9cb9a7febc41645c0a8"
dependencies = [
"arborium-sysroot",
"cc",
"tree-sitter-language",
]

[[package]]
name = "arborium-sql"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc2c5bd1ed383d8ed8f0269d0cfddb475af9c98580d43f76cc4bb85a6f052633"
dependencies = [
"arborium-sysroot",
"cc",
"tree-sitter-language",
]

[[package]]
name = "arborium-sysroot"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d25c6fe8f35b7803048ca9f0846432011510d5196eb1089cf3a4bb37c35d094"
dependencies = [
"cc",
"dlmalloc",
]

[[package]]
name = "arborium-theme"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9da38b2109b8af45b7e0bce0c96f7db1c17831a62a23ae586c5705efac635758"

[[package]]
name = "arborium-toml"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b391b5bf276950b457d0b05efc0089c74b94e2f0939f8a63d98b4b84da5ebf12"
dependencies = [
"arborium-sysroot",
"cc",
"tree-sitter-language",
]

[[package]]
name = "arborium-tree-sitter"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "936e30ab0ba24131c37823d6d087b4ab6b7d59c6dd26fd1f1470e50582dc07ba"
dependencies = [
"arborium-sysroot",
"cc",
"regex",
"regex-syntax",
"streaming-iterator",
"tree-sitter-language",
]

[[package]]
name = "arborium-typescript"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9cd96fae6737d469b2042e2f7c37e37e6276465ba1c15162182e4106c189fc41"
dependencies = [
"arborium-javascript",
"arborium-sysroot",
"cc",
"tree-sitter-language",
]

[[package]]
name = "arborium-yaml"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef47437d2fe489ba8aa718dd1be3152b80b988b976183e84cfb1865a17cae1e8"
dependencies = [
"arborium-sysroot",
"cc",
"tree-sitter-language",
]

[[package]]
name = "arrayref"
version = "0.3.9"
Expand Down Expand Up @@ -1265,6 +1502,17 @@ version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8975ffdaa0ef3661bfe02dbdcc06c9f829dfafe6a3c474de366a8d5e44276921"

[[package]]
name = "dlmalloc"
version = "0.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6738d2e996274e499bc7b0d693c858b7720b9cd2543a0643a3087e6cb0a4fa16"
dependencies = [
"cfg-if",
"libc",
"windows-sys 0.61.2",
]

[[package]]
name = "dyn-clone"
version = "1.0.20"
Expand Down Expand Up @@ -4861,6 +5109,7 @@ dependencies = [
name = "rustdoc"
version = "0.0.0"
dependencies = [
"arborium",
"arrayvec",
"askama",
"base64",
Expand Down Expand Up @@ -5329,6 +5578,12 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"

[[package]]
name = "streaming-iterator"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b2231b7c3057d5e4ad0156fb3dc807d900806020c5ffa3ee6ff2c8c76fb8520"

[[package]]
name = "string_cache"
version = "0.8.9"
Expand Down Expand Up @@ -5851,6 +6106,12 @@ dependencies = [
"tracing-subscriber",
]

[[package]]
name = "tree-sitter-language"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ae62f7eae5eb549c71b76658648b72cc6111f2d87d24a1e31fa907f4943e3ce"

[[package]]
name = "twox-hash"
version = "1.6.3"
Expand Down
17 changes: 17 additions & 0 deletions src/librustdoc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@ path = "lib.rs"

[dependencies]
# tidy-alphabetical-start
arborium = { version = "2.0.0", default-features = false, features = [
"lang-bash",
"lang-c",
"lang-cpp",
"lang-css",
"lang-go",
"lang-html",
"lang-java",
"lang-javascript",
"lang-json",
"lang-python",
"lang-ruby",
"lang-sql",
"lang-toml",
"lang-typescript",
"lang-yaml",
] }
arrayvec = { version = "0.7", default-features = false }
askama = { version = "0.14", default-features = false, features = ["alloc", "config", "derive"] }
base64 = "0.21.7"
Expand Down
4 changes: 4 additions & 0 deletions src/librustdoc/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ pub(crate) struct RenderOptions {
pub(crate) disable_minification: bool,
/// If `true`, HTML source pages will generate the possibility to expand macros.
pub(crate) generate_macro_expansion: bool,
/// If `true`, non-Rust code blocks will be syntax-highlighted using tree-sitter.
pub(crate) highlight_foreign_code: bool,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -805,6 +807,7 @@ impl Options {
let no_capture = matches.opt_present("no-capture");
let generate_link_to_definition = matches.opt_present("generate-link-to-definition");
let generate_macro_expansion = matches.opt_present("generate-macro-expansion");
let highlight_foreign_code = matches.opt_present("highlight-foreign-code");
let extern_html_root_takes_precedence =
matches.opt_present("extern-html-root-takes-precedence");
let html_no_source = matches.opt_present("html-no-source");
Expand Down Expand Up @@ -916,6 +919,7 @@ impl Options {
include_parts_dir,
parts_out_dir,
disable_minification,
highlight_foreign_code,
};
Some((input, options, render_options, loaded_paths))
}
Expand Down
2 changes: 2 additions & 0 deletions src/librustdoc/externalfiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl ExternalHtml {
edition,
playground,
heading_offset: HeadingOffset::H2,
highlight_foreign_code: false,
}
.write_into(&mut bc)
.unwrap();
Expand All @@ -63,6 +64,7 @@ impl ExternalHtml {
edition,
playground,
heading_offset: HeadingOffset::H2,
highlight_foreign_code: false,
}
.write_into(&mut ac)
.unwrap();
Expand Down
26 changes: 26 additions & 0 deletions src/librustdoc/html/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1475,5 +1475,31 @@ fn string_without_closing_tag<T: Display>(
}
}

/// Highlights non-Rust code using arborium (tree-sitter based).
/// Returns `None` if the language is not supported, in which case
/// the caller should fall back to plain escaped text.
pub(crate) fn highlight_foreign_code(lang: &str, code: &str) -> Option<String> {
use std::cell::RefCell;

thread_local! {
static HIGHLIGHTER: RefCell<arborium::Highlighter> =
RefCell::new(arborium::Highlighter::new());
}

// Map common language aliases to arborium grammar names
let lang = match lang {
"js" => "javascript",
"ts" => "typescript",
"py" => "python",
"rb" => "ruby",
"sh" | "shell" | "zsh" => "bash",
"yml" => "yaml",
"c++" | "cxx" => "cpp",
other => other,
};

HIGHLIGHTER.with_borrow_mut(|h| h.highlight(lang, code).ok())
}

#[cfg(test)]
mod tests;
Loading
Loading