markdown_linkify/transform/
mod.rs

1use std::fmt::Debug;
2
3use regex::Regex;
4use serde::{Deserialize, Serialize};
5
6use crate::link::Link;
7
8use self::substitution::Substitution;
9
10pub mod docs_rustlang_replacer;
11pub mod docsrs_replacer;
12pub mod substitution;
13
14/// If a link transformer matches on a tag, it shall produce a meaningful regex-substituted transformation.
15pub trait LinkTransformer: Debug + dyn_clone::DynClone {
16    /// Apply the link transformation.
17    fn apply(&self, link: &mut Link) -> anyhow::Result<()>;
18
19    /// Text on which the link transformer can operate shall start with this string.
20    fn tag(&self) -> String;
21
22    /// Regex which will be applied to extract the salient part of the link destination.
23    fn pattern(&self) -> Regex {
24        Regex::new(format!("^{}(?<i>.+)", self.tag()).as_str()).expect("Invalid regex")
25    }
26
27    fn strip_tag(&self) -> bool {
28        true
29    }
30}
31
32dyn_clone::clone_trait_object!(LinkTransformer);
33
34#[derive(Debug, Clone, Default, Deserialize, Serialize)]
35pub struct Transformers {
36    pub regex: Vec<Substitution>,
37}
38
39impl Transformers {
40    /// An example configuration, used to generate a template file.
41    #[must_use]
42    pub fn example() -> Self {
43        Self {
44            regex: vec![Substitution::example()],
45        }
46    }
47}