Better custom style
This commit is contained in:
parent
2eb355ac5e
commit
dd5b861db2
2 changed files with 17 additions and 18 deletions
|
@ -3,7 +3,6 @@
|
||||||
|
|
||||||
# Defining a custom style
|
# Defining a custom style
|
||||||
```Lua
|
```Lua
|
||||||
%<[main]
|
|
||||||
function undercustom_start(color)
|
function undercustom_start(color)
|
||||||
nml.raw.push("inline", "<span style=\"border-bottom: 1px dashed " .. color .. "\">")
|
nml.raw.push("inline", "<span style=\"border-bottom: 1px dashed " .. color .. "\">")
|
||||||
end
|
end
|
||||||
|
@ -12,8 +11,8 @@ function undercustom_end()
|
||||||
nml.raw.push("inline", "</span>")
|
nml.raw.push("inline", "</span>")
|
||||||
end
|
end
|
||||||
|
|
||||||
nml.custom_style.define_toggled("Undercustom Red", "~", "undercustom_start(\"red\")", "undercustom_end()")
|
nml.custom_style.define_toggled("Undercustom Red", "~", function() undercustom_start("red") end, undercustom_end)
|
||||||
nml.custom_style.define_paired("Undercustom Green", "[|", "|]", "undercustom_start(\"Green\")", "undercustom_end()")
|
nml.custom_style.define_paired("Undercustom Green", "[|", "|]", function() undercustom_start("green") end, undercustom_end)
|
||||||
>%
|
>%
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -26,8 +25,8 @@ function undercustom_end()
|
||||||
nml.raw.push("inline", "</span>")
|
nml.raw.push("inline", "</span>")
|
||||||
end
|
end
|
||||||
|
|
||||||
nml.custom_style.define_toggled("Undercustom Red", "~", "undercustom_start(\"red\")", "undercustom_end()")
|
nml.custom_style.define_toggled("Undercustom Red", "~", function() undercustom_start("red") end, undercustom_end)
|
||||||
nml.custom_style.define_paired("Undercustom Green", "[|", "|]", "undercustom_start(\"Green\")", "undercustom_end()")
|
nml.custom_style.define_paired("Undercustom Green", "[|", "|]", function() undercustom_start("green") end, undercustom_end)
|
||||||
>%
|
>%
|
||||||
Results in the following:
|
Results in the following:
|
||||||
* ``Plain Text,~Dashed underline~`` → ~Dashed underline~
|
* ``Plain Text,~Dashed underline~`` → ~Dashed underline~
|
||||||
|
|
|
@ -34,8 +34,8 @@ use super::paragraph::Paragraph;
|
||||||
struct LuaCustomStyle {
|
struct LuaCustomStyle {
|
||||||
pub(self) name: String,
|
pub(self) name: String,
|
||||||
pub(self) tokens: CustomStyleToken,
|
pub(self) tokens: CustomStyleToken,
|
||||||
pub(self) start: String,
|
pub(self) start: mlua::Function<'static>,
|
||||||
pub(self) end: String,
|
pub(self) end: mlua::Function<'static>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CustomStyle for LuaCustomStyle {
|
impl CustomStyle for LuaCustomStyle {
|
||||||
|
@ -56,8 +56,8 @@ impl CustomStyle for LuaCustomStyle {
|
||||||
|
|
||||||
let mut reports = vec![];
|
let mut reports = vec![];
|
||||||
kernel.run_with_context(&mut ctx, |lua| {
|
kernel.run_with_context(&mut ctx, |lua| {
|
||||||
let chunk = lua.load(self.start.as_str());
|
if let Err(err) = self.start.call::<_, ()>(())
|
||||||
if let Err(err) = chunk.eval::<()>() {
|
{
|
||||||
report_err!(
|
report_err!(
|
||||||
&mut reports,
|
&mut reports,
|
||||||
location.source(),
|
location.source(),
|
||||||
|
@ -86,8 +86,8 @@ impl CustomStyle for LuaCustomStyle {
|
||||||
|
|
||||||
let mut reports = vec![];
|
let mut reports = vec![];
|
||||||
kernel.run_with_context(&mut ctx, |lua| {
|
kernel.run_with_context(&mut ctx, |lua| {
|
||||||
let chunk = lua.load(self.end.as_str());
|
if let Err(err) = self.end.call::<_, ()>(())
|
||||||
if let Err(err) = chunk.eval::<()>() {
|
{
|
||||||
report_err!(
|
report_err!(
|
||||||
&mut reports,
|
&mut reports,
|
||||||
location.source(),
|
location.source(),
|
||||||
|
@ -344,14 +344,14 @@ impl Rule for CustomStyleRule {
|
||||||
bindings.push((
|
bindings.push((
|
||||||
"define_toggled".into(),
|
"define_toggled".into(),
|
||||||
lua.create_function(
|
lua.create_function(
|
||||||
|_, (name, token, on_start, on_end): (String, String, String, String)| {
|
|_, (name, token, on_start, on_end): (String, String, mlua::Function, mlua::Function)| {
|
||||||
let mut result = Ok(());
|
let mut result = Ok(());
|
||||||
|
|
||||||
let style = LuaCustomStyle {
|
let style = LuaCustomStyle {
|
||||||
tokens: CustomStyleToken::Toggle(token),
|
tokens: CustomStyleToken::Toggle(token),
|
||||||
name: name.clone(),
|
name: name.clone(),
|
||||||
start: on_start,
|
start: unsafe { std::mem::transmute(on_start.clone()) },
|
||||||
end: on_end,
|
end: unsafe { std::mem::transmute(on_start.clone()) },
|
||||||
};
|
};
|
||||||
|
|
||||||
CTX.with_borrow(|ctx| {
|
CTX.with_borrow(|ctx| {
|
||||||
|
@ -393,8 +393,8 @@ impl Rule for CustomStyleRule {
|
||||||
String,
|
String,
|
||||||
String,
|
String,
|
||||||
String,
|
String,
|
||||||
String,
|
mlua::Function,
|
||||||
String,
|
mlua::Function,
|
||||||
)| {
|
)| {
|
||||||
let mut result = Ok(());
|
let mut result = Ok(());
|
||||||
|
|
||||||
|
@ -413,8 +413,8 @@ impl Rule for CustomStyleRule {
|
||||||
let style = LuaCustomStyle {
|
let style = LuaCustomStyle {
|
||||||
tokens: CustomStyleToken::Pair(token_start, token_end),
|
tokens: CustomStyleToken::Pair(token_start, token_end),
|
||||||
name: name.clone(),
|
name: name.clone(),
|
||||||
start: on_start,
|
start: unsafe { std::mem::transmute(on_start.clone()) },
|
||||||
end: on_end,
|
end: unsafe { std::mem::transmute(on_start.clone()) },
|
||||||
};
|
};
|
||||||
|
|
||||||
CTX.with_borrow(|ctx| {
|
CTX.with_borrow(|ctx| {
|
||||||
|
|
Loading…
Reference in a new issue