aboutsummaryrefslogtreecommitdiff
path: root/code/src
diff options
context:
space:
mode:
Diffstat (limited to 'code/src')
-rw-r--r--code/src/main.rs2
-rw-r--r--code/src/markdown/parser.rs3
-rw-r--r--code/src/markdown/render.rs25
-rw-r--r--code/src/syntax_highlight/grammar.rs53
-rw-r--r--code/src/syntax_highlight/theme.rs2
5 files changed, 57 insertions, 28 deletions
diff --git a/code/src/main.rs b/code/src/main.rs
index 21732d7..83869f0 100644
--- a/code/src/main.rs
+++ b/code/src/main.rs
@@ -1,3 +1,5 @@
+#![feature(const_fmt_arguments_new)]
+
pub mod atom;
pub mod html;
pub mod markdown;
diff --git a/code/src/markdown/parser.rs b/code/src/markdown/parser.rs
index cb025a6..94eab49 100644
--- a/code/src/markdown/parser.rs
+++ b/code/src/markdown/parser.rs
@@ -102,10 +102,9 @@ fn try_list(mut s: &str) -> Option<(Block, &str)> {
}
s = &s[1..];
let mut lf = s.find("\n").unwrap();
- while s[lf + 1..].starts_with(" -") {
+ while s[lf + 1..].starts_with(" ") {
lf += 2 + s[lf + 2..].find("\n").unwrap();
}
- eprintln!("{:?}", &s[..lf]);
let mut k = s[..lf]
.split("\n")
.map(|l| if l.starts_with(" ") { &l[2..] } else { &l })
diff --git a/code/src/markdown/render.rs b/code/src/markdown/render.rs
index 39f204f..f6b7686 100644
--- a/code/src/markdown/render.rs
+++ b/code/src/markdown/render.rs
@@ -15,7 +15,10 @@ pub fn span_to_html(ss: Vec<Span>) -> String {
Span::Image(_, _) => todo!(),
Span::Emphasis(c) => format!("<i>{}</i>", span_to_html(c)),
Span::Strong(c) => format!("<b>{}</b>", span_to_html(c)),
- Span::Latex(_) => format!("TODO: Inline Latex"),
+ Span::Latex(s) => fix_katex(
+ &katex::render_with_opts(&s, &katex::OptsBuilder::default().build().unwrap())
+ .unwrap(),
+ ),
}
.as_str()
}
@@ -55,7 +58,16 @@ pub fn blocks_to_html(blocks: Vec<Block>) -> String {
}
Block::Raw(r) => r,
Block::Hr => format!("<hr/>"),
- Block::LatexBlock(_) => format!("TODO: Latex block"),
+ Block::LatexBlock(s) => fix_katex(
+ &katex::render_with_opts(
+ &s,
+ &katex::OptsBuilder::default()
+ .display_mode(true)
+ .build()
+ .unwrap(),
+ )
+ .unwrap(),
+ ),
}
.as_str();
}
@@ -69,3 +81,12 @@ pub fn escape(text: &str) -> String {
.replace("'", "&#8217;")
.replace("\"", "&quot;")
}
+
+// TODO this is *really* bad fix
+fn fix_katex<'a>(s: &str) -> String {
+ let e = s.find("<span class=\"katex-html\"").unwrap();
+ s[0..e].replace(
+ "<mspace linebreak=\"newline\"></mspace>",
+ "</mrow></semantics></math><math xmlns=\"http://www.w3.org/1998/Math/MathML\" display=\"block\"><semantics><mrow>",
+ )
+}
diff --git a/code/src/syntax_highlight/grammar.rs b/code/src/syntax_highlight/grammar.rs
index 0878b33..0cecac9 100644
--- a/code/src/syntax_highlight/grammar.rs
+++ b/code/src/syntax_highlight/grammar.rs
@@ -1,3 +1,8 @@
+const CONST: &str = "[\\. \\(,=^]([A-Z][A-Z0-9_]*)[\\. ,\\)=$]";
+const TYPE: &str = "[\\. \\(,=^]([A-Z][a-zA-Z0-9_]*)[\\. ,\\)\\(=$]";
+const IDENT: &str = "[\\. \\(,=^]([a-z][a-zA-Z0-9_]*)[\\. ,\\)=$]";
+const FUNC: &str = "[\\. \\(,=^]([a-z][A-Z0-9_]*)\\(";
+
pub fn grammar_for(syntax: &str) -> &'static [(&'static str, &'static [&'static str])] {
match syntax {
"rs" | "rust" => &[
@@ -11,41 +16,43 @@ pub fn grammar_for(syntax: &str) -> &'static [(&'static str, &'static [&'static
(
"type",
&[
- "[A-Z][a-z0-9]*",
- "bool",
- "usize",
- "u8",
- "u16",
- "u32",
- "u64",
- "u128",
- "i8",
- "i16",
- "i32",
- "i64",
- "i128",
- "isize",
- "f32",
- "f64",
+ TYPE, "bool", "usize", "u8", "u16", "u32", "u64", "u128", "i8", "i16", "i32",
+ "i64", "i128", "isize", "f32", "f64",
],
),
+ ("constant", &[CONST]),
("comment", &["(?m)(//.*)$", "(?ms)/\\*.*?\\*/"]),
- ("macro", &["([a-z_][A-Za-z0-9_]*!)\\s*"]),
- ("identifier", &["([a-z_][A-Za-z0-9_]*)\\s*\\("]),
- ("literal", &["\".*?\"", "\\d", "true", "false"]),
+ ("macro", &["[\\. ,=^]([a-z_][A-Za-z0-9_]*!)[\\. ,=$]"]),
+ ("identifier", &[IDENT]),
+ ("function", &[FUNC]),
+ (
+ "literal",
+ &["\".*?\"", "(0x|0o|0b)[0-9a-f]+", "\\d+", "true", "false"],
+ ),
],
"py" | "python" => &[
("type", &["bytes", "bool", "int", "str", "float"]),
(
"keyword",
&[
- "from", "for", "def", "if", "else", "elif", "while", "with", "in", "as",
- "import",
+ "from", "for", "def", "if", "else", "elif", "while", "with", "in", "assert",
+ "global", "nonlocal", "as", "import",
],
),
("comment", &["(?m)(#.*)$"]),
- ("identifier", &["([a-z_][A-Za-z0-9_]*)\\s*\\("]),
- ("literal", &["b?\".*?\"", "\\d", "true", "false"]),
+ ("constant", &[CONST]),
+ ("identifier", &[IDENT]),
+ (
+ "literal",
+ &[
+ "b?f?\".*?\"",
+ "b?f?'.*?'",
+ "(0x|0o|0b)[0-9a-f]+",
+ "\\d+",
+ "true",
+ "false",
+ ],
+ ),
],
"tree" => &[("keyword", &["[├─└│]+"])],
// makefile doesnt really match the token-kinds, i'll just use something that looks goo
diff --git a/code/src/syntax_highlight/theme.rs b/code/src/syntax_highlight/theme.rs
index 288662a..2dfd10d 100644
--- a/code/src/syntax_highlight/theme.rs
+++ b/code/src/syntax_highlight/theme.rs
@@ -6,7 +6,7 @@ pub fn theme(kind: &str) -> &'static str {
"function" => "#26ffbc",
"types" => "#26ff34",
"identifier" => "#ccff26",
- "" => "#ff7c26",
+ "constant" => "#ff7c26",
"comment" => "#6e6e6e",
_ => "#ff00ff",
}