Compare commits

..

No commits in common. "554a83a63c6d719bc99698dc6575ac613aaf6729" and "8c712582121b306330d772ca8120b5fa8f284fa4" have entirely different histories.

3 changed files with 2824 additions and 967 deletions

3503
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -24,7 +24,6 @@ use crate::compiler::compiler::Target;
use crate::document::document::Document;
use crate::document::element::ElemKind;
use crate::document::element::Element;
use crate::lua::kernel::CTX;
use crate::parser::parser::Parser;
use crate::parser::rule::RegexRule;
use crate::parser::source::Source;
@ -42,15 +41,6 @@ enum CodeKind {
Inline,
}
impl From<&CodeKind> for ElemKind {
fn from(value: &CodeKind) -> Self {
match value {
CodeKind::FullBlock | CodeKind::MiniBlock => ElemKind::Block,
CodeKind::Inline => ElemKind::Inline,
}
}
}
#[derive(Debug)]
struct Code {
location: Token,
@ -83,19 +73,12 @@ impl Code {
}
}
pub fn get_syntaxes() -> &'static SyntaxSet {
lazy_static! {
static ref syntax_set: SyntaxSet = SyntaxSet::load_defaults_newlines();
}
&syntax_set
}
fn highlight_html(&self, compiler: &Compiler) -> Result<String, String> {
lazy_static! {
static ref syntax_set: SyntaxSet = SyntaxSet::load_defaults_newlines();
static ref theme_set: ThemeSet = ThemeSet::load_defaults();
}
let syntax = match Code::get_syntaxes().find_syntax_by_name(self.language.as_str()) {
let syntax = match syntax_set.find_syntax_by_name(self.language.as_str()) {
Some(syntax) => syntax,
None => {
return Err(format!(
@ -133,7 +116,7 @@ impl Code {
// Code
result += "</td><td class=\"code-block-line\"><pre>";
match h.highlight_line(line, Code::get_syntaxes()) {
match h.highlight_line(line, &syntax_set) {
Err(e) => {
return Err(format!(
"Error highlighting line `{line}`: {}",
@ -168,7 +151,7 @@ impl Code {
for line in self.code.split(|c| c == '\n') {
result += "<tr><td class=\"code-block-line\"><pre>";
// Code
match h.highlight_line(line, Code::get_syntaxes()) {
match h.highlight_line(line, &syntax_set) {
Err(e) => {
return Err(format!(
"Error highlighting line `{line}`: {}",
@ -198,7 +181,7 @@ impl Code {
result += "</table></div></div>";
} else if self.block == CodeKind::Inline {
result += "<a class=\"inline-code\"><code>";
match h.highlight_line(self.code.as_str(), Code::get_syntaxes()) {
match h.highlight_line(self.code.as_str(), &syntax_set) {
Err(e) => {
return Err(format!(
"Error highlighting line `{}`: {}",
@ -259,7 +242,13 @@ impl Cached for Code {
impl Element for Code {
fn location(&self) -> &Token { &self.location }
fn kind(&self) -> ElemKind { (&self.block).into() }
fn kind(&self) -> ElemKind {
if self.block == CodeKind::Inline {
ElemKind::Inline
} else {
ElemKind::Block
}
}
fn element_name(&self) -> &'static str { "Code Block" }
@ -387,11 +376,11 @@ impl RegexRule for CodeRule {
let code_lang = match matches.get(2) {
None => "Plain Text".to_string(),
Some(lang) => {
let code_lang = lang.as_str().trim_start().trim_end().to_string();
let code_lang = lang.as_str().trim_end().trim_start().to_string();
if code_lang.is_empty() {
reports.push(
Report::build(ReportKind::Error, token.source(), lang.start())
.with_message("Missing Code Language")
.with_message("Missing code language")
.with_label(
Label::new((token.source().clone(), lang.range()))
.with_message("No language specified")
@ -402,26 +391,8 @@ impl RegexRule for CodeRule {
return reports;
}
if Code::get_syntaxes()
.find_syntax_by_name(code_lang.as_str())
.is_none()
{
reports.push(
Report::build(ReportKind::Error, token.source(), lang.start())
.with_message("Unknown Code Language")
.with_label(
Label::new((token.source().clone(), lang.range()))
.with_message(format!(
"Language `{}` cannot be found",
code_lang.fg(parser.colors().info)
))
.with_color(parser.colors().error),
)
.finish(),
);
return reports;
}
// TODO: validate language
code_lang
}
@ -545,214 +516,5 @@ impl RegexRule for CodeRule {
}
// TODO
fn lua_bindings<'lua>(&self, lua: &'lua Lua) -> Vec<(String, Function<'lua>)> {
let mut bindings = vec![];
bindings.push((
"push_inline".to_string(),
lua.create_function(|_, (language, content): (String, String)| {
CTX.with_borrow(|ctx| {
ctx.as_ref().map(|ctx| {
let theme = ctx
.document
.get_variable("code.theme")
.and_then(|var| Some(var.to_string()));
ctx.parser.push(
ctx.document,
Box::new(Code {
location: ctx.location.clone(),
block: CodeKind::Inline,
language,
name: None,
code: content,
theme,
line_offset: 1,
}),
);
})
});
Ok(())
})
.unwrap(),
));
bindings.push((
"push_miniblock".to_string(),
lua.create_function(
|_, (language, content, line_offset): (String, String, Option<usize>)| {
CTX.with_borrow(|ctx| {
ctx.as_ref().map(|ctx| {
let theme = ctx
.document
.get_variable("code.theme")
.and_then(|var| Some(var.to_string()));
ctx.parser.push(
ctx.document,
Box::new(Code {
location: ctx.location.clone(),
block: CodeKind::MiniBlock,
language,
name: None,
code: content,
theme,
line_offset: line_offset.unwrap_or(1),
}),
);
})
});
Ok(())
},
)
.unwrap(),
));
bindings.push((
"push_block".to_string(),
lua.create_function(
|_, (language, name, content, line_offset): (String, Option<String>, String, Option<usize>)| {
CTX.with_borrow(|ctx| {
ctx.as_ref().map(|ctx| {
let theme = ctx
.document
.get_variable("code.theme")
.and_then(|var| Some(var.to_string()));
ctx.parser.push(
ctx.document,
Box::new(Code {
location: ctx.location.clone(),
block: CodeKind::FullBlock,
language,
name,
code: content,
theme,
line_offset: line_offset.unwrap_or(1),
}),
);
})
});
Ok(())
},
)
.unwrap(),
));
bindings
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::parser::langparser::LangParser;
use crate::parser::source::SourceFile;
#[test]
fn code_block() {
let source = Rc::new(SourceFile::with_content(
"".to_string(),
r#"
```[line_offset=32] C, Some Code...
static int INT32_MIN = 0x80000000;
```
%<nml.code.push_block("Lua", "From Lua", "print(\"Hello, World!\")", nil)>%
``Rust
fn fact(n: usize) -> usize
{
match n
{
0 | 1 => 1,
_ => n * fact(n-1)
}
}
``
%<nml.code.push_miniblock("Bash", "NUM=$(($RANDOM % 10))", 18)>%
"#
.to_string(),
None,
));
let parser = LangParser::default();
//let compiler = Compiler::new(Target::HTML, None);
let doc = parser.parse(source, None);
let borrow = doc.content().borrow();
let found = borrow
.iter()
.filter_map(|e| e.downcast_ref::<Code>())
.collect::<Vec<_>>();
assert_eq!(found[0].block, CodeKind::FullBlock);
assert_eq!(found[0].language, "C");
assert_eq!(found[0].name, Some("Some Code...".to_string()));
assert_eq!(found[0].code, "static int INT32_MIN = 0x80000000;");
assert_eq!(found[0].line_offset, 32);
assert_eq!(found[1].block, CodeKind::FullBlock);
assert_eq!(found[1].language, "Lua");
assert_eq!(found[1].name, Some("From Lua".to_string()));
assert_eq!(found[1].code, "print(\"Hello, World!\")");
assert_eq!(found[1].line_offset, 1);
assert_eq!(found[2].block, CodeKind::MiniBlock);
assert_eq!(found[2].language, "Rust");
assert_eq!(found[2].name, None);
assert_eq!(found[2].code, "fn fact(n: usize) -> usize\n{\n\tmatch n\n\t{\n\t\t0 | 1 => 1,\n\t\t_ => n * fact(n-1)\n\t}\n}");
assert_eq!(found[2].line_offset, 1);
assert_eq!(found[3].block, CodeKind::MiniBlock);
assert_eq!(found[3].language, "Bash");
assert_eq!(found[3].name, None);
assert_eq!(found[3].code, "NUM=$(($RANDOM % 10))");
assert_eq!(found[3].line_offset, 18);
}
#[test]
fn code_inline() {
let source = Rc::new(SourceFile::with_content(
"".to_string(),
r#"
``C, int fact(int n)``
``Plain Text, Text in a code block!``
%<nml.code.push_inline("C++", "std::vector<std::vector<int>> u;")>%
"#
.to_string(),
None,
));
let parser = LangParser::default();
//let compiler = Compiler::new(Target::HTML, None);
let doc = parser.parse(source, None);
let borrow = doc.content().borrow();
let found = borrow
.first()
.unwrap()
.as_container()
.unwrap()
.contained()
.iter()
.filter_map(|e| e.downcast_ref::<Code>())
.collect::<Vec<_>>();
assert_eq!(found[0].block, CodeKind::Inline);
assert_eq!(found[0].language, "C");
assert_eq!(found[0].name, None);
assert_eq!(found[0].code, "int fact(int n)");
assert_eq!(found[0].line_offset, 1);
assert_eq!(found[1].block, CodeKind::Inline);
assert_eq!(found[1].language, "Plain Text");
assert_eq!(found[1].name, None);
assert_eq!(found[1].code, "Text in a code block!");
assert_eq!(found[1].line_offset, 1);
assert_eq!(found[2].block, CodeKind::Inline);
assert_eq!(found[2].language, "C++");
assert_eq!(found[2].name, None);
assert_eq!(found[2].code, "std::vector<std::vector<int>> u;");
assert_eq!(found[2].line_offset, 1);
}
fn lua_bindings<'lua>(&self, _lua: &'lua Lua) -> Vec<(String, Function<'lua>)> { vec![] }
}

View file

@ -68,7 +68,6 @@ impl From<&TexKind> for ElemKind {
#[derive(Debug)]
struct Tex {
pub(self) location: Token,
pub(self) mathmode: bool,
pub(self) kind: TexKind,
pub(self) env: String,
pub(self) tex: String,
@ -177,7 +176,7 @@ impl Element for Tex {
let preamble = document
.get_variable(format!("tex.{}.preamble", self.env).as_str())
.map_or("".to_string(), |var| var.to_string());
let prepend = if self.mathmode {
let prepend = if self.kind == TexKind::Inline {
"".to_string()
} else {
document
@ -185,10 +184,11 @@ impl Element for Tex {
.map_or("".to_string(), |var| var.to_string() + "\n")
};
let latex = if self.mathmode {
Tex::format_latex(&fontsize, &preamble, &format!("${{{}}}$", self.tex))
} else {
Tex::format_latex(&fontsize, &preamble, &format!("{prepend}{}", self.tex))
let latex = match self.kind {
TexKind::Inline => {
Tex::format_latex(&fontsize, &preamble, &format!("${{{}}}$", self.tex))
}
_ => Tex::format_latex(&fontsize, &preamble, &format!("{prepend}{}", self.tex)),
};
if let Some(mut con) = compiler.cache() {
@ -236,8 +236,7 @@ impl TexRule {
);
Self {
re: [
Regex::new(r"\$\|(?:\[((?:\\.|[^\\\\])*?)\])?(?:((?:\\.|[^\\\\])*?)\|\$)?")
.unwrap(),
Regex::new(r"\$\|(?:\[((?:\\.|[^\\\\])*?)\])?(?:((?:\\.|[^\\\\])*?)\|\$)?").unwrap(),
Regex::new(r"\$(?:\[((?:\\.|[^\\\\])*?)\])?(?:((?:\\.|[^\\\\])*?)\$)?").unwrap(),
],
properties: PropertyParser::new(props),
@ -404,7 +403,6 @@ impl RegexRule for TexRule {
parser.push(
document,
Box::new(Tex {
mathmode: index == 1,
location: token,
kind: tex_kind,
env: tex_env.to_string(),
@ -440,6 +438,7 @@ $[kind=block,env=another] e^{i\pi}=-1$
None,
));
let parser = LangParser::default();
let compiler = Compiler::new(Target::HTML, None);
let doc = parser.parse(source, None);
let borrow = doc.content().borrow();
@ -470,6 +469,7 @@ $[env=another] e^{i\pi}=-1$
None,
));
let parser = LangParser::default();
let compiler = Compiler::new(Target::HTML, None);
let doc = parser.parse(source, None);
let borrow = doc.content().borrow();