From 038798a2d644efa87f01084b519e7d7f241d782c Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Thu, 29 Apr 2021 22:35:05 +0200 Subject: [PATCH] feat(config): parse rich text matches --- .../src/matches/group/loader/yaml/mod.rs | 105 ++++++++++-------- .../src/matches/group/loader/yaml/parse.rs | 6 +- espanso-config/src/matches/mod.rs | 16 ++- 3 files changed, 74 insertions(+), 53 deletions(-) diff --git a/espanso-config/src/matches/group/loader/yaml/mod.rs b/espanso-config/src/matches/group/loader/yaml/mod.rs index ccef02c..1b0aadc 100644 --- a/espanso-config/src/matches/group/loader/yaml/mod.rs +++ b/espanso-config/src/matches/group/loader/yaml/mod.rs @@ -17,17 +17,11 @@ * along with espanso. If not, see . */ -use crate::{ - counter::next_id, - matches::{ - group::{path::resolve_imports, MatchGroup}, - Match, UpperCasingStyle, Variable, Params, Value, - }, -}; +use crate::{counter::next_id, matches::{Match, Params, TextFormat, UpperCasingStyle, Value, Variable, group::{path::resolve_imports, MatchGroup}}}; use anyhow::Result; use log::{error, warn}; use parse::YAMLMatchGroup; -use regex::{Regex, Captures}; +use regex::{Captures, Regex}; use std::convert::{TryFrom, TryInto}; use self::{ @@ -147,49 +141,66 @@ impl TryFrom for Match { MatchCause::None }; - let effect = if let Some(replace) = yaml_match.replace { - let vars: Result> = yaml_match - .vars - .unwrap_or_default() - .into_iter() - .map(|var| var.try_into()) - .collect(); - MatchEffect::Text(TextEffect { - replace, - vars: vars?, - }) - } else if let Some(form_layout) = yaml_match.form { // TODO: test form case - // Replace all the form fields with actual variables - let resolved_layout = VAR_REGEX.replace_all(&form_layout, |caps: &Captures| { - let var_name = caps.get(1).unwrap().as_str(); - format!("{{{{form1.{}}}}}", var_name) - }).to_string(); + 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!(); + }; - // Convert escaped brakets in forms - let resolved_layout = resolved_layout.replace("\\{", "{ ").replace("\\}", " }"); + let vars: Result> = yaml_match + .vars + .unwrap_or_default() + .into_iter() + .map(|var| var.try_into()) + .collect(); + MatchEffect::Text(TextEffect { + replace, + vars: vars?, + format, + }) + } else if let Some(form_layout) = yaml_match.form { + // TODO: test form case + // Replace all the form fields with actual variables + let resolved_layout = VAR_REGEX + .replace_all(&form_layout, |caps: &Captures| { + let var_name = caps.get(1).unwrap().as_str(); + format!("{{{{form1.{}}}}}", var_name) + }) + .to_string(); - // Convert the form data to valid variables - let mut params = Params::new(); - params.insert("layout".to_string(), Value::String(form_layout)); + // Convert escaped brakets in forms + let resolved_layout = resolved_layout.replace("\\{", "{ ").replace("\\}", " }"); - if let Some(fields) = yaml_match.form_fields { - params.insert("fields".to_string(), Value::Object(convert_params(fields)?)); - } + // Convert the form data to valid variables + let mut params = Params::new(); + params.insert("layout".to_string(), Value::String(form_layout)); - let vars = vec![Variable { - id: next_id(), - name: "form1".to_owned(), - var_type: "form".to_owned(), - params, - }]; + if let Some(fields) = yaml_match.form_fields { + params.insert("fields".to_string(), Value::Object(convert_params(fields)?)); + } - MatchEffect::Text(TextEffect { - replace: resolved_layout, - vars, - }) - } else { - MatchEffect::None - }; + let vars = vec![Variable { + id: next_id(), + name: "form1".to_owned(), + var_type: "form".to_owned(), + params, + }]; + + MatchEffect::Text(TextEffect { + replace: resolved_layout, + vars, + format: TextFormat::Plain, + }) + } else { + MatchEffect::None + }; if let MatchEffect::None = effect { warn!( @@ -493,6 +504,7 @@ mod tests { effect: MatchEffect::Text(TextEffect { replace: "world".to_string(), vars, + ..Default::default() }), ..Default::default() } @@ -526,6 +538,7 @@ mod tests { effect: MatchEffect::Text(TextEffect { replace: "world".to_string(), vars, + ..Default::default() }), ..Default::default() } diff --git a/espanso-config/src/matches/group/loader/yaml/parse.rs b/espanso-config/src/matches/group/loader/yaml/parse.rs index 0bb73bd..b7a444d 100644 --- a/espanso-config/src/matches/group/loader/yaml/parse.rs +++ b/espanso-config/src/matches/group/loader/yaml/parse.rs @@ -98,13 +98,13 @@ pub struct YAMLMatch { pub force_clipboard: Option, #[serde(default)] - pub markdown: Option, // TODO: map + pub markdown: Option, #[serde(default)] - pub paragraph: Option, // TODO: map + pub paragraph: Option, #[serde(default)] - pub html: Option, // TODO: map + pub html: Option, } #[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] diff --git a/espanso-config/src/matches/mod.rs b/espanso-config/src/matches/mod.rs index a452487..e9bfcc6 100644 --- a/espanso-config/src/matches/mod.rs +++ b/espanso-config/src/matches/mod.rs @@ -17,11 +17,11 @@ * along with espanso. If not, see . */ -use std::collections::{BTreeMap}; use enum_as_inner::EnumAsInner; use ordered_float::OrderedFloat; +use std::collections::BTreeMap; -use crate::counter::{StructId}; +use crate::counter::StructId; pub(crate) mod group; pub mod store; @@ -95,13 +95,20 @@ pub enum MatchEffect { None, Text(TextEffect), // TODO: image - // TODO: rich text } #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct TextEffect { pub replace: String, pub vars: Vec, + pub format: TextFormat, +} + +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub enum TextFormat { + Plain, + Markdown, + Html, } impl Default for TextEffect { @@ -109,6 +116,7 @@ impl Default for TextEffect { Self { replace: String::new(), vars: Vec::new(), + format: TextFormat::Plain, } } } @@ -148,4 +156,4 @@ pub enum Value { pub enum Number { Integer(i64), Float(OrderedFloat), -} \ No newline at end of file +}