Compare commits

..

No commits in common. "master" and "parser-refactor" have entirely different histories.

57 changed files with 758 additions and 2845 deletions

21
Cargo.lock generated
View file

@ -59,16 +59,6 @@ dependencies = [
"syn 2.0.72",
]
[[package]]
name = "auto-registry"
version = "0.0.4"
dependencies = [
"lazy_static",
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "auto_impl"
version = "1.2.0"
@ -743,7 +733,6 @@ name = "nml"
version = "0.1.0"
dependencies = [
"ariadne",
"auto-registry",
"dashmap 6.0.1",
"downcast-rs",
"getopts",
@ -754,7 +743,6 @@ dependencies = [
"mlua",
"rand 0.8.5",
"regex",
"runtime-format",
"rusqlite",
"rust-crypto",
"serde",
@ -1106,15 +1094,6 @@ version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
[[package]]
name = "runtime-format"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09958d5b38bca768ede7928c767c89a08ba568144a7b61992aecae79b03c8c94"
dependencies = [
"tinyvec",
]
[[package]]
name = "rusqlite"
version = "0.31.0"

View file

@ -17,7 +17,6 @@ inherits = "release"
debug = true
[dependencies]
auto-registry = { path = "crates/auto-registry" }
ariadne = "0.4.1"
dashmap = "6.0.1"
downcast-rs = "1.2.1"
@ -33,16 +32,11 @@ rust-crypto = "0.2.36"
serde = "1.0.204"
serde_json = "1.0.120"
syntect = "5.2.0"
tokio = { version = "1.38.1", features = [
"macros",
"rt-multi-thread",
"io-std",
] }
tokio = { version = "1.38.1", features = ["macros", "rt-multi-thread", "io-std"]}
tower-lsp = "0.20.0"
unicode-segmentation = "1.11.0"
walkdir = "2.5.0"
runtime-format = "0.1.3"
[dev-dependencies]
rand = "0.8.5"

View file

@ -35,9 +35,9 @@ cargo build --release --bin nml
- [x] LaTeX rendering
- [x] Graphviz rendering
- [x] Media
- [x] References
- [x] Navigation
- [x] Cross-Document references
- [ ] References
- [ ] Navigation
- [ ] Cross-Document references
- [ ] Complete Lua api
- [ ] Documentation
- [ ] Table

View file

@ -1,54 +0,0 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "auto-registry"
version = "0.0.4"
dependencies = [
"lazy_static",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "lazy_static"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
[[package]]
name = "proc-macro2"
version = "1.0.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7"
dependencies = [
"proc-macro2",
]
[[package]]
name = "syn"
version = "1.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"

View file

@ -1,13 +0,0 @@
[package]
name = "auto-registry"
version = "0.0.4"
edition = "2021"
[lib]
proc-macro = true
[dependencies]
proc-macro2 = { version = "1.0"}
quote = "1.0"
syn = { version = "1.0", features = [ "full" ] }
lazy_static = "1.5.0"

View file

@ -1,296 +0,0 @@
#![feature(proc_macro_span)]
use std::cell::RefCell;
use std::collections::HashMap;
use lazy_static::lazy_static;
use proc_macro::TokenStream;
use quote::quote;
use std::sync::Mutex;
use syn::parse::Parse;
use syn::parse::ParseStream;
use syn::parse_macro_input;
use syn::ItemStruct;
lazy_static! {
/// The registry, each key corresponds to an identifier that needs to be
/// valid in the context of the [`genegenerate_registry`] macro.
static ref REGISTRY: Mutex<RefCell<HashMap<String, Vec<String>>>> =
Mutex::new(RefCell::new(HashMap::new()));
}
/// Arguments for the [`auto_registry`] proc macro
struct AutoRegistryArgs {
/// The registry name
registry: syn::LitStr,
/// The absolute path to the struct, if not specified the macro will try
/// to automatically infer the full path.
path: Option<syn::LitStr>,
}
/// Parser for [`AutoRegistryArgs`]
impl Parse for AutoRegistryArgs {
fn parse(input: ParseStream) -> syn::Result<Self> {
let mut registry = None;
let mut path = None;
loop {
let key: syn::Ident = input.parse()?;
input.parse::<syn::Token![=]>()?;
let value: syn::LitStr = input.parse()?;
match key.to_string().as_str() {
"registry" => registry = Some(value),
"path" => path = Some(value),
_ => {
return Err(syn::Error::new(
key.span(),
format!(
"Unknown attribute `{}`, excepted `registry` or `path`",
key.to_string()
),
))
}
}
if input.is_empty() {
break;
}
input.parse::<syn::Token![,]>()?;
}
if registry.is_none() {
return Err(syn::Error::new(
input.span(),
"Missing required attribute `registry`".to_string(),
));
}
Ok(AutoRegistryArgs {
registry: registry.unwrap(),
path,
})
}
}
/// The proc macro used on a struct to add it to the registry
///
/// # Attributes
/// - registry: (String) Name of the registry to collect the struct into
/// - path: (Optional String) The crate path in which the struct is located
/// If left empty, the path will be try to be automatically-deduced
///
/// # Note
///
/// Due to a lacking implementation of `proc_macro_span` in rust-analyzer,
/// it is highly advised the set the `path` attribute when using this macro.
/// See https://github.com/rust-lang/rust-analyzer/issues/15950
#[proc_macro_attribute]
pub fn auto_registry(attr: TokenStream, input: TokenStream) -> TokenStream {
let args = parse_macro_input!(attr as AutoRegistryArgs);
let input = parse_macro_input!(input as ItemStruct);
let ident = &input.ident;
let path = if let Some(path) = args.path {
let value = path.value();
if value.is_empty() {
value
} else {
format!("{}::{}", value, ident.to_string().as_str())
}
} else {
// Attempt to get the path in a hacky way in case the path wasn't
// specified as an attribute to the macro
let path = match input
.ident
.span()
.unwrap()
.source_file()
.path()
.canonicalize()
{
Ok(path) => path,
Err(e) => {
return syn::Error::new(
input.ident.span(),
format!("Failed to canonicalize path: {}", e),
)
.to_compile_error()
.into();
}
};
let crate_path = std::env::var("CARGO_MANIFEST_DIR").unwrap();
let relative_path = path.strip_prefix(&crate_path).unwrap();
let relative_path_str = relative_path.to_string_lossy();
// Remove the first path component e.g "src/"
let pos = if let Some(pos) = relative_path_str.find("/") {
pos + 1
} else {
0
};
let module_path = relative_path_str
.split_at(pos)
.1
.strip_suffix(".rs")
.unwrap()
.replace("/", "::");
if module_path.is_empty() {
format!("crate::{}", ident.to_string())
} else {
format!("crate::{module_path}::{}", ident.to_string())
}
};
let reg_mtx = REGISTRY.lock().unwrap();
let mut reg_borrow = reg_mtx.borrow_mut();
if let Some(ref mut vec) = reg_borrow.get_mut(args.registry.value().as_str()) {
vec.push(path);
} else {
reg_borrow.insert(args.registry.value(), vec![path]);
}
quote! {
#input
}
.into()
}
/// Arguments for the [`generate_registry`] proc macro
struct GenerateRegistryArgs {
/// The registry name
registry: syn::LitStr,
/// The target, i.e the generated function name
target: syn::Ident,
/// The maker macro, takes all constructed items and processes them
maker: syn::Expr,
/// The return type for the function
return_type: syn::Type,
}
/// Parser for [`GenerateRegistryArgs`]
impl Parse for GenerateRegistryArgs {
fn parse(input: ParseStream) -> syn::Result<Self> {
let mut registry = None;
let mut target = None;
let mut maker = None;
let mut return_type = None;
loop {
let key: syn::Ident = input.parse()?;
input.parse::<syn::Token![=]>()?;
match key.to_string().as_str() {
"registry" => registry = Some(input.parse()?),
"target" => target = Some(input.parse()?),
"maker" => maker = Some(input.parse()?),
"return_type" => return_type = Some(input.parse()?),
_ => {
return Err(syn::Error::new(
key.span(),
format!(
"Unknown attribute `{}`, excepted `registry` or `target`",
key.to_string()
),
))
}
}
if input.is_empty() {
break;
}
input.parse::<syn::Token![,]>()?;
}
if registry.is_none() {
return Err(syn::Error::new(
input.span(),
"Missing required attribute `registry`".to_string(),
));
} else if target.is_none() {
return Err(syn::Error::new(
input.span(),
"Missing required attribute `target`".to_string(),
));
} else if maker.is_none() {
return Err(syn::Error::new(
input.span(),
"Missing required attribute `maker`".to_string(),
));
} else if return_type.is_none() {
return Err(syn::Error::new(
input.span(),
"Missing required attribute `return_type`".to_string(),
));
}
Ok(GenerateRegistryArgs {
registry: registry.unwrap(),
target: target.unwrap(),
maker: maker.unwrap(),
return_type: return_type.unwrap(),
})
}
}
/// The proc macro that generates the function to build the registry
///
/// # Attributes
/// - registry: (String) Name of the registry to generate
/// - target: (Identifier) Name of the resulting function
/// - maker: (Macro) A macro that will take all the newly constructed objects
/// comma-separated and create the resulting expression
/// - return_type: (Type) The return type of the generated function.
/// Must match the type of the macro invocation
///
/// # Example
/// ```
/// macro_rules! create_listeners {
/// ( $($construct:expr),+ $(,)? ) => {{
/// vec![$(Box::new($construct) as Box<dyn Listener>,)+]
/// }};
/// }
/// #[generate_registry(
/// registry = "listeners",
/// target = build_listeners,
/// return_type = Vec<Box<dyn Listener>>,
/// maker = create_listeners)]
///
/// fn main()
/// {
/// let all_listeners : Vec<Box<dyn Listener>> = build_listeners();
/// }
/// ```
#[proc_macro_attribute]
pub fn generate_registry(attr: TokenStream, input: TokenStream) -> TokenStream {
let args = parse_macro_input!(attr as GenerateRegistryArgs);
let reg_mtx = REGISTRY.lock().unwrap();
let mut stream = proc_macro2::TokenStream::new();
if let Some(names) = reg_mtx.borrow().get(args.registry.value().as_str()) {
for name in names {
let struct_name: proc_macro2::TokenStream = name.parse().unwrap();
stream.extend(quote::quote_spanned!(proc_macro2::Span::call_site() =>
#struct_name::new(),
));
}
} else {
panic!(
"Unable to find registry item with key=`{}`",
args.registry.value()
);
}
let function = args.target;
let return_type = args.return_type;
let maker = args.maker;
let rest: proc_macro2::TokenStream = input.into();
quote! {
fn #function() -> #return_type {
#maker!(
#stream
)
}
#rest
}
.into()
}

View file

@ -1,59 +0,0 @@
@import ../template.nml
@nav.previous = Blockquote
%<make_doc({"Blocks"}, "Blockquotes", "Blockquotes")>%
# Blockquotes
>[author=Lennart Poettering, cite=SystemD github issue 5998, url=https://github.com/systemd/systemd/pull/5998]
>>IMO, you shouldn't see the assignment of a CVE as a negative thing. The bug exists whether or not a CVE is assigned. The assignment of a CVE allows for people to consider what this issue means for them.
>
>Well, that makes no sense. You don't assign CVEs to every single random bugfix we do, do you? So why this one? I understand your currency is CVEs, but this just makes CVEs useless. And hardly anymore useful than a git history...
>
>I mean, I am fine with security bureaucracy if it actually helps anyone, but you just create noise where there shouldn't be any. And that way you just piss off the upstreams whose cooperation you actually should be interested in. Your at least made sure that my own interest in helping your efforts goes to zero...
# Nesting blockquotes
> Quotes can be nested
>> Here's a subquote
>>>[author=With author, cite=With cite]
>>> Here's another subquote
>> Back to the subquote
>
>> Another subquote
> This issue is getting a bit too heated, locking right now
```Markdown, Given by the following
> Nest quotes can be nested
>> Here's a subquote
>>>[author=With author, cite=With cite]
>>> Here's another subquote
>> Back to the subquote
>
>> Another subquote
> This issue is getting a bit too heated, locking right now
```
# Properties
Properties must be specified on the first `>` of the quote, inside brackets.
* ``author`` The quote author
* ``cite`` The quote source name
* ``url`` The quote source url (used for accessibility)
# Blockquotes styling
The blockquotes styling controls how the author, cite and url are rendered. This is controlled by style key ``style.blockquote``.
* ``author_pos`` Position of the author statement, available options:
*- `None` Hides the author
*- `Before` Displays the author before the quote
*- `After` Displays the author after the quote (default)
* ``format`` An array with 3 format strings to control how the author is displayed:
*-[offset=0] Format for author+cite
*- Format for author onl
*- Format for cite only
```JSON, Default Style
{
"author_pos": "After",
"format": ["{author}, {cite}", "{author}", "{cite}"],
}
```

View file

@ -8,50 +8,28 @@
digraph {
bgcolor=transparent;
graph[fontcolor=darkgray];
node[shape=box,fontcolor=darkgray];
edge[fontcolor=darkgray, color=gray];
filelist [color=orange, label="File List"];
doclist [color=orange, label="Document List"];
node[fontcolor=darkgray];
edge[fontcolor=darkgray, color=gray90];
filelist [shape=box, color=orange, label="File List"];
doclist [shape=box, color=orange, label="Document List"];
iscached [shape=diamond, color=red, label="Cached?"];
parse [color=white, label=Parse];
compile [color=white, label=Compile];
cache [color=orange, label=Cache];
parse [shape=box, color=white, label=Parse];
compile [shape=box,color=white, label=Compile];
cache [shape=box, color=orange, label=Cache];
filelist -> iscached;
iscached -> cache[dir=both,color=lightblue,style=dashed];
iscached -> doclist[label="Yes",color=lightblue,style=dashed];
iscached -> parse[label="No",color=lightblue,style=dashed];
subgraph cluster_0 {
style=dotted;
color=white;
label = "Processing";
labeljust="l";
parse -> compile;
}
iscached -> cache[dir=both];
iscached -> doclist[label="Yes"];
iscached -> parse[label="No"];
parse -> compile;
compile -> cache[label=""];
compile -> doclist[label=""];
buildnav [color=white, label="Build Navigation"];
xref [color=white, label="Resolve Cross-References"];
doclist -> xref;
doclist -> buildnav[label="Cached",color=lightblue,style=dashed];
subgraph cluster_1 {
style=dotted;
color=white;
label = "Post-Processing";
labeljust="l";
xref -> buildnav;
}
xref -> cache[color=lightblue,style=dashed];
output [color=orange, label="Output"];
doclist -> buildnav;
output [color=white, label="Output"];
buildnav -> output;
}
[/graph]
@ -65,7 +43,7 @@ Graphs blocks are delimited by `` [graph]...[/graph]``
# Properties
* ``layout`` The layout engine, defaults to `dot`
see [Graphviz's documentation](https://graphviz.org/docs/layouts/). Allowed values:
see [Graphviz's documentation](https://graphviz.org/docs/layouts/), allowed values:
*- [`dot`](https://graphviz.org/docs/layouts/dot/)
*- [`neato`](https://graphviz.org/docs/layouts/neato/)
*- [`fdp`](https://graphviz.org/docs/layouts/fdp/)
@ -335,10 +313,3 @@ digraph UML_Class_diagram {
Graphviz graphs that have been rendered to **svg** are stored in the cache database, under table ``cached_dot``.
Unless you modify the graph or it's properties, it won't be rendered again, instead it will be sourced from the database.
# Bindigs
* ``Lua, nml.graphviz.push(layout, width, dot)``
** ``layout`` *(string)* the layout engine
** ``width`` *(string)* the width property (empty string for default)
** ``dot`` *(string)* the graphviz code

View file

@ -3,14 +3,7 @@
@LaTeX = $|[kind=inline, caption=LaTeX]\LaTeX|$
#+LAYOUT_BEGIN Centered
*Bring some %LaTeX% unto your document!*
#+LAYOUT_END
# Requirements
In order to use LaTeX processing, you need to have a %LaTeX% distribution installed. We recommend the [TeX Live](https://en.wikipedia.org/wiki/TeX_Live) distribution.
You'll also need to install the [latex2svg](https://github.com/ef3d0c3e/nml/blob/master/third/latex2svg) python script provided with NML. You'll have to follow the installation instructions from the [original latex2svg repository](https://github.com/Moonbase59/latex2svg). If you don't want to add the script to your `\$PATH`, you can set the executable path in the §{tex_env}[caption=LaTeX environment].
# Inline Math
@ -71,7 +64,7 @@ $|\begin{tikzpicture}
\end{tikzpicture}|$
#+LAYOUT_END
#{tex_env} LaTeX environment
# LaTeX environment
You can define multiple %LaTeX% environment, the default being `main`
* ``@tex.env.fontsize`` The fontsize (in pt) specified to `latex2svg` (default: `12`).
@ -105,20 +98,4 @@ To set the environment you wish to use for a particular %LaTeX% element, set the
%LaTeX% elements that have been successfully rendered to **svg** are stored in the cache database, to avoid processing them a second time.
Note that this cache is shared between documents, so you don't need to reprocess them if they share the same environment.
They are stored under the table named ``Plain Text,cached_tex``, if you modify the `env` all elements will be reprocessed which may take a while...
# Bindings
* ``Lua, nml.tex.push_math(kind, tex [, env [, caption]])``
inserts a math mode %LaTeX% element.
** ``kind`` *(string)* the element kind (inline or block)
** ``tex`` *(string)* the %LaTeX% code
** ``env`` *(string)* the %LaTeX% environment (defaults to `main`)
** ``caption`` *(string)* the accessibility caption
* ``Lua, nml.tex.push(kind, tex [, env [, caption]])``
inserts a non-math %LaTeX% element.
** ``kind`` *(string)* the element kind (inline or block)
** ``tex`` *(string)* the %LaTeX% code
** ``env`` *(string)* the %LaTeX% environment (defaults to `main`)
** ``caption`` *(string)* the accessibility caption
They are stored under the table named ``cached_tex``, if you modify the `env` all elements will be reprocessed which may take a while...

View file

@ -1,31 +0,0 @@
@import template.nml
@nav.previous = Sections
%<make_doc({}, "References", "References")>%
#{internal_references} Internal references
Internal references allow you to create references to elements defined within the current document.
Reference the the current section: ``§{internal_reference}`` → §{internal_references}
## Media references
![flower](assets/flower.webm)[caption = Flower]
When you reference a medium from the current document, the reference can be hovered to show the referenced medium: §{flower}.
# External references
You can reference elements from other documents by adding the document's name before the reference name (separated by a ``#``).
The document name refers to the output file (as defined by the variable `compiler.output`) excluding the extension.
* ``§{doc#ref}``: Finds reference named `ref` in document named `doc`.
* ``§{#ref}``: Finds reference named `ref` in all documents.
Note that this will fail if there are multiple documents defining reference `ref`.
For instance:
* ``§{LaTeX#tex_env}[caption=LaTeX environment]`` → §{LaTeX#tex_env}[caption=LaTeX environment]
* ``§{#tex_env}[caption=LaTeX environment]`` → §{#tex_env}[caption=LaTeX environment]
# Properties
* ``caption`` The display caption for the reference

View file

@ -54,14 +54,3 @@ The styling for the section link is controlled by the style key ``style.section`
"link": ["", "🔗", " "]
}
```
# Bindings
* ``Lua, nml.section.push(title, depth, [, kind [, reference]])``
** ``title`` *(string)* the section display title
** ``depth`` *(number)* the section depth
** ``kind`` *(string)* the section kind
**- `\*` for unnumbered
**- `+` for outside of the table of content
**- `\*+` or `+\*` for both
** ``reference`` *(string)* the section reference name

8
src/cache/cache.rs vendored
View file

@ -23,7 +23,7 @@ pub trait Cached {
fn key(&self) -> <Self as Cached>::Key;
fn init(con: &Connection) -> Result<(), rusqlite::Error> {
fn init(con: &mut Connection) -> Result<(), rusqlite::Error> {
con.execute(<Self as Cached>::sql_table(), ()).map(|_| ())
}
@ -38,7 +38,7 @@ pub trait Cached {
/// Note that on error, [`f`] may still have been called
fn cached<E, F>(
&self,
con: &Connection,
con: &mut Connection,
f: F,
) -> Result<<Self as Cached>::Value, CachedError<E>>
where
@ -62,10 +62,10 @@ pub trait Cached {
if let Some(value) = value {
// Found in cache
Ok(value)
return Ok(value);
} else {
// Compute a value
let value = match f(self) {
let value = match f(&self) {
Ok(val) => val,
Err(e) => return Err(CachedError::GenErr(e)),
};

View file

@ -1,54 +1,56 @@
use std::cell::Ref;
use std::cell::RefCell;
use std::cell::RefMut;
use std::collections::HashMap;
use std::rc::Rc;
use rusqlite::Connection;
use crate::document::document::CrossReference;
use crate::document::document::Document;
use crate::document::document::ElemReference;
use crate::document::variable::Variable;
use super::postprocess::PostProcess;
#[derive(Clone, Copy)]
pub enum Target {
HTML,
#[allow(unused)]
LATEX,
}
pub struct Compiler<'a> {
pub struct Compiler {
target: Target,
cache: Option<&'a Connection>,
cache: Option<RefCell<Connection>>,
reference_count: RefCell<HashMap<String, HashMap<String, usize>>>,
// TODO: External references, i.e resolved later
sections_counter: RefCell<Vec<usize>>,
unresolved_references: RefCell<Vec<(usize, CrossReference)>>,
}
impl<'a> Compiler<'a> {
pub fn new(target: Target, con: Option<&'a Connection>) -> Self {
impl Compiler {
pub fn new(target: Target, db_path: Option<String>) -> Self {
let cache = match db_path {
None => None,
Some(path) => match Connection::open(path) {
Err(e) => panic!("Cannot connect to database: {e}"),
Ok(con) => Some(con),
},
};
Self {
target,
cache: con,
cache: cache.map(|con| RefCell::new(con)),
reference_count: RefCell::new(HashMap::new()),
sections_counter: RefCell::new(vec![]),
unresolved_references: RefCell::new(vec![]),
}
}
/// Gets the section counter for a given depth
/// This function modifies the section counter
pub fn section_counter(&self, depth: usize) -> Ref<'_, Vec<usize>> {
pub fn section_counter(&self, depth: usize) -> Ref<'_, Vec<usize>>
{
// Increment current counter
if self.sections_counter.borrow().len() == depth {
self.sections_counter
.borrow_mut()
.last_mut()
self.sections_counter.borrow_mut().last_mut()
.map(|id| *id += 1);
return Ref::map(self.sections_counter.borrow(), |b| b);
return Ref::map(self.sections_counter.borrow(), |b| &*b);
}
// Close
@ -61,7 +63,7 @@ impl<'a> Compiler<'a> {
self.sections_counter.borrow_mut().push(1);
}
Ref::map(self.sections_counter.borrow(), |b| b)
Ref::map(self.sections_counter.borrow(), |b| &*b)
}
/// Sanitizes text for a [`Target`]
@ -77,51 +79,6 @@ impl<'a> Compiler<'a> {
}
}
/// Sanitizes a format string for a [`Target`]
///
/// # Notes
///
/// This function may process invalid format string, which will be caught later
/// by runtime_format.
pub fn sanitize_format<S: AsRef<str>>(target: Target, str: S) -> String {
match target {
Target::HTML => {
let mut out = String::new();
let mut braces = 0;
for c in str.as_ref().chars() {
if c == '{' {
out.push(c);
braces += 1;
continue;
} else if c == '}' {
out.push(c);
if braces != 0 {
braces -= 1;
}
continue;
}
// Inside format args
if braces % 2 == 1 {
out.push(c);
continue;
}
match c {
'&' => out += "&amp;",
'<' => out += "&lt;",
'>' => out += "&gt;",
'"' => out += "&quot;",
_ => out.push(c),
}
}
out
}
_ => todo!("Sanitize not implemented"),
}
}
/// Gets a reference name
pub fn refname<S: AsRef<str>>(target: Target, str: S) -> String {
Self::sanitize(target, str).replace(' ', "_")
@ -131,7 +88,7 @@ impl<'a> Compiler<'a> {
///
/// # Parameters
/// - [`reference`] The reference to get or insert
pub fn reference_id(&self, document: &dyn Document, reference: ElemReference) -> usize {
pub fn reference_id<'a>(&self, document: &'a dyn Document, reference: ElemReference) -> usize {
let mut borrow = self.reference_count.borrow_mut();
let reference = document.get_from_reference(&reference).unwrap();
let refkey = reference.refcount_key();
@ -157,18 +114,10 @@ impl<'a> Compiler<'a> {
}
}
/// Inserts a new crossreference
pub fn insert_crossreference(&self, pos: usize, reference: CrossReference) {
self.unresolved_references
.borrow_mut()
.push((pos, reference));
}
pub fn target(&self) -> Target { self.target }
pub fn cache(&self) -> Option<&'a Connection> {
self.cache
//self.cache.as_ref().map(RefCell::borrow_mut)
pub fn cache(&self) -> Option<RefMut<'_, Connection>> {
self.cache.as_ref().map(RefCell::borrow_mut)
}
pub fn header(&self, document: &dyn Document) -> String {
@ -176,13 +125,16 @@ impl<'a> Compiler<'a> {
document: &dyn Document,
var_name: &'static str,
) -> Option<Rc<dyn Variable>> {
document.get_variable(var_name).or_else(|| {
println!(
"Missing variable `{var_name}` in {}",
document.source().name()
);
None
})
document
.get_variable(var_name)
.and_then(|var| Some(var))
.or_else(|| {
println!(
"Missing variable `{var_name}` in {}",
document.source().name()
);
None
})
}
let mut result = String::new();
@ -226,7 +178,7 @@ impl<'a> Compiler<'a> {
result
}
pub fn compile(&self, document: &dyn Document) -> (CompiledDocument, PostProcess) {
pub fn compile(&self, document: &dyn Document) -> CompiledDocument {
let borrow = document.content().borrow();
// Header
@ -237,7 +189,7 @@ impl<'a> Compiler<'a> {
for i in 0..borrow.len() {
let elem = &borrow[i];
match elem.compile(self, document, body.len()) {
match elem.compile(self, document) {
Ok(result) => body.push_str(result.as_str()),
Err(err) => println!("Unable to compile element: {err}\n{elem:#?}"),
}
@ -256,35 +208,14 @@ impl<'a> Compiler<'a> {
.map(|(key, var)| (key.clone(), var.to_string()))
.collect::<HashMap<String, String>>();
// References
let references = document
.scope()
.borrow_mut()
.referenceable
.iter()
.map(|(key, reference)| {
let elem = document.get_from_reference(reference).unwrap();
let refid = self.reference_id(document, *reference);
(key.clone(), elem.refid(self, refid))
})
.collect::<HashMap<String, String>>();
let postprocess = PostProcess {
resolve_references: self.unresolved_references.replace(vec![]),
};
let cdoc = CompiledDocument {
CompiledDocument {
input: document.source().name().clone(),
mtime: 0,
variables,
references,
header,
body,
footer,
};
(cdoc, postprocess)
}
}
}
@ -295,14 +226,12 @@ pub struct CompiledDocument {
/// Modification time (i.e seconds since last epoch)
pub mtime: u64,
/// All the variables defined in the document
/// with values mapped by [`Variable::to_string()`]
// TODO: Also store exported references
// so they can be referenced from elsewhere
// This will also require rebuilding in case some exported references have changed...
/// Variables exported to string, so they can be querried later
pub variables: HashMap<String, String>,
/// All the referenceable elements in the document
/// with values mapped by [`ReferenceableElement::refid()`]
pub references: HashMap<String, String>,
/// Compiled document's header
pub header: String,
/// Compiled document's body
@ -315,11 +244,10 @@ impl CompiledDocument {
pub fn get_variable(&self, name: &str) -> Option<&String> { self.variables.get(name) }
fn sql_table() -> &'static str {
"CREATE TABLE IF NOT EXISTS compiled_documents(
"CREATE TABLE IF NOT EXISTS compiled_documents (
input TEXT PRIMARY KEY,
mtime INTEGER NOT NULL,
variables TEXT NOT NULL,
internal_references TEXT NOT NULL,
header TEXT NOT NULL,
body TEXT NOT NULL,
footer TEXT NOT NULL
@ -329,7 +257,7 @@ impl CompiledDocument {
fn sql_get_query() -> &'static str { "SELECT * FROM compiled_documents WHERE input = (?1)" }
fn sql_insert_query() -> &'static str {
"INSERT OR REPLACE INTO compiled_documents (input, mtime, variables, internal_references, header, body, footer) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)"
"INSERT OR REPLACE INTO compiled_documents (input, mtime, variables, header, body, footer) VALUES (?1, ?2, ?3, ?4, ?5, ?6)"
}
pub fn init_cache(con: &Connection) -> Result<usize, rusqlite::Error> {
@ -342,16 +270,15 @@ impl CompiledDocument {
input: input.to_string(),
mtime: row.get_unwrap::<_, u64>(1),
variables: serde_json::from_str(row.get_unwrap::<_, String>(2).as_str()).unwrap(),
references: serde_json::from_str(row.get_unwrap::<_, String>(3).as_str()).unwrap(),
header: row.get_unwrap::<_, String>(4),
body: row.get_unwrap::<_, String>(5),
footer: row.get_unwrap::<_, String>(6),
header: row.get_unwrap::<_, String>(3),
body: row.get_unwrap::<_, String>(4),
footer: row.get_unwrap::<_, String>(5),
})
})
.ok()
}
/// Interts [`CompiledDocument`] into cache
/// Inserts [`CompiledDocument`] into cache
pub fn insert_cache(&self, con: &Connection) -> Result<usize, rusqlite::Error> {
con.execute(
Self::sql_insert_query(),
@ -359,7 +286,6 @@ impl CompiledDocument {
&self.input,
&self.mtime,
serde_json::to_string(&self.variables).unwrap(),
serde_json::to_string(&self.references).unwrap(),
&self.header,
&self.body,
&self.footer,
@ -367,19 +293,3 @@ impl CompiledDocument {
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sanitize_test() {
assert_eq!(Compiler::sanitize(Target::HTML, "<a>"), "&lt;a&gt;");
assert_eq!(Compiler::sanitize(Target::HTML, "&lt;"), "&amp;lt;");
assert_eq!(Compiler::sanitize(Target::HTML, "\""), "&quot;");
assert_eq!(Compiler::sanitize_format(Target::HTML, "{<>&\"}"), "{<>&\"}");
assert_eq!(Compiler::sanitize_format(Target::HTML, "{{<>}}"), "{{&lt;&gt;}}");
assert_eq!(Compiler::sanitize_format(Target::HTML, "{{<"), "{{&lt;");
}
}

View file

@ -1,4 +1,2 @@
pub mod compiler;
pub mod navigation;
pub mod process;
pub mod postprocess;

View file

@ -1,35 +1,22 @@
use std::cell::RefCell;
use std::collections::HashMap;
use crate::compiler::compiler::Compiler;
use super::compiler::CompiledDocument;
use super::compiler::Target;
use super::postprocess::PostProcess;
#[derive(Debug, Default, PartialEq, Eq, Clone)]
pub struct NavEntry {
title: String,
path: String,
previous: Option<String>,
}
#[derive(Debug, Default)]
pub struct NavEntries {
pub(self) entries: Vec<NavEntry>,
pub(self) children: HashMap<String, NavEntries>,
pub struct NavEntry {
pub(self) entries: Vec<(String, String, Option<String>)>,
pub(self) children: HashMap<String, NavEntry>,
}
impl NavEntries {
impl NavEntry {
// FIXME: Sanitize
pub fn compile(&self, target: Target, doc: &RefCell<CompiledDocument>) -> String {
let doc_borrow = doc.borrow();
pub fn compile(&self, target: Target, doc: &CompiledDocument) -> String {
let categories = vec![
doc_borrow
.get_variable("nav.category")
.map_or("", |s| s.as_str()),
doc_borrow
.get_variable("nav.subcategory")
doc.get_variable("nav.category").map_or("", |s| s.as_str()),
doc.get_variable("nav.subcategory")
.map_or("", |s| s.as_str()),
];
@ -43,16 +30,16 @@ impl NavEntries {
categories: &Vec<&str>,
did_match: bool,
result: &mut String,
entry: &NavEntries,
entry: &NavEntry,
depth: usize,
) {
// Orphans = Links
for entry in &entry.entries {
for (title, path, _) in &entry.entries {
result.push_str(
format!(
r#"<li><a href="{}">{}</a></li>"#,
Compiler::sanitize(target, entry.path.as_str()),
Compiler::sanitize(target, entry.title.as_str())
Compiler::sanitize(target, path),
Compiler::sanitize(target, title)
)
.as_str(),
);
@ -90,48 +77,32 @@ impl NavEntries {
}
fn sort_entry(
entrymap: &HashMap<String, Option<String>>,
left_title: &str,
right_title: &str,
left: &(String, String, Option<String>),
right: &(String, String, Option<String>),
) -> std::cmp::Ordering {
let left_previous = entrymap.get(left_title).unwrap();
let right_previous = entrymap.get(right_title).unwrap();
match (left_previous, right_previous) {
(Some(lp), Some(rp)) => {
if lp.as_str() == right_title {
std::cmp::Ordering::Greater
} else if rp.as_str() == left_title {
std::cmp::Ordering::Less
} else if rp.as_str() == lp.as_str() {
left_title.cmp(right_title)
} else {
Self::sort_entry(entrymap, lp.as_str(), rp.as_str())
}
}
match (&left.2, &right.2) {
(Some(_), Some(_)) => left.0.cmp(&right.0),
(Some(lp), None) => {
if right_title == lp.as_str() {
if &right.0 == lp {
std::cmp::Ordering::Greater
} else {
left_title.cmp(right_title)
left.0.cmp(&right.0)
}
}
(None, Some(rp)) => {
if left_title == rp.as_str() {
if &left.0 == rp {
std::cmp::Ordering::Less
} else {
left_title.cmp(right_title)
left.0.cmp(&right.0)
}
}
(None, None) => left_title.cmp(right_title),
(None, None) => left.0.cmp(&right.0),
}
}
}
pub fn create_navigation(
docs: &Vec<(RefCell<CompiledDocument>, Option<PostProcess>)>,
) -> Result<NavEntries, String> {
let mut nav = NavEntries {
pub fn create_navigation(docs: &Vec<CompiledDocument>) -> Result<NavEntry, String> {
let mut nav = NavEntry {
entries: vec![],
children: HashMap::new(),
};
@ -139,20 +110,19 @@ pub fn create_navigation(
// All paths (for duplicate checking)
let mut all_paths = HashMap::new();
for (doc, _) in docs {
let doc_borrow = doc.borrow();
let cat = doc_borrow.get_variable("nav.category");
let subcat = doc_borrow.get_variable("nav.subcategory");
let title = doc_borrow
for doc in docs {
let cat = doc.get_variable("nav.category");
let subcat = doc.get_variable("nav.subcategory");
let title = doc
.get_variable("nav.title")
.or(doc_borrow.get_variable("doc.title"));
let previous = doc_borrow.get_variable("nav.previous").cloned();
let path = doc_borrow.get_variable("compiler.output");
.or(doc.get_variable("doc.title"));
let previous = doc.get_variable("nav.previous").map(|s| s.clone());
let path = doc.get_variable("compiler.output");
let (title, path) = match (title, path) {
(Some(title), Some(path)) => (title, path),
_ => {
eprintln!("Skipping navigation generation for `{}`, must have a defined `@nav.title` and `@compiler.output`", doc_borrow.input);
eprintln!("Skipping navigation generation for `{}`, must have a defined `@nav.title` and `@compiler.output`", doc.input);
continue;
}
};
@ -164,7 +134,7 @@ pub fn create_navigation(
None => {
eprintln!(
"Skipping `{}`: No `@nav.category`, but `@nav.subcategory` is set",
doc_borrow.input
doc.input
);
continue;
}
@ -174,7 +144,7 @@ pub fn create_navigation(
Some(cat_ent) => cat_ent,
None => {
// Insert
nav.children.insert(cat.clone(), NavEntries::default());
nav.children.insert(cat.clone(), NavEntry::default());
nav.children.get_mut(cat.as_str()).unwrap()
}
};
@ -183,9 +153,7 @@ pub fn create_navigation(
Some(subcat_ent) => subcat_ent,
None => {
// Insert
cat_ent
.children
.insert(subcat.clone(), NavEntries::default());
cat_ent.children.insert(subcat.clone(), NavEntry::default());
cat_ent.children.get_mut(subcat.as_str()).unwrap()
}
}
@ -194,7 +162,7 @@ pub fn create_navigation(
Some(cat_ent) => cat_ent,
None => {
// Insert
nav.children.insert(cat.clone(), NavEntries::default());
nav.children.insert(cat.clone(), NavEntry::default());
nav.children.get_mut(cat.as_str()).unwrap()
}
}
@ -203,13 +171,13 @@ pub fn create_navigation(
};
// Find duplicates titles in current parent
for entry in &pent.entries {
if &entry.title == title {
for (ent_title, _, _) in &pent.entries {
if ent_title == title {
return Err(format!(
"Conflicting entry title `{title}` for entries with the same parent: ({})",
pent.entries
.iter()
.map(|entry| entry.title.clone())
.map(|(title, _, _)| title.clone())
.collect::<Vec<_>>()
.join(", ")
));
@ -222,22 +190,13 @@ pub fn create_navigation(
}
all_paths.insert(path.clone(), title.clone());
pent.entries.push(NavEntry {
title: title.clone(),
path: path.clone(),
previous,
});
pent.entries.push((title.clone(), path.clone(), previous));
}
// Sort entries
fn sort_entries(nav: &mut NavEntries) {
let entrymap = nav
.entries
.iter()
.map(|ent| (ent.title.clone(), ent.previous.clone()))
.collect::<HashMap<String, Option<String>>>();
fn sort_entries(nav: &mut NavEntry) {
nav.entries
.sort_by(|l, r| NavEntries::sort_entry(&entrymap, l.title.as_str(), r.title.as_str()));
.sort_unstable_by(|l, r| NavEntry::sort_entry(l, r));
for (_, child) in &mut nav.children {
sort_entries(child);
@ -250,110 +209,30 @@ pub fn create_navigation(
#[cfg(test)]
mod tests {
use rand::prelude::SliceRandom;
use rand::rngs::OsRng;
use crate::compiler::process::process_from_memory;
use rand::RngCore;
use super::*;
#[test]
fn sort() {
let entries: Vec<NavEntry> = vec![
NavEntry {
title: "Index".into(),
path: "".into(),
previous: None,
},
NavEntry {
title: "AB".into(),
path: "".into(),
previous: Some("Index".into()),
},
NavEntry {
title: "Getting Started".into(),
path: "".into(),
previous: Some("Index".into()),
},
NavEntry {
title: "Sections".into(),
path: "".into(),
previous: Some("Getting Started".into()),
},
NavEntry {
title: "Style".into(),
path: "".into(),
previous: Some("Getting Started".into()),
},
let entries: Vec<(String, String, Option<String>)> = vec![
("Index".into(), "".into(), None),
("AB".into(), "".into(), Some("Index".into())),
("Getting Started".into(), "".into(), Some("Index".into())),
("Sections".into(), "".into(), Some("Getting Started".into())),
("Style".into(), "".into(), Some("Getting Started".into())),
];
let mut shuffled = entries.clone();
for _ in 0..10 {
let mut rng = OsRng {};
shuffled.shuffle(&mut rng);
for i in 0..5 {
let pos = OsRng.next_u64() % entries.len() as u64;
shuffled.swap(i, pos as usize);
}
let entrymap = shuffled
.iter()
.map(|ent| (ent.title.clone(), ent.previous.clone()))
.collect::<HashMap<String, Option<String>>>();
shuffled.sort_by(|l, r| {
NavEntries::sort_entry(&entrymap, l.title.as_str(), r.title.as_str())
});
shuffled.sort_by(|l, r| NavEntry::sort_entry(l, r));
assert_eq!(shuffled, entries);
}
}
#[test]
pub fn batch() {
let result = process_from_memory(
Target::HTML,
vec![
r#"
@html.page_title = 0
@compiler.output = 0.html
@nav.title = C
@nav.category = First
"#
.into(),
r#"
@html.page_title = 1
@compiler.output = 1.html
@nav.title = A
@nav.category = First
"#
.into(),
r#"
@html.page_title = 2
@compiler.output = 2.html
@nav.title = B
@nav.category = First
"#
.into(),
],
)
.unwrap();
let nav = create_navigation(&result).unwrap();
assert_eq!(
nav.children.get("First").unwrap().entries,
vec![
NavEntry {
title: "A".to_string(),
path: "1.html".to_string(),
previous: None
},
NavEntry {
title: "B".to_string(),
path: "2.html".to_string(),
previous: None
},
NavEntry {
title: "C".to_string(),
path: "0.html".to_string(),
previous: None
},
]
);
}
}

View file

@ -1,81 +0,0 @@
use std::cell::RefCell;
use crate::document::document::CrossReference;
use super::compiler::CompiledDocument;
use super::compiler::Target;
/// Represents the list of tasks that have to run after the document has been compiled and the
/// compiled document list has been built. Every task is stored with a raw byte position in the
/// compiled document's body. The position represents the original position and thus should be
/// offset accordingly to other post-processing tasks.
pub struct PostProcess {
/// List of references to resolve i.e insert the resolved refname at a certain byte position
/// in the document's body
pub resolve_references: Vec<(usize, CrossReference)>,
}
impl PostProcess {
/// Applies postprocessing to a [`CompiledDocument`]
pub fn apply(
&self,
_target: Target,
list: &Vec<(RefCell<CompiledDocument>, Option<PostProcess>)>,
doc: &RefCell<CompiledDocument>,
) -> Result<String, String> {
let mut content = doc.borrow().body.clone();
let mut offset = 0;
for (pos, cross_ref) in &self.resolve_references {
// Cross-references
let mut found_ref: Option<(String, &RefCell<CompiledDocument>)> = None;
match cross_ref {
CrossReference::Unspecific(name) => {
for (doc, _) in list {
if let Some(found) = doc.borrow().references.get(name) {
// Check for duplicates
if let Some((_, previous_doc)) = &found_ref {
return Err(format!("Cannot use an unspecific reference for reference named: `{name}`. Found in document `{}` but also in `{}`. Specify the source of the reference to resolve the conflict.", previous_doc.borrow().input, doc.borrow().input));
}
found_ref = Some((found.clone(), doc));
}
}
}
CrossReference::Specific(doc_name, name) => {
let ref_doc = list.iter().find(|(doc, _)| {
let doc_borrow = doc.borrow();
if let Some(outname) = doc_borrow.variables.get("compiler.output") {
// Strip extension
let split_at = outname.rfind('.').unwrap_or(outname.len());
return doc_name == outname.split_at(split_at).0;
}
false
});
if ref_doc.is_none() {
return Err(format!(
"Cannot find document `{doc_name}` for reference `{name}` in `{}`",
doc.borrow().input
));
}
if let Some(found) = ref_doc.unwrap().0.borrow().references.get(name) {
found_ref = Some((found.clone(), &ref_doc.unwrap().0));
}
}
}
if let Some((found_ref, found_doc)) = &found_ref {
let found_borrow = found_doc.borrow();
let found_path = found_borrow.get_variable("compiler.output").ok_or("Unable to get the output. Aborting postprocessing.".to_string())?;
let insert_content = format!("{found_path}#{found_ref}");
content.insert_str(pos + offset, insert_content.as_str());
offset += insert_content.len();
} else {
return Err(format!("Cannot find reference `{cross_ref}` from document `{}`. Aborting postprocessing.", doc.borrow().input));
}
}
Ok(content)
}
}

View file

@ -1,196 +0,0 @@
use std::cell::RefCell;
use std::path::PathBuf;
use std::rc::Rc;
use std::time::UNIX_EPOCH;
use rusqlite::Connection;
use crate::document::document::Document;
use crate::parser::langparser::LangParser;
use crate::parser::parser::Parser;
use crate::parser::parser::ParserState;
use crate::parser::source::Source;
use crate::parser::source::SourceFile;
use super::compiler::CompiledDocument;
use super::compiler::Compiler;
use super::compiler::Target;
use super::postprocess::PostProcess;
/// Parses a source file into a document
fn parse(
parser: &LangParser,
source: Rc<dyn Source>,
debug_opts: &Vec<String>,
) -> Result<Box<dyn Document<'static>>, String> {
// Parse
//let source = SourceFile::new(input.to_string(), None).unwrap();
let (doc, _) = parser.parse(ParserState::new(parser, None), source.clone(), None);
if debug_opts.contains(&"ast".to_string()) {
println!("-- BEGIN AST DEBUGGING --");
doc.content()
.borrow()
.iter()
.for_each(|elem| println!("{elem:#?}"));
println!("-- END AST DEBUGGING --");
}
if debug_opts.contains(&"ref".to_string()) {
println!("-- BEGIN REFERENCES DEBUGGING --");
let sc = doc.scope().borrow();
sc.referenceable.iter().for_each(|(name, reference)| {
println!(" - {name}: `{:#?}`", doc.get_from_reference(reference));
});
println!("-- END REFERENCES DEBUGGING --");
}
if debug_opts.contains(&"var".to_string()) {
println!("-- BEGIN VARIABLES DEBUGGING --");
let sc = doc.scope().borrow();
sc.variables.iter().for_each(|(_name, var)| {
println!(" - `{:#?}`", var);
});
println!("-- END VARIABLES DEBUGGING --");
}
if parser.has_error() {
return Err("Parsing failed due to errors while parsing".to_string());
}
Ok(doc)
}
/// Takes a list of paths and processes it into a list of compiled documents
pub fn process(
target: Target,
files: Vec<PathBuf>,
db_path: &Option<String>,
force_rebuild: bool,
debug_opts: &Vec<String>,
) -> Result<Vec<(RefCell<CompiledDocument>, Option<PostProcess>)>, String> {
let mut compiled = vec![];
let current_dir = std::env::current_dir()
.map_err(|err| format!("Unable to get the current working directory: {err}"))?;
let con = db_path
.as_ref()
.map_or(Connection::open_in_memory(), Connection::open)
.map_err(|err| format!("Unable to open connection to the database: {err}"))?;
CompiledDocument::init_cache(&con)
.map_err(|err| format!("Failed to initialize cached document table: {err}"))?;
let parser = LangParser::default();
for file in files {
let meta = std::fs::metadata(&file)
.map_err(|err| format!("Failed to get metadata for `{file:#?}`: {err}"))?;
let modified = meta
.modified()
.map_err(|err| format!("Unable to query modification time for `{file:#?}`: {err}"))?;
// Move to file's directory
let file_parent_path = file
.parent()
.ok_or(format!("Failed to get parent path for `{file:#?}`"))?;
std::env::set_current_dir(file_parent_path)
.map_err(|err| format!("Failed to move to path `{file_parent_path:#?}`: {err}"))?;
let parse_and_compile = || -> Result<(CompiledDocument, Option<PostProcess>), String> {
// Parse
let source = SourceFile::new(file.to_str().unwrap().to_string(), None).unwrap();
println!("Parsing {}...", source.name());
let doc = parse(&parser, Rc::new(source), debug_opts)?;
// Compile
let compiler = Compiler::new(target, Some(&con));
let (mut compiled, postprocess) = compiler.compile(&*doc);
compiled.mtime = modified.duration_since(UNIX_EPOCH).unwrap().as_secs();
Ok((compiled, Some(postprocess)))
};
let (cdoc, post) = if force_rebuild {
parse_and_compile()?
} else {
match CompiledDocument::from_cache(&con, file.to_str().unwrap()) {
Some(compiled) => {
if compiled.mtime < modified.duration_since(UNIX_EPOCH).unwrap().as_secs() {
parse_and_compile()?
} else {
(compiled, None)
}
}
None => parse_and_compile()?,
}
};
compiled.push((RefCell::new(cdoc), post));
}
for (doc, postprocess) in &compiled {
if postprocess.is_none() {
continue;
}
// Post processing
let body = postprocess
.as_ref()
.unwrap()
.apply(target, &compiled, doc)?;
doc.borrow_mut().body = body;
// Insert into cache
doc.borrow().insert_cache(&con).map_err(|err| {
format!(
"Failed to insert compiled document from `{}` into cache: {err}",
doc.borrow().input
)
})?;
}
std::env::set_current_dir(current_dir)
.map_err(|err| format!("Failed to set current directory: {err}"))?;
Ok(compiled)
}
/// Processes sources from in-memory strings
/// This function is indented for testing
#[cfg(test)]
pub fn process_from_memory(target: Target, sources: Vec<String>) -> Result<Vec<(RefCell<CompiledDocument>, Option<PostProcess>)>, String> {
let mut compiled = vec![];
let parser = LangParser::default();
for (idx, content) in sources.iter().enumerate() {
let parse_and_compile = || -> Result<(CompiledDocument, Option<PostProcess>), String> {
// Parse
let source = SourceFile::with_content(format!("{idx}"), content.clone(), None);
let doc = parse(&parser, Rc::new(source), &vec![])?;
// Compile
let compiler = Compiler::new(target, None);
let (compiled, postprocess) = compiler.compile(&*doc);
Ok((compiled, Some(postprocess)))
};
let (cdoc, post) = parse_and_compile()?;
compiled.push((RefCell::new(cdoc), post));
}
for (doc, postprocess) in &compiled {
if postprocess.is_none() {
continue;
}
// Post processing
let body = postprocess
.as_ref()
.unwrap()
.apply(target, &compiled, doc)?;
doc.borrow_mut().body = body;
}
Ok(compiled)
}

View file

@ -4,17 +4,13 @@ use std::cell::RefMut;
use std::collections::hash_map::HashMap;
use std::rc::Rc;
use serde::Deserialize;
use serde::Serialize;
use crate::parser::source::Source;
use super::element::Element;
use super::element::ReferenceableElement;
use super::variable::Variable;
/// For references inside the current document
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[derive(Debug, Clone, Copy)]
pub enum ElemReference {
Direct(usize),
@ -22,30 +18,10 @@ pub enum ElemReference {
Nested(usize, usize),
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum CrossReference {
/// When the referenced document is unspecified
Unspecific(String),
/// When the referenced document is specified
Specific(String, String),
}
impl core::fmt::Display for CrossReference {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self
{
CrossReference::Unspecific(name) => write!(f, "#{name}"),
CrossReference::Specific(doc_name, name) => write!(f, "{doc_name}#{name}"),
}
}
}
#[derive(Debug)]
pub struct Scope {
/// List of all referenceable elements in current scope.
/// All elements in this should return a non empty element
/// when [`Element::as_referenceable`] is called
/// All elements in this should return a non empty
pub referenceable: HashMap<String, ElemReference>,
pub variables: HashMap<String, Rc<dyn Variable>>,
}
@ -75,7 +51,7 @@ impl Scope {
// Variables
self.variables
.extend(other.variables.drain());
.extend(other.variables.drain().map(|(name, var)| (name, var)));
}
false => {
// References
@ -165,15 +141,15 @@ pub trait Document<'a>: core::fmt::Debug {
fn get_variable(&self, name: &str) -> Option<Rc<dyn Variable>> {
match self.scope().borrow().variables.get(name) {
Some(variable) => {
Some(variable.clone())
return Some(variable.clone());
}
// Continue search recursively
None => match self.parent() {
Some(parent) => parent.get_variable(name),
Some(parent) => return parent.get_variable(name),
// Not found
None => None,
None => return None,
},
}
}
@ -189,23 +165,27 @@ pub trait Document<'a>: core::fmt::Debug {
scope: &RefCell<Scope>,
merge_as: Option<&String>,
) {
if let Some(merge_as) = merge_as { self.scope().borrow_mut().merge(
&mut scope.borrow_mut(),
merge_as,
self.content().borrow().len(),
) }
match merge_as {
Some(merge_as) => self.scope().borrow_mut().merge(
&mut *scope.borrow_mut(),
merge_as,
self.content().borrow().len(),
),
_ => {}
}
// Content
self.content()
.borrow_mut()
.extend((content.borrow_mut()).drain(..));
.extend((content.borrow_mut()).drain(..).map(|value| value));
}
fn get_reference(&self, refname: &str) -> Option<ElemReference> {
self.scope()
.borrow()
.referenceable
.get(refname).copied()
.get(refname)
.and_then(|reference| Some(*reference))
}
fn get_from_reference(

View file

@ -1,7 +1,7 @@
use std::str::FromStr;
use crate::compiler::compiler::Compiler;
use crate::elements::reference::InternalReference;
use crate::elements::reference::Reference;
use crate::parser::source::Token;
use downcast_rs::impl_downcast;
use downcast_rs::Downcast;
@ -50,7 +50,7 @@ pub trait Element: Downcast + core::fmt::Debug {
fn as_container(&self) -> Option<&dyn ContainerElement> { None }
/// Compiles element
fn compile(&self, compiler: &Compiler, document: &dyn Document, cursor: usize) -> Result<String, String>;
fn compile(&self, compiler: &Compiler, document: &dyn Document) -> Result<String, String>;
}
impl_downcast!(Element);
@ -66,13 +66,9 @@ pub trait ReferenceableElement: Element {
&self,
compiler: &Compiler,
document: &dyn Document,
reference: &InternalReference,
reference: &Reference,
refid: usize,
) -> Result<String, String>;
/// Gets the refid for a compiler. The refid is some key that can be used from an external
/// document to reference this element.
fn refid(&self, compiler: &Compiler, refid: usize) -> String;
}
pub trait ContainerElement: Element {
@ -93,7 +89,7 @@ impl Element for DocumentEnd {
fn element_name(&self) -> &'static str { "Document End" }
fn compile(&self, _compiler: &Compiler, _document: &dyn Document, _cursor: usize) -> Result<String, String> {
fn compile(&self, _compiler: &Compiler, _document: &dyn Document) -> Result<String, String> {
Ok(String::new())
}
}

View file

@ -20,8 +20,8 @@ pub struct LangDocument<'a> {
impl<'a> LangDocument<'a> {
pub fn new(source: Rc<dyn Source>, parent: Option<&'a dyn Document<'a>>) -> Self {
Self {
source,
parent,
source: source,
parent: parent,
content: RefCell::new(Vec::new()),
scope: RefCell::new(Scope::new()),
}
@ -32,7 +32,7 @@ impl<'a> Document<'a> for LangDocument<'a> {
fn source(&self) -> Rc<dyn Source> { self.source.clone() }
fn parent(&self) -> Option<&'a dyn Document<'a>> {
self.parent.map(|p| p as &dyn Document<'a>)
self.parent.and_then(|p| Some(p as &dyn Document<'a>))
}
fn content(&self) -> &RefCell<Vec<Box<dyn Element>>> { &self.content }

View file

@ -45,7 +45,7 @@ pub mod tests {
use crate::parser::langparser::LangParser;
use crate::parser::parser::Parser;
use crate::parser::source::SourceFile;
use crate::parser::parser::ParserState;
use crate::ParserState;
#[test]
fn validate_refname_tests() {

View file

@ -92,13 +92,13 @@ impl Variable for PathVariable {
fn name(&self) -> &str { self.name.as_str() }
fn from_string(&mut self, str: &str) -> Option<String> {
self.path = std::fs::canonicalize(str).unwrap();
self.path = PathBuf::from(std::fs::canonicalize(str).unwrap());
None
}
fn to_string(&self) -> String { self.path.to_str().unwrap().to_string() }
fn parse(&self, state: &ParserState, location: Token, document: &dyn Document) {
fn parse<'a>(&self, state: &ParserState, location: Token, document: &'a dyn Document) {
let source = Rc::new(VirtualSource::new(
location,
self.name().to_string(),

View file

@ -1,522 +0,0 @@
use core::fmt;
use std::any::Any;
use std::collections::HashMap;
use std::ops::Range;
use std::rc::Rc;
use ariadne::Label;
use ariadne::Report;
use ariadne::ReportKind;
use blockquote_style::AuthorPos::After;
use blockquote_style::AuthorPos::Before;
use blockquote_style::BlockquoteStyle;
use regex::Match;
use regex::Regex;
use runtime_format::FormatArgs;
use runtime_format::FormatError;
use runtime_format::FormatKey;
use runtime_format::FormatKeyError;
use crate::compiler::compiler::Compiler;
use crate::compiler::compiler::Target;
use crate::compiler::compiler::Target::HTML;
use crate::document::document::Document;
use crate::document::element::ContainerElement;
use crate::document::element::DocumentEnd;
use crate::document::element::ElemKind;
use crate::document::element::Element;
use crate::elements::paragraph::Paragraph;
use crate::elements::text::Text;
use crate::parser::parser::ParserState;
use crate::parser::rule::Rule;
use crate::parser::source::Cursor;
use crate::parser::source::Source;
use crate::parser::source::Token;
use crate::parser::source::VirtualSource;
use crate::parser::style::StyleHolder;
use crate::parser::util::process_escaped;
use crate::parser::util::Property;
use crate::parser::util::PropertyParser;
#[derive(Debug)]
pub struct Blockquote {
pub(self) location: Token,
pub(self) content: Vec<Box<dyn Element>>,
pub(self) author: Option<String>,
pub(self) cite: Option<String>,
pub(self) url: Option<String>,
/// Style of the blockquote
pub(self) style: Rc<blockquote_style::BlockquoteStyle>,
}
struct FmtPair<'a>(Target, &'a Blockquote);
impl FormatKey for FmtPair<'_> {
fn fmt(&self, key: &str, f: &mut fmt::Formatter<'_>) -> Result<(), FormatKeyError> {
match key {
"author" => write!(
f,
"{}",
Compiler::sanitize(self.0, self.1.author.as_ref().unwrap_or(&"".into()))
)
.map_err(FormatKeyError::Fmt),
"cite" => write!(
f,
"{}",
Compiler::sanitize(self.0, self.1.cite.as_ref().unwrap_or(&"".into()))
)
.map_err(FormatKeyError::Fmt),
_ => Err(FormatKeyError::UnknownKey),
}
}
}
impl Element for Blockquote {
fn location(&self) -> &Token { &self.location }
fn kind(&self) -> ElemKind { ElemKind::Block }
fn element_name(&self) -> &'static str { "Blockquote" }
fn compile(
&self,
compiler: &Compiler,
document: &dyn Document,
cursor: usize,
) -> Result<String, String> {
match compiler.target() {
HTML => {
let mut result = r#"<div class="blockquote-content">"#.to_string();
let format_author = || -> Result<String, String> {
let mut result = String::new();
if self.cite.is_some() || self.author.is_some() {
result += r#"<p class="blockquote-author">"#;
let fmt_pair = FmtPair(compiler.target(), self);
let format_string = match (self.author.is_some(), self.cite.is_some()) {
(true, true) => {
Compiler::sanitize_format(fmt_pair.0, self.style.format[0].as_str())
}
(true, false) => {
Compiler::sanitize_format(fmt_pair.0, self.style.format[1].as_str())
}
(false, false) => {
Compiler::sanitize_format(fmt_pair.0, self.style.format[2].as_str())
}
_ => panic!(""),
};
let args = FormatArgs::new(format_string.as_str(), &fmt_pair);
args.status().map_err(|err| {
format!("Failed to format Blockquote style `{format_string}`: {err}")
})?;
result += args.to_string().as_str();
result += "</p>";
}
Ok(result)
};
if let Some(url) = &self.url {
result += format!(r#"<blockquote cite="{}">"#, Compiler::sanitize(HTML, url))
.as_str();
} else {
result += "<blockquote>";
}
if self.style.author_pos == Before {
result += format_author()?.as_str();
}
let mut in_paragraph = false;
for elem in &self.content {
if elem.downcast_ref::<DocumentEnd>().is_some() {
} else if elem.downcast_ref::<Blockquote>().is_some() {
if in_paragraph {
result += "</p>";
in_paragraph = false;
}
result += elem
.compile(compiler, document, cursor + result.len())?
.as_str();
} else {
if !in_paragraph {
result += "<p>";
in_paragraph = true;
}
result += elem
.compile(compiler, document, cursor + result.len())?
.as_str();
}
}
if in_paragraph {
result += "</p>";
}
result += "</blockquote>";
if self.style.author_pos == After {
result += format_author().map_err(|err| err.to_string())?.as_str();
}
result += "</div>";
Ok(result)
}
_ => todo!(""),
}
}
fn as_container(&self) -> Option<&dyn ContainerElement> { Some(self) }
}
impl ContainerElement for Blockquote {
fn contained(&self) -> &Vec<Box<dyn Element>> { &self.content }
fn push(&mut self, elem: Box<dyn Element>) -> Result<(), String> {
if elem.kind() == ElemKind::Block {
return Err("Cannot add block element inside a blockquote".to_string());
}
self.content.push(elem);
Ok(())
}
}
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::blockquote")]
pub struct BlockquoteRule {
start_re: Regex,
continue_re: Regex,
properties: PropertyParser,
}
impl BlockquoteRule {
pub fn new() -> Self {
let mut props = HashMap::new();
props.insert(
"author".to_string(),
Property::new(false, "Quote author".to_string(), None),
);
props.insert(
"cite".to_string(),
Property::new(false, "Quote source".to_string(), None),
);
props.insert(
"url".to_string(),
Property::new(false, "Quote source url".to_string(), None),
);
Self {
start_re: Regex::new(r"(?:^|\n)>(?:\[((?:\\.|[^\\\\])*?)\])?\s*?(.*)").unwrap(),
continue_re: Regex::new(r"(?:^|\n)>\s*?(.*)").unwrap(),
properties: PropertyParser { properties: props },
}
}
fn parse_properties(
&self,
m: Match,
) -> Result<(Option<String>, Option<String>, Option<String>), String> {
let processed = process_escaped('\\', "]", m.as_str());
let pm = self.properties.parse(processed.as_str())?;
let author = pm
.get("author", |_, s| -> Result<String, ()> { Ok(s.to_string()) })
.map(|(_, s)| s)
.ok();
let cite = pm
.get("cite", |_, s| -> Result<String, ()> { Ok(s.to_string()) })
.map(|(_, s)| s)
.ok();
let url = pm
.get("url", |_, s| -> Result<String, ()> { Ok(s.to_string()) })
.map(|(_, s)| s)
.ok();
Ok((author, cite, url))
}
}
impl Rule for BlockquoteRule {
fn name(&self) -> &'static str { "Blockquote" }
fn previous(&self) -> Option<&'static str> { Some("List") }
fn next_match(&self, _state: &ParserState, cursor: &Cursor) -> Option<(usize, Box<dyn Any>)> {
self.start_re
.find_at(cursor.source.content(), cursor.pos)
.map(|m| (m.start(), Box::new([false; 0]) as Box<dyn Any>))
}
fn on_match<'a>(
&self,
state: &ParserState,
document: &'a (dyn Document<'a> + 'a),
cursor: Cursor,
_match_data: Box<dyn Any>,
) -> (Cursor, Vec<Report<'_, (Rc<dyn Source>, Range<usize>)>>) {
let mut reports = vec![];
let content = cursor.source.content();
let mut end_cursor = cursor.clone();
if let Some(captures) = self.start_re.captures_at(content, end_cursor.pos) {
if captures.get(0).unwrap().start() != end_cursor.pos {
return (end_cursor, reports);
}
// Advance cursor
end_cursor = end_cursor.at(captures.get(0).unwrap().end());
// Properties
let mut author = None;
let mut cite = None;
let mut url = None;
if let Some(properties) = captures.get(1) {
match self.parse_properties(properties) {
Err(err) => {
reports.push(
Report::build(
ReportKind::Warning,
cursor.source.clone(),
properties.start(),
)
.with_message("Invalid Blockquote Properties")
.with_label(
Label::new((cursor.source.clone(), properties.range()))
.with_message(err)
.with_color(state.parser.colors().warning),
)
.finish(),
);
return (end_cursor, reports);
}
Ok(props) => (author, cite, url) = props,
}
}
// Content
let entry_start = captures.get(0).unwrap().start();
let mut entry_content = captures.get(2).unwrap().as_str().to_string();
while let Some(captures) = self.continue_re.captures_at(content, end_cursor.pos) {
if captures.get(0).unwrap().start() != end_cursor.pos {
break;
}
// Advance cursor
end_cursor = end_cursor.at(captures.get(0).unwrap().end());
let trimmed = captures.get(1).unwrap().as_str().trim_start().trim_end();
entry_content += "\n";
entry_content += trimmed;
}
// Parse entry content
let token = Token::new(entry_start..end_cursor.pos, end_cursor.source.clone());
let entry_src = Rc::new(VirtualSource::new(
token.clone(),
"Blockquote Entry".to_string(),
entry_content,
));
// Parse content
let parsed_doc = state.with_state(|new_state| {
new_state
.parser
.parse(new_state, entry_src, Some(document))
.0
});
// Extract paragraph and nested blockquotes
let mut parsed_content: Vec<Box<dyn Element>> = vec![];
for mut elem in parsed_doc.content().borrow_mut().drain(..) {
if let Some(paragraph) = elem.downcast_mut::<Paragraph>() {
if let Some(last) = parsed_content.last() {
if last.kind() == ElemKind::Inline {
parsed_content.push(Box::new(Text {
location: Token::new(
last.location().end()..last.location().end(),
last.location().source(),
),
content: " ".to_string(),
}) as Box<dyn Element>);
}
}
parsed_content.extend(std::mem::take(&mut paragraph.content));
} else if elem.downcast_ref::<Blockquote>().is_some() {
parsed_content.push(elem);
} else {
reports.push(
Report::build(ReportKind::Error, token.source(), token.range.start)
.with_message("Unable to Parse Blockquote Entry")
.with_label(
Label::new((token.source(), token.range.clone()))
.with_message("Blockquotes may only contain paragraphs and other blockquotes")
.with_color(state.parser.colors().error),
)
.finish(),
);
return (end_cursor, reports);
}
}
// Get style
let style = state
.shared
.styles
.borrow()
.current(blockquote_style::STYLE_KEY)
.downcast_rc::<BlockquoteStyle>()
.unwrap();
state.push(
document,
Box::new(Blockquote {
location: Token::new(entry_start..end_cursor.pos, end_cursor.source.clone()),
content: parsed_content,
author,
cite,
url,
style,
}),
);
}
(end_cursor, reports)
}
fn register_styles(&self, holder: &mut StyleHolder) {
holder.set_current(Rc::new(BlockquoteStyle::default()));
}
}
mod blockquote_style {
use serde::Deserialize;
use serde::Serialize;
use crate::impl_elementstyle;
pub static STYLE_KEY: &str = "style.blockquote";
#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)]
pub enum AuthorPos {
Before,
After,
None,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct BlockquoteStyle {
pub author_pos: AuthorPos,
pub format: [String; 3],
}
impl Default for BlockquoteStyle {
fn default() -> Self {
Self {
author_pos: AuthorPos::After,
format: [
"{author}, {cite}".into(),
"{author}".into(),
"{cite}".into(),
],
}
}
}
impl_elementstyle!(BlockquoteStyle, STYLE_KEY);
}
#[cfg(test)]
mod tests {
use crate::elements::paragraph::Paragraph;
use crate::elements::style::Style;
use crate::elements::text::Text;
use crate::parser::langparser::LangParser;
use crate::parser::parser::Parser;
use crate::parser::source::SourceFile;
use crate::validate_document;
use super::*;
#[test]
pub fn parser() {
let source = Rc::new(SourceFile::with_content(
"".to_string(),
r#"
BEFORE
>[author=A, cite=B, url=C] Some entry
> contin**ued here
> **
AFTER
> Another
>
> quote
>>[author=B] Nested
>>> More nested
END
"#
.to_string(),
None,
));
let parser = LangParser::default();
let (doc, _) = parser.parse(ParserState::new(&parser, None), source, None);
validate_document!(doc.content().borrow(), 0,
Paragraph { Text{ content == "BEFORE" }; };
Blockquote {
author == Some("A".to_string()),
cite == Some("B".to_string()),
url == Some("C".to_string())
} {
Text { content == "Some entry contin" };
Style;
Text { content == "ued here" };
Style;
};
Paragraph { Text{ content == "AFTER" }; };
Blockquote {
Text { content == "Another" };
Text { content == " " };
Text { content == "quote" };
Blockquote { author == Some("B".to_string()) } {
Text { content == "Nested" };
Blockquote {
Text { content == "More nested" };
};
};
};
Paragraph { Text{ content == "END" }; };
);
}
#[test]
pub fn style() {
let source = Rc::new(SourceFile::with_content(
"".to_string(),
r#"
@@style.blockquote = {
"author_pos": "Before",
"format": ["{cite} by {author}", "Author: {author}", "From: {cite}"]
}
PRE
>[author=A, cite=B, url=C] Some entry
> contin**ued here
> **
AFTER
"#
.to_string(),
None,
));
let parser = LangParser::default();
let (_, state) = parser.parse(ParserState::new(&parser, None), source, None);
let style = state
.shared
.styles
.borrow()
.current(blockquote_style::STYLE_KEY)
.downcast_rc::<BlockquoteStyle>()
.unwrap();
assert_eq!(style.author_pos, Before);
assert_eq!(
style.format,
[
"{cite} by {author}".to_string(),
"Author: {author}".to_string(),
"From: {cite}".to_string()
]
);
}
}

View file

@ -123,8 +123,8 @@ impl Code {
}
result +=
"<div class=\"code-block-content\"><table cellspacing=\"0\">".to_string().as_str();
for (line_id, line) in self.code.split('\n').enumerate() {
format!("<div class=\"code-block-content\"><table cellspacing=\"0\">").as_str();
for (line_id, line) in self.code.split(|c| c == '\n').enumerate() {
result += "<tr><td class=\"code-block-gutter\">";
// Line number
@ -137,7 +137,7 @@ impl Code {
Err(e) => {
return Err(format!(
"Error highlighting line `{line}`: {}",
e
e.to_string()
))
}
Ok(regions) => {
@ -146,7 +146,7 @@ impl Code {
syntect::html::IncludeBackground::No,
) {
Err(e) => {
return Err(format!("Error highlighting code: {}", e))
return Err(format!("Error highlighting code: {}", e.to_string()))
}
Ok(highlighted) => {
result += if highlighted.is_empty() {
@ -165,14 +165,14 @@ impl Code {
} else if self.block == CodeKind::MiniBlock {
result += "<div class=\"code-block\"><div class=\"code-block-content\"><table cellspacing=\"0\">";
for line in self.code.split('\n') {
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()) {
Err(e) => {
return Err(format!(
"Error highlighting line `{line}`: {}",
e
e.to_string()
))
}
Ok(regions) => {
@ -181,7 +181,7 @@ impl Code {
syntect::html::IncludeBackground::No,
) {
Err(e) => {
return Err(format!("Error highlighting code: {}", e))
return Err(format!("Error highlighting code: {}", e.to_string()))
}
Ok(highlighted) => {
result += if highlighted.is_empty() {
@ -203,7 +203,7 @@ impl Code {
return Err(format!(
"Error highlighting line `{}`: {}",
self.code,
e
e.to_string()
))
}
Ok(regions) => {
@ -212,7 +212,7 @@ impl Code {
syntect::html::IncludeBackground::No,
) {
Err(e) => {
return Err(format!("Error highlighting code: {}", e))
return Err(format!("Error highlighting code: {}", e.to_string()))
}
Ok(highlighted) => result += highlighted.as_str(),
}
@ -244,10 +244,11 @@ impl Cached for Code {
fn key(&self) -> <Self as Cached>::Key {
let mut hasher = Sha512::new();
hasher.input((self.block as usize).to_be_bytes().as_slice());
hasher.input(self.line_offset.to_be_bytes().as_slice());
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()) }
hasher.input((self.line_offset as usize).to_be_bytes().as_slice());
self.theme
.as_ref()
.map(|theme| hasher.input(theme.as_bytes()));
self.name.as_ref().map(|name| hasher.input(name.as_bytes()));
hasher.input(self.language.as_bytes());
hasher.input(self.code.as_bytes());
@ -262,20 +263,20 @@ impl Element for Code {
fn element_name(&self) -> &'static str { "Code Block" }
fn compile(&self, compiler: &Compiler, _document: &dyn Document, _cursor: usize) -> Result<String, String> {
fn compile(&self, compiler: &Compiler, _document: &dyn Document) -> Result<String, String> {
match compiler.target() {
Target::HTML => {
static CACHE_INIT: Once = Once::new();
CACHE_INIT.call_once(|| {
if let Some(con) = compiler.cache() {
if let Err(e) = Code::init(con) {
if let Some(mut con) = compiler.cache() {
if let Err(e) = Code::init(&mut con) {
eprintln!("Unable to create cache table: {e}");
}
}
});
if let Some(con) = compiler.cache() {
match self.cached(con, |s| s.highlight_html(compiler)) {
if let Some(mut con) = compiler.cache() {
match self.cached(&mut con, |s| s.highlight_html(compiler)) {
Ok(s) => Ok(s),
Err(e) => match e {
CachedError::SqlErr(e) => {
@ -295,7 +296,6 @@ impl Element for Code {
}
}
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::code")]
pub struct CodeRule {
re: [Regex; 2],
properties: PropertyParser,
@ -330,15 +330,14 @@ impl CodeRule {
impl RegexRule for CodeRule {
fn name(&self) -> &'static str { "Code" }
fn previous(&self) -> Option<&'static str> { Some("Blockquote") }
fn regexes(&self) -> &[regex::Regex] { &self.re }
fn on_regex_match(
fn on_regex_match<'a>(
&self,
index: usize,
state: &ParserState,
document: &dyn Document,
document: &'a dyn Document,
token: Token,
matches: Captures,
) -> Vec<Report<'_, (Rc<dyn Source>, Range<usize>)>> {
@ -431,7 +430,7 @@ impl RegexRule for CodeRule {
} else {
util::process_escaped('\\', "``", matches.get(3).unwrap().as_str())
};
if code_content.bytes().last() == Some(b'\n')
if code_content.bytes().last() == Some('\n' as u8)
// Remove newline
{
code_content.pop();
@ -452,7 +451,8 @@ impl RegexRule for CodeRule {
}
let theme = document
.get_variable("code.theme").map(|var| var.to_string());
.get_variable("code.theme")
.and_then(|var| Some(var.to_string()));
if index == 0
// Block
@ -551,7 +551,8 @@ impl RegexRule for CodeRule {
ctx.as_ref().map(|ctx| {
let theme = ctx
.document
.get_variable("code.theme").map(|var| var.to_string());
.get_variable("code.theme")
.and_then(|var| Some(var.to_string()));
ctx.state.push(
ctx.document,
@ -581,7 +582,8 @@ impl RegexRule for CodeRule {
ctx.as_ref().map(|ctx| {
let theme = ctx
.document
.get_variable("code.theme").map(|var| var.to_string());
.get_variable("code.theme")
.and_then(|var| Some(var.to_string()));
ctx.state.push(
ctx.document,
@ -618,7 +620,8 @@ impl RegexRule for CodeRule {
ctx.as_ref().map(|ctx| {
let theme = ctx
.document
.get_variable("code.theme").map(|var| var.to_string());
.get_variable("code.theme")
.and_then(|var| Some(var.to_string()));
ctx.state.push(
ctx.document,

View file

@ -16,21 +16,28 @@ use std::rc::Rc;
#[derive(Debug)]
pub struct Comment {
pub location: Token,
#[allow(unused)]
pub content: String,
location: Token,
content: String,
}
impl Comment {
pub fn new(location: Token, content: String) -> Self {
Self {
location: location,
content,
}
}
}
impl Element for Comment {
fn location(&self) -> &Token { &self.location }
fn kind(&self) -> ElemKind { ElemKind::Invisible }
fn element_name(&self) -> &'static str { "Comment" }
fn compile(&self, _compiler: &Compiler, _document: &dyn Document, _cursor: usize) -> Result<String, String> {
fn compile(&self, _compiler: &Compiler, _document: &dyn Document) -> Result<String, String> {
Ok("".to_string())
}
}
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::comment")]
pub struct CommentRule {
re: [Regex; 1],
}
@ -46,15 +53,13 @@ impl CommentRule {
impl RegexRule for CommentRule {
fn name(&self) -> &'static str { "Comment" }
fn previous(&self) -> Option<&'static str> { None }
fn regexes(&self) -> &[Regex] { &self.re }
fn on_regex_match(
fn on_regex_match<'a>(
&self,
_: usize,
state: &ParserState,
document: &dyn Document,
document: &'a dyn Document,
token: Token,
matches: Captures,
) -> Vec<Report<'_, (Rc<dyn Source>, Range<usize>)>> {
@ -89,7 +94,7 @@ impl RegexRule for CommentRule {
}),
);
reports
return reports;
}
}

View file

@ -140,10 +140,13 @@ impl RuleState for CustomStyleState {
let paragraph = document.last_element::<Paragraph>().unwrap();
let paragraph_end = paragraph
.content
.last().map(|last| (
.last()
.and_then(|last| {
Some((
last.location().source(),
last.location().end() - 1..last.location().end(),
))
})
.unwrap();
reports.push(
@ -161,7 +164,7 @@ impl RuleState for CustomStyleState {
.with_label(
Label::new(paragraph_end)
.with_order(1)
.with_message("Paragraph ends here".to_string())
.with_message(format!("Paragraph ends here"))
.with_color(state.parser.colors().error),
)
.with_note("Styles cannot span multiple documents (i.e @import)")
@ -169,22 +172,16 @@ impl RuleState for CustomStyleState {
);
});
reports
return reports;
}
}
static STATE_NAME: &str = "elements.custom_style";
static STATE_NAME: &'static str = "elements.custom_style";
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::customstyle")]
pub struct CustomStyleRule;
impl CustomStyleRule {
pub fn new() -> Self { Self{} }
}
impl Rule for CustomStyleRule {
fn name(&self) -> &'static str { "Custom Style" }
fn previous(&self) -> Option<&'static str> { Some("Style") }
fn next_match(&self, state: &ParserState, cursor: &Cursor) -> Option<(usize, Box<dyn Any>)> {
let content = cursor.source.content();

View file

@ -21,7 +21,6 @@ use crate::parser::rule::Rule;
use crate::parser::source::Cursor;
use crate::parser::source::Source;
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::elemstyle")]
pub struct ElemStyleRule {
start_re: Regex,
}
@ -59,11 +58,13 @@ impl ElemStyleRule {
impl Rule for ElemStyleRule {
fn name(&self) -> &'static str { "Element Style" }
fn previous(&self) -> Option<&'static str> { Some("Script") }
fn next_match(&self, _state: &ParserState, cursor: &Cursor) -> Option<(usize, Box<dyn Any>)> {
self.start_re
.find_at(cursor.source.content(), cursor.pos).map(|m| (m.start(), Box::new([false; 0]) as Box<dyn Any>))
.find_at(cursor.source.content(), cursor.pos)
.map_or(None, |m| {
Some((m.start(), Box::new([false; 0]) as Box<dyn Any>))
})
}
fn on_match<'a>(
@ -90,7 +91,7 @@ impl Rule for ElemStyleRule {
.with_message("Empty Style Key")
.with_label(
Label::new((cursor.source.clone(), key.range()))
.with_message("Expected a non-empty style key".to_string())
.with_message(format!("Expected a non-empty style key",))
.with_color(state.parser.colors().error),
)
.finish(),
@ -132,7 +133,9 @@ impl Rule for ElemStyleRule {
.with_message("Invalid Style Value")
.with_label(
Label::new((cursor.source.clone(), matches.get(0).unwrap().range()))
.with_message("Unable to parse json string after style key".to_string())
.with_message(format!(
"Unable to parse json string after style key",
))
.with_color(state.parser.colors().error),
)
.finish(),

View file

@ -1,10 +1,8 @@
use std::collections::HashMap;
use std::ops::Range;
use std::rc::Rc;
use std::sync::Arc;
use std::sync::Once;
use crate::lua::kernel::CTX;
use crate::parser::parser::ParserState;
use crate::parser::util::Property;
use crate::parser::util::PropertyMapError;
@ -18,9 +16,6 @@ use crypto::sha2::Sha512;
use graphviz_rust::cmd::Format;
use graphviz_rust::cmd::Layout;
use graphviz_rust::exec_dot;
use mlua::Error::BadArgument;
use mlua::Function;
use mlua::Lua;
use regex::Captures;
use regex::Regex;
@ -116,26 +111,21 @@ impl Element for Graphviz {
fn element_name(&self) -> &'static str { "Graphviz" }
fn compile(
&self,
compiler: &Compiler,
_document: &dyn Document,
_cursor: usize,
) -> Result<String, String> {
fn compile(&self, compiler: &Compiler, _document: &dyn Document) -> Result<String, String> {
match compiler.target() {
Target::HTML => {
static CACHE_INIT: Once = Once::new();
CACHE_INIT.call_once(|| {
if let Some(con) = compiler.cache() {
if let Err(e) = Graphviz::init(con) {
if let Some(mut con) = compiler.cache() {
if let Err(e) = Graphviz::init(&mut con) {
eprintln!("Unable to create cache table: {e}");
}
}
});
// TODO: Format svg in a div
if let Some(con) = compiler.cache() {
match self.cached(con, |s| s.dot_to_svg()) {
if let Some(mut con) = compiler.cache() {
match self.cached(&mut con, |s| s.dot_to_svg()) {
Ok(s) => Ok(s),
Err(e) => match e {
CachedError::SqlErr(e) => {
@ -156,7 +146,6 @@ impl Element for Graphviz {
}
}
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::graphviz")]
pub struct GraphRule {
re: [Regex; 1],
properties: PropertyParser,
@ -188,8 +177,7 @@ impl GraphRule {
}
impl RegexRule for GraphRule {
fn name(&self) -> &'static str { "Graphviz" }
fn previous(&self) -> Option<&'static str> { Some("Tex") }
fn name(&self) -> &'static str { "Graph" }
fn regexes(&self) -> &[regex::Regex] { &self.re }
@ -370,102 +358,4 @@ impl RegexRule for GraphRule {
reports
}
fn register_bindings<'lua>(&self, lua: &'lua Lua) -> Vec<(String, Function<'lua>)> {
let mut bindings = vec![];
bindings.push((
"push".to_string(),
lua.create_function(|_, (layout, width, dot): (String, String, String)| {
let mut result = Ok(());
CTX.with_borrow(|ctx| {
ctx.as_ref().map(|ctx| {
let layout = match layout_from_str(layout.as_str()) {
Err(err) => {
result = Err(BadArgument {
to: Some("push".to_string()),
pos: 1,
name: Some("layout".to_string()),
cause: Arc::new(mlua::Error::external(format!(
"Unable to get layout type: {err}"
))),
});
return;
}
Ok(layout) => layout,
};
ctx.state.push(
ctx.document,
Box::new(Graphviz {
location: ctx.location.clone(),
dot,
layout,
width,
}),
);
})
});
result
})
.unwrap(),
));
bindings
}
}
#[cfg(test)]
mod tests {
use crate::parser::langparser::LangParser;
use crate::parser::parser::Parser;
use crate::parser::source::SourceFile;
use crate::validate_document;
use super::*;
#[test]
pub fn parse() {
let source = Rc::new(SourceFile::with_content(
"".to_string(),
r#"
[graph][width=200px, layout=neato]
Some graph...
[/graph]
[graph]
Another graph
[/graph]
"#
.to_string(),
None,
));
let parser = LangParser::default();
let (doc, _) = parser.parse(ParserState::new(&parser, None), source, None);
validate_document!(doc.content().borrow(), 0,
Graphviz { width == "200px", dot == "Some graph..." };
Graphviz { dot == "Another graph" };
);
}
#[test]
pub fn lua() {
let source = Rc::new(SourceFile::with_content(
"".to_string(),
r#"
%<nml.graphviz.push("neato", "200px", "Some graph...")>%
%<nml.graphviz.push("dot", "", "Another graph")>%
"#
.to_string(),
None,
));
let parser = LangParser::default();
let (doc, _) = parser.parse(ParserState::new(&parser, None), source, None);
validate_document!(doc.content().borrow(), 0,
Graphviz { width == "200px", dot == "Some graph..." };
Graphviz { dot == "Another graph" };
);
}
}

View file

@ -17,7 +17,6 @@ use std::rc::Rc;
use super::paragraph::Paragraph;
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::import")]
pub struct ImportRule {
re: [Regex; 1],
}
@ -41,7 +40,6 @@ impl ImportRule {
impl RegexRule for ImportRule {
fn name(&self) -> &'static str { "Import" }
fn previous(&self) -> Option<&'static str> { Some("Paragraph") }
fn regexes(&self) -> &[Regex] { &self.re }
@ -179,6 +177,6 @@ impl RegexRule for ImportRule {
);
}
result
return result;
}
}

View file

@ -232,7 +232,7 @@ impl Element for Layout {
fn location(&self) -> &Token { &self.location }
fn kind(&self) -> ElemKind { ElemKind::Block }
fn element_name(&self) -> &'static str { "Layout" }
fn compile(&self, compiler: &Compiler, document: &dyn Document, _cursor: usize) -> Result<String, String> {
fn compile(&self, compiler: &Compiler, document: &dyn Document) -> Result<String, String> {
self.layout
.compile(self.token, self.id, &self.properties, compiler, document)
}
@ -280,11 +280,10 @@ impl RuleState for LayoutState {
);
}
reports
return reports;
}
}
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::layout")]
pub struct LayoutRule {
re: [Regex; 3],
}
@ -375,11 +374,10 @@ impl LayoutRule {
}
}
static STATE_NAME: &str = "elements.layout";
static STATE_NAME: &'static str = "elements.layout";
impl RegexRule for LayoutRule {
fn name(&self) -> &'static str { "Layout" }
fn previous(&self) -> Option<&'static str> { Some("Media") }
fn regexes(&self) -> &[regex::Regex] { &self.re }
@ -649,7 +647,7 @@ impl RegexRule for LayoutRule {
}),
);
reports
return reports;
}
// TODO: Add method to create new layouts
@ -746,7 +744,7 @@ impl RegexRule for LayoutRule {
to: Some("push".to_string()),
pos: 1,
name: Some("token".to_string()),
cause: Arc::new(mlua::Error::external("Unable set next layout: No active layout found".to_string())),
cause: Arc::new(mlua::Error::external(format!("Unable set next layout: No active layout found"))),
});
return;
}
@ -794,7 +792,7 @@ impl RegexRule for LayoutRule {
to: Some("push".to_string()),
pos: 1,
name: Some("token".to_string()),
cause: Arc::new(mlua::Error::external("Unable set layout end: No active layout found".to_string())),
cause: Arc::new(mlua::Error::external(format!("Unable set layout end: No active layout found"))),
});
return;
}

View file

@ -37,7 +37,7 @@ impl Element for Link {
fn location(&self) -> &Token { &self.location }
fn kind(&self) -> ElemKind { ElemKind::Inline }
fn element_name(&self) -> &'static str { "Link" }
fn compile(&self, compiler: &Compiler, document: &dyn Document, cursor: usize) -> Result<String, String> {
fn compile(&self, compiler: &Compiler, document: &dyn Document) -> Result<String, String> {
match compiler.target() {
Target::HTML => {
let mut result = format!(
@ -46,7 +46,7 @@ impl Element for Link {
);
for elem in &self.display {
result += elem.compile(compiler, document, cursor+result.len())?.as_str();
result += elem.compile(compiler, document)?.as_str();
}
result += "</a>";
@ -71,7 +71,6 @@ impl ContainerElement for Link {
}
}
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::link")]
pub struct LinkRule {
re: [Regex; 1],
}
@ -86,7 +85,6 @@ impl LinkRule {
impl RegexRule for LinkRule {
fn name(&self) -> &'static str { "Link" }
fn previous(&self) -> Option<&'static str> { Some("Link") }
fn regexes(&self) -> &[Regex] { &self.re }
@ -153,7 +151,7 @@ impl RegexRule for LinkRule {
);
return reports;
}
Ok(mut paragraph) => std::mem::take(&mut paragraph.content),
Ok(mut paragraph) => std::mem::replace(&mut paragraph.content, vec![]),
}
}
_ => panic!("Empty link name"),
@ -176,7 +174,7 @@ impl RegexRule for LinkRule {
}
let text_content = util::process_text(document, url.as_str());
if text_content.is_empty() {
if text_content.as_str().is_empty() {
reports.push(
Report::build(ReportKind::Error, token.source(), url.start())
.with_message("Empty link url")
@ -207,7 +205,7 @@ impl RegexRule for LinkRule {
}),
);
reports
return reports;
}
fn register_bindings<'lua>(&self, lua: &'lua Lua) -> Vec<(String, Function<'lua>)> {
@ -238,7 +236,7 @@ impl RegexRule for LinkRule {
return;
}
Ok(mut paragraph) => {
std::mem::take(&mut paragraph.content)
std::mem::replace(&mut paragraph.content, vec![])
}
};

View file

@ -48,7 +48,7 @@ impl Element for ListMarker {
fn element_name(&self) -> &'static str { "List Marker" }
fn compile(&self, compiler: &Compiler, _document: &dyn Document, _cursor: usize) -> Result<String, String> {
fn compile(&self, compiler: &Compiler, _document: &dyn Document) -> Result<String, String> {
match compiler.target() {
Target::HTML => match (self.kind, self.numbered) {
(MarkerKind::Close, true) => Ok("</ol>".to_string()),
@ -76,21 +76,12 @@ impl Element for ListEntry {
fn element_name(&self) -> &'static str { "List Entry" }
fn compile(&self, compiler: &Compiler, document: &dyn Document, cursor: usize) -> Result<String, String> {
fn compile(&self, compiler: &Compiler, document: &dyn Document) -> Result<String, String> {
match compiler.target() {
Target::HTML => {
let mut result = String::new();
if let Some((numbered, number)) = self.numbering.last()
{
if *numbered {
result += format!("<li value=\"{number}\">").as_str();
}
else {
result += "<li>";
}
}
let mut result = "<li>".to_string();
for elem in &self.content {
result += elem.compile(compiler, document, cursor+result.len())?.as_str();
result += elem.compile(compiler, document)?.as_str();
}
result += "</li>";
Ok(result)
@ -115,7 +106,6 @@ impl ContainerElement for ListEntry {
}
}
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::list")]
pub struct ListRule {
start_re: Regex,
continue_re: Regex,
@ -207,13 +197,14 @@ impl ListRule {
fn parse_depth(depth: &str, document: &dyn Document, offset: usize) -> Vec<(bool, usize)> {
let mut parsed = vec![];
// FIXME: Previous iteration used to recursively retrieve the list indent
let prev_entry = document
.last_element::<ListEntry>()
.and_then(|entry| Ref::filter_map(entry, |e| Some(&e.numbering)).ok());
let mut continue_match = true;
depth.chars().enumerate().for_each(|(idx, c)| {
let number = if offset == usize::MAX {
let number = if offset == 0 {
prev_entry
.as_ref()
.and_then(|v| {
@ -252,17 +243,19 @@ impl ListRule {
}
});
parsed
return parsed;
}
}
impl Rule for ListRule {
fn name(&self) -> &'static str { "List" }
fn previous(&self) -> Option<&'static str> { Some("Raw") }
fn next_match(&self, _state: &ParserState, cursor: &Cursor) -> Option<(usize, Box<dyn Any>)> {
self.start_re
.find_at(cursor.source.content(), cursor.pos).map(|m| (m.start(), Box::new([false; 0]) as Box<dyn Any>))
.find_at(cursor.source.content(), cursor.pos)
.map_or(None, |m| {
Some((m.start(), Box::new([false; 0]) as Box<dyn Any>))
})
}
fn on_match<'a>(
@ -320,7 +313,7 @@ impl Rule for ListRule {
let depth = ListRule::parse_depth(
captures.get(1).unwrap().as_str(),
document,
offset.unwrap_or(usize::MAX),
offset.unwrap_or(0),
);
// Content
@ -334,7 +327,7 @@ impl Rule for ListRule {
.get(2)
.unwrap()
.as_str()
.find(['*', '-'])
.find(|c| c == '*' || c == '-')
== Some(0)
{
break;
@ -398,7 +391,7 @@ impl Rule for ListRule {
);
break;
}
Ok(mut paragraph) => std::mem::take(&mut paragraph.content),
Ok(mut paragraph) => std::mem::replace(&mut paragraph.content, vec![]),
};
if let Some(previous_depth) = document

View file

@ -35,7 +35,7 @@ use crate::parser::util::PropertyMapError;
use crate::parser::util::PropertyParser;
use super::paragraph::Paragraph;
use super::reference::InternalReference;
use super::reference::Reference;
#[derive(Debug, PartialEq, Eq)]
pub enum MediaType {
@ -72,14 +72,17 @@ impl Element for Media {
fn as_container(&self) -> Option<&dyn ContainerElement> { Some(self) }
fn compile(&self, compiler: &Compiler, document: &dyn Document, cursor: usize) -> Result<String, String> {
fn compile(&self, compiler: &Compiler, document: &dyn Document) -> Result<String, String> {
match compiler.target() {
Target::HTML => {
let mut result = String::new();
result.push_str("<div class=\"media\">");
for medium in &self.media {
result += medium.compile(compiler, document, cursor+result.len())?.as_str();
match medium.compile(compiler, document) {
Ok(r) => result.push_str(r.as_str()),
Err(e) => return Err(e),
}
}
result.push_str("</div>");
@ -132,23 +135,21 @@ impl Element for Medium {
fn as_referenceable(&self) -> Option<&dyn ReferenceableElement> { Some(self) }
fn compile(&self, compiler: &Compiler, document: &dyn Document, cursor: usize) -> Result<String, String> {
fn compile(&self, compiler: &Compiler, document: &dyn Document) -> Result<String, String> {
match compiler.target() {
Target::HTML => {
let mut result = String::new();
// Reference
let elemref = document.get_reference(self.reference.as_str()).unwrap();
let refcount = compiler.reference_id(document, elemref);
let width = self
.width
.as_ref()
.map_or(String::new(), |w| format!(r#" style="width:{w};""#));
result.push_str(format!(r#"<div id="{}" class="medium"{width}>"#, self.refid(compiler, refcount)).as_str());
result.push_str(format!(r#"<div class="medium"{width}>"#).as_str());
result += match self.media_type {
MediaType::IMAGE => format!(r#"<a href="{0}"><img src="{0}"></a>"#, self.uri),
MediaType::VIDEO => format!(r#"<video controls{width}><source src="{0}"></video>"#, self.uri
MediaType::VIDEO => format!(
r#"<video controls{width}><source src="{0}"></video>"#,
self.uri
),
MediaType::AUDIO => {
format!(r#"<audio controls src="{0}"{width}></audio>"#, self.uri)
@ -158,17 +159,26 @@ impl Element for Medium {
let caption = self
.caption
.as_ref().map(|cap| format!(
.as_ref()
.and_then(|cap| {
Some(format!(
" {}",
Compiler::sanitize(compiler.target(), cap.as_str())
))
.unwrap_or_default();
})
.unwrap_or(String::new());
// Reference
let elemref = document.get_reference(self.reference.as_str()).unwrap();
let refcount = compiler.reference_id(document, elemref);
result.push_str(
format!(r#"<p class="medium-refname">({refcount}){caption}</p>"#).as_str(),
);
if let Some(paragraph) = self.description.as_ref() {
result += paragraph.compile(compiler, document, cursor+result.len())?.as_str();
match paragraph.compile(compiler, document) {
Ok(res) => result.push_str(res.as_str()),
Err(err) => return Err(err),
}
}
result.push_str("</div>");
@ -188,7 +198,7 @@ impl ReferenceableElement for Medium {
&self,
compiler: &Compiler,
_document: &dyn Document,
reference: &InternalReference,
reference: &Reference,
refid: usize,
) -> Result<String, String> {
match compiler.target() {
@ -200,11 +210,7 @@ impl ReferenceableElement for Medium {
// TODO Handle other kind of media
match self.media_type {
MediaType::IMAGE => Ok(format!(
"<a class=\"medium-ref\" href=\"#medium-{refid}\">{caption}<img src=\"{}\"></a>",
self.uri
)),
MediaType::VIDEO => Ok(format!(
"<a class=\"medium-ref\" href=\"#medium-{refid}\">{caption}<video><source src=\"{0}\"></video></a>",
r#"<a class="medium-ref">{caption}<img src="{}"></a>"#,
self.uri
)),
_ => todo!(""),
@ -213,13 +219,8 @@ impl ReferenceableElement for Medium {
_ => todo!(""),
}
}
fn refid(&self, _compiler: &Compiler, refid: usize) -> String {
format!("medium-{refid}")
}
}
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::media")]
pub struct MediaRule {
re: [Regex; 1],
properties: PropertyParser,
@ -324,7 +325,6 @@ impl MediaRule {
impl RegexRule for MediaRule {
fn name(&self) -> &'static str { "Media" }
fn previous(&self) -> Option<&'static str> { Some("Graphviz") }
fn regexes(&self) -> &[regex::Regex] { &self.re }
@ -433,13 +433,15 @@ impl RegexRule for MediaRule {
.get("width", |_, value| -> Result<String, ()> {
Ok(value.clone())
})
.ok().map(|(_, s)| s);
.ok()
.and_then(|(_, s)| Some(s));
let caption = properties
.get("caption", |_, value| -> Result<String, ()> {
Ok(value.clone())
})
.ok().map(|(_, value)| value);
.ok()
.and_then(|(_, value)| Some(value));
let description = match matches.get(4) {
Some(content) => {

View file

@ -9,6 +9,7 @@ pub mod media;
pub mod paragraph;
pub mod raw;
pub mod reference;
pub mod registrar;
pub mod script;
pub mod section;
pub mod style;
@ -17,4 +18,3 @@ pub mod text;
pub mod variable;
pub mod elemstyle;
pub mod customstyle;
pub mod blockquote;

View file

@ -48,7 +48,7 @@ impl Element for Paragraph {
fn element_name(&self) -> &'static str { "Paragraph" }
fn compile(&self, compiler: &Compiler, document: &dyn Document, cursor: usize) -> Result<String, String> {
fn compile(&self, compiler: &Compiler, document: &dyn Document) -> Result<String, String> {
if self.content.is_empty() {
return Ok(String::new());
}
@ -63,7 +63,7 @@ impl Element for Paragraph {
result.push_str("<p>");
for elems in &self.content {
result += elems.compile(compiler, document, cursor+result.len())?.as_str();
result += elems.compile(compiler, document)?.as_str();
}
result.push_str("</p>");
@ -91,7 +91,6 @@ impl ContainerElement for Paragraph {
}
}
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::paragraph")]
pub struct ParagraphRule {
re: Regex,
}
@ -105,12 +104,12 @@ impl ParagraphRule {
}
impl Rule for ParagraphRule {
fn name(&self) -> &'static str { "Paragraph" }
fn previous(&self) -> Option<&'static str> { Some("Comment") }
fn name(&self) -> &'static str { "Paragraphing" }
fn next_match(&self, _state: &ParserState, cursor: &Cursor) -> Option<(usize, Box<dyn Any>)> {
self.re
.find_at(cursor.source.content(), cursor.pos).map(|m| (m.start(), Box::new([false; 0]) as Box<dyn Any>))
.find_at(cursor.source.content(), cursor.pos)
.and_then(|m| Some((m.start(), Box::new([false; 0]) as Box<dyn Any>)))
}
fn on_match(

View file

@ -39,12 +39,11 @@ impl Element for Raw {
fn element_name(&self) -> &'static str { "Raw" }
fn compile(&self, _compiler: &Compiler, _document: &dyn Document, _cursor: usize) -> Result<String, String> {
fn compile(&self, _compiler: &Compiler, _document: &dyn Document) -> Result<String, String> {
Ok(self.content.clone())
}
}
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::raw")]
pub struct RawRule {
re: [Regex; 1],
properties: PropertyParser,
@ -73,7 +72,6 @@ impl RawRule {
impl RegexRule for RawRule {
fn name(&self) -> &'static str { "Raw" }
fn previous(&self) -> Option<&'static str> { Some("Variable Substitution") }
fn regexes(&self) -> &[regex::Regex] { &self.re }

View file

@ -2,20 +2,16 @@ use std::collections::HashMap;
use std::ops::Range;
use std::rc::Rc;
use ariadne::Fmt;
use ariadne::Label;
use ariadne::Report;
use ariadne::ReportKind;
use reference_style::ExternalReferenceStyle;
use regex::Captures;
use regex::Match;
use regex::Regex;
use runtime_format::FormatArgs;
use runtime_format::FormatKey;
use runtime_format::FormatKeyError;
use crate::compiler::compiler::Compiler;
use crate::compiler::compiler::Target;
use crate::document::document::CrossReference;
use crate::document::document::Document;
use crate::document::element::ElemKind;
use crate::document::element::Element;
@ -25,44 +21,33 @@ use crate::parser::parser::ReportColors;
use crate::parser::rule::RegexRule;
use crate::parser::source::Source;
use crate::parser::source::Token;
use crate::parser::style::StyleHolder;
use crate::parser::util;
use crate::parser::util::Property;
use crate::parser::util::PropertyMap;
use crate::parser::util::PropertyParser;
#[derive(Debug)]
pub struct InternalReference {
pub struct Reference {
pub(self) location: Token,
pub(self) refname: String,
pub(self) caption: Option<String>,
}
impl InternalReference {
impl Reference {
pub fn caption(&self) -> Option<&String> { self.caption.as_ref() }
}
impl Element for InternalReference {
impl Element for Reference {
fn location(&self) -> &Token { &self.location }
fn kind(&self) -> ElemKind { ElemKind::Inline }
fn element_name(&self) -> &'static str { "Reference" }
fn compile(
&self,
compiler: &Compiler,
document: &dyn Document,
_cursor: usize,
) -> Result<String, String> {
fn compile(&self, compiler: &Compiler, document: &dyn Document) -> Result<String, String> {
match compiler.target() {
Target::HTML => {
let elemref = document
.get_reference(self.refname.as_str())
.ok_or(format!(
"Unable to find reference `{}` in current document",
self.refname
))?;
let elemref = document.get_reference(self.refname.as_str()).unwrap();
let elem = document.get_from_reference(&elemref).unwrap();
elem.compile_reference(
@ -77,89 +62,6 @@ impl Element for InternalReference {
}
}
#[derive(Debug)]
pub struct ExternalReference {
pub(self) location: Token,
pub(self) reference: CrossReference,
pub(self) caption: Option<String>,
pub(self) style: Rc<reference_style::ExternalReferenceStyle>,
}
struct FmtPair<'a>(Target, &'a ExternalReference);
impl FormatKey for FmtPair<'_> {
fn fmt(&self, key: &str, f: &mut std::fmt::Formatter<'_>) -> Result<(), FormatKeyError> {
match &self.1.reference {
CrossReference::Unspecific(refname) => match key {
"refname" => write!(f, "{}", Compiler::sanitize(self.0, refname))
.map_err(FormatKeyError::Fmt),
_ => Err(FormatKeyError::UnknownKey),
},
CrossReference::Specific(refdoc, refname) => match key {
"refdoc" => {
write!(f, "{}", Compiler::sanitize(self.0, refdoc)).map_err(FormatKeyError::Fmt)
}
"refname" => write!(f, "{}", Compiler::sanitize(self.0, refname))
.map_err(FormatKeyError::Fmt),
_ => Err(FormatKeyError::UnknownKey),
},
}
}
}
impl Element for ExternalReference {
fn location(&self) -> &Token { &self.location }
fn kind(&self) -> ElemKind { ElemKind::Inline }
fn element_name(&self) -> &'static str { "Reference" }
fn compile(
&self,
compiler: &Compiler,
_document: &dyn Document,
cursor: usize,
) -> Result<String, String> {
match compiler.target() {
Target::HTML => {
let mut result = "<a href=\"".to_string();
// Link position
let crossreference_pos = cursor + result.len();
if let Some(caption) = &self.caption {
result +=
format!("\">{}</a>", Compiler::sanitize(Target::HTML, caption)).as_str();
} else {
// Use style
let fmt_pair = FmtPair(compiler.target(), self);
let format_string = match &self.reference {
CrossReference::Unspecific(_) => Compiler::sanitize_format(
fmt_pair.0,
self.style.format_unspecific.as_str(),
),
CrossReference::Specific(_, _) => Compiler::sanitize_format(
fmt_pair.0,
self.style.format_specific.as_str(),
),
};
let args = FormatArgs::new(format_string.as_str(), &fmt_pair);
args.status().map_err(|err| {
format!("Failed to format ExternalReference style `{format_string}`: {err}")
})?;
result += format!("\">{}</a>", args.to_string()).as_str();
}
// Add crossreference
compiler.insert_crossreference(crossreference_pos, self.reference.clone());
Ok(result)
}
_ => todo!(""),
}
}
}
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::reference")]
pub struct ReferenceRule {
re: [Regex; 1],
properties: PropertyParser,
@ -225,7 +127,6 @@ impl ReferenceRule {
impl RegexRule for ReferenceRule {
fn name(&self) -> &'static str { "Reference" }
fn previous(&self) -> Option<&'static str> { Some("Text") }
fn regexes(&self) -> &[regex::Regex] { &self.re }
@ -239,50 +140,41 @@ impl RegexRule for ReferenceRule {
) -> Vec<Report<'_, (Rc<dyn Source>, Range<usize>)>> {
let mut reports = vec![];
let (refdoc, refname) = if let Some(refname_match) = matches.get(1) {
if let Some(sep) = refname_match.as_str().find('#')
// External reference
{
let refdoc = refname_match.as_str().split_at(sep).0;
match validate_refname(document, refname_match.as_str().split_at(sep + 1).1, false)
{
Err(err) => {
reports.push(
Report::build(ReportKind::Error, token.source(), refname_match.start())
.with_message("Invalid Reference Refname")
.with_label(
Label::new((token.source().clone(), refname_match.range()))
.with_message(err),
)
.finish(),
);
return reports;
}
Ok(refname) => (Some(refdoc.to_string()), refname.to_string()),
}
} else
// Internal reference
{
match validate_refname(document, refname_match.as_str(), false) {
Err(err) => {
reports.push(
Report::build(ReportKind::Error, token.source(), refname_match.start())
.with_message("Invalid Reference Refname")
.with_label(
Label::new((token.source().clone(), refname_match.range()))
.with_message(err),
)
.finish(),
);
return reports;
}
Ok(refname) => (None, refname.to_string()),
let refname = match (
matches.get(1).unwrap(),
validate_refname(document, matches.get(1).unwrap().as_str(), false),
) {
(m, Ok(refname)) => {
if document.get_reference(refname).is_none() {
reports.push(
Report::build(ReportKind::Error, token.source(), m.start())
.with_message("Uknown Reference Refname")
.with_label(
Label::new((token.source().clone(), m.range())).with_message(
format!(
"Could not find element with reference: `{}`",
refname.fg(state.parser.colors().info)
),
),
)
.finish(),
);
return reports;
}
refname.to_string()
}
(m, Err(err)) => {
reports.push(
Report::build(ReportKind::Error, token.source(), m.start())
.with_message("Invalid Reference Refname")
.with_label(
Label::new((token.source().clone(), m.range())).with_message(err),
)
.finish(),
);
return reports;
}
} else {
panic!("Unknown error")
};
// Properties
let properties = match self.parse_properties(state.parser.colors(), &token, &matches.get(3))
{
@ -298,193 +190,17 @@ impl RegexRule for ReferenceRule {
Ok(value.clone())
})
.ok()
.map(|(_, s)| s);
.and_then(|(_, s)| Some(s));
if let Some(refdoc) = refdoc {
// Get style
let style = state
.shared
.styles
.borrow()
.current(reference_style::STYLE_KEY)
.downcast_rc::<reference_style::ExternalReferenceStyle>()
.unwrap();
// §{#refname}
if refdoc.is_empty() {
state.push(
document,
Box::new(ExternalReference {
location: token,
reference: CrossReference::Unspecific(refname),
caption,
style,
}),
);
// §{docname#refname}
} else {
state.push(
document,
Box::new(ExternalReference {
location: token,
reference: CrossReference::Specific(refdoc, refname),
caption,
style,
}),
);
}
} else {
state.push(
document,
Box::new(InternalReference {
location: token,
refname,
caption,
}),
);
}
state.push(
document,
Box::new(Reference {
location: token,
refname,
caption,
}),
);
reports
}
fn register_styles(&self, holder: &mut StyleHolder) {
holder.set_current(Rc::new(ExternalReferenceStyle::default()));
}
}
mod reference_style {
use serde::Deserialize;
use serde::Serialize;
use crate::impl_elementstyle;
pub static STYLE_KEY: &str = "style.external_reference";
#[derive(Debug, Serialize, Deserialize)]
pub struct ExternalReferenceStyle {
pub format_unspecific: String,
pub format_specific: String,
}
impl Default for ExternalReferenceStyle {
fn default() -> Self {
Self {
format_unspecific: "(#{refname})".into(),
format_specific: "({refdoc}#{refname})".into(),
}
}
}
impl_elementstyle!(ExternalReferenceStyle, STYLE_KEY);
}
#[cfg(test)]
mod tests {
use crate::compiler::process::process_from_memory;
use crate::elements::paragraph::Paragraph;
use crate::elements::section::Section;
use crate::parser::langparser::LangParser;
use crate::parser::parser::Parser;
use crate::parser::source::SourceFile;
use crate::validate_document;
use super::*;
#[test]
pub fn parse_internal() {
let source = Rc::new(SourceFile::with_content(
"".to_string(),
r#"
#{ref} Referenceable section
§{ref}[caption=Section]
§{ref}[caption=Another]
§{ref2}[caption=Before]
#{ref2} Another section
"#
.to_string(),
None,
));
let parser = LangParser::default();
let (doc, _) = parser.parse(ParserState::new(&parser, None), source, None);
validate_document!(doc.content().borrow(), 0,
Section;
Paragraph {
InternalReference { refname == "ref", caption == Some("Section".to_string()) };
InternalReference { refname == "ref", caption == Some("Another".to_string()) };
InternalReference { refname == "ref2", caption == Some("Before".to_string()) };
};
Paragraph;
Section;
);
}
#[test]
pub fn parse_external() {
let source = Rc::new(SourceFile::with_content(
"".to_string(),
r#"
§{DocA#ref}[caption=Section]
§{DocB#ref}
§{#ref}[caption='ref' from any document]
"#
.to_string(),
None,
));
let parser = LangParser::default();
let (doc, _) = parser.parse(ParserState::new(&parser, None), source, None);
validate_document!(doc.content().borrow(), 0,
Paragraph {
ExternalReference { reference == CrossReference::Specific("DocA".into(), "ref".into()), caption == Some("Section".to_string()) };
ExternalReference { reference == CrossReference::Specific("DocB".into(), "ref".into()), caption == None::<String> };
ExternalReference { reference == CrossReference::Unspecific("ref".into()), caption == Some("'ref' from any document".to_string()) };
};
);
}
#[test]
pub fn test_external() {
let result = process_from_memory(
Target::HTML,
vec![
r#"
@html.page_title = 0
@compiler.output = a.html
#{ref} Referenceable section
"#
.into(),
r#"
@html.page_title = 1
@compiler.output = b.html
§{#ref}
§{a#ref}
#{ref2} Another Referenceable section
"#
.into(),
r#"
@html.page_title = 2
@@style.external_reference = {
"format_unspecific": "[UNSPECIFIC {refname}]",
"format_specific": "[SPECIFIC {refdoc}:{refname}]"
}
§{#ref}[caption=from 0]
§{#ref}
§{#ref2}[caption=from 1]
§{b#ref2}
"#
.into(),
],
)
.unwrap();
assert!(result[1].0.borrow().body.starts_with("<div class=\"content\"><p><a href=\"a.html#Referenceable_section\">(#ref)</a><a href=\"a.html#Referenceable_section\">(a#ref)</a></p>"));
assert!(result[2].0.borrow().body.starts_with("<div class=\"content\"><p><a href=\"a.html#Referenceable_section\">from 0</a><a href=\"a.html#Referenceable_section\">[UNSPECIFIC ref]</a><a href=\"b.html#Another_Referenceable_section\">from 1</a><a href=\"b.html#Another_Referenceable_section\">[SPECIFIC b:ref2]</a></p>"));
}
}

46
src/elements/registrar.rs Normal file
View file

@ -0,0 +1,46 @@
use crate::parser::parser::Parser;
use super::code::CodeRule;
use super::comment::CommentRule;
use super::elemstyle::ElemStyleRule;
use super::graphviz::GraphRule;
use super::import::ImportRule;
use super::layout::LayoutRule;
use super::link::LinkRule;
use super::list::ListRule;
use super::media::MediaRule;
use super::paragraph::ParagraphRule;
use super::raw::RawRule;
use super::script::ScriptRule;
use super::section::SectionRule;
use super::style::StyleRule;
use super::customstyle::CustomStyleRule;
use super::tex::TexRule;
use super::text::TextRule;
use super::variable::VariableRule;
use super::variable::VariableSubstitutionRule;
use super::reference::ReferenceRule;
pub fn register<P: Parser>(parser: &mut P) {
parser.add_rule(Box::new(CommentRule::new()), None).unwrap();
parser.add_rule(Box::new(ParagraphRule::new()), None).unwrap();
parser.add_rule(Box::new(ImportRule::new()), None).unwrap();
parser.add_rule(Box::new(ScriptRule::new()), None).unwrap();
parser.add_rule(Box::new(ElemStyleRule::new()), None).unwrap();
parser.add_rule(Box::new(VariableRule::new()), None).unwrap();
parser.add_rule(Box::new(VariableSubstitutionRule::new()), None).unwrap();
parser.add_rule(Box::new(RawRule::new()), None).unwrap();
parser.add_rule(Box::new(ListRule::new()), None).unwrap();
parser.add_rule(Box::new(CodeRule::new()), None).unwrap();
parser.add_rule(Box::new(TexRule::new()), None).unwrap();
parser.add_rule(Box::new(GraphRule::new()), None).unwrap();
parser.add_rule(Box::new(MediaRule::new()), None).unwrap();
parser.add_rule(Box::new(LayoutRule::new()), None).unwrap();
parser.add_rule(Box::new(StyleRule::new()), None).unwrap();
parser.add_rule(Box::new(CustomStyleRule{}), None).unwrap();
parser.add_rule(Box::new(SectionRule::new()), None).unwrap();
parser.add_rule(Box::new(LinkRule::new()), None).unwrap();
parser.add_rule(Box::new(TextRule::default()), None).unwrap();
parser.add_rule(Box::new(ReferenceRule::new()), None).unwrap();
}

View file

@ -20,7 +20,6 @@ use std::rc::Rc;
use super::text::Text;
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::script")]
pub struct ScriptRule {
re: [Regex; 2],
eval_kinds: [(&'static str, &'static str); 3],
@ -78,7 +77,6 @@ impl ScriptRule {
impl RegexRule for ScriptRule {
fn name(&self) -> &'static str { "Script" }
fn previous(&self) -> Option<&'static str> { Some("Import") }
fn regexes(&self) -> &[regex::Regex] { &self.re }
@ -172,7 +170,7 @@ impl RegexRule for ScriptRule {
Label::new((source.clone(), 0..source.content().len()))
.with_message(format!(
"Kernel execution failed:\n{}",
e
e.to_string()
))
.with_color(state.parser.colors().error),
)
@ -215,7 +213,7 @@ impl RegexRule for ScriptRule {
Label::new((source.clone(), 0..source.content().len()))
.with_message(format!(
"Kernel evaluation failed:\n{}",
e
e.to_string()
))
.with_color(state.parser.colors().error),
)
@ -263,7 +261,7 @@ impl RegexRule for ScriptRule {
Label::new((source.clone(), 0..source.content().len()))
.with_message(format!(
"Kernel evaluation failed:\n{}",
e
e.to_string()
))
.with_color(state.parser.colors().error),
)

View file

@ -24,8 +24,6 @@ use std::ops::Range;
use std::rc::Rc;
use std::sync::Arc;
use super::reference::InternalReference;
#[derive(Debug)]
pub struct Section {
pub(self) location: Token,
@ -45,7 +43,7 @@ impl Element for Section {
fn location(&self) -> &Token { &self.location }
fn kind(&self) -> ElemKind { ElemKind::Block }
fn element_name(&self) -> &'static str { "Section" }
fn compile(&self, compiler: &Compiler, _document: &dyn Document, _cursor: usize) -> Result<String, String> {
fn compile(&self, compiler: &Compiler, _document: &dyn Document) -> Result<String, String> {
match compiler.target() {
Target::HTML => {
// Section numbering
@ -114,7 +112,7 @@ impl ReferenceableElement for Section {
&self,
compiler: &Compiler,
_document: &dyn Document,
reference: &InternalReference,
reference: &super::reference::Reference,
_refid: usize,
) -> Result<String, String> {
match compiler.target() {
@ -135,13 +133,8 @@ impl ReferenceableElement for Section {
_ => todo!(""),
}
}
fn refid(&self, compiler: &Compiler, _refid: usize) -> String {
Compiler::refname(compiler.target(), self.title.as_str())
}
}
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::section")]
pub struct SectionRule {
re: [Regex; 1],
}
@ -162,7 +155,6 @@ pub mod section_kind {
impl RegexRule for SectionRule {
fn name(&self) -> &'static str { "Section" }
fn previous(&self) -> Option<&'static str> { Some("Custom Style") }
fn regexes(&self) -> &[Regex] { &self.re }
@ -222,7 +214,7 @@ impl RegexRule for SectionRule {
.with_message(format!("`{}` previously defined here",
refname.as_str().fg(state.parser.colors().highlight)))
.with_color(state.parser.colors().warning))
.with_note("Previous reference was overwritten".to_string())
.with_note(format!("Previous reference was overwritten"))
.finish());
}
Some(refname.as_str().to_string())
@ -247,7 +239,7 @@ impl RegexRule for SectionRule {
"+".fg(state.parser.colors().info),
kind.as_str().fg(state.parser.colors().highlight)))
.with_color(state.parser.colors().error))
.with_help("Leave empty for a numbered listed section".to_string())
.with_help(format!("Leave empty for a numbered listed section"))
.finish());
return result;
}
@ -321,7 +313,7 @@ impl RegexRule for SectionRule {
}),
);
result
return result;
}
fn register_bindings<'lua>(&self, lua: &'lua Lua) -> Vec<(String, Function<'lua>)> {
@ -330,8 +322,8 @@ impl RegexRule for SectionRule {
bindings.push((
"push".to_string(),
lua.create_function(
|_, (title, depth, kind, reference): (String, usize, Option<String>, Option<String>)| {
let kind = match kind.as_deref().unwrap_or("") {
|_, (title, depth, kind, reference): (String, usize, String, Option<String>)| {
let kind = match kind.as_str() {
"*+" | "+*" => section_kind::NO_NUMBER | section_kind::NO_TOC,
"*" => section_kind::NO_NUMBER,
"+" => section_kind::NO_TOC,
@ -341,7 +333,9 @@ impl RegexRule for SectionRule {
to: Some("push".to_string()),
pos: 3,
name: Some("kind".to_string()),
cause: Arc::new(mlua::Error::external("Unknown section kind specified".to_string())),
cause: Arc::new(mlua::Error::external(format!(
"Unknown section kind specified"
))),
})
}
};
@ -392,7 +386,7 @@ mod section_style {
use crate::impl_elementstyle;
pub static STYLE_KEY: &str = "style.section";
pub static STYLE_KEY: &'static str = "style.section";
#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)]
pub enum SectionLinkPos {

View file

@ -4,7 +4,6 @@ use crate::document::document::Document;
use crate::document::document::DocumentAccessors;
use crate::document::element::ElemKind;
use crate::document::element::Element;
use crate::lua::kernel::CTX;
use crate::parser::parser::ParserState;
use crate::parser::rule::RegexRule;
use crate::parser::source::Source;
@ -15,13 +14,11 @@ use ariadne::Fmt;
use ariadne::Label;
use ariadne::Report;
use ariadne::ReportKind;
use mlua::Function;
use regex::Captures;
use regex::Regex;
use std::cell::RefCell;
use std::ops::Range;
use std::rc::Rc;
use std::sync::Arc;
use super::paragraph::Paragraph;
@ -45,8 +42,8 @@ impl Style {
impl Element for Style {
fn location(&self) -> &Token { &self.location }
fn kind(&self) -> ElemKind { ElemKind::Inline }
fn element_name(&self) -> &'static str { "Style" }
fn compile(&self, compiler: &Compiler, _document: &dyn Document, _cursor: usize) -> Result<String, String> {
fn element_name(&self) -> &'static str { "Section" }
fn compile(&self, compiler: &Compiler, _document: &dyn Document) -> Result<String, String> {
match compiler.target() {
Target::HTML => {
Ok([
@ -99,10 +96,13 @@ impl RuleState for StyleState {
let paragraph = document.last_element::<Paragraph>().unwrap();
let paragraph_end = paragraph
.content
.last().map(|last| (
.last()
.and_then(|last| {
Some((
last.location().source(),
last.location().end() - 1..last.location().end(),
))
})
.unwrap();
reports.push(
@ -120,7 +120,7 @@ impl RuleState for StyleState {
.with_label(
Label::new(paragraph_end)
.with_order(1)
.with_message("Paragraph ends here".to_string())
.with_message(format!("Paragraph ends here"))
.with_color(state.parser.colors().error),
)
.with_note("Styles cannot span multiple documents (i.e @import)")
@ -128,11 +128,10 @@ impl RuleState for StyleState {
);
});
reports
return reports;
}
}
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::style")]
pub struct StyleRule {
re: [Regex; 4],
}
@ -154,11 +153,10 @@ impl StyleRule {
}
}
static STATE_NAME: &str = "elements.style";
static STATE_NAME: &'static str = "elements.style";
impl RegexRule for StyleRule {
fn name(&self) -> &'static str { "Style" }
fn previous(&self) -> Option<&'static str> { Some("Layout") }
fn regexes(&self) -> &[regex::Regex] { &self.re }
@ -196,80 +194,14 @@ impl RegexRule for StyleRule {
Box::new(Style::new(
token.clone(),
index,
style_state.toggled[index].is_none(),
!style_state.toggled[index].is_some(),
)),
);
} else {
panic!("Invalid state at `{STATE_NAME}`");
}
vec![]
}
fn register_bindings<'lua>(&self, lua: &'lua mlua::Lua) -> Vec<(String, Function<'lua>)> {
let mut bindings = vec![];
bindings.push((
"toggle".to_string(),
lua.create_function(|_, style: String| {
let kind = match style.as_str() {
"bold" | "Bold" => 0,
"italic" | "Italic" => 1,
"underline" | "Underline" => 2,
"emphasis" | "Emphasis" => 3,
_ => {
return Err(mlua::Error::BadArgument {
to: Some("toggle".to_string()),
pos: 1,
name: Some("style".to_string()),
cause: Arc::new(mlua::Error::external("Unknown style specified".to_string())),
})
}
};
CTX.with_borrow(|ctx| {
ctx.as_ref().map(|ctx| {
let query = ctx.state.shared.rule_state.borrow().get(STATE_NAME);
let style_state = match query {
Some(state) => state,
None => {
// Insert as a new state
match ctx.state.shared.rule_state.borrow_mut().insert(
STATE_NAME.into(),
Rc::new(RefCell::new(StyleState::new())),
) {
Err(_) => panic!("Unknown error"),
Ok(state) => state,
}
}
};
if let Some(style_state) =
style_state.borrow_mut().downcast_mut::<StyleState>()
{
style_state.toggled[kind] = style_state.toggled[kind]
.clone()
.map_or(Some(ctx.location.clone()), |_| None);
ctx.state.push(
ctx.document,
Box::new(Style::new(
ctx.location.clone(),
kind,
style_state.toggled[kind].is_none(),
)),
);
} else {
panic!("Invalid state at `{STATE_NAME}`");
};
})
});
Ok(())
})
.unwrap(),
));
bindings
return vec![];
}
}
@ -323,45 +255,4 @@ __`UNDERLINE+EM`__
};
);
}
#[test]
fn lua() {
let source = Rc::new(SourceFile::with_content(
"".to_string(),
r#"
Some %<nml.style.toggle("italic")>%style
terminated here%<nml.style.toggle("Italic")>%
%<nml.style.toggle("Bold")>%NOLD + %<nml.style.toggle("italic")>%italic%<nml.style.toggle("bold") nml.style.toggle("italic")>%
%<nml.style.toggle("Underline") nml.style.toggle("Emphasis")>%UNDERLINE+EM%<nml.style.toggle("emphasis")>%%<nml.style.toggle("underline")>%
"#
.to_string(),
None,
));
let parser = LangParser::default();
let (doc, _) = parser.parse(ParserState::new(&parser, None), source, None);
validate_document!(doc.content().borrow(), 0,
Paragraph {
Text;
Style { kind == 1, close == false };
Text;
Style { kind == 1, close == true };
};
Paragraph {
Style { kind == 0, close == false }; // **
Text;
Style { kind == 1, close == false }; // *
Text;
Style { kind == 0, close == true }; // **
Style { kind == 1, close == true }; // *
Style { kind == 2, close == false }; // __
Style { kind == 3, close == false }; // `
Text;
Style { kind == 3, close == true }; // `
Style { kind == 2, close == true }; // __
};
);
}
}

View file

@ -6,7 +6,6 @@ use std::process::Command;
use std::process::Stdio;
use std::rc::Rc;
use std::str::FromStr;
use std::sync::Arc;
use std::sync::Once;
use ariadne::Fmt;
@ -15,8 +14,6 @@ use ariadne::Report;
use ariadne::ReportKind;
use crypto::digest::Digest;
use crypto::sha2::Sha512;
use mlua::Function;
use mlua::Lua;
use regex::Captures;
use regex::Match;
use regex::Regex;
@ -28,7 +25,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::ParserState;
use crate::parser::parser::ReportColors;
use crate::parser::rule::RegexRule;
@ -114,7 +110,10 @@ impl FormattedTex {
}
let mut result = String::new();
if let Err(e) = process.stdout.unwrap().read_to_string(&mut result) { panic!("Unable to read `latex2svg` stdout: {}", e) }
match process.stdout.unwrap().read_to_string(&mut result) {
Err(e) => panic!("Unable to read `latex2svg` stdout: {}", e),
Ok(_) => {}
}
println!("Done!");
Ok(result)
@ -150,18 +149,13 @@ impl Element for Tex {
fn element_name(&self) -> &'static str { "LaTeX" }
fn compile(
&self,
compiler: &Compiler,
document: &dyn Document,
_cursor: usize,
) -> Result<String, String> {
fn compile(&self, compiler: &Compiler, document: &dyn Document) -> Result<String, String> {
match compiler.target() {
Target::HTML => {
static CACHE_INIT: Once = Once::new();
CACHE_INIT.call_once(|| {
if let Some(con) = compiler.cache() {
if let Err(e) = FormattedTex::init(con) {
if let Some(mut con) = compiler.cache() {
if let Err(e) = FormattedTex::init(&mut con) {
eprintln!("Unable to create cache table: {e}");
}
}
@ -191,8 +185,8 @@ impl Element for Tex {
Tex::format_latex(&fontsize, &preamble, &format!("{prepend}{}", self.tex))
};
let result = if let Some(con) = compiler.cache() {
match latex.cached(con, |s| s.latex_to_svg(&exec, &fontsize)) {
let result = if let Some(mut con) = compiler.cache() {
match latex.cached(&mut con, |s| s.latex_to_svg(&exec, &fontsize)) {
Ok(s) => Ok(s),
Err(e) => match e {
CachedError::SqlErr(e) => {
@ -225,7 +219,6 @@ impl Element for Tex {
}
}
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::tex")]
pub struct TexRule {
re: [Regex; 2],
properties: PropertyParser,
@ -303,7 +296,6 @@ impl TexRule {
impl RegexRule for TexRule {
fn name(&self) -> &'static str { "Tex" }
fn previous(&self) -> Option<&'static str> { Some("Code") }
fn regexes(&self) -> &[regex::Regex] { &self.re }
@ -407,14 +399,16 @@ impl RegexRule for TexRule {
.get("caption", |_, value| -> Result<String, ()> {
Ok(value.clone())
})
.ok().map(|(_, value)| value);
.ok()
.and_then(|(_, value)| Some(value));
// Environ
let tex_env = properties
.get("env", |_, value| -> Result<String, ()> {
Ok(value.clone())
})
.ok().map(|(_, value)| value)
.ok()
.and_then(|(_, value)| Some(value))
.unwrap();
state.push(
@ -431,95 +425,6 @@ impl RegexRule for TexRule {
reports
}
fn register_bindings<'lua>(&self, lua: &'lua Lua) -> Vec<(String, Function<'lua>)> {
let mut bindings = vec![];
bindings.push((
"push_math".to_string(),
lua.create_function(
|_, (kind, tex, env, caption): (String, String, Option<String>, Option<String>)| {
let mut result = Ok(());
CTX.with_borrow(|ctx| {
ctx.as_ref().map(|ctx| {
let kind = match TexKind::from_str(kind.as_str()) {
Ok(kind) => kind,
Err(err) => {
result = Err(mlua::Error::BadArgument {
to: Some("push".to_string()),
pos: 2,
name: Some("kind".to_string()),
cause: Arc::new(mlua::Error::external(format!(
"Unable to get tex kind: {err}"
))),
});
return;
}
};
ctx.state.push(
ctx.document,
Box::new(Tex {
location: ctx.location.clone(),
mathmode: true,
kind,
env: env.unwrap_or("main".to_string()),
tex,
caption,
}),
);
})
});
result
},
)
.unwrap(),
));
bindings.push((
"push".to_string(),
lua.create_function(
|_, (kind, tex, env, caption): (String, String, Option<String>, Option<String>)| {
let mut result = Ok(());
CTX.with_borrow(|ctx| {
ctx.as_ref().map(|ctx| {
let kind = match TexKind::from_str(kind.as_str()) {
Ok(kind) => kind,
Err(err) => {
result = Err(mlua::Error::BadArgument {
to: Some("push".to_string()),
pos: 2,
name: Some("kind".to_string()),
cause: Arc::new(mlua::Error::external(format!(
"Unable to get tex kind: {err}"
))),
});
return;
}
};
ctx.state.push(
ctx.document,
Box::new(Tex {
location: ctx.location.clone(),
mathmode: false,
kind,
env: env.unwrap_or("main".to_string()),
tex,
caption,
}),
);
})
});
result
},
)
.unwrap(),
));
bindings
}
}
#[cfg(test)]
@ -540,9 +445,6 @@ mod tests {
$[kind=block, caption=Some\, text\\] 1+1=2 $
$|[env=another] Non Math \LaTeX |$
$[kind=block,env=another] e^{i\pi}=-1$
%<nml.tex.push_math("block", "1+1=2", nil, "Some, text\\")>%
%<nml.tex.push("block", "Non Math \\LaTeX", "another", nil)>%
%<nml.tex.push_math("block", "e^{i\\pi}=-1", "another", nil)>%
"#
.to_string(),
None,
@ -554,9 +456,6 @@ $[kind=block,env=another] e^{i\pi}=-1$
Tex { mathmode == true, tex == "1+1=2", env == "main", caption == Some("Some, text\\".to_string()) };
Tex { mathmode == false, tex == "Non Math \\LaTeX", env == "another" };
Tex { mathmode == true, tex == "e^{i\\pi}=-1", env == "another" };
Tex { mathmode == true, tex == "1+1=2", env == "main", caption == Some("Some, text\\".to_string()) };
Tex { mathmode == false, tex == "Non Math \\LaTeX", env == "another" };
Tex { mathmode == true, tex == "e^{i\\pi}=-1", env == "another" };
);
}
@ -568,9 +467,6 @@ $[kind=block,env=another] e^{i\pi}=-1$
$[ caption=Some\, text\\] 1+1=2 $
$|[env=another, kind=inline , caption = Enclosed \]. ] Non Math \LaTeX|$
$[env=another] e^{i\pi}=-1$
%<nml.tex.push_math("inline", "1+1=2", "main", "Some, text\\")>%
%<nml.tex.push("inline", "Non Math \\LaTeX", "another", "Enclosed ].")>%
%<nml.tex.push_math("inline", "e^{i\\pi}=-1", "another", nil)>%
"#
.to_string(),
None,
@ -581,10 +477,7 @@ $[env=another] e^{i\pi}=-1$
validate_document!(doc.content().borrow(), 0,
Paragraph {
Tex { mathmode == true, tex == "1+1=2", env == "main", caption == Some("Some, text\\".to_string()) };
Tex { mathmode == false, tex == "Non Math \\LaTeX", env == "another", caption == Some("Enclosed ].".to_string()) };
Tex { mathmode == true, tex == "e^{i\\pi}=-1", env == "another" };
Tex { mathmode == true, tex == "1+1=2", env == "main", caption == Some("Some, text\\".to_string()) };
Tex { mathmode == false, tex == "Non Math \\LaTeX", env == "another", caption == Some("Enclosed ].".to_string()) };
Tex { mathmode == false, tex == "Non Math \\LaTeX", env == "another" };
Tex { mathmode == true, tex == "e^{i\\pi}=-1", env == "another" };
};
);

View file

@ -26,8 +26,8 @@ pub struct Text {
impl Text {
pub fn new(location: Token, content: String) -> Text {
Text {
location,
content,
location: location,
content: content,
}
}
}
@ -37,25 +37,18 @@ impl Element for Text {
fn kind(&self) -> ElemKind { ElemKind::Inline }
fn element_name(&self) -> &'static str { "Text" }
fn compile(&self, compiler: &Compiler, _document: &dyn Document, _cursor: usize) -> Result<String, String> {
fn compile(&self, compiler: &Compiler, _document: &dyn Document) -> Result<String, String> {
Ok(Compiler::sanitize(compiler.target(), self.content.as_str()))
}
}
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::text")]
#[derive(Default)]
pub struct TextRule;
impl TextRule {
pub fn new() -> Self { Self {} }
}
impl Rule for TextRule {
fn name(&self) -> &'static str { "Text" }
fn previous(&self) -> Option<&'static str> { Some("Link") }
fn next_match(&self, _state: &ParserState, _cursor: &Cursor) -> Option<(usize, Box<dyn Any>)> {
None
}
fn next_match(&self, _state: &ParserState, _cursor: &Cursor) -> Option<(usize, Box<dyn Any>)> { None }
fn on_match(
&self,

View file

@ -37,7 +37,6 @@ impl FromStr for VariableKind {
}
}
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::variable")]
pub struct VariableRule {
re: [Regex; 1],
kinds: Vec<(String, String)>,
@ -67,7 +66,7 @@ impl VariableRule {
Ok(path) => Ok(Rc::new(PathVariable::new(location, name, path))),
Err(e) => Err(format!("Unable to canonicalize path `{}`: {}",
value.fg(colors.highlight),
e))
e.to_string()))
}
}
_ => panic!("Unhandled variable kind"),
@ -83,7 +82,7 @@ impl VariableRule {
if name.contains("%") {
return Err(format!("Name cannot contain '{}'", "%".fg(colors.info)));
}
Ok(name)
return Ok(name);
}
pub fn validate_value(original_value: &str) -> Result<String, String> {
@ -118,15 +117,14 @@ impl VariableRule {
impl RegexRule for VariableRule {
fn name(&self) -> &'static str { "Variable" }
fn previous(&self) -> Option<&'static str> { Some("Element Style") }
fn regexes(&self) -> &[Regex] { &self.re }
fn on_regex_match(
fn on_regex_match<'a>(
&self,
_: usize,
state: &ParserState,
document: &dyn Document,
document: &'a dyn Document,
token: Token,
matches: regex::Captures,
) -> Vec<Report<'_, (Rc<dyn Source>, Range<usize>)>> {
@ -180,7 +178,7 @@ impl RegexRule for VariableRule {
};
let var_name = match matches.get(2) {
Some(name) => match VariableRule::validate_name(state.parser.colors(), name.as_str()) {
Some(name) => match VariableRule::validate_name(&state.parser.colors(), name.as_str()) {
Ok(var_name) => var_name,
Err(msg) => {
result.push(
@ -228,7 +226,7 @@ impl RegexRule for VariableRule {
};
match self.make_variable(
state.parser.colors(),
&state.parser.colors(),
token.clone(),
var_kind,
var_name.to_string(),
@ -256,7 +254,7 @@ impl RegexRule for VariableRule {
}
}
result
return result;
}
fn register_bindings<'lua>(&self, lua: &'lua Lua) -> Vec<(String, Function<'lua>)> {
@ -297,7 +295,6 @@ impl RegexRule for VariableRule {
}
}
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::variable")]
pub struct VariableSubstitutionRule {
re: [Regex; 1],
}
@ -312,7 +309,6 @@ impl VariableSubstitutionRule {
impl RegexRule for VariableSubstitutionRule {
fn name(&self) -> &'static str { "Variable Substitution" }
fn previous(&self) -> Option<&'static str> { Some("Variable") }
fn regexes(&self) -> &[regex::Regex] { &self.re }
@ -335,7 +331,7 @@ impl RegexRule for VariableSubstitutionRule {
.with_message("Empty variable name")
.with_label(
Label::new((token.source(), matches.get(0).unwrap().range()))
.with_message("Missing variable name for substitution".to_string())
.with_message(format!("Missing variable name for substitution"))
.with_color(state.parser.colors().error),
)
.finish(),
@ -350,7 +346,7 @@ impl RegexRule for VariableSubstitutionRule {
.with_message("Invalid variable name")
.with_label(
Label::new((token.source(), name.range()))
.with_message("Variable names contains leading spaces".to_string())
.with_message(format!("Variable names contains leading spaces"))
.with_color(state.parser.colors().error),
)
.with_help("Remove leading spaces")
@ -366,7 +362,9 @@ impl RegexRule for VariableSubstitutionRule {
.with_message("Invalid variable name")
.with_label(
Label::new((token.source(), name.range()))
.with_message("Variable names contains trailing spaces".to_string())
.with_message(format!(
"Variable names contains trailing spaces"
))
.with_color(state.parser.colors().error),
)
.with_help("Remove trailing spaces")
@ -376,20 +374,23 @@ impl RegexRule for VariableSubstitutionRule {
return result;
}
// Invalid name
if let Err(msg) = VariableRule::validate_name(state.parser.colors(), name.as_str()) {
result.push(
Report::build(ReportKind::Error, token.source(), name.start())
.with_message("Invalid variable name")
.with_label(
Label::new((token.source(), name.range()))
.with_message(msg)
.with_color(state.parser.colors().error),
)
.finish(),
);
match VariableRule::validate_name(&state.parser.colors(), name.as_str()) {
Err(msg) => {
result.push(
Report::build(ReportKind::Error, token.source(), name.start())
.with_message("Invalid variable name")
.with_label(
Label::new((token.source(), name.range()))
.with_message(msg)
.with_color(state.parser.colors().error),
)
.finish(),
);
return result;
}
return result;
}
_ => {}
}
// Get variable
match document.get_variable(name.as_str()) {
@ -417,6 +418,6 @@ impl RegexRule for VariableSubstitutionRule {
variable.parse(state, token, document);
result
return result;
}
}

View file

@ -94,3 +94,85 @@ impl From<&LineCursor> for Cursor
}
}
}
#[derive(Debug)]
pub struct LsParser
{
rules: Vec<Box<dyn Rule>>,
colors: ReportColors,
// Parser state
pub state: RefCell<StateHolder>,
pub kernels: RefCell<HashMap<String, Kernel>>,
}
impl Parser for LsParser
{
fn colors(&self) -> &ReportColors { &self.colors }
fn rules(&self) -> &Vec<Box<dyn Rule>> { &self.rules }
fn rules_mut(&mut self) -> &mut Vec<Box<dyn Rule>> { &mut self.rules }
fn state(&self) -> Ref<'_, StateHolder> { self.state.borrow() }
fn state_mut(&self) -> std::cell::RefMut<'_, StateHolder> { self.state.borrow_mut() }
fn has_error(&self) -> bool { true }
fn push<'a>(&self, doc: &dyn Document, elem: Box<dyn Element>) {
todo!()
}
fn parse<'a>(&self, source: Rc<dyn Source>, parent: Option<&'a dyn Document<'a>>) -> Box<dyn Document<'a>+'a> {
todo!()
}
fn parse_into<'a>(&self, source: Rc<dyn Source>, document: &'a dyn Document<'a>) {
todo!()
}
}
impl KernelHolder for LsParser
{
fn get_kernel(&self, name: &str)
-> Option<RefMut<'_, Kernel>> {
RefMut::filter_map(self.kernels.borrow_mut(),
|map| map.get_mut(name)).ok()
}
fn insert_kernel(&self, name: String, kernel: Kernel)
-> RefMut<'_, Kernel> {
//TODO do not get
self.kernels.borrow_mut()
.insert(name.clone(), kernel);
self.get_kernel(name.as_str()).unwrap()
}
}
impl StyleHolder for LsParser {
fn element_styles(&self) -> Ref<'_, HashMap<String, Rc<dyn ElementStyle>>> {
todo!()
}
fn element_styles_mut(&self) -> RefMut<'_, HashMap<String, Rc<dyn ElementStyle>>> {
todo!()
}
}
impl LayoutHolder for LsParser {
fn layouts(&self) -> Ref<'_, HashMap<String, Rc<dyn LayoutType>>> {
todo!()
}
fn layouts_mut(&self) -> RefMut<'_, HashMap<String, Rc<dyn LayoutType>>> {
todo!()
}
}
impl CustomStyleHolder for LsParser {
fn custom_styles(&self) -> Ref<'_, HashMap<String, Rc<dyn CustomStyle>>> {
todo!()
}
fn custom_styles_mut(&self) -> RefMut<'_, HashMap<String, Rc<dyn CustomStyle>>> {
todo!()
}
}

View file

@ -15,7 +15,7 @@ pub struct KernelContext<'a, 'b, 'c> {
}
thread_local! {
pub static CTX: RefCell<Option<KernelContext<'static, 'static, 'static>>> = const { RefCell::new(None) };
pub static CTX: RefCell<Option<KernelContext<'static, 'static, 'static>>> = RefCell::new(None);
}
#[derive(Debug)]

View file

@ -8,13 +8,24 @@ mod parser;
use std::env;
use std::io::BufWriter;
use std::io::Write;
use std::path::PathBuf;
use std::process::ExitCode;
use std::rc::Rc;
use std::time::UNIX_EPOCH;
use compiler::compiler::CompiledDocument;
use compiler::compiler::Compiler;
use compiler::compiler::Target;
use compiler::navigation::create_navigation;
use document::document::Document;
use getopts::Options;
use parser::langparser::LangParser;
use parser::parser::Parser;
use parser::parser::ParserState;
use rusqlite::Connection;
use walkdir::WalkDir;
use crate::parser::source::SourceFile;
extern crate getopts;
fn print_usage(program: &str, opts: Options) {
@ -36,6 +47,126 @@ NML version: 0.4\n"
);
}
fn parse(
parser: &LangParser,
input: &str,
debug_opts: &Vec<String>,
) -> Result<Box<dyn Document<'static>>, String> {
println!("Parsing {input}...");
// Parse
let source = SourceFile::new(input.to_string(), None).unwrap();
let (doc, _) = parser.parse(ParserState::new(parser, None), Rc::new(source), None);
if debug_opts.contains(&"ast".to_string()) {
println!("-- BEGIN AST DEBUGGING --");
doc.content()
.borrow()
.iter()
.for_each(|elem| println!("{elem:#?}"));
println!("-- END AST DEBUGGING --");
}
if debug_opts.contains(&"ref".to_string()) {
println!("-- BEGIN REFERENCES DEBUGGING --");
let sc = doc.scope().borrow();
sc.referenceable.iter().for_each(|(name, reference)| {
println!(" - {name}: `{:#?}`", doc.get_from_reference(reference));
});
println!("-- END REFERENCES DEBUGGING --");
}
if debug_opts.contains(&"var".to_string()) {
println!("-- BEGIN VARIABLES DEBUGGING --");
let sc = doc.scope().borrow();
sc.variables.iter().for_each(|(_name, var)| {
println!(" - `{:#?}`", var);
});
println!("-- END VARIABLES DEBUGGING --");
}
if parser.has_error() {
return Err("Parsing failed aborted due to errors while parsing".to_string());
}
Ok(doc)
}
fn process(
target: Target,
files: Vec<PathBuf>,
db_path: &Option<String>,
force_rebuild: bool,
debug_opts: &Vec<String>,
) -> Result<Vec<CompiledDocument>, String> {
let mut compiled = vec![];
let current_dir = std::env::current_dir()
.map_err(|err| format!("Unable to get the current working directory: {err}"))?;
let con = db_path
.as_ref()
.map_or(Connection::open_in_memory(), |path| Connection::open(path))
.map_err(|err| format!("Unable to open connection to the database: {err}"))?;
CompiledDocument::init_cache(&con)
.map_err(|err| format!("Failed to initialize cached document table: {err}"))?;
let parser = LangParser::default();
for file in files {
let meta = std::fs::metadata(&file)
.map_err(|err| format!("Failed to get metadata for `{file:#?}`: {err}"))?;
let modified = meta
.modified()
.map_err(|err| format!("Unable to query modification time for `{file:#?}`: {err}"))?;
// Move to file's directory
let file_parent_path = file
.parent()
.ok_or(format!("Failed to get parent path for `{file:#?}`"))?;
std::env::set_current_dir(file_parent_path)
.map_err(|err| format!("Failed to move to path `{file_parent_path:#?}`: {err}"))?;
let parse_and_compile = || -> Result<CompiledDocument, String> {
// Parse
let doc = parse(&parser, file.to_str().unwrap(), debug_opts)?;
// Compile
let compiler = Compiler::new(target, db_path.clone());
let mut compiled = compiler.compile(&*doc);
// Insert into cache
compiled.mtime = modified.duration_since(UNIX_EPOCH).unwrap().as_secs();
compiled.insert_cache(&con).map_err(|err| {
format!("Failed to insert compiled document from `{file:#?}` into cache: {err}")
})?;
Ok(compiled)
};
let cdoc = if force_rebuild {
parse_and_compile()?
} else {
match CompiledDocument::from_cache(&con, file.to_str().unwrap()) {
Some(compiled) => {
if compiled.mtime < modified.duration_since(UNIX_EPOCH).unwrap().as_secs() {
parse_and_compile()?
} else {
compiled
}
}
None => parse_and_compile()?,
}
};
compiled.push(cdoc);
}
std::env::set_current_dir(current_dir)
.map_err(|err| format!("Failed to set current directory: {err}"))?;
Ok(compiled)
}
fn main() -> ExitCode {
let args: Vec<String> = env::args().collect();
let program = args[0].clone();
@ -213,21 +344,19 @@ fn main() -> ExitCode {
}
// Parse, compile using the cache
let processed =
match compiler::process::process(Target::HTML, files, &db_path, force_rebuild, &debug_opts)
{
Ok(processed) => processed,
Err(e) => {
eprintln!("{e}");
return ExitCode::FAILURE;
}
};
let compiled = match process(Target::HTML, files, &db_path, force_rebuild, &debug_opts) {
Ok(compiled) => compiled,
Err(e) => {
eprintln!("{e}");
return ExitCode::FAILURE;
}
};
if input_meta.is_dir()
// Batch mode
{
// Build navigation
let navigation = match create_navigation(&processed) {
let navigation = match create_navigation(&compiled) {
Ok(nav) => nav,
Err(e) => {
eprintln!("{e}");
@ -236,54 +365,38 @@ fn main() -> ExitCode {
};
// Output
for (doc, _) in &processed {
for doc in compiled {
let out_path = match doc
.borrow()
.get_variable("compiler.output")
.or(input_meta.is_file().then_some(&output))
{
Some(path) => path.clone(),
None => {
eprintln!("Unable to get output file for `{}`", doc.borrow().input);
eprintln!("Unable to get output file for `{}`", doc.input);
continue;
}
};
let nav = navigation.compile(Target::HTML, doc);
let nav = navigation.compile(Target::HTML, &doc);
let file = std::fs::File::create(output.clone() + "/" + out_path.as_str()).unwrap();
let mut writer = BufWriter::new(file);
write!(
writer,
"{}{}{}{}",
doc.borrow().header,
nav,
doc.borrow().body,
doc.borrow().footer
)
.unwrap();
write!(writer, "{}{}{}{}", doc.header, nav, doc.body, doc.footer).unwrap();
writer.flush().unwrap();
}
} else
// Single file
{
for (doc, _) in &processed {
for doc in compiled {
let file = std::fs::File::create(output.clone()).unwrap();
let mut writer = BufWriter::new(file);
write!(
writer,
"{}{}{}",
doc.borrow().header,
doc.borrow().body,
doc.borrow().footer
)
.unwrap();
write!(writer, "{}{}{}", doc.header, doc.body, doc.footer).unwrap();
writer.flush().unwrap();
}
}
ExitCode::SUCCESS
return ExitCode::SUCCESS;
}

View file

@ -45,7 +45,8 @@ pub struct CustomStyleHolder {
impl CustomStyleHolder {
pub fn get(&self, style_name: &str) -> Option<Rc<dyn CustomStyle>> {
self.custom_styles
.get(style_name).cloned()
.get(style_name)
.map(|style| style.clone())
}
pub fn insert(&mut self, style: Rc<dyn CustomStyle>) {

View file

@ -4,6 +4,7 @@ use std::rc::Rc;
use crate::document::document::Document;
use crate::document::element::DocumentEnd;
use crate::document::langdocument::LangDocument;
use crate::elements::registrar::register;
use crate::elements::text::Text;
use super::parser::Parser;
@ -34,10 +35,8 @@ impl LangParser {
};
// Register rules
for rule in super::rule::get_rule_registry()
{
s.add_rule(rule).unwrap();
}
// TODO: use https://docs.rs/inventory/latest/inventory/
register(&mut s);
s
}
@ -120,7 +119,7 @@ impl Parser for LangParser {
))),
);
(Box::new(doc), state)
return (Box::new(doc), state);
}
fn parse_into<'p, 'a, 'doc>(
@ -164,7 +163,7 @@ impl Parser for LangParser {
}
}
state
return state;
// State
//self.handle_reports(source.clone(),
// self.state_mut().on_scope_end(&self, &document, super::state::Scope::DOCUMENT));

View file

@ -36,7 +36,7 @@ pub struct LayoutHolder {
impl LayoutHolder {
pub fn get(&self, layout_name: &str) -> Option<Rc<dyn LayoutType>> {
self.layouts.get(layout_name).cloned()
self.layouts.get(layout_name).map(|layout| layout.clone())
}
pub fn insert(&mut self, layout: Rc<dyn LayoutType>) {

View file

@ -19,6 +19,7 @@ use crate::document::document::DocumentAccessors;
use crate::document::element::ContainerElement;
use crate::document::element::ElemKind;
use crate::document::element::Element;
use crate::elements::customstyle::CustomStyleRule;
use crate::elements::paragraph::Paragraph;
use crate::lua::kernel::Kernel;
use crate::lua::kernel::KernelHolder;
@ -89,8 +90,8 @@ impl SharedState {
// Default styles & layouts
parser.rules().iter().for_each(|rule| {
rule.register_styles(&mut s.styles.borrow_mut());
rule.register_layouts(&mut s.layouts.borrow_mut());
rule.register_styles(&mut *s.styles.borrow_mut());
rule.register_layouts(&mut *s.layouts.borrow_mut());
});
s
@ -185,6 +186,7 @@ impl<'a, 'b> ParserState<'a, 'b> {
.for_each(|(rule, (matched_at, match_data))| {
// Don't upate if not stepped over yet
if *matched_at > cursor.pos {
// TODO: maybe we should expose matches() so it becomes possible to dynamically register a new rule
return;
}
@ -198,7 +200,7 @@ impl<'a, 'b> ParserState<'a, 'b> {
let mut escaped = false;
'inner: loop {
let g = graphemes.next_back();
if g.is_none() || g.unwrap() != "\\" {
if !g.is_some() || g.unwrap() != "\\" {
break 'inner;
}
@ -236,10 +238,10 @@ impl<'a, 'b> ParserState<'a, 'b> {
return (cursor.at(content.len()), None);
}
(
return (
cursor.at(next_pos),
Some((winner, matches_borrow[winner].1.take().unwrap())),
)
);
}
/// Add an [`Element`] to the [`Document`]
@ -262,7 +264,7 @@ impl<'a, 'b> ParserState<'a, 'b> {
if doc.last_element::<Paragraph>().is_some_and(|_| true) {
self.parser
.handle_reports(self.shared.rule_state.borrow_mut().on_scope_end(
self,
&self,
doc,
super::state::Scope::PARAGRAPH,
));
@ -367,11 +369,11 @@ pub trait Parser {
/// # Warning
///
/// This method must not be called if a [`ParserState`] for this parser exists.
fn add_rule(&mut self, rule: Box<dyn Rule>) -> Result<(), String> {
if self
fn add_rule(&mut self, rule: Box<dyn Rule>, after: Option<&'static str>) -> Result<(), String> {
if let Some(_) = self
.rules()
.iter()
.any(|other_rule| other_rule.name() == rule.name())
.find(|other_rule| other_rule.name() == rule.name())
{
return Err(format!(
"Attempted to introduce duplicate rule: `{}`",
@ -379,7 +381,23 @@ pub trait Parser {
));
}
self.rules_mut().push(rule);
// Try to insert after
if let Some(after) = after {
let index = self
.rules()
.iter()
.enumerate()
.find(|(_, rule)| rule.name() == after)
.map(|(idx, _)| idx);
if let Some(index) = index {
self.rules_mut().insert(index, rule);
} else {
return Err(format!("Unable to find rule `{after}` to insert after"));
}
} else {
self.rules_mut().push(rule);
}
Ok(())
}
@ -391,11 +409,14 @@ pub trait Parser {
let mut sources: HashSet<Rc<dyn Source>> = HashSet::new();
fn recurse_source(sources: &mut HashSet<Rc<dyn Source>>, source: Rc<dyn Source>) {
sources.insert(source.clone());
if let Some(parent) = source.location() {
let parent_source = parent.source();
if sources.get(&parent_source).is_none() {
recurse_source(sources, parent_source);
match source.location() {
Some(parent) => {
let parent_source = parent.source();
if sources.get(&parent_source).is_none() {
recurse_source(sources, parent_source);
}
}
None => {}
}
}
@ -420,8 +441,10 @@ pub trait Parser {
if let Some(_s) = source.downcast_ref::<VirtualSource>() {
let start = location.start()
+ if location.source().content().as_bytes()[location.start()]
== b'\n' { 1 } else { 0 };
+ (location.source().content().as_bytes()[location.start()]
== '\n' as u8)
.then_some(1)
.unwrap_or(0);
report.labels.push(
Label::new((location.source(), start..location.end()))
.with_message("In evaluation of")

View file

@ -12,66 +12,12 @@ use mlua::Function;
use mlua::Lua;
use std::any::Any;
use std::collections::HashMap;
use std::ops::Range;
use std::rc::Rc;
macro_rules! create_registry {
( $($construct:expr),+ $(,)? ) => {{
let mut map = HashMap::new();
$(
let boxed = Box::new($construct) as Box<dyn Rule>;
map.insert(boxed.name(), boxed);
)+
map
}};
}
/// Gets the list of all rules exported with the [`auto_registry`] proc macro.
/// Rules are sorted according to topological order using the [`Rule::previous`] method.
#[auto_registry::generate_registry(registry = "rules", target = make_rules, return_type = HashMap<&'static str, Box<dyn Rule>>, maker = create_registry)]
pub fn get_rule_registry() -> Vec<Box<dyn Rule>> {
fn cmp(
map: &HashMap<&'static str, Box<dyn Rule>>,
lname: &'static str,
rname: &'static str,
) -> std::cmp::Ordering {
let l = map.get(lname).unwrap();
let r = map.get(rname).unwrap();
if l.previous() == Some(r.name()) {
std::cmp::Ordering::Greater
} else if r.previous() == Some(l.name()) {
std::cmp::Ordering::Less
} else if l.previous().is_some() && r.previous().is_none() {
std::cmp::Ordering::Greater
} else if r.previous().is_some() && l.previous().is_none() {
std::cmp::Ordering::Less
} else if let (Some(pl), Some(pr)) = (l.previous(), r.previous()) {
cmp(map, pl, pr)
} else {
std::cmp::Ordering::Equal
}
}
let mut map = make_rules();
let mut sorted_keys = map.keys().copied().collect::<Vec<_>>();
sorted_keys.sort_by(|l, r| cmp(&map, l, r));
let mut owned = Vec::with_capacity(sorted_keys.len());
for key in sorted_keys {
let rule = map.remove(key).unwrap();
owned.push(rule);
}
owned
}
pub trait Rule: Downcast {
/// The rule name
/// Returns rule's name
fn name(&self) -> &'static str;
/// The name of the rule that should come before this one
fn previous(&self) -> Option<&'static str>;
/// Finds the next match starting from [`cursor`]
fn next_match(&self, state: &ParserState, cursor: &Cursor) -> Option<(usize, Box<dyn Any>)>;
/// Callback when rule matches
@ -101,12 +47,8 @@ impl core::fmt::Debug for dyn Rule {
}
pub trait RegexRule {
/// The rule name
fn name(&self) -> &'static str;
/// The name of the rule that should come before this one
fn previous(&self) -> Option<&'static str>;
/// Returns the rule's regexes
fn regexes(&self) -> &[regex::Regex];
@ -127,7 +69,6 @@ pub trait RegexRule {
impl<T: RegexRule + 'static> Rule for T {
fn name(&self) -> &'static str { RegexRule::name(self) }
fn previous(&self) -> Option<&'static str> { RegexRule::previous(self) }
/// Finds the next match starting from [`cursor`]
fn next_match(&self, _state: &ParserState, cursor: &Cursor) -> Option<(usize, Box<dyn Any>)> {
@ -136,18 +77,18 @@ impl<T: RegexRule + 'static> Rule for T {
self.regexes().iter().enumerate().for_each(|(id, re)| {
if let Some(m) = re.find_at(content.as_str(), cursor.pos) {
found = found
.map(|(f_pos, f_id)| {
.and_then(|(f_pos, f_id)| {
if f_pos > m.start() {
(m.start(), id)
Some((m.start(), id))
} else {
(f_pos, f_id)
Some((f_pos, f_id))
}
})
.or(Some((m.start(), id)));
}
});
found.map(|(pos, id)| (pos, Box::new(id) as Box<dyn Any>))
return found.map(|(pos, id)| (pos, Box::new(id) as Box<dyn Any>));
}
fn on_match<'a>(
@ -179,42 +120,3 @@ impl<T: RegexRule + 'static> Rule for T {
fn register_layouts(&self, holder: &mut LayoutHolder) { self.register_layouts(holder); }
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn registry() {
let rules = get_rule_registry();
let names: Vec<&'static str> = rules.iter().map(|rule| rule.name()).collect();
assert_eq!(
names,
vec![
"Comment",
"Paragraph",
"Import",
"Script",
"Element Style",
"Variable",
"Variable Substitution",
"Raw",
"List",
"Blockquote",
"Code",
"Tex",
"Graphviz",
"Media",
"Layout",
"Style",
"Custom Style",
"Section",
"Link",
"Text",
"Reference",
]
);
}
}

View file

@ -24,6 +24,7 @@ impl core::fmt::Display for dyn Source {
}
impl core::fmt::Debug for dyn Source {
// TODO
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Source{{{}}}", self.name())
}
@ -54,10 +55,10 @@ impl SourceFile {
pub fn new(path: String, location: Option<Token>) -> Result<Self, String> {
match fs::read_to_string(&path) {
Err(_) => {
Err(format!(
return Err(String::from(format!(
"Unable to read file content: `{}`",
path
))
)))
}
Ok(content) => Ok(Self {
location,
@ -69,9 +70,9 @@ impl SourceFile {
pub fn with_content(path: String, content: String, location: Option<Token>) -> Self {
Self {
location,
path,
content,
location: location,
path: path,
content: content,
}
}
}
@ -161,7 +162,7 @@ impl Token {
}
pub fn source(&self) -> Rc<dyn Source> {
self.source.clone()
return self.source.clone();
}
/// Construct Token from a range
@ -175,10 +176,10 @@ impl Token {
}
pub fn start(&self) -> usize {
self.range.start
return self.range.start;
}
pub fn end(&self) -> usize {
self.range.end
return self.range.end;
}
}

View file

@ -64,7 +64,7 @@ impl RuleStateHolder {
}
pub fn get(&self, state_name: &str) -> Option<Rc<RefCell<dyn RuleState>>> {
self.states.get(state_name).cloned()
self.states.get(state_name).map(|state| state.clone())
}
pub fn on_scope_end(
@ -88,6 +88,6 @@ impl RuleStateHolder {
}
});
reports
return reports;
}
}

View file

@ -38,7 +38,7 @@ impl StyleHolder {
/// NOTE: Will panic if a style is not defined for a given element
/// If you need to process user input, use [`is_registered`]
pub fn current(&self, style_key: &str) -> Rc<dyn ElementStyle> {
self.styles.get(style_key).cloned().unwrap()
self.styles.get(style_key).map(|rc| rc.clone()).unwrap()
}
/// Sets the [`style`]
@ -50,17 +50,17 @@ impl StyleHolder {
#[macro_export]
macro_rules! impl_elementstyle {
($t:ty, $key:expr) => {
impl $crate::parser::style::ElementStyle for $t {
impl crate::parser::style::ElementStyle for $t {
fn key(&self) -> &'static str { $key }
fn from_json(
&self,
json: &str,
) -> Result<std::rc::Rc<dyn $crate::parser::style::ElementStyle>, String> {
) -> Result<std::rc::Rc<dyn crate::parser::style::ElementStyle>, String> {
serde_json::from_str::<$t>(json)
.map_err(|e| e.to_string())
.map(|obj| {
std::rc::Rc::new(obj) as std::rc::Rc<dyn $crate::parser::style::ElementStyle>
std::rc::Rc::new(obj) as std::rc::Rc<dyn crate::parser::style::ElementStyle>
})
}
@ -68,9 +68,9 @@ macro_rules! impl_elementstyle {
&self,
lua: &mlua::Lua,
value: mlua::Value,
) -> Result<std::rc::Rc<dyn $crate::parser::style::ElementStyle>, mlua::Error> {
) -> Result<std::rc::Rc<dyn crate::parser::style::ElementStyle>, mlua::Error> {
mlua::LuaSerdeExt::from_value::<$t>(lua, value).map(|obj| {
std::rc::Rc::new(obj) as std::rc::Rc<dyn $crate::parser::style::ElementStyle>
std::rc::Rc::new(obj) as std::rc::Rc<dyn crate::parser::style::ElementStyle>
})
}
}

View file

@ -36,7 +36,7 @@ pub fn process_text(document: &dyn Document, content: &str) -> String {
.last_element::<Paragraph>()
.and_then(|par| {
par.find_back(|e| e.kind() != ElemKind::Invisible)
.map(|e| e.kind() == ElemKind::Inline)
.and_then(|e| Some(e.kind() == ElemKind::Inline))
})
.unwrap_or(false)
{
@ -79,12 +79,12 @@ pub fn process_text(document: &dyn Document, content: &str) -> String {
}
}
(out + g, Some(g))
return (out + g, Some(g));
})
.0
.to_string();
processed
return processed;
}
/// Processed a string and escapes a single token out of it
@ -111,7 +111,7 @@ pub fn process_escaped<S: AsRef<str>>(escape: char, token: &'static str, content
escaped += 1;
} else if escaped % 2 == 1 && token_it.peek().map_or(false, |p| *p == c) {
let _ = token_it.next();
if token_it.peek().is_none() {
if token_it.peek() == None {
(0..(escaped / 2)).for_each(|_| processed.push(escape));
escaped = 0;
token_it = token.chars().peekable();
@ -333,8 +333,9 @@ impl PropertyParser {
escaped = 0;
in_name = true;
try_insert(&name, &value)?;
if let Err(e) = try_insert(&name, &value) {
return Err(e);
}
name.clear();
value.clear();
} else {
@ -360,7 +361,9 @@ impl PropertyParser {
return Err("Expected non empty property list.".to_string());
}
try_insert(&name, &value)?;
if let Err(e) = try_insert(&name, &value) {
return Err(e);
}
if let Err(e) = self.properties.iter().try_for_each(|(key, prop)| {
if !properties.properties.contains_key(key) {
@ -420,10 +423,7 @@ mod tests {
(&doc as &dyn Document)
.last_element_mut::<Paragraph>()
.unwrap()
.push(Box::new(Comment {
location: tok.clone(),
content: "COMMENT".into(),
}))
.push(Box::new(Comment::new(tok.clone(), "COMMENT".to_string())))
.unwrap();
assert_eq!(process_text(&doc, "\na"), "a");

View file

@ -137,11 +137,6 @@ a.inline-code {
content: "";
}
/* Sections */
a.section-link {
text-decoration: none;
}
/* Code blocks */
div.code-block-title {
background-color: #20202a;
@ -201,7 +196,7 @@ div.code-block-content .code-block-line {
margin-right: .5em;
}
.medium img, video, audio {
.medium img {
max-width: 100%;
}
@ -227,7 +222,6 @@ a.medium-ref {
font-weight: bold;
color: #d367c1;
text-decoration: none;
}
a.medium-ref:hover {
@ -239,50 +233,11 @@ a.medium-ref img {
margin: 1.3em 0 0 0;
}
a.medium-ref video {
display: none;
margin: 1.3em 0 0 0;
}
a:hover.medium-ref img {
max-width: 25%;
left: 37.5%;
max-width: 50%;
margin: auto;
display: inline-block;
position: absolute;
box-shadow: 0px 0px 6px 2px rgba(0, 0, 0, 0.75);
}
a:hover.medium-ref video {
max-width: 25%;
left: 37.5%;
display: inline-block;
position: absolute;
box-shadow: 0px 0px 6px 2px rgba(0, 0, 0, 0.75);
}
/* Blockquote */
blockquote {
margin-left: 0.2em;
padding-left: 0.6em;
border-left: 4px solid #0ff08b;
}
blockquote p::before {
content: '\201C';
}
blockquote p::after {
content: '\201D';
}
.blockquote-author:before {
content: '—';
}
.blockquote-author {
margin-left: 0.2em;
}