1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use log::{debug};
use mdbook::preprocess::{Preprocessor, PreprocessorContext, CmdPreprocessor};
use mdbook::book::Book;
use mdbook::errors::Error;
use clap::ArgMatches;
use std::{process, io};
use mdbook::BookItem;
use regex::Regex;
use serde_derive::{Deserialize, Serialize};


#[macro_use]
extern crate lazy_static;


lazy_static! {
    /// if you use mermaid, may be use `Flow1 ==description==> Flow2`, this string will ignore
    static ref RE : Regex= Regex::new(r"==(?P<c>\S+?)==[^>]").unwrap();
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(default, rename_all = "kebab-case")]
pub struct RegexReplacerItem {
    regex: String,
    rep: String
}

impl Default for RegexReplacerItem {
    fn default() -> Self {
        RegexReplacerItem{
            regex:"".to_string(),
            rep:"".to_string(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(default, rename_all = "kebab-case")]
pub struct RegexReplacerConfigure {
    command: Option<String>,
    items: Option<Vec<RegexReplacerItem>>,
}

impl Default for RegexReplacerConfigure{
    fn default() -> Self {
        RegexReplacerConfigure{command: Option::None, items: Option::None}
    }
}

#[test]
fn test_replace(){
    let c = (Regex::new("==(?P<c>.+?)==").unwrap(), "<mark>$c</mark>".to_string());
    let f = replace_all(&c, "==sasasas== s");
    print!("{}", f);
}

pub fn replace_all(e: & (Regex, String), s: &str) -> String {
    e.0.replace_all(s, e.1.as_str()).into_owned()
}

pub fn handle_each_item(book_item: &mut BookItem, regexes: & Vec<(Regex, String)>) {
    match book_item {
        BookItem::Chapter(chapter) => {

            for e in regexes {
                chapter.content = replace_all(e, chapter.content.as_str());
                debug!("after regex placer: {} => {}:\n{}", e.0, e.1, chapter.content);
            }

            for item in &mut chapter.sub_items {
                handle_each_item(item, regexes);
            }
        }
        _ => {}
    }
}

pub struct RegexReplacerPreprocessor {}

impl Preprocessor for RegexReplacerPreprocessor {

    fn name(&self) -> &str {
        "regex-replacer"
    }

    fn run(&self, ctx: &PreprocessorContext, mut book: Book) -> Result<Book, Error> {

        let config = ctx.config.get_preprocessor(self.name()).unwrap();
        let config_string = toml::to_string(config).unwrap();
        let regex_replacer_configure: RegexReplacerConfigure = toml::from_str(config_string.as_str()).unwrap();

        let mut regexes: Vec<(Regex, String)> = Vec::new();

        if let Some(items) = &regex_replacer_configure.items {
            for e in items{
                let regex = Regex::new(e.regex.as_str()).unwrap();
                regexes.push((regex, e.rep.clone()));
            }
        }

        let ii = &mut book.sections;
        for section in ii {
            handle_each_item(section, & regexes);
        }
        Ok(book)
    }

    fn supports_renderer(&self, _renderer: &str) -> bool {
        _renderer == "html"
    }
}

pub fn handle_preprocessor(pre: &dyn Preprocessor) -> Result<(), Error> {
    debug!("mark start");
    let (ctx, book) = CmdPreprocessor::parse_input(io::stdin())?;

    if ctx.mdbook_version != mdbook::MDBOOK_VERSION {
        // We should probably use the `semver` crate to check compatibility
        // here...
        eprintln!(
            "Warning: The {} plugin was built against version {} of mdbook, \
             but we're being called from version {}",
            pre.name(),
            mdbook::MDBOOK_VERSION,
            ctx.mdbook_version
        );
    }

    let processed_book = pre.run(&ctx, book)?;

    serde_json::to_writer(io::stdout(), &processed_book)?;

    Ok(())
}

pub fn handle_supports(pre: &dyn Preprocessor, sub_args: &ArgMatches) -> ! {
    let renderer = sub_args.value_of("renderer").expect("Required argument");
    let supported = pre.supports_renderer(&renderer);

    // Signal whether the renderer is supported by exiting with 1 or 0.
    if supported {
        process::exit(0);
    } else {
        process::exit(1);
    }
}