#markdown-generator #generator #markdown #pattern #rust

bin+lib prk_mdgen

Generate and execute Rust projects from annotated Markdown files

10 releases

Uses new Rust 2024

new 0.1.9 Apr 21, 2025
0.1.8 Apr 21, 2025

#1392 in Command line utilities

Download history 997/week @ 2025-04-14

997 downloads per month

MIT license

42KB
902 lines

๐Ÿฆ€ Markdown to Rust Project Generator

This tool parses specially formatted Markdown files to generate fully structured Rust projects โ€” and even executes them (via cargo run or cargo test depending on the code)!

It can also extract a real Rust project from disk into a Markdown spec file using the extract command!


๐Ÿ“ฆ What It Does

This CLI tool supports two main workflows:

๐Ÿ›  1. Project Generation

  • Scans all Markdown (.md) files in the current directory
  • Extracts embedded Rust code annotated with file paths
  • Generates full Rust projects (files, folders, Cargo.toml, etc.)
  • Builds and runs main.rs projects using cargo run
  • Tests lib.rs projects using cargo test
  • Saves execution output to run_output.log or test_output.log

๐Ÿ“ค 2. Project Extraction

  • Takes an existing Rust codebase and generates a single Markdown file
  • All .rs files are converted to annotated code blocks with file paths
  • Supports .gitignore and additional --skip rules
  • Great for documentation, LLM prompts, or reproducible specs

๐Ÿง  Supported Markdown Formats

You can annotate code blocks in your Markdown using several supported patterns:

โœ… Supported Patterns

  1. XML-style tag

    <code path="src/main.rs">
    fn main() {
        println!("Hello!");
    }
    </code>
    
  2. Markdown heading

    ### src/lib.rs
    ```rust
    pub fn add(a: i32, b: i32) -> i32 {
        a + b
    }
    
    
    
  3. Delimiter marker

    ======== src/utils.rs ========
    ```rust
    pub fn double(x: i32) -> i32 {
        x * 2
    }
    
    
    
  4. Raw comment before code

    // file: src/math.rs
    pub fn square(x: i32) -> i32 {
        x * x
    }
    
  5. File Fence

    ### <file> src/main.rs </file>
    ```rust
    use std::sync::Arc;
    use std::error::Error;
    use std::future::Future;
    
    
    

๐Ÿš€ Getting Started

๐Ÿ›  Prerequisites

  • Rust & Cargo installed (https://rustup.rs)

๐Ÿ“ฅ Installation

cargo install prk_mdgen

๐Ÿงช Usage

๐Ÿ” Markdown โ†’ Rust (Default Mode)

prk_mdgen

Generate Rust projects from all .md files in the current directory.

๐Ÿ“ค Extract Rust โ†’ Markdown

prk_mdgen extract -o ./docs --skip target,.git,tests

This will scan the current Rust project and generate docs/codebase.md with annotated code blocks for each file.


๐Ÿ”ง Additional CLI Options

USAGE:
    prk_mdgen [OPTIONS]

OPTIONS:
    -o, --output-dir <DIR>     Output directory [default: output]
    -p, --pattern <PATTERN>    Force a specific pattern (code-tag, hash, delimiter, raw, file-code, file-fence)
    -c, --command <COMMAND>    sample | prompt | extract
    -e, --execute              Run `cargo run` or `cargo test` on generated projects
        --skip <ITEMS>         Comma-separated list of files or folders to skip
        --project-type <TYPE>  (Optional) Language hint during extraction (e.g. rust, node, flutter)

๐Ÿ” Example Workflows

# Generate a prompt template
prk_mdgen prompt

# Generate sample Markdown to test parsing
prk_mdgen sample

# Extract an existing Rust project to Markdown
prk_mdgen extract -o ./docs --skip target,.git

# Generate projects from Markdown and run them
prk_mdgen -o ./output -e

๐Ÿ“‚ Output Structure

When generating a project from Markdown:

output/
โ””โ”€โ”€ my_project/
    โ”œโ”€โ”€ Cargo.toml
    โ”œโ”€โ”€ src/
    โ”‚   โ””โ”€โ”€ main.rs
    โ””โ”€โ”€ run_output.log   # if main.rs exists

When extracting a project to Markdown:

output/
โ””โ”€โ”€ codebase.md   # contains annotated code blocks for each source file

๐Ÿงช Execution Behavior

  • If src/main.rs is present: runs cargo run, output is saved to run_output.log
  • If src/lib.rs is present: runs cargo test, output is saved to test_output.log

๐Ÿ“š Development

To test parsing or generation logic:

prk_mdgen sample

To add new parsing formats, see parser.rs and extend the MdPatternType enum and detection logic.


๐Ÿง‘โ€๐Ÿ’ป Contributing

Pull requests welcome! Please:

  • Add a Markdown test case
  • Use one or more supported code block patterns
  • Run cargo fmt before committing

๐Ÿชช License

MIT


โค๏ธ Made with Rust

This tool is built with love in Rust using:

  • clap for CLI parsing
  • rayon for parallel file processing

Dependencies

~6โ€“15MB
~183K SLoC