Skip to content

Generate Documentation from code #127

Description

@LuchoBazz
import os
import re

# Directories
SNIPPETS_DIR = 'brute_force'  # Adjust this if you have a different root directory for snippets
DOCS_DIR = os.path.join('website', 'docs')  # Directory where the Markdown files are located

# Regular expression pattern to find ${{path_to_file}}
SNIPPET_PATTERN = r'\$\{\{(.*?)\}\}'

def replace_snippets_in_file(file_path):
    """Replaces snippet references in a Markdown file with the actual code content."""
    with open(file_path, 'r', encoding='utf-8') as file:
        content = file.read()

    # Find all snippet references in the file
    snippets = re.findall(SNIPPET_PATTERN, content)
    
    for snippet_path in snippets:
        snippet_full_path = os.path.join(SNIPPETS_DIR, snippet_path)
        if os.path.exists(snippet_full_path):
            with open(snippet_full_path, 'r', encoding='utf-8') as snippet_file:
                snippet_code = snippet_file.read()
            # Replace the reference with the actual code content
            content = content.replace(f'${{{{{snippet_path}}}}}', f'```cpp\n{snippet_code}\n```')
        else:
            print(f"Warning: The file {snippet_full_path} does not exist.")
    
    # Save the modified content back to the same file
    with open(file_path, 'w', encoding='utf-8') as file:
        file.write(content)

def replace_snippets_in_docs():
    """Processes all Markdown files within DOCS_DIR and replaces snippet references."""
    for root, dirs, files in os.walk(DOCS_DIR):
        for file_name in files:
            if file_name.endswith('.md'):
                file_path = os.path.join(root, file_name)
                replace_snippets_in_file(file_path)
                print(f"Processed: {file_path}")

if __name__ == '__main__':
    replace_snippets_in_docs()
"scripts": {
  "prebuild": "python replace_snippets.py",  // This runs before the build process
  "build": "docusaurus build",               // This is the main build command
  "postbuild": "python replace_snippets.py'"     // This runs after the build process
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions