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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
pub mod replacement_rule_part;


use std::fmt::Debug;
use std::sync::Arc;
use getset::{Getters, Setters};
use log;
use regex::Regex;
use replacement_rule_part::ReplacementRuleReplacerPart;
use crate::compilable_text::CompilableText;
use crate::compiler::compilation_configuration::compilation_configuration_overlay::CompilationConfigurationOverLay;
use crate::compiler::compilation_configuration::CompilationConfiguration;
use crate::output_format::OutputFormat;
use super::CompilationRule;
use crate::compiler::compilation_error::CompilationError;


pub type ReplacementRuleParts = Vec<Arc<dyn ReplacementRuleReplacerPart>>;


/// Rule to replace a NMD text based on a specific pattern matching rule
#[derive(Debug, Clone, Getters, Setters)]
pub struct ReplacementRule {

    #[getset(set)]
    search_pattern: String,

    #[getset(set)]
    search_pattern_regex: Regex,

    #[getset(get = "pub", set = "pub")]
    replacer_parts: ReplacementRuleParts,
}

impl ReplacementRule {
    
    /// Returns a new instance having a search pattern and a replication pattern
    pub fn new(searching_pattern: String, replacers: ReplacementRuleParts) -> Self {

        log::debug!("created new compilation rule with search_pattern: '{}'", searching_pattern);

        Self {
            search_pattern_regex: Regex::new(&searching_pattern).unwrap(),
            search_pattern: searching_pattern,
            replacer_parts: replacers,
        }
    }

}

impl CompilationRule for ReplacementRule {

    /// Compile the content using internal search and replacement pattern
    fn standard_compile(&self, compilable: &CompilableText, format: &OutputFormat, compilation_configuration: &CompilationConfiguration, compilation_configuration_overlay: CompilationConfigurationOverLay) -> Result<CompilableText, CompilationError> {

        log::debug!("compile:\n{:#?}\nusing '{}'->'{:?}'", compilable, self.search_pattern(), self.replacer_parts);

        let mut compiled_parts = Vec::new();

        let compilable_content = compilable.compilable_content();

        let captures_matches = self.search_pattern_regex.captures_iter(&compilable_content);

        for captures in captures_matches {

            for replacer_part in &self.replacer_parts {

                compiled_parts.append(&mut replacer_part.compile(&captures, compilable, format, compilation_configuration, compilation_configuration_overlay.clone())?.into())
            }   
        }

        Ok(CompilableText::new(compiled_parts))
    }
    
    fn search_pattern(&self) -> &String {
        &self.search_pattern
    }
    
    fn search_pattern_regex(&self) -> &Regex {
        &self.search_pattern_regex
    }
}



#[cfg(test)]
mod test {

    use std::sync::Arc;

    use crate::{codex::modifier::{standard_text_modifier::StandardTextModifier, ModifiersBucket}, compilable_text::{compilable_text_part::{CompilableTextPart, CompilableTextPartType}, CompilableText}, compiler::{compilation_configuration::{compilation_configuration_overlay::CompilationConfigurationOverLay, CompilationConfiguration}, compilation_rule::{constants::ESCAPE_HTML, replacement_rule::{replacement_rule_part::{closure_replacement_rule_part::ClosureReplacementRuleReplacerPart, fixed_replacement_rule_part::FixedReplacementRuleReplacerPart, single_capture_group_replacement_rule_part::SingleCaptureGroupReplacementRuleReplacerPart, ReplacementRuleReplacerPart}, ReplacementRule}, CompilationRule}}, output_format::OutputFormat};


    #[test]
    fn bold_compiling() {

        // valid pattern with a valid text modifier
        let replacement_rule = ReplacementRule::new(StandardTextModifier::BoldStarVersion.modifier_pattern(), vec![
            Arc::new(FixedReplacementRuleReplacerPart::new(String::from("<strong>"))) as Arc<dyn ReplacementRuleReplacerPart>,
            Arc::new(ClosureReplacementRuleReplacerPart::new(Arc::new(|captures, compilable, _, _, _| {
                
                let capture1 = captures.get(1).unwrap();
                
                let slice = compilable.parts_slice(capture1.start(), capture1.end())?;

                Ok(CompilableText::new(slice))
            }))),
            Arc::new(FixedReplacementRuleReplacerPart::new(String::from("</strong>"))),
        ]);

        let text_to_compile = r"A piece of **bold text** and **bold text2**";
        let compilation_configuration = CompilationConfiguration::default();

        let compilable = CompilableText::new(
            vec![
                CompilableTextPart::new(
                    text_to_compile.to_string(),
                    CompilableTextPartType::Compilable { incompatible_modifiers: ModifiersBucket::None }
                )
        ]);
        
        let outcome = replacement_rule.compile(&compilable, &OutputFormat::Html, &compilation_configuration, CompilationConfigurationOverLay::default()).unwrap();

        assert_eq!(outcome.content(), r"<strong>bold text</strong><strong>bold text2</strong>");

        // without text modifier
        let text_to_compile = r"A piece of text without bold text";

        let compilable = CompilableText::new(
            vec![
                CompilableTextPart::new(
                    text_to_compile.to_string(),
                    CompilableTextPartType::Compilable { incompatible_modifiers: ModifiersBucket::None }
                )
        ]);

        let outcome = replacement_rule.compile(&compilable, &OutputFormat::Html, &compilation_configuration, CompilationConfigurationOverLay::default()).unwrap();

        assert_eq!(outcome.content(), r"");


    }

    #[test]
    fn input_with_fixed_parts() {
        let replacement_rule = ReplacementRule::new(StandardTextModifier::ItalicStarVersion.modifier_pattern(), vec![
            Arc::new(FixedReplacementRuleReplacerPart::new(String::from("<em>"))) as Arc<dyn ReplacementRuleReplacerPart>,
            Arc::new(SingleCaptureGroupReplacementRuleReplacerPart::new(1, ESCAPE_HTML.clone(), ModifiersBucket::None)),
            Arc::new(FixedReplacementRuleReplacerPart::new(String::from("</em>"))),
        ]);

        let compilation_configuration = CompilationConfiguration::default();

        // ==== case 1 ====
        let compilable = CompilableText::new(
            vec![
                CompilableTextPart::new(
                    String::from("*start "),
                    CompilableTextPartType::Compilable { incompatible_modifiers: ModifiersBucket::None }
                ),
                CompilableTextPart::new_fixed(String::from("<strong>")),
                CompilableTextPart::new_compilable(String::from("fixed"), ModifiersBucket::None),
                CompilableTextPart::new_fixed(String::from("</strong>")),
                CompilableTextPart::new(
                    String::from(" end*"),
                    CompilableTextPartType::Compilable { incompatible_modifiers: ModifiersBucket::None }
                ),
        ]);
        
        let outcome = replacement_rule.compile(&compilable, &OutputFormat::Html, &compilation_configuration, CompilationConfigurationOverLay::default()).unwrap();

        assert_eq!(outcome.content(), r"<em>start <strong>fixed</strong> end</em>");


        // ==== case 2 ====
        let compilable = CompilableText::new(
            vec![
                CompilableTextPart::new(
                    String::from("*start "),
                    CompilableTextPartType::Compilable { incompatible_modifiers: ModifiersBucket::None }
                ),
                CompilableTextPart::new_fixed(String::from("<strong>")),
                CompilableTextPart::new_compilable(String::from("fixed"), ModifiersBucket::None),
                CompilableTextPart::new_fixed(String::from("</strong>")),
                CompilableTextPart::new(
                    String::from("*"),
                    CompilableTextPartType::Compilable { incompatible_modifiers: ModifiersBucket::None }
                ),
        ]);
        
        let outcome = replacement_rule.compile(&compilable, &OutputFormat::Html, &compilation_configuration, CompilationConfigurationOverLay::default()).unwrap();

        assert_eq!(outcome.content(), r"<em>start <strong>fixed</strong></em>");


        // ==== case 3 ====
        let compilable = CompilableText::new(
            vec![
                CompilableTextPart::new(
                    String::from("*"),
                    CompilableTextPartType::Compilable { incompatible_modifiers: ModifiersBucket::None }
                ),
                CompilableTextPart::new_fixed(String::from("<strong>")),
                CompilableTextPart::new_compilable(String::from("fixed"), ModifiersBucket::None),
                CompilableTextPart::new_fixed(String::from("</strong>")),
                CompilableTextPart::new(
                    String::from(" end*"),
                    CompilableTextPartType::Compilable { incompatible_modifiers: ModifiersBucket::None }
                ),
        ]);
        
        let outcome = replacement_rule.compile(&compilable, &OutputFormat::Html, &compilation_configuration, CompilationConfigurationOverLay::default()).unwrap();

        assert_eq!(outcome.content(), r"<em><strong>fixed</strong> end</em>");

    }

    // #[test]
    // fn heading_parsing() {

    //     let codex = Codex::of_html(CodexConfiguration::default());

    //     let compilation_configuration = CompilationConfiguration::default();

    //     let parsing_rule = ReplacementRule::new(StandardHeading::HeadingGeneralExtendedVersion(6).modifier_pattern().clone(), vec![
    //         ReplacementRuleReplacerPart::new_fixed(String::from("<h6>")),
    //         ReplacementRuleReplacerPart::new_mutable(String::from("$1")),
    //         ReplacementRuleReplacerPart::new_fixed(String::from("</h6>")),
    //     ]);

    //     let text_to_parse = r"###### title 6";

    //     let compilable: Box<dyn Compilable> = Box::new(GenericCompilable::from(text_to_parse.to_string()));

    //     let parsed_text = parsing_rule.compile(&compilable, &OutputFormat::Html, &codex, &compilation_configuration, Arc::new(RwLock::new(CompilationConfigurationOverLay::default()))).unwrap();

    //     assert_eq!(parsed_text.content(), r"<h6>title 6</h6>");
    // }

    // #[test]
    // fn code_block() {

    //     let codex = Codex::of_html(CodexConfiguration::default());

    //     let compilation_configuration = CompilationConfiguration::default();

    //     let parsing_rule = ReplacementRule::new(StandardParagraphModifier::CodeBlock.modifier_pattern_with_paragraph_separator().clone(), vec![
    //         ReplacementRuleReplacerPart::new_fixed(String::from(r#"<pre><code class="language-$1 codeblock">"#)),
    //         ReplacementRuleReplacerPart::new_mutable(String::from("$2")),
    //         ReplacementRuleReplacerPart::new_fixed(String::from("</code></pre>")),
    //     ]);

    //     let text_to_parse = concat!(
    //         "\n\n",
    //         "```python\n\n",
    //         r#"print("hello world")"#,
    //         "\n\n```\n\n"
    //     );
        
    //     let compilable: Box<dyn Compilable> = Box::new(GenericCompilable::from(text_to_parse.to_string()));

    //     let parsed_text = parsing_rule.compile(&compilable, &OutputFormat::Html, &codex, &compilation_configuration, Arc::new(RwLock::new(CompilationConfigurationOverLay::default()))).unwrap();

    //     assert_eq!(parsed_text.content(), "<pre><code class=\"language-python codeblock\">print(\"hello world\")</code></pre>");
    // }

    
}