markdown-code-runner 0.1.0

Automatically update Markdown files with code block output
Documentation
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
//! State machine for processing Markdown files.

use anyhow::{bail, Result};
use once_cell::sync::Lazy;
use regex::Regex;
use std::collections::HashMap;
use std::path::PathBuf;

use crate::executor::{execute_code, Language};
use crate::markers::{
    get_indent, is_code_backticks_end, is_code_backticks_start, is_code_comment_bash_start,
    is_code_comment_end, is_code_comment_python_start, is_output_end, is_output_start, is_skip,
    remove_md_comment, WARNING,
};

/// Pattern to extract key=value options from a backtick line.
static OPTION_PATTERN: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"(?P<key>\w+)=(?P<value>\S+)").unwrap());

/// Current section being processed.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Section {
    Normal,
    Output,
    CodeCommentPython,
    CodeCommentBash,
    CodeBackticks,
}

/// Backtick options extracted from the code fence line.
#[derive(Debug, Clone, Default)]
pub struct BacktickOptions {
    pub language: String,
    pub filename: Option<PathBuf>,
    pub other: HashMap<String, String>,
}

impl BacktickOptions {
    /// Extract backtick options from a code fence line.
    pub fn from_line(line: &str) -> Self {
        let mut options = BacktickOptions::default();

        // Extract language
        static LANG_PATTERN: Lazy<Regex> =
            Lazy::new(|| Regex::new(r"```(?P<language>\w+)").unwrap());

        if let Some(caps) = LANG_PATTERN.captures(line) {
            options.language = caps["language"].to_string();
        }

        // Extract key=value options after markdown-code-runner
        if line.contains("markdown-code-runner") {
            for caps in OPTION_PATTERN.captures_iter(line) {
                let key = caps["key"].to_string();
                let value = caps["value"].to_string();
                if key == "filename" {
                    options.filename = Some(PathBuf::from(value));
                } else {
                    options.other.insert(key, value);
                }
            }
        }

        options
    }
}

/// Processing state for the Markdown file.
pub struct ProcessingState {
    /// Current section being processed.
    pub section: Section,
    /// Code lines collected from the current code block.
    pub code: Vec<String>,
    /// Original output lines (preserved when skipping).
    pub original_output: Vec<String>,
    /// Whether to skip the next code block.
    pub skip_code_block: bool,
    /// Output from the last executed code block.
    pub output: Option<Vec<String>>,
    /// New lines being built.
    pub new_lines: Vec<String>,
    /// Options from the current backtick code block.
    pub backtick_options: BacktickOptions,
    /// Whether to standardize backticks (remove markdown-code-runner).
    pub backtick_standardize: bool,
    /// Indentation of the current code block.
    pub indent: String,
    /// Verbose mode for debugging.
    pub verbose: bool,
    /// All Python code blocks seen so far (for context sharing).
    python_blocks: Vec<Vec<String>>,
}

impl ProcessingState {
    /// Create a new processing state.
    pub fn new(backtick_standardize: bool, verbose: bool) -> Self {
        Self {
            section: Section::Normal,
            code: Vec::new(),
            original_output: Vec::new(),
            skip_code_block: false,
            output: None,
            new_lines: Vec::new(),
            backtick_options: BacktickOptions::default(),
            backtick_standardize,
            indent: String::new(),
            verbose,
            python_blocks: Vec::new(),
        }
    }

    /// Process a single line of the Markdown file.
    pub fn process_line(&mut self, line: &str) -> Result<()> {
        if is_skip(line) {
            self.skip_code_block = true;
            self.new_lines.push(line.to_string());
        } else if is_output_start(line).is_some() {
            self.process_output_start(line);
        } else if is_output_end(line) {
            self.process_output_end(line);
        } else {
            match self.section {
                Section::CodeCommentPython | Section::CodeCommentBash => {
                    self.process_comment_code(line)?;
                }
                Section::CodeBackticks => {
                    self.process_backtick_code(line)?;
                }
                Section::Output => {
                    self.original_output.push(line.to_string());
                }
                Section::Normal => {
                    let processed_line = self.process_start_markers(line);
                    self.new_lines
                        .push(processed_line.unwrap_or_else(|| line.to_string()));
                    return Ok(());
                }
            }
            if self.section != Section::Output {
                self.new_lines.push(line.to_string());
            }
        }
        Ok(())
    }

    /// Process start markers (code block starts).
    fn process_start_markers(&mut self, line: &str) -> Option<String> {
        // Check for Python code comment start
        if is_code_comment_python_start(line).is_some() {
            self.output = None;
            self.section = Section::CodeCommentPython;
            self.indent = get_indent(line);
            return Some(line.to_string());
        }

        // Check for Bash code comment start
        if is_code_comment_bash_start(line).is_some() {
            self.output = None;
            self.section = Section::CodeCommentBash;
            self.indent = get_indent(line);
            return Some(line.to_string());
        }

        // Check for backtick code block start
        if let Some(caps) = is_code_backticks_start(line) {
            self.output = None;
            self.backtick_options = BacktickOptions::from_line(line);
            self.section = Section::CodeBackticks;
            self.indent = caps.name("spaces").map_or("", |m| m.as_str()).to_string();

            // Standardize backticks if needed
            if self.backtick_standardize && line.contains("markdown-code-runner") {
                static STRIP_PATTERN: Lazy<Regex> =
                    Lazy::new(|| Regex::new(r"\s+markdown-code-runner.*").unwrap());
                return Some(STRIP_PATTERN.replace(line, "").to_string());
            }
            return Some(line.to_string());
        }

        None
    }

    /// Process output start marker.
    fn process_output_start(&mut self, line: &str) {
        self.section = Section::Output;
        if !self.skip_code_block {
            // Get the output, panicking if it's None (this is a programming error)
            let output = self.output.as_ref().unwrap_or_else(|| {
                panic!("Output must be set before OUTPUT:START, line: {}", line)
            });
            let indent = get_indent(line);

            // Add the output start marker
            self.new_lines.push(line.to_string());

            // Add the warning comment with indentation
            self.new_lines.push(format!("{}{}", indent, WARNING));

            // Add each output line with proper indentation and trailing whitespace trimmed
            for ol in output {
                let trimmed = ol.trim_end();
                if trimmed.is_empty() {
                    self.new_lines.push(String::new());
                } else {
                    self.new_lines.push(format!("{}{}", indent, trimmed));
                }
            }
        } else {
            self.original_output.push(line.to_string());
        }
    }

    /// Process output end marker.
    fn process_output_end(&mut self, line: &str) {
        self.section = Section::Normal;
        if self.skip_code_block {
            self.new_lines.append(&mut self.original_output);
            self.skip_code_block = false;
        }
        self.new_lines.push(line.to_string());
        self.original_output.clear();
        self.output = None;
    }

    /// Strip the code block's indentation prefix from a line.
    fn strip_indent(&self, line: &str) -> String {
        if !self.indent.is_empty() && line.starts_with(&self.indent) {
            line[self.indent.len()..].to_string()
        } else {
            line.to_string()
        }
    }

    /// Process code inside a comment block.
    fn process_comment_code(&mut self, line: &str) -> Result<()> {
        if is_code_comment_end(line) {
            if !self.skip_code_block {
                let language = match self.section {
                    Section::CodeCommentPython => Language::Python,
                    Section::CodeCommentBash => Language::Bash,
                    _ => unreachable!(),
                };
                self.execute_current_block(language)?;
            }
            self.section = Section::Normal;
            self.code.clear();
            self.backtick_options = BacktickOptions::default();
            self.indent.clear();
        } else {
            // Remove markdown comment and add to code
            if let Some(code_line) = remove_md_comment(line) {
                self.code.push(code_line);
            }
        }
        Ok(())
    }

    /// Process code inside a backtick block.
    fn process_backtick_code(&mut self, line: &str) -> Result<()> {
        if is_code_backticks_end(line) {
            if !self.skip_code_block {
                let language = Language::parse(&self.backtick_options.language);
                // Clone the filename to avoid borrow issues
                let output_file = self.backtick_options.filename.clone();

                if language.is_none() && output_file.is_none() {
                    bail!("Specify 'output_file' for non-Python/Bash languages.");
                }

                if let Some(lang) = language {
                    self.execute_current_block_with_file(lang, output_file.as_deref())?;
                } else {
                    // Write to file for non-executable languages
                    let code = self.code.clone();
                    let verbose = self.verbose;
                    self.output = Some(execute_code(
                        &code,
                        Language::Python,
                        output_file.as_deref(),
                        verbose,
                    )?);
                }
            }
            self.section = Section::Normal;
            self.code.clear();
            self.backtick_options = BacktickOptions::default();
            self.indent.clear();
        } else {
            // Strip the block indentation from the code line
            let stripped = self.strip_indent(line);
            self.code.push(stripped);
        }
        Ok(())
    }

    /// Execute the current code block for Python with context sharing.
    fn execute_current_block(&mut self, language: Language) -> Result<()> {
        self.execute_current_block_with_file(language, None)
    }

    /// Execute the current code block with optional output file.
    fn execute_current_block_with_file(
        &mut self,
        language: Language,
        output_file: Option<&std::path::Path>,
    ) -> Result<()> {
        if output_file.is_some() {
            // Write to file, no execution
            self.output = Some(execute_code(
                &self.code,
                language,
                output_file,
                self.verbose,
            )?);
        } else if language == Language::Python {
            // For Python, we need to share context between blocks
            // Add current block to the list of Python blocks
            self.python_blocks.push(self.code.clone());

            // We need to capture only the output from the current block
            // The Python version uses exec() with a shared context, but since we're
            // shelling out, we need to use a different approach.
            //
            // Strategy: Add a marker before the current block's output
            let marker = format!("__MCR_MARKER_{}__", self.python_blocks.len());
            let mut code_with_marker: Vec<String> = Vec::new();

            // Add all previous blocks
            for (i, block) in self.python_blocks.iter().enumerate() {
                if i == self.python_blocks.len() - 1 {
                    // Current block - add marker before it
                    code_with_marker.push(format!("print('{}')", marker));
                }
                code_with_marker.extend(block.iter().cloned());
            }

            let output = execute_code(&code_with_marker, Language::Python, None, self.verbose)?;

            // Extract only the output after our marker
            let mut in_current_block = false;
            let mut current_output: Vec<String> = Vec::new();
            for line in output {
                if line == marker {
                    in_current_block = true;
                } else if in_current_block {
                    current_output.push(line);
                }
            }

            self.output = Some(current_output);
        } else {
            // Bash doesn't need context sharing
            self.output = Some(execute_code(&self.code, language, None, self.verbose)?);
        }
        Ok(())
    }
}

/// Process markdown content and return the modified lines.
pub fn process_markdown(
    content: &[String],
    verbose: bool,
    backtick_standardize: bool,
    execute: bool,
) -> Result<Vec<String>> {
    if !execute {
        return Ok(content.to_vec());
    }

    let mut state = ProcessingState::new(backtick_standardize, verbose);

    for (i, line) in content.iter().enumerate() {
        if verbose {
            eprintln!("\x1b[1mline {:4}\x1b[0m: {}", i, line);
        }
        state.process_line(line)?;
    }

    Ok(state.new_lines)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_backtick_options_from_line() {
        let opts = BacktickOptions::from_line("```python markdown-code-runner filename=test.py");
        assert_eq!(opts.language, "python");
        assert_eq!(opts.filename, Some(PathBuf::from("test.py")));

        let opts = BacktickOptions::from_line("```bash markdown-code-runner");
        assert_eq!(opts.language, "bash");
        assert_eq!(opts.filename, None);

        let opts = BacktickOptions::from_line("```python");
        assert_eq!(opts.language, "python");
        assert_eq!(opts.filename, None);
    }

    #[test]
    fn test_process_simple_python() {
        let input = vec![
            "Some text".to_string(),
            "```python markdown-code-runner".to_string(),
            "print('Hello, world!')".to_string(),
            "```".to_string(),
            "<!-- OUTPUT:START -->".to_string(),
            "old output".to_string(),
            "<!-- OUTPUT:END -->".to_string(),
        ];

        let output = process_markdown(&input, false, false, true).unwrap();
        assert!(output.contains(&"Hello, world!".to_string()));
        assert!(!output.contains(&"old output".to_string()));
    }

    #[test]
    fn test_process_with_skip() {
        let input = vec![
            "<!-- CODE:SKIP -->".to_string(),
            "```python markdown-code-runner".to_string(),
            "print('Hello, world!')".to_string(),
            "```".to_string(),
            "<!-- OUTPUT:START -->".to_string(),
            "old output".to_string(),
            "<!-- OUTPUT:END -->".to_string(),
        ];

        let output = process_markdown(&input, false, false, true).unwrap();
        assert!(output.contains(&"old output".to_string()));
    }

    #[test]
    fn test_process_execute_false() {
        let input = vec![
            "```python markdown-code-runner".to_string(),
            "print('Hello')".to_string(),
            "```".to_string(),
        ];

        let output = process_markdown(&input, false, false, false).unwrap();
        assert_eq!(input, output);
    }
}