Skip to content
Merged
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
2 changes: 1 addition & 1 deletion generator/_scripts/_run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ find $WRKDIR/documentation/hugo/content -name "*.include.markdown" -type f -dele
cp -rn $WRKDIR/nt-docs/* $WRKDIR/documentation/hugo/
cd $WRKDIR/documentation/hugo
npm ci
npm run build:all
npm run build:all || exit 2

cd $WRKDIR/documentation/generator
mkdir -p $WRKDIR/documentation/generator/_site/assets/searchIndex
Expand Down
62 changes: 62 additions & 0 deletions generator/_scripts/cfdoc_codeblock_resolver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import re


def run(config):
markdown_files = config["markdown_files"]
for file in markdown_files:
process(file)


def process(file_path):
"""
Reads a markdown file, searches for code block that have one-word flags
and replace with flag=value. Otherwise Hugo will fail to parse markdown
with error `failed to parse Markdown attributes; you may need to quote the values`
"""
try:
# Read the file
with open(file_path, "r", encoding="utf-8") as file:
content = file.read()

# Find and replace codeblocks
transformed_content = transform_codeblocks(content)

# Write file if content was changed
if transformed_content != content:
with open(file_path, "w", encoding="utf-8") as file:
file.write(transformed_content)

except Exception as e:
print(f"Error processing file {file_path}: {str(e)}")
raise


def transform_codeblocks(content):
pattern = re.compile(
r"^\s*```([a-zA-Z0-9_-]+)" # language
r"[ \t]+\{([^}]*)\}\s*$", # flags
re.MULTILINE,
Comment on lines +36 to +38
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aleksandrychev I don't think these can be multiline?

)

def replacer(match):
language = match.group(1)
all_flags = match.group(2).strip()

if not all_flags:
return f"```{language}"

flag_parts = all_flags.split()
transformed_flags = []

for flag in flag_parts:
if "=" in flag:
# Already in key=value format, keep as is
transformed_flags.append(flag)
else:
# One-word flag, transform to flag=""
transformed_flags.append(f'{flag}=""')

flags_str = " ".join(transformed_flags)
return f"```{language} {{{flags_str}}}"

return pattern.sub(replacer, content)
2 changes: 2 additions & 0 deletions generator/_scripts/cfdoc_preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import cfdoc_references_resolver as references_resolver
import cfdoc_shortcodes_resolver as shortcodes_resolver
import cfdoc_images_path_resolver as images_path_resolver
import cfdoc_codeblock_resolver as codeblock_resolver
import sys
import os

Expand All @@ -48,3 +49,4 @@
references_resolver.run(config)
shortcodes_resolver.run(config)
images_path_resolver.run(config)
codeblock_resolver.run(config)