feat(config): parse rich text matches

This commit is contained in:
Federico Terzi 2021-04-29 22:35:05 +02:00
parent 302a45aa88
commit 038798a2d6
3 changed files with 74 additions and 53 deletions

View File

@ -17,17 +17,11 @@
* along with espanso. If not, see <https://www.gnu.org/licenses/>. * along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/ */
use crate::{ use crate::{counter::next_id, matches::{Match, Params, TextFormat, UpperCasingStyle, Value, Variable, group::{path::resolve_imports, MatchGroup}}};
counter::next_id,
matches::{
group::{path::resolve_imports, MatchGroup},
Match, UpperCasingStyle, Variable, Params, Value,
},
};
use anyhow::Result; use anyhow::Result;
use log::{error, warn}; use log::{error, warn};
use parse::YAMLMatchGroup; use parse::YAMLMatchGroup;
use regex::{Regex, Captures}; use regex::{Captures, Regex};
use std::convert::{TryFrom, TryInto}; use std::convert::{TryFrom, TryInto};
use self::{ use self::{
@ -147,7 +141,19 @@ impl TryFrom<YAMLMatch> for Match {
MatchCause::None MatchCause::None
}; };
let effect = if let Some(replace) = yaml_match.replace { let effect =
if yaml_match.replace.is_some() || yaml_match.markdown.is_some() || yaml_match.html.is_some()
{ // TODO: test markdown and html cases
let (replace, format) = if let Some(plain) = yaml_match.replace {
(plain, TextFormat::Plain)
} else if let Some(markdown) = yaml_match.markdown {
(markdown, TextFormat::Markdown)
} else if let Some(html) = yaml_match.html {
(html, TextFormat::Html)
} else {
unreachable!();
};
let vars: Result<Vec<Variable>> = yaml_match let vars: Result<Vec<Variable>> = yaml_match
.vars .vars
.unwrap_or_default() .unwrap_or_default()
@ -157,13 +163,17 @@ impl TryFrom<YAMLMatch> for Match {
MatchEffect::Text(TextEffect { MatchEffect::Text(TextEffect {
replace, replace,
vars: vars?, vars: vars?,
format,
}) })
} else if let Some(form_layout) = yaml_match.form { // TODO: test form case } else if let Some(form_layout) = yaml_match.form {
// TODO: test form case
// Replace all the form fields with actual variables // Replace all the form fields with actual variables
let resolved_layout = VAR_REGEX.replace_all(&form_layout, |caps: &Captures| { let resolved_layout = VAR_REGEX
.replace_all(&form_layout, |caps: &Captures| {
let var_name = caps.get(1).unwrap().as_str(); let var_name = caps.get(1).unwrap().as_str();
format!("{{{{form1.{}}}}}", var_name) format!("{{{{form1.{}}}}}", var_name)
}).to_string(); })
.to_string();
// Convert escaped brakets in forms // Convert escaped brakets in forms
let resolved_layout = resolved_layout.replace("\\{", "{ ").replace("\\}", " }"); let resolved_layout = resolved_layout.replace("\\{", "{ ").replace("\\}", " }");
@ -186,6 +196,7 @@ impl TryFrom<YAMLMatch> for Match {
MatchEffect::Text(TextEffect { MatchEffect::Text(TextEffect {
replace: resolved_layout, replace: resolved_layout,
vars, vars,
format: TextFormat::Plain,
}) })
} else { } else {
MatchEffect::None MatchEffect::None
@ -493,6 +504,7 @@ mod tests {
effect: MatchEffect::Text(TextEffect { effect: MatchEffect::Text(TextEffect {
replace: "world".to_string(), replace: "world".to_string(),
vars, vars,
..Default::default()
}), }),
..Default::default() ..Default::default()
} }
@ -526,6 +538,7 @@ mod tests {
effect: MatchEffect::Text(TextEffect { effect: MatchEffect::Text(TextEffect {
replace: "world".to_string(), replace: "world".to_string(),
vars, vars,
..Default::default()
}), }),
..Default::default() ..Default::default()
} }

View File

@ -98,13 +98,13 @@ pub struct YAMLMatch {
pub force_clipboard: Option<bool>, pub force_clipboard: Option<bool>,
#[serde(default)] #[serde(default)]
pub markdown: Option<String>, // TODO: map pub markdown: Option<String>,
#[serde(default)] #[serde(default)]
pub paragraph: Option<bool>, // TODO: map pub paragraph: Option<bool>,
#[serde(default)] #[serde(default)]
pub html: Option<String>, // TODO: map pub html: Option<String>,
} }
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] #[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]

View File

@ -17,11 +17,11 @@
* along with espanso. If not, see <https://www.gnu.org/licenses/>. * along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/ */
use std::collections::{BTreeMap};
use enum_as_inner::EnumAsInner; use enum_as_inner::EnumAsInner;
use ordered_float::OrderedFloat; use ordered_float::OrderedFloat;
use std::collections::BTreeMap;
use crate::counter::{StructId}; use crate::counter::StructId;
pub(crate) mod group; pub(crate) mod group;
pub mod store; pub mod store;
@ -95,13 +95,20 @@ pub enum MatchEffect {
None, None,
Text(TextEffect), Text(TextEffect),
// TODO: image // TODO: image
// TODO: rich text
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TextEffect { pub struct TextEffect {
pub replace: String, pub replace: String,
pub vars: Vec<Variable>, pub vars: Vec<Variable>,
pub format: TextFormat,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum TextFormat {
Plain,
Markdown,
Html,
} }
impl Default for TextEffect { impl Default for TextEffect {
@ -109,6 +116,7 @@ impl Default for TextEffect {
Self { Self {
replace: String::new(), replace: String::new(),
vars: Vec::new(), vars: Vec::new(),
format: TextFormat::Plain,
} }
} }
} }