Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Autumn #33

Merged
merged 1 commit into from
Apr 11, 2024
Merged
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
4 changes: 3 additions & 1 deletion lib/mix/tasks/mdex.generate_samples.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ defmodule Mix.Tasks.Mdex.GenerateSamples do
line-height: 1.5;
}
body {
background-color: #ffffff;
padding: 50px;
}
pre {
font-size: 15px;
margin: 20px;
padding: 50px;
border-radius: 10px;
}
</style>
</head>
Expand Down
4 changes: 2 additions & 2 deletions native/comrak_nif/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions native/comrak_nif/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,29 @@ phf = { version = "0.11", features = ["macros"] }
tree-sitter = "0.20"
tree-sitter-highlight = "0.20"
v_htmlescape = "0.15"
inkjet = { version = "0.10.2" , default-features = false, features = [
inkjet = { version = "0.10.4" , default-features = false, features = [
"html",
"language-bash",
"language-c",
"language-clojure",
"language-c-sharp",
"language-clojure",
"language-commonlisp",
"language-cpp",
"language-css",
"language-diff",
"language-dockerfile",
"language-eex",
"language-elisp",
"language-elixir",
"language-elm",
"language-erlang",
"language-gleam",
"language-go",
"language-haskell",
"language-hcl",
"language-heex",
"language-html",
"language-iex",
"language-java",
"language-javascript",
"language-json",
Expand All @@ -48,7 +51,11 @@ inkjet = { version = "0.10.2" , default-features = false, features = [
"language-llvm",
"language-lua",
"language-make",
"language-ocaml",
"language-ocaml-interface",
"language-pascal",
"language-php",
"language-plaintext",
"language-proto",
"language-python",
"language-r",
Expand All @@ -58,10 +65,11 @@ inkjet = { version = "0.10.2" , default-features = false, features = [
"language-scala",
"language-scss",
"language-sql",
"language-svelte",
"language-swift",
"language-toml",
"language-tsx",
"language-typescript",
"language-tsx",
"language-vim",
"language-yaml",
"language-zig",
Expand Down
27 changes: 11 additions & 16 deletions native/comrak_nif/src/inkjet_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ impl<'a> SyntaxHighlighterAdapter for InkjetAdapter<'a> {
source: &str,
) -> io::Result<()> {
let mut highlighter = Highlighter::new();
let lang = lang.unwrap_or("diff");
let lang = Language::from_token(lang).unwrap_or(Language::Diff);
let lang = lang.unwrap_or("plaintext");
let lang = Language::from_token(lang).unwrap_or(Language::Plaintext);
let config = lang.config();

let highlights = highlighter
Expand All @@ -44,13 +44,11 @@ impl<'a> SyntaxHighlighterAdapter for InkjetAdapter<'a> {
None => None,
},
)
// TODO: fallback to plain text
.expect("expected to generate the syntax highlight events");

for event in highlights {
// TODO: fallback to plain text
let event = event.expect("expected a highlight event");
let inner_highlights = autumn::inner_highlights(source, event, self.theme);
let inner_highlights = autumn::inner_highlights(source, event, self.theme, true);
write!(output, "{}", inner_highlights)?
}

Expand All @@ -62,7 +60,7 @@ impl<'a> SyntaxHighlighterAdapter for InkjetAdapter<'a> {
output: &mut dyn Write,
_attributes: HashMap<String, String>,
) -> io::Result<()> {
let pre_tag = autumn::open_pre_tag(self.theme, None);
let pre_tag = autumn::open_pre_tag(self.theme, None, true);
write!(output, "{}", pre_tag)
}

Expand All @@ -71,15 +69,12 @@ impl<'a> SyntaxHighlighterAdapter for InkjetAdapter<'a> {
output: &mut dyn Write,
attributes: HashMap<String, String>,
) -> io::Result<()> {
if attributes.contains_key("class") {
// lang does not matter since class will be replaced
let code_tag =
autumn::open_code_tag(Language::Diff, Some(attributes["class"].as_str()));
write!(output, "{}", code_tag)
} else {
// assume there's no language and fallbacks to plain text
let code_tag = autumn::open_code_tag(Language::Diff, Some("language-plain-text"));
write!(output, "{}", code_tag)
}
let plaintext = "language-plaintext".to_string();
let language = attributes.get("class").unwrap_or(&plaintext);
let split: Vec<&str> = language.split('-').collect();
let language = split.get(1).unwrap_or(&"plaintext");
let language = Language::from_token(language).unwrap_or(Language::Plaintext);
let code_tag = autumn::open_code_tag(language);
write!(output, "{}", code_tag)
}
}
19 changes: 16 additions & 3 deletions native/comrak_nif/vendor/autumn/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,45 @@ phf = { version = "0.11", features = ["macros"] }
v_htmlescape = "0.15"
tree-sitter = "0.20"
tree-sitter-highlight = "0.20"
inkjet = { version = "0.10.1" , default-features = false, features = [
# inkjet = { git = "https://github.com/fork/inkjet.git", branch = "master", version = "0.10.4" , default-features = false, features = [
# inkjet = { path = "/path/to/inkjet" , version = "0.10.4" , default-features = false, features = [
inkjet = { version = "0.10.4" , default-features = false, features = [
"html",
"language-bash",
"language-c",
"language-clojure",
"language-c-sharp",
"language-clojure",
"language-commonlisp",
"language-cpp",
"language-css",
"language-diff",
"language-dockerfile",
"language-eex",
"language-elisp",
"language-elixir",
"language-elm",
"language-erlang",
"language-gleam",
"language-go",
"language-haskell",
"language-hcl",
"language-heex",
"language-html",
"language-iex",
"language-java",
"language-javascript",
"language-json",
"language-jsx",
"language-kotlin",
"language-latex",
"language-llvm",
"language-lua",
"language-make",
"language-ocaml",
"language-ocaml-interface",
"language-pascal",
"language-php",
"language-plaintext",
"language-proto",
"language-python",
"language-r",
Expand All @@ -52,9 +62,12 @@ inkjet = { version = "0.10.1" , default-features = false, features = [
"language-scala",
"language-scss",
"language-sql",
"language-svelte",
"language-swift",
"language-toml",
"language-typescript",
"language-tsx",
"language-vim",
"language-yaml",
"language-zig",
] }
] }
93 changes: 51 additions & 42 deletions native/comrak_nif/vendor/autumn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn highlight_source_code(
lang: Language,
theme: &Theme,
pre_class: Option<&str>,
code_class: Option<&str>,
inline_style: bool,
) -> String {
let mut output = String::new();
let mut highlighter = Highlighter::new();
Expand All @@ -25,22 +25,20 @@ pub fn highlight_source_code(
None => None,
},
)
// TODO: fallback to plain text
.expect("expected to generate the syntax highlight events");

output.push_str(
format!(
"{}{}",
open_pre_tag(theme, pre_class),
open_code_tag(lang, code_class)
open_pre_tag(theme, pre_class, inline_style),
open_code_tag(lang)
)
.as_str(),
);

for event in highlights {
// TODO: fallback to plain text
let event = event.expect("expected a highlight event");
let highlight = inner_highlights(source, event, theme);
let highlight = inner_highlights(source, event, theme, inline_style);
output.push_str(highlight.as_str())
}

Expand All @@ -53,62 +51,79 @@ pub fn open_tags(
lang: Language,
theme: &Theme,
pre_class: Option<&str>,
code_class: Option<&str>,
inline_style: bool,
) -> String {
format!(
"{}{}",
open_pre_tag(theme, pre_class),
open_code_tag(lang, code_class)
open_pre_tag(theme, pre_class, inline_style),
open_code_tag(lang)
)
}

pub fn close_tags() -> String {
String::from("</code></pre>")
}

pub fn open_pre_tag(theme: &Theme, class: Option<&str>) -> String {
let class = class.unwrap_or("autumn highlight");
let (_class, background_style) = theme.get_scope("background");
let (_class, text_style) = theme.get_scope("text");
pub fn open_pre_tag(theme: &Theme, class: Option<&str>, inline_style: bool) -> String {
let class = match class {
Some(class) => format!("autumn-hl {}", class),
None => "autumn-hl".to_string(),
};

format!(
"<pre class=\"{}\" style=\"{} {}\">",
class, background_style, text_style
)
if inline_style {
let background_style = theme.get_global_style("background");
let foreground_style = theme.get_global_style("foreground");

format!(
"<pre class=\"{}\" style=\"{} {}\">",
class, background_style, foreground_style
)
} else {
format!("<pre class=\"{}\">", class)
}
}

pub fn close_pre_tag() -> String {
String::from("</pre>")
}

pub fn open_code_tag(lang: Language, class: Option<&str>) -> String {
let lang = format!("{}-{}", "language", format!("{:?}", lang).to_lowercase());
let class = class.unwrap_or_else(|| &lang);

pub fn open_code_tag(lang: Language) -> String {
let class = format!("language-{}", format!("{:?}", lang).to_lowercase());
format!("<code class=\"{}\" translate=\"no\">", class)
}

pub fn close_code_tag() -> String {
String::from("</code>")
}

pub fn inner_highlights(source: &str, event: HighlightEvent, theme: &Theme) -> String {
pub fn inner_highlights(
source: &str,
event: HighlightEvent,
theme: &Theme,
inline_style: bool,
) -> String {
let mut output = String::new();

match event {
HighlightEvent::Source { start, end } => {
let span = source
.get(start..end)
// TODO: fallback to plain text
.expect("source bounds should be in bounds!");
let span = v_htmlescape::escape(span).to_string();
output.push_str(span.as_str())
}
HighlightEvent::HighlightStart(idx) => {
let scope = inkjet::constants::HIGHLIGHT_NAMES[idx.0];
let (class, style) = theme.get_scope(scope);
let element = format!("<span class=\"{}\" style=\"{}\">", class, style);
output.push_str(element.as_str())
let class = themes::HIGHLIGHT_CLASS_NAMES[idx.0];

if inline_style {
let style = theme.get_style(scope);
let element = format!("<span class=\"{}\" style=\"{}\">", class, style);
output.push_str(element.as_str())
} else {
let element = format!("<span class=\"{}\">", class);
output.push_str(element.as_str())
}
}
HighlightEvent::HighlightEnd => output.push_str("</span>"),
}
Expand All @@ -121,47 +136,41 @@ mod tests {
use super::*;

#[test]
fn test_highlight_source_code() {
fn test_highlight_source_code_with_defaults() {
let theme = themes::theme("onedark").unwrap();
let output = highlight_source_code("mod themes;", Language::Rust, theme, None, None);
let output = highlight_source_code("mod themes;", Language::Rust, theme, None, true);

assert_eq!(
output,
"<pre class=\"autumn highlight\" style=\"background-color: #282C34; color: #ABB2BF;\"><code class=\"language-rust\" translate=\"no\"><span class=\"keyword control import\" style=\"color: #E06C75;\">mod</span> <span class=\"namespace\" style=\"color: #61AFEF;\">themes</span><span class=\"\" style=\"color: #ABB2BF;\">;</span></code></pre>"
"<pre class=\"autumn-hl\" style=\"background-color: #282C34; color: #ABB2BF;\"><code class=\"language-rust\" translate=\"no\"><span class=\"ahl-keyword ahl-control ahl-import\" style=\"color: #E06C75;\">mod</span> <span class=\"ahl-namespace\" style=\"color: #61AFEF;\">themes</span><span class=\"ahl-punctuation ahl-delimiter\" style=\"color: #ABB2BF;\">;</span></code></pre>"
);
}

#[test]
fn test_highlight_source_code_with_pre_class() {
fn test_highlight_source_code_without_inline_style() {
let theme = themes::theme("onedark").unwrap();
let output = highlight_source_code(
"mod themes;",
Language::Rust,
theme,
Some("pre_class"),
None,
);
let output = highlight_source_code("mod themes;", Language::Rust, theme, None, false);

assert_eq!(
output,
"<pre class=\"pre_class\" style=\"background-color: #282C34; color: #ABB2BF;\"><code class=\"language-rust\" translate=\"no\"><span class=\"keyword control import\" style=\"color: #E06C75;\">mod</span> <span class=\"namespace\" style=\"color: #61AFEF;\">themes</span><span class=\"\" style=\"color: #ABB2BF;\">;</span></code></pre>"
"<pre class=\"autumn-hl\"><code class=\"language-rust\" translate=\"no\"><span class=\"ahl-keyword ahl-control ahl-import\">mod</span> <span class=\"ahl-namespace\">themes</span><span class=\"ahl-punctuation ahl-delimiter\">;</span></code></pre>"
);
}

#[test]
fn test_highlight_source_code_with_code_class() {
fn test_highlight_source_code_with_pre_class() {
let theme = themes::theme("onedark").unwrap();
let output = highlight_source_code(
"mod themes;",
Language::Rust,
theme,
None,
Some("code_class"),
Some("pre_class"),
true,
);

assert_eq!(
output,
"<pre class=\"autumn highlight\" style=\"background-color: #282C34; color: #ABB2BF;\"><code class=\"code_class\" translate=\"no\"><span class=\"keyword control import\" style=\"color: #E06C75;\">mod</span> <span class=\"namespace\" style=\"color: #61AFEF;\">themes</span><span class=\"\" style=\"color: #ABB2BF;\">;</span></code></pre>"
"<pre class=\"autumn-hl pre_class\" style=\"background-color: #282C34; color: #ABB2BF;\"><code class=\"language-rust\" translate=\"no\"><span class=\"ahl-keyword ahl-control ahl-import\" style=\"color: #E06C75;\">mod</span> <span class=\"ahl-namespace\" style=\"color: #61AFEF;\">themes</span><span class=\"ahl-punctuation ahl-delimiter\" style=\"color: #ABB2BF;\">;</span></code></pre>"
);
}
}
Loading
Loading