Skip to content

Generate Documentation from code #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
LuchoBazz opened this issue Oct 8, 2024 · 0 comments
Open

Generate Documentation from code #127

LuchoBazz opened this issue Oct 8, 2024 · 0 comments

Comments

@LuchoBazz
Copy link
Owner

LuchoBazz commented Oct 8, 2024

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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant