Skip to content

Commit 5364c63

Browse files
Added cfdoc_codeblock_resolver to pre-process scripts
Ticket: ENT-12752 cfdoc_codeblock_resolver searches for code block headers that include specific flags (file=..., noindent, noparse, noeval), and moves those findings to a new line inside square brackets. If any changes are made, the file is updated. Signed-off-by: Ihor Aleksandrychiev <ihor.aleksandrychiev@northern.tech>
1 parent dd22b8c commit 5364c63

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import re
2+
3+
def run(config):
4+
markdown_files = config["markdown_files"]
5+
for file in markdown_files:
6+
process(file)
7+
8+
def process(file_path):
9+
"""
10+
Reads a markdown file, searches for code block headers that include specific flags
11+
(file=..., noindent, noparse, noeval), and moves file=... finding to a new line inside
12+
square brackets and ignores the rest flags as we don't use them in the markdown rendering.
13+
"""
14+
try:
15+
# Read the file
16+
with open(file_path, 'r', encoding='utf-8') as file:
17+
content = file.read()
18+
19+
# Find and replace codeblocks
20+
transformed_content = transform_codeblocks(content)
21+
22+
# Write file if content was changed
23+
if transformed_content != content:
24+
with open(file_path, 'w', encoding='utf-8') as file:
25+
file.write(transformed_content)
26+
27+
except Exception as e:
28+
print(f"Error processing file {file_path}: {str(e)}")
29+
raise
30+
31+
def transform_codeblocks(content):
32+
"""
33+
Find code block headers that include file=... and optional flags (noindent, noparse, noeval),
34+
remove all flags except file=... because we don't process them in docs,
35+
and move file=... to a new line inside square brackets.
36+
"""
37+
38+
pattern = re.compile(
39+
r'^```([a-zA-Z0-9_-]+)' # language
40+
r'((?:\s+(file=[^\s]+|noindent|noparse|noeval))+)\s*$', # flags
41+
re.MULTILINE
42+
)
43+
44+
def replacer(match):
45+
language = match.group(1)
46+
all_flags = match.group(2)
47+
48+
# Extract the file=... part
49+
file_flag_match = re.search(r'file=[^\s]+', all_flags)
50+
if file_flag_match:
51+
file_flag = file_flag_match.group()
52+
return f"```{language}\n[{file_flag}]"
53+
else:
54+
return f"```{language}"
55+
56+
return pattern.sub(replacer, content)

generator/_scripts/cfdoc_preprocess.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import cfdoc_environment as environment
2626
import cfdoc_metadata as metadata
2727
import cfdoc_linkresolver as linkresolver
28+
import cfdoc_codeblock_resolver as codeblock_resolver
2829
import cfdoc_macros as macros
2930
import cfdoc_printsource as printsource
3031
import cfdoc_git as git
@@ -52,6 +53,14 @@
5253
print(sys.exc_info())
5354
exit(3)
5455

56+
try:
57+
codeblock_resolver.run(config)
58+
except:
59+
print("cfdoc_preprocess: Fatal error processing codeblocks")
60+
sys.stdout.write(" Exception: ")
61+
print(sys.exc_info())
62+
exit(3)
63+
5564
try:
5665
macros.run(config)
5766
except:

0 commit comments

Comments
 (0)