Fix layout regex & add prop parser for centered
This commit is contained in:
parent
57756d8048
commit
a00db70bf6
1 changed files with 111 additions and 60 deletions
|
@ -21,6 +21,7 @@ use mlua::Lua;
|
||||||
use regex::Captures;
|
use regex::Captures;
|
||||||
use regex::Match;
|
use regex::Match;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
use regex::RegexBuilder;
|
||||||
use std::any::Any;
|
use std::any::Any;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
@ -64,46 +65,10 @@ mod default_layouts {
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
|
||||||
pub struct Centered;
|
|
||||||
|
|
||||||
impl LayoutType for Centered {
|
|
||||||
fn name(&self) -> &'static str { "Centered" }
|
|
||||||
|
|
||||||
fn expects(&self) -> Range<usize> { 1..1 }
|
|
||||||
|
|
||||||
fn parse_properties(&self, properties: &str) -> Result<Option<Box<dyn Any>>, String> {
|
|
||||||
if !properties.is_empty() {
|
|
||||||
return Err(format!("Layout {} excepts no properties", self.name()));
|
|
||||||
}
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn compile(
|
|
||||||
&self,
|
|
||||||
token: LayoutToken,
|
|
||||||
_id: usize,
|
|
||||||
_properties: &Option<Box<dyn Any>>,
|
|
||||||
compiler: &Compiler,
|
|
||||||
_document: &dyn Document,
|
|
||||||
) -> Result<String, String> {
|
|
||||||
match compiler.target() {
|
|
||||||
Target::HTML => match token {
|
|
||||||
LayoutToken::BEGIN => Ok(r#"<div class="centered">"#.to_string()),
|
|
||||||
LayoutToken::NEXT => panic!(),
|
|
||||||
LayoutToken::END => Ok(r#"</div>"#.to_string()),
|
|
||||||
},
|
|
||||||
_ => todo!(""),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Split {
|
pub struct Centered(PropertyParser);
|
||||||
properties: PropertyParser,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for Split {
|
impl Default for Centered {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
let mut properties = HashMap::new();
|
let mut properties = HashMap::new();
|
||||||
properties.insert(
|
properties.insert(
|
||||||
|
@ -115,22 +80,20 @@ mod default_layouts {
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
Self {
|
Self(PropertyParser { properties })
|
||||||
properties: PropertyParser { properties },
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LayoutType for Split {
|
impl LayoutType for Centered {
|
||||||
fn name(&self) -> &'static str { "Split" }
|
fn name(&self) -> &'static str { "Centered" }
|
||||||
|
|
||||||
fn expects(&self) -> Range<usize> { 2..usize::MAX }
|
fn expects(&self) -> Range<usize> { 1..1 }
|
||||||
|
|
||||||
fn parse_properties(&self, properties: &str) -> Result<Option<Box<dyn Any>>, String> {
|
fn parse_properties(&self, properties: &str) -> Result<Option<Box<dyn Any>>, String> {
|
||||||
let props = if properties.is_empty() {
|
let props = if properties.is_empty() {
|
||||||
self.properties.default()
|
self.0.default()
|
||||||
} else {
|
} else {
|
||||||
self.properties.parse(properties)
|
self.0.parse(properties)
|
||||||
}
|
}
|
||||||
.map_err(|err| {
|
.map_err(|err| {
|
||||||
format!(
|
format!(
|
||||||
|
@ -163,10 +126,89 @@ mod default_layouts {
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.downcast_ref::<String>()
|
.downcast_ref::<String>()
|
||||||
.unwrap().as_str()
|
.unwrap()
|
||||||
|
.as_str()
|
||||||
{
|
{
|
||||||
"" => "".to_string(),
|
"" => "".to_string(),
|
||||||
str => format!(r#" style={}"#, Compiler::sanitize(compiler.target(), str))
|
str => format!(r#" style={}"#, Compiler::sanitize(compiler.target(), str)),
|
||||||
|
};
|
||||||
|
match token {
|
||||||
|
LayoutToken::BEGIN => Ok(format!(r#"<div class="centered"{style}>"#)),
|
||||||
|
LayoutToken::NEXT => panic!(),
|
||||||
|
LayoutToken::END => Ok(r#"</div>"#.to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => todo!(""),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Split(PropertyParser);
|
||||||
|
|
||||||
|
impl Default for Split {
|
||||||
|
fn default() -> Self {
|
||||||
|
let mut properties = HashMap::new();
|
||||||
|
properties.insert(
|
||||||
|
"style".to_string(),
|
||||||
|
Property::new(
|
||||||
|
true,
|
||||||
|
"Additional style for the split".to_string(),
|
||||||
|
Some("".to_string()),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
Self(PropertyParser { properties })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LayoutType for Split {
|
||||||
|
fn name(&self) -> &'static str { "Split" }
|
||||||
|
|
||||||
|
fn expects(&self) -> Range<usize> { 2..usize::MAX }
|
||||||
|
|
||||||
|
fn parse_properties(&self, properties: &str) -> Result<Option<Box<dyn Any>>, String> {
|
||||||
|
let props = if properties.is_empty() {
|
||||||
|
self.0.default()
|
||||||
|
} else {
|
||||||
|
self.0.parse(properties)
|
||||||
|
}
|
||||||
|
.map_err(|err| {
|
||||||
|
format!(
|
||||||
|
"Failed to parse properties for layout {}: {err}",
|
||||||
|
self.name()
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let style = props
|
||||||
|
.get("style", |_, value| -> Result<String, ()> {
|
||||||
|
Ok(value.clone())
|
||||||
|
})
|
||||||
|
.map_err(|err| format!("Failed to parse style: {err:#?}"))
|
||||||
|
.map(|(_, value)| value)?;
|
||||||
|
|
||||||
|
Ok(Some(Box::new(style)))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn compile(
|
||||||
|
&self,
|
||||||
|
token: LayoutToken,
|
||||||
|
_id: usize,
|
||||||
|
properties: &Option<Box<dyn Any>>,
|
||||||
|
compiler: &Compiler,
|
||||||
|
_document: &dyn Document,
|
||||||
|
) -> Result<String, String> {
|
||||||
|
match compiler.target() {
|
||||||
|
Target::HTML => {
|
||||||
|
let style = match properties
|
||||||
|
.as_ref()
|
||||||
|
.unwrap()
|
||||||
|
.downcast_ref::<String>()
|
||||||
|
.unwrap()
|
||||||
|
.as_str()
|
||||||
|
{
|
||||||
|
"" => "".to_string(),
|
||||||
|
str => format!(r#" style={}"#, Compiler::sanitize(compiler.target(), str)),
|
||||||
};
|
};
|
||||||
match token {
|
match token {
|
||||||
LayoutToken::BEGIN => Ok(format!(
|
LayoutToken::BEGIN => Ok(format!(
|
||||||
|
@ -225,12 +267,6 @@ impl State for LayoutState {
|
||||||
reports.push(
|
reports.push(
|
||||||
Report::build(ReportKind::Error, start.source(), start.start())
|
Report::build(ReportKind::Error, start.source(), start.start())
|
||||||
.with_message("Unterminated Layout")
|
.with_message("Unterminated Layout")
|
||||||
//.with_label(
|
|
||||||
// Label::new((document.source(), active_range.clone()))
|
|
||||||
// .with_order(0)
|
|
||||||
// .with_message(format!("Style {} is not terminated before the end of paragraph",
|
|
||||||
// name.fg(parser.colors().info)))
|
|
||||||
// .with_color(parser.colors().error))
|
|
||||||
.with_label(
|
.with_label(
|
||||||
Label::new((start.source(), start.range.start + 1..start.range.end))
|
Label::new((start.source(), start.range.start + 1..start.range.end))
|
||||||
.with_order(1)
|
.with_order(1)
|
||||||
|
@ -271,9 +307,24 @@ impl LayoutRule {
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
re: [
|
re: [
|
||||||
Regex::new(r"(?:^|\n)#\+LAYOUT_BEGIN(?:\[((?:\\.|[^\\\\])*?)\])?(.*)").unwrap(),
|
RegexBuilder::new(
|
||||||
Regex::new(r"(?:^|\n)#\+LAYOUT_NEXT(?:\[((?:\\.|[^\\\\])*?)\])?(?:$|\n)").unwrap(),
|
r"(?:^|\n)(?:[^\S\n]*)#\+LAYOUT_BEGIN(?:\[((?:\\.|[^\\\\])*?)\])?(.*)",
|
||||||
Regex::new(r"(?:^|\n)#\+LAYOUT_END(?:\[((?:\\.|[^\\\\])*?)\])?(?:$|\n)").unwrap(),
|
)
|
||||||
|
.multi_line(true)
|
||||||
|
.build()
|
||||||
|
.unwrap(),
|
||||||
|
RegexBuilder::new(
|
||||||
|
r"(?:^|\n)(?:[^\S\n]*)#\+LAYOUT_NEXT(?:\[((?:\\.|[^\\\\])*?)\])?$",
|
||||||
|
)
|
||||||
|
.multi_line(true)
|
||||||
|
.build()
|
||||||
|
.unwrap(),
|
||||||
|
RegexBuilder::new(
|
||||||
|
r"(?:^|\n)(?:[^\S\n]*)#\+LAYOUT_END(?:\[((?:\\.|[^\\\\])*?)\])?$",
|
||||||
|
)
|
||||||
|
.multi_line(true)
|
||||||
|
.build()
|
||||||
|
.unwrap(),
|
||||||
],
|
],
|
||||||
layouts,
|
layouts,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue