2024-07-25 13:13:12 +02:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::sync::Once;
|
|
|
|
|
|
|
|
use ariadne::Fmt;
|
|
|
|
use crypto::digest::Digest;
|
|
|
|
use crypto::sha2::Sha512;
|
|
|
|
use mlua::Function;
|
|
|
|
use mlua::Lua;
|
2024-11-01 22:15:33 +01:00
|
|
|
use parser::util::escape_source;
|
2024-07-25 13:13:12 +02:00
|
|
|
use regex::Captures;
|
|
|
|
use regex::Regex;
|
|
|
|
use syntect::easy::HighlightLines;
|
|
|
|
use syntect::highlighting::ThemeSet;
|
|
|
|
use syntect::parsing::SyntaxSet;
|
|
|
|
|
|
|
|
use crate::cache::cache::Cached;
|
|
|
|
use crate::cache::cache::CachedError;
|
|
|
|
use crate::compiler::compiler::Compiler;
|
|
|
|
use crate::compiler::compiler::Target;
|
|
|
|
use crate::document::document::Document;
|
|
|
|
use crate::document::element::ElemKind;
|
|
|
|
use crate::document::element::Element;
|
2024-10-20 10:06:31 +02:00
|
|
|
use crate::lsp::semantic::Semantics;
|
2024-07-27 08:23:15 +02:00
|
|
|
use crate::lua::kernel::CTX;
|
2024-10-20 19:38:15 +02:00
|
|
|
use crate::parser::parser::ParseMode;
|
2024-08-05 18:40:17 +02:00
|
|
|
use crate::parser::parser::ParserState;
|
2024-11-01 22:15:33 +01:00
|
|
|
use crate::parser::property::Property;
|
|
|
|
use crate::parser::property::PropertyParser;
|
2024-10-24 10:18:49 +02:00
|
|
|
use crate::parser::reports::macros::*;
|
|
|
|
use crate::parser::reports::*;
|
2024-07-25 13:13:12 +02:00
|
|
|
use crate::parser::rule::RegexRule;
|
|
|
|
use crate::parser::source::Token;
|
|
|
|
use crate::parser::util::{self};
|
2024-07-19 11:52:12 +02:00
|
|
|
use lazy_static::lazy_static;
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
2024-07-25 13:13:12 +02:00
|
|
|
enum CodeKind {
|
2024-07-19 11:52:12 +02:00
|
|
|
FullBlock,
|
|
|
|
MiniBlock,
|
|
|
|
Inline,
|
|
|
|
}
|
|
|
|
|
2024-07-27 08:07:06 +02:00
|
|
|
impl From<&CodeKind> for ElemKind {
|
|
|
|
fn from(value: &CodeKind) -> Self {
|
|
|
|
match value {
|
|
|
|
CodeKind::FullBlock | CodeKind::MiniBlock => ElemKind::Block,
|
|
|
|
CodeKind::Inline => ElemKind::Inline,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-19 11:52:12 +02:00
|
|
|
#[derive(Debug)]
|
2024-07-25 13:13:12 +02:00
|
|
|
struct Code {
|
2024-07-19 11:52:12 +02:00
|
|
|
location: Token,
|
|
|
|
block: CodeKind,
|
|
|
|
language: String,
|
|
|
|
name: Option<String>,
|
|
|
|
code: String,
|
|
|
|
theme: Option<String>,
|
|
|
|
line_offset: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Code {
|
2024-07-25 13:13:12 +02:00
|
|
|
fn new(
|
|
|
|
location: Token,
|
|
|
|
block: CodeKind,
|
|
|
|
language: String,
|
|
|
|
name: Option<String>,
|
|
|
|
code: String,
|
|
|
|
theme: Option<String>,
|
|
|
|
line_offset: usize,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
location,
|
|
|
|
block,
|
|
|
|
language,
|
|
|
|
name,
|
|
|
|
code,
|
|
|
|
theme,
|
|
|
|
line_offset,
|
|
|
|
}
|
|
|
|
}
|
2024-07-19 11:52:12 +02:00
|
|
|
|
2024-07-27 08:07:06 +02:00
|
|
|
pub fn get_syntaxes() -> &'static SyntaxSet {
|
2024-07-19 11:52:12 +02:00
|
|
|
lazy_static! {
|
2024-07-25 13:13:12 +02:00
|
|
|
static ref syntax_set: SyntaxSet = SyntaxSet::load_defaults_newlines();
|
2024-07-27 08:07:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
&syntax_set
|
|
|
|
}
|
|
|
|
|
|
|
|
fn highlight_html(&self, compiler: &Compiler) -> Result<String, String> {
|
|
|
|
lazy_static! {
|
2024-07-25 13:13:12 +02:00
|
|
|
static ref theme_set: ThemeSet = ThemeSet::load_defaults();
|
2024-07-19 11:52:12 +02:00
|
|
|
}
|
2024-07-27 08:07:06 +02:00
|
|
|
let syntax = match Code::get_syntaxes().find_syntax_by_name(self.language.as_str()) {
|
2024-07-19 11:52:12 +02:00
|
|
|
Some(syntax) => syntax,
|
2024-07-25 13:13:12 +02:00
|
|
|
None => {
|
|
|
|
return Err(format!(
|
|
|
|
"Unable to find syntax for language: {}",
|
|
|
|
self.language
|
|
|
|
))
|
|
|
|
}
|
2024-07-19 11:52:12 +02:00
|
|
|
};
|
|
|
|
|
2024-07-25 13:13:12 +02:00
|
|
|
let theme_string = match self.theme.as_ref() {
|
2024-07-19 11:52:12 +02:00
|
|
|
Some(theme) => theme.as_str(),
|
|
|
|
None => "base16-ocean.dark",
|
|
|
|
};
|
|
|
|
let mut h = HighlightLines::new(syntax, &theme_set.themes[theme_string]);
|
|
|
|
|
|
|
|
let mut result = String::new();
|
2024-07-25 13:13:12 +02:00
|
|
|
if self.block == CodeKind::FullBlock {
|
2024-07-19 11:52:12 +02:00
|
|
|
result += "<div class=\"code-block\">";
|
2024-07-25 13:13:12 +02:00
|
|
|
if let Some(name) = &self.name {
|
|
|
|
result += format!(
|
|
|
|
"<div class=\"code-block-title\">{}</div>",
|
2024-07-29 16:45:14 +02:00
|
|
|
Compiler::sanitize(compiler.target(), name.as_str())
|
2024-07-25 13:13:12 +02:00
|
|
|
)
|
|
|
|
.as_str();
|
2024-07-19 11:52:12 +02:00
|
|
|
}
|
|
|
|
|
2024-10-20 10:06:31 +02:00
|
|
|
result += "<div class=\"code-block-content\"><table cellspacing=\"0\">"
|
|
|
|
.to_string()
|
|
|
|
.as_str();
|
2024-08-14 22:50:32 +02:00
|
|
|
for (line_id, line) in self.code.split('\n').enumerate() {
|
2024-07-19 11:52:12 +02:00
|
|
|
result += "<tr><td class=\"code-block-gutter\">";
|
|
|
|
|
|
|
|
// Line number
|
2024-07-25 13:13:12 +02:00
|
|
|
result +=
|
|
|
|
format!("<pre><span>{}</span></pre>", line_id + self.line_offset).as_str();
|
2024-07-19 11:52:12 +02:00
|
|
|
|
|
|
|
// Code
|
|
|
|
result += "</td><td class=\"code-block-line\"><pre>";
|
2024-07-27 08:07:06 +02:00
|
|
|
match h.highlight_line(line, Code::get_syntaxes()) {
|
2024-10-20 10:06:31 +02:00
|
|
|
Err(e) => return Err(format!("Error highlighting line `{line}`: {}", e)),
|
2024-07-19 11:52:12 +02:00
|
|
|
Ok(regions) => {
|
2024-07-25 13:13:12 +02:00
|
|
|
match syntect::html::styled_line_to_highlighted_html(
|
|
|
|
®ions[..],
|
|
|
|
syntect::html::IncludeBackground::No,
|
|
|
|
) {
|
2024-10-20 10:06:31 +02:00
|
|
|
Err(e) => return Err(format!("Error highlighting code: {}", e)),
|
2024-07-25 13:13:12 +02:00
|
|
|
Ok(highlighted) => {
|
|
|
|
result += if highlighted.is_empty() {
|
|
|
|
"<br>"
|
|
|
|
} else {
|
|
|
|
highlighted.as_str()
|
|
|
|
}
|
|
|
|
}
|
2024-07-19 11:52:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result += "</pre></td></tr>";
|
|
|
|
}
|
|
|
|
|
|
|
|
result += "</table></div></div>";
|
2024-07-25 13:13:12 +02:00
|
|
|
} else if self.block == CodeKind::MiniBlock {
|
2024-07-19 11:52:12 +02:00
|
|
|
result += "<div class=\"code-block\"><div class=\"code-block-content\"><table cellspacing=\"0\">";
|
|
|
|
|
2024-08-14 22:50:32 +02:00
|
|
|
for line in self.code.split('\n') {
|
2024-07-19 11:52:12 +02:00
|
|
|
result += "<tr><td class=\"code-block-line\"><pre>";
|
|
|
|
// Code
|
2024-07-27 08:07:06 +02:00
|
|
|
match h.highlight_line(line, Code::get_syntaxes()) {
|
2024-10-20 10:06:31 +02:00
|
|
|
Err(e) => return Err(format!("Error highlighting line `{line}`: {}", e)),
|
2024-07-19 11:52:12 +02:00
|
|
|
Ok(regions) => {
|
2024-07-25 13:13:12 +02:00
|
|
|
match syntect::html::styled_line_to_highlighted_html(
|
|
|
|
®ions[..],
|
|
|
|
syntect::html::IncludeBackground::No,
|
|
|
|
) {
|
2024-10-20 10:06:31 +02:00
|
|
|
Err(e) => return Err(format!("Error highlighting code: {}", e)),
|
2024-07-25 13:13:12 +02:00
|
|
|
Ok(highlighted) => {
|
|
|
|
result += if highlighted.is_empty() {
|
|
|
|
"<br>"
|
|
|
|
} else {
|
|
|
|
highlighted.as_str()
|
|
|
|
}
|
|
|
|
}
|
2024-07-19 11:52:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result += "</pre></td></tr>";
|
|
|
|
}
|
|
|
|
result += "</table></div></div>";
|
2024-07-25 13:13:12 +02:00
|
|
|
} else if self.block == CodeKind::Inline {
|
2024-07-19 11:52:12 +02:00
|
|
|
result += "<a class=\"inline-code\"><code>";
|
2024-07-27 08:07:06 +02:00
|
|
|
match h.highlight_line(self.code.as_str(), Code::get_syntaxes()) {
|
2024-10-20 10:06:31 +02:00
|
|
|
Err(e) => return Err(format!("Error highlighting line `{}`: {}", self.code, e)),
|
2024-07-19 11:52:12 +02:00
|
|
|
Ok(regions) => {
|
2024-07-25 13:13:12 +02:00
|
|
|
match syntect::html::styled_line_to_highlighted_html(
|
|
|
|
®ions[..],
|
|
|
|
syntect::html::IncludeBackground::No,
|
|
|
|
) {
|
2024-10-20 10:06:31 +02:00
|
|
|
Err(e) => return Err(format!("Error highlighting code: {}", e)),
|
2024-07-25 13:13:12 +02:00
|
|
|
Ok(highlighted) => result += highlighted.as_str(),
|
2024-07-19 11:52:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result += "</code></a>";
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-25 13:13:12 +02:00
|
|
|
impl Cached for Code {
|
|
|
|
type Key = String;
|
|
|
|
type Value = String;
|
2024-07-19 11:52:12 +02:00
|
|
|
|
2024-07-25 13:13:12 +02:00
|
|
|
fn sql_table() -> &'static str {
|
2024-07-19 11:52:12 +02:00
|
|
|
"CREATE TABLE IF NOT EXISTS cached_code (
|
|
|
|
digest TEXT PRIMARY KEY,
|
|
|
|
highlighted BLOB NOT NULL);"
|
2024-07-25 13:13:12 +02:00
|
|
|
}
|
2024-07-19 11:52:12 +02:00
|
|
|
|
2024-07-25 13:13:12 +02:00
|
|
|
fn sql_get_query() -> &'static str { "SELECT highlighted FROM cached_code WHERE digest = (?1)" }
|
2024-07-19 11:52:12 +02:00
|
|
|
|
2024-07-25 13:13:12 +02:00
|
|
|
fn sql_insert_query() -> &'static str {
|
2024-07-19 11:52:12 +02:00
|
|
|
"INSERT INTO cached_code (digest, highlighted) VALUES (?1, ?2)"
|
2024-07-25 13:13:12 +02:00
|
|
|
}
|
2024-07-19 11:52:12 +02:00
|
|
|
|
2024-07-25 13:13:12 +02:00
|
|
|
fn key(&self) -> <Self as Cached>::Key {
|
2024-07-19 11:52:12 +02:00
|
|
|
let mut hasher = Sha512::new();
|
|
|
|
hasher.input((self.block as usize).to_be_bytes().as_slice());
|
2024-08-14 22:50:32 +02:00
|
|
|
hasher.input(self.line_offset.to_be_bytes().as_slice());
|
2024-10-20 10:06:31 +02:00
|
|
|
if let Some(theme) = self.theme.as_ref() {
|
|
|
|
hasher.input(theme.as_bytes())
|
|
|
|
}
|
|
|
|
if let Some(name) = self.name.as_ref() {
|
|
|
|
hasher.input(name.as_bytes())
|
|
|
|
}
|
2024-07-19 11:52:12 +02:00
|
|
|
hasher.input(self.language.as_bytes());
|
|
|
|
hasher.input(self.code.as_bytes());
|
|
|
|
|
|
|
|
hasher.result_str()
|
2024-07-25 13:13:12 +02:00
|
|
|
}
|
2024-07-19 11:52:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Element for Code {
|
2024-07-25 13:13:12 +02:00
|
|
|
fn location(&self) -> &Token { &self.location }
|
2024-07-19 11:52:12 +02:00
|
|
|
|
2024-07-27 08:07:06 +02:00
|
|
|
fn kind(&self) -> ElemKind { (&self.block).into() }
|
2024-07-19 11:52:12 +02:00
|
|
|
|
2024-07-25 13:13:12 +02:00
|
|
|
fn element_name(&self) -> &'static str { "Code Block" }
|
2024-07-19 11:52:12 +02:00
|
|
|
|
2024-10-20 10:06:31 +02:00
|
|
|
fn compile(
|
|
|
|
&self,
|
|
|
|
compiler: &Compiler,
|
|
|
|
_document: &dyn Document,
|
|
|
|
_cursor: usize,
|
|
|
|
) -> Result<String, String> {
|
2024-07-25 13:13:12 +02:00
|
|
|
match compiler.target() {
|
2024-07-19 11:52:12 +02:00
|
|
|
Target::HTML => {
|
2024-07-25 13:13:12 +02:00
|
|
|
static CACHE_INIT: Once = Once::new();
|
|
|
|
CACHE_INIT.call_once(|| {
|
2024-08-14 11:09:42 +02:00
|
|
|
if let Some(con) = compiler.cache() {
|
|
|
|
if let Err(e) = Code::init(con) {
|
2024-07-25 13:13:12 +02:00
|
|
|
eprintln!("Unable to create cache table: {e}");
|
|
|
|
}
|
2024-07-19 11:52:12 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-08-14 11:09:42 +02:00
|
|
|
if let Some(con) = compiler.cache() {
|
|
|
|
match self.cached(con, |s| s.highlight_html(compiler)) {
|
2024-07-19 11:52:12 +02:00
|
|
|
Ok(s) => Ok(s),
|
2024-07-25 13:13:12 +02:00
|
|
|
Err(e) => match e {
|
|
|
|
CachedError::SqlErr(e) => {
|
|
|
|
Err(format!("Querying the cache failed: {e}"))
|
|
|
|
}
|
|
|
|
CachedError::GenErr(e) => Err(e),
|
|
|
|
},
|
2024-07-19 11:52:12 +02:00
|
|
|
}
|
2024-07-25 13:13:12 +02:00
|
|
|
} else {
|
2024-07-19 11:52:12 +02:00
|
|
|
self.highlight_html(compiler)
|
|
|
|
}
|
|
|
|
}
|
2024-07-25 13:13:12 +02:00
|
|
|
Target::LATEX => {
|
|
|
|
todo!("")
|
|
|
|
}
|
2024-07-19 11:52:12 +02:00
|
|
|
}
|
2024-07-25 13:13:12 +02:00
|
|
|
}
|
2024-07-19 11:52:12 +02:00
|
|
|
}
|
|
|
|
|
2024-08-08 17:11:32 +02:00
|
|
|
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::code")]
|
2024-07-19 11:52:12 +02:00
|
|
|
pub struct CodeRule {
|
|
|
|
re: [Regex; 2],
|
|
|
|
properties: PropertyParser,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CodeRule {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
let mut props = HashMap::new();
|
2024-07-25 13:13:12 +02:00
|
|
|
props.insert(
|
|
|
|
"line_offset".to_string(),
|
2024-11-01 22:15:33 +01:00
|
|
|
Property::new("Line number offset".to_string(), Some("1".to_string())),
|
2024-07-25 13:13:12 +02:00
|
|
|
);
|
2024-07-19 11:52:12 +02:00
|
|
|
Self {
|
|
|
|
re: [
|
2024-07-25 13:13:12 +02:00
|
|
|
Regex::new(
|
|
|
|
r"(?:^|\n)```(?:\[((?:\\.|[^\\\\])*?)\])?(.*?)(?:,(.*))?\n((?:\\(?:.|\n)|[^\\\\])*?)```",
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
Regex::new(
|
2024-10-20 12:25:52 +02:00
|
|
|
r"``(?:\[((?:\\.|[^\\\\])*?)\])?(?:([^\r\n`]*?)(?:,|\n))?((?:\\(?:.|\n)|[^\\\\])*?)``",
|
2024-07-25 13:13:12 +02:00
|
|
|
)
|
2024-10-20 19:38:15 +02:00
|
|
|
.unwrap(),
|
2024-07-19 11:52:12 +02:00
|
|
|
],
|
2024-08-06 18:58:41 +02:00
|
|
|
properties: PropertyParser { properties: props },
|
2024-07-19 11:52:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-25 13:13:12 +02:00
|
|
|
impl RegexRule for CodeRule {
|
|
|
|
fn name(&self) -> &'static str { "Code" }
|
2024-10-20 19:38:15 +02:00
|
|
|
|
2024-08-12 11:25:17 +02:00
|
|
|
fn previous(&self) -> Option<&'static str> { Some("Blockquote") }
|
2024-07-19 11:52:12 +02:00
|
|
|
|
2024-07-25 13:13:12 +02:00
|
|
|
fn regexes(&self) -> &[regex::Regex] { &self.re }
|
2024-07-19 11:52:12 +02:00
|
|
|
|
2024-10-24 10:18:49 +02:00
|
|
|
fn enabled(&self, mode: &ParseMode, id: usize) -> bool { !mode.paragraph_only || id != 0 }
|
2024-10-20 19:38:15 +02:00
|
|
|
|
2024-08-14 22:50:32 +02:00
|
|
|
fn on_regex_match(
|
2024-07-25 13:13:12 +02:00
|
|
|
&self,
|
|
|
|
index: usize,
|
2024-08-06 18:58:41 +02:00
|
|
|
state: &ParserState,
|
2024-08-14 22:50:32 +02:00
|
|
|
document: &dyn Document,
|
2024-07-25 13:13:12 +02:00
|
|
|
token: Token,
|
|
|
|
matches: Captures,
|
2024-10-24 09:54:52 +02:00
|
|
|
) -> Vec<Report> {
|
2024-07-19 11:52:12 +02:00
|
|
|
let mut reports = vec![];
|
|
|
|
|
2024-11-01 22:15:33 +01:00
|
|
|
// Properties
|
|
|
|
let prop_source = escape_source(
|
|
|
|
token.source(),
|
|
|
|
matches.get(1).map_or(0..0, |m| m.range()),
|
|
|
|
"Code Properties".into(),
|
|
|
|
'\\',
|
|
|
|
"]",
|
|
|
|
);
|
|
|
|
let properties =
|
|
|
|
match self
|
|
|
|
.properties
|
|
|
|
.parse("Code", &mut reports, state, prop_source.into())
|
|
|
|
{
|
|
|
|
Some(props) => props,
|
|
|
|
None => return reports,
|
|
|
|
};
|
2024-07-25 13:13:12 +02:00
|
|
|
|
|
|
|
let code_lang = match matches.get(2) {
|
2024-07-19 11:52:12 +02:00
|
|
|
None => "Plain Text".to_string(),
|
|
|
|
Some(lang) => {
|
2024-10-25 10:05:13 +02:00
|
|
|
let mut code_lang = lang.as_str().trim_start().trim_end().to_string();
|
2024-07-25 13:13:12 +02:00
|
|
|
if code_lang.is_empty() {
|
2024-10-25 10:05:13 +02:00
|
|
|
code_lang = "Plain Text".into();
|
2024-07-19 11:52:12 +02:00
|
|
|
}
|
2024-07-27 08:07:06 +02:00
|
|
|
if Code::get_syntaxes()
|
|
|
|
.find_syntax_by_name(code_lang.as_str())
|
|
|
|
.is_none()
|
|
|
|
{
|
2024-10-24 09:54:52 +02:00
|
|
|
report_err!(
|
|
|
|
&mut reports,
|
|
|
|
token.source(),
|
|
|
|
"Unknown Code Language".into(),
|
|
|
|
span(
|
|
|
|
lang.range(),
|
|
|
|
format!(
|
|
|
|
"Language `{}` cannot be found",
|
|
|
|
code_lang.fg(state.parser.colors().info)
|
2024-07-27 08:07:06 +02:00
|
|
|
)
|
2024-10-24 09:54:52 +02:00
|
|
|
)
|
2024-07-27 08:07:06 +02:00
|
|
|
);
|
2024-07-25 13:13:12 +02:00
|
|
|
|
2024-07-27 08:07:06 +02:00
|
|
|
return reports;
|
|
|
|
}
|
2024-07-19 11:52:12 +02:00
|
|
|
|
|
|
|
code_lang
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-07-25 13:13:12 +02:00
|
|
|
let mut code_content = if index == 0 {
|
2024-11-02 09:29:37 +01:00
|
|
|
util::escape_text('\\', "```", matches.get(4).unwrap().as_str(), false)
|
2024-07-25 13:13:12 +02:00
|
|
|
} else {
|
2024-11-03 10:10:36 +01:00
|
|
|
util::escape_text(
|
|
|
|
'\\',
|
|
|
|
"``",
|
|
|
|
matches.get(3).unwrap().as_str(),
|
|
|
|
!matches.get(3).unwrap().as_str().contains('\n'),
|
|
|
|
)
|
2024-07-25 13:13:12 +02:00
|
|
|
};
|
2024-08-14 22:50:32 +02:00
|
|
|
if code_content.bytes().last() == Some(b'\n')
|
2024-07-25 13:13:12 +02:00
|
|
|
// Remove newline
|
2024-07-19 11:52:12 +02:00
|
|
|
{
|
|
|
|
code_content.pop();
|
|
|
|
}
|
|
|
|
|
2024-07-25 13:13:12 +02:00
|
|
|
if code_content.is_empty() {
|
2024-10-24 09:54:52 +02:00
|
|
|
report_err!(
|
|
|
|
&mut reports,
|
|
|
|
token.source(),
|
|
|
|
"Empty Code Content".into(),
|
2024-10-24 10:18:49 +02:00
|
|
|
span(token.range.clone(), "Code content cannot be empty".into())
|
2024-07-25 13:13:12 +02:00
|
|
|
);
|
2024-07-19 11:52:12 +02:00
|
|
|
return reports;
|
|
|
|
}
|
|
|
|
|
2024-07-25 13:13:12 +02:00
|
|
|
let theme = document
|
2024-10-20 10:06:31 +02:00
|
|
|
.get_variable("code.theme")
|
|
|
|
.map(|var| var.to_string());
|
2024-07-19 11:52:12 +02:00
|
|
|
|
2024-07-25 13:13:12 +02:00
|
|
|
if index == 0
|
|
|
|
// Block
|
2024-07-19 11:52:12 +02:00
|
|
|
{
|
2024-07-25 13:13:12 +02:00
|
|
|
let code_name = matches.get(3).and_then(|name| {
|
|
|
|
let code_name = name.as_str().trim_end().trim_start().to_string();
|
|
|
|
(!code_name.is_empty()).then_some(code_name)
|
|
|
|
});
|
2024-11-01 22:15:33 +01:00
|
|
|
let line_offset = match properties.get(&mut reports, "line_offset", |_, value| {
|
|
|
|
value.value.parse::<usize>()
|
2024-10-24 10:18:49 +02:00
|
|
|
}) {
|
2024-11-01 22:15:33 +01:00
|
|
|
Some(line_offset) => line_offset,
|
|
|
|
_ => return reports,
|
2024-10-24 10:18:49 +02:00
|
|
|
};
|
2024-07-25 13:13:12 +02:00
|
|
|
|
2024-08-06 18:58:41 +02:00
|
|
|
state.push(
|
2024-07-25 13:13:12 +02:00
|
|
|
document,
|
|
|
|
Box::new(Code::new(
|
|
|
|
token.clone(),
|
|
|
|
CodeKind::FullBlock,
|
|
|
|
code_lang,
|
|
|
|
code_name,
|
|
|
|
code_content,
|
|
|
|
theme,
|
|
|
|
line_offset,
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
} else
|
|
|
|
// Maybe inline
|
|
|
|
{
|
|
|
|
let block = if code_content.contains('\n') {
|
|
|
|
CodeKind::MiniBlock
|
|
|
|
} else {
|
|
|
|
CodeKind::Inline
|
2024-07-19 11:52:12 +02:00
|
|
|
};
|
|
|
|
|
2024-08-06 18:58:41 +02:00
|
|
|
state.push(
|
2024-07-25 13:13:12 +02:00
|
|
|
document,
|
|
|
|
Box::new(Code::new(
|
|
|
|
token.clone(),
|
|
|
|
block,
|
|
|
|
code_lang,
|
|
|
|
None,
|
|
|
|
code_content,
|
|
|
|
theme,
|
|
|
|
1,
|
|
|
|
)),
|
|
|
|
);
|
2024-07-19 11:52:12 +02:00
|
|
|
}
|
|
|
|
|
2024-10-26 17:30:10 +02:00
|
|
|
if let Some((sems, tokens)) = Semantics::from_source(token.source(), &state.shared.lsp) {
|
2024-10-20 10:06:31 +02:00
|
|
|
let range = matches
|
|
|
|
.get(0)
|
|
|
|
.map(|m| {
|
|
|
|
if token.source().content().as_bytes()[m.start()] == b'\n' {
|
|
|
|
m.start() + 1..m.end()
|
|
|
|
} else {
|
|
|
|
m.range()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
sems.add(
|
|
|
|
range.start..range.start + if index == 0 { 3 } else { 2 },
|
|
|
|
tokens.code_sep,
|
|
|
|
);
|
|
|
|
if let Some(props) = matches.get(1).map(|m| m.range()) {
|
|
|
|
sems.add(props.start - 1..props.start, tokens.code_props_sep);
|
|
|
|
sems.add(props.end..props.end + 1, tokens.code_props_sep);
|
|
|
|
}
|
|
|
|
if let Some(lang) = matches.get(2).map(|m| m.range()) {
|
|
|
|
sems.add(lang.clone(), tokens.code_lang);
|
|
|
|
}
|
|
|
|
if index == 0 {
|
|
|
|
if let Some(title) = matches.get(3).map(|m| m.range()) {
|
|
|
|
sems.add(title.clone(), tokens.code_title);
|
|
|
|
}
|
|
|
|
sems.add(matches.get(4).unwrap().range(), tokens.code_content);
|
|
|
|
} else {
|
|
|
|
sems.add(matches.get(3).unwrap().range(), tokens.code_content);
|
|
|
|
}
|
|
|
|
sems.add(
|
|
|
|
range.end - if index == 0 { 3 } else { 2 }..range.end,
|
|
|
|
tokens.code_sep,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-07-19 11:52:12 +02:00
|
|
|
reports
|
2024-07-25 13:13:12 +02:00
|
|
|
}
|
2024-07-21 15:56:56 +02:00
|
|
|
|
2024-08-05 18:40:17 +02:00
|
|
|
fn register_bindings<'lua>(&self, lua: &'lua Lua) -> Vec<(String, Function<'lua>)> {
|
2024-07-27 08:23:15 +02:00
|
|
|
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
|
2024-10-20 10:06:31 +02:00
|
|
|
.get_variable("code.theme")
|
|
|
|
.map(|var| var.to_string());
|
2024-07-27 08:23:15 +02:00
|
|
|
|
2024-08-06 18:58:41 +02:00
|
|
|
ctx.state.push(
|
2024-07-27 08:23:15 +02:00
|
|
|
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
|
2024-10-20 10:06:31 +02:00
|
|
|
.get_variable("code.theme")
|
|
|
|
.map(|var| var.to_string());
|
2024-07-27 08:23:15 +02:00
|
|
|
|
2024-08-06 18:58:41 +02:00
|
|
|
ctx.state.push(
|
2024-07-27 08:23:15 +02:00
|
|
|
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(
|
2024-07-29 16:45:14 +02:00
|
|
|
|_,
|
|
|
|
(language, name, content, line_offset): (
|
|
|
|
String,
|
|
|
|
Option<String>,
|
|
|
|
String,
|
|
|
|
Option<usize>,
|
|
|
|
)| {
|
2024-07-27 08:23:15 +02:00
|
|
|
CTX.with_borrow(|ctx| {
|
|
|
|
ctx.as_ref().map(|ctx| {
|
|
|
|
let theme = ctx
|
|
|
|
.document
|
2024-10-20 10:06:31 +02:00
|
|
|
.get_variable("code.theme")
|
|
|
|
.map(|var| var.to_string());
|
2024-07-27 08:23:15 +02:00
|
|
|
|
2024-08-06 18:58:41 +02:00
|
|
|
ctx.state.push(
|
2024-07-27 08:23:15 +02:00
|
|
|
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(),
|
|
|
|
));
|
|
|
|
|
2024-08-05 18:40:17 +02:00
|
|
|
bindings
|
2024-07-27 08:23:15 +02:00
|
|
|
}
|
2024-07-19 11:52:12 +02:00
|
|
|
}
|
2024-07-27 08:07:06 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use crate::parser::langparser::LangParser;
|
2024-08-06 18:58:41 +02:00
|
|
|
use crate::parser::parser::Parser;
|
2024-07-27 08:07:06 +02:00
|
|
|
use crate::parser::source::SourceFile;
|
2024-10-20 10:06:31 +02:00
|
|
|
use crate::validate_semantics;
|
2024-10-24 10:18:49 +02:00
|
|
|
use std::rc::Rc;
|
2024-07-27 08:07:06 +02:00
|
|
|
|
|
|
|
#[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;
|
|
|
|
```
|
2024-07-27 08:23:15 +02:00
|
|
|
%<nml.code.push_block("Lua", "From Lua", "print(\"Hello, World!\")", nil)>%
|
2024-08-01 14:23:41 +02:00
|
|
|
``Rust,
|
2024-07-27 08:07:06 +02:00
|
|
|
fn fact(n: usize) -> usize
|
|
|
|
{
|
|
|
|
match n
|
|
|
|
{
|
|
|
|
0 | 1 => 1,
|
|
|
|
_ => n * fact(n-1)
|
|
|
|
}
|
|
|
|
}
|
2024-07-27 08:23:15 +02:00
|
|
|
``
|
|
|
|
%<nml.code.push_miniblock("Bash", "NUM=$(($RANDOM % 10))", 18)>%
|
|
|
|
"#
|
2024-07-27 08:07:06 +02:00
|
|
|
.to_string(),
|
|
|
|
None,
|
|
|
|
));
|
|
|
|
let parser = LangParser::default();
|
2024-10-20 19:38:15 +02:00
|
|
|
let (doc, _) = parser.parse(
|
|
|
|
ParserState::new(&parser, None),
|
|
|
|
source,
|
|
|
|
None,
|
|
|
|
ParseMode::default(),
|
|
|
|
);
|
2024-07-27 08:07:06 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2024-07-27 08:23:15 +02:00
|
|
|
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!\")");
|
2024-07-27 08:07:06 +02:00
|
|
|
assert_eq!(found[1].line_offset, 1);
|
2024-07-27 08:23:15 +02:00
|
|
|
|
|
|
|
assert_eq!(found[2].block, CodeKind::MiniBlock);
|
|
|
|
assert_eq!(found[2].language, "Rust");
|
|
|
|
assert_eq!(found[2].name, None);
|
2024-11-03 00:27:31 +01:00
|
|
|
assert_eq!(found[2].code, "\nfn 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}");
|
2024-07-27 08:23:15 +02:00
|
|
|
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);
|
2024-07-27 08:07:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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!``
|
2024-07-27 08:23:15 +02:00
|
|
|
%<nml.code.push_inline("C++", "std::vector<std::vector<int>> u;")>%
|
2024-07-27 08:07:06 +02:00
|
|
|
"#
|
|
|
|
.to_string(),
|
|
|
|
None,
|
|
|
|
));
|
|
|
|
let parser = LangParser::default();
|
2024-10-20 19:38:15 +02:00
|
|
|
let (doc, _) = parser.parse(
|
|
|
|
ParserState::new(&parser, None),
|
|
|
|
source,
|
|
|
|
None,
|
|
|
|
ParseMode::default(),
|
|
|
|
);
|
2024-07-27 08:07:06 +02:00
|
|
|
|
|
|
|
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);
|
2024-07-27 08:23:15 +02:00
|
|
|
|
|
|
|
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);
|
2024-07-27 08:07:06 +02:00
|
|
|
}
|
2024-10-20 10:06:31 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn semantic() {
|
|
|
|
let source = Rc::new(SourceFile::with_content(
|
|
|
|
"".to_string(),
|
|
|
|
r#"
|
|
|
|
```[line_offset=15] C, Title
|
|
|
|
test code
|
|
|
|
```
|
|
|
|
``C, Single Line``
|
|
|
|
"#
|
|
|
|
.to_string(),
|
|
|
|
None,
|
|
|
|
));
|
|
|
|
let parser = LangParser::default();
|
|
|
|
let (_, state) = parser.parse(
|
|
|
|
ParserState::new_with_semantics(&parser, None),
|
|
|
|
source.clone(),
|
|
|
|
None,
|
2024-10-20 19:38:15 +02:00
|
|
|
ParseMode::default(),
|
2024-10-20 10:06:31 +02:00
|
|
|
);
|
|
|
|
validate_semantics!(state, source.clone(), 0,
|
|
|
|
code_sep { delta_line == 1, delta_start == 0, length == 3 };
|
|
|
|
code_props_sep { delta_line == 0, delta_start == 3, length == 1 };
|
2024-11-01 22:15:33 +01:00
|
|
|
prop_name { delta_line == 0, delta_start == 1, length == 11 };
|
|
|
|
prop_equal { delta_line == 0, delta_start == 11, length == 1 };
|
|
|
|
prop_value { delta_line == 0, delta_start == 1, length == 2 };
|
|
|
|
code_props_sep { delta_line == 0, delta_start == 2, length == 1 };
|
2024-10-20 10:06:31 +02:00
|
|
|
code_lang { delta_line == 0, delta_start == 1, length == 2 };
|
|
|
|
code_title { delta_line == 0, delta_start == 3, length == 6 };
|
|
|
|
code_content { delta_line == 1, delta_start == 0, length == 10 };
|
|
|
|
code_sep { delta_line == 1, delta_start == 0, length == 3 };
|
|
|
|
|
|
|
|
code_sep { delta_line == 1, delta_start == 0, length == 2 };
|
|
|
|
code_lang { delta_line == 0, delta_start == 2, length == 1 };
|
|
|
|
code_content { delta_line == 0, delta_start == 2, length == 12 };
|
|
|
|
code_sep { delta_line == 0, delta_start == 12, length == 2 };
|
|
|
|
);
|
|
|
|
}
|
2024-07-27 08:07:06 +02:00
|
|
|
}
|