Skip to content

Allow exclusions when extracting example code #296

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion docs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ project(msft_proxy_docs)

find_package(Python3 REQUIRED COMPONENTS Interpreter)

file(GLOB_RECURSE DOC_FILES "*.md")
set(EXTRACTION_SCRIPT ${CMAKE_SOURCE_DIR}/tools/extract_example_code_from_docs.py)
set(EXAMPLES_DIR ${CMAKE_BINARY_DIR}/examples_from_docs)
file(MAKE_DIRECTORY "${EXAMPLES_DIR}")
Expand Down
59 changes: 40 additions & 19 deletions tools/extract_example_code_from_docs.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,63 @@
import os
import re
import sys

def extract_cpp_code(md_path, cpp_path):
with open(md_path, 'r', encoding='utf-8') as f:
from typing import Set

def extract_cpp_code(input_dir: str, output_dir: str, md_rel_path: str) -> None:
"""
Extract every C++ code block that immediately follows a '## Example'
header in *input_dir/md_rel_path*.

• If exactly one block is found, create:
example_<rel_path_no_ext>.cpp
• If multiple blocks are found, create:
example_<rel_path_no_ext>_1.cpp, _2.cpp, …

Directory separators in *md_rel_path* are replaced by '_' so the output
lives flat inside *output_dir*.
"""
md_path = os.path.join(input_dir, md_rel_path)
with open(md_path, "r", encoding="utf-8") as f:
content = f.read()

pattern = r'## Example\r?\n\r?\n```cpp\r?\n(.*?)\r?\n```'
code_blocks = re.findall(pattern, content, re.DOTALL)

if len(code_blocks) == 0:
return # No match, skip
elif len(code_blocks) > 1:
raise ValueError(f"File '{md_path}' contains more than one '## Example' C++ code block.")

cpp_code = code_blocks[0]
header = f"// This file was auto-generated from: {md_path}\n// Do not edit this file manually.\n\n"
rel_base = os.path.splitext(md_rel_path)[0].replace(os.sep, '_')
if len(code_blocks) == 1:
targets = [os.path.join(output_dir, f"example_{rel_base}.cpp")]
else:
targets = [
os.path.join(output_dir, f"example_{rel_base}_{i}.cpp")
for i in range(1, len(code_blocks) + 1)
]

with open(cpp_path, 'w', encoding='utf-8') as out:
out.write(header)
out.write(cpp_code)
header = f"// This file was auto-generated from: {md_path}\n// Do not edit this file manually.\n\n"
for cpp_path, cpp_code in zip(targets, code_blocks):
with open(cpp_path, "w", encoding="utf-8") as out:
out.write(header)
out.write(cpp_code)

def main():
if len(sys.argv) != 3:
print("Usage: python extract_example_code_from_docs.py <input_dir> <output_dir>")
if len(sys.argv) < 3:
print("Usage: python extract_example_code_from_docs.py <input_dir> <output_dir> [exclusion ...]")
sys.exit(1)

input_dir = sys.argv[1]
output_dir = sys.argv[2]
exclusions: Set[str] = {os.path.normpath(p) for p in sys.argv[3:]}

for root, _, files in os.walk(input_dir):
for file in files:
if file.endswith('.md'):
md_path = os.path.join(root, file)
rel_path = os.path.relpath(md_path, input_dir)
rel_base = os.path.splitext(rel_path)[0].replace(os.sep, '_')
cpp_path = os.path.join(output_dir, f"example_{rel_base}.cpp")
extract_cpp_code(md_path, cpp_path)
if not file.endswith('.md'):
continue
md_path = os.path.join(root, file)
rel_path = os.path.relpath(md_path, input_dir)
if rel_path in exclusions:
continue
extract_cpp_code(input_dir, output_dir, rel_path)

if __name__ == '__main__':
main()