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
5 changes: 3 additions & 2 deletions .github/workflows/formatting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ jobs:
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install CFEngine CLI
- name: Install tools
run: |
pipx install cfengine
pipx install cfengine black
- name: Run formatting command to (hopefully not) make changes
run: |
cfengine dev docs-format
black .
- name: Check output.log file for warnings
run: |
! grep WARNING output.log
Expand Down
31 changes: 18 additions & 13 deletions generator/_scripts/cfdoc_images_path_resolver.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,49 @@
import re
import os


def run(config):
""" Fixes images paths
The markdown files other than _index.markdown need img tag src values adjusted if they are relative.
They need to reference the parent directory with ../ prefixed to the relative location of the image file.
"""Fixes images paths
The markdown files other than _index.markdown need img tag src values adjusted if they are relative.
They need to reference the parent directory with ../ prefixed to the relative location of the image file.
"""
markdown_files = config["markdown_files"]

for file in markdown_files:
process(file)


def load_references(file_path):
try:
with open(file_path, 'r', encoding='utf-8') as f:
with open(file_path, "r", encoding="utf-8") as f:
return f.read()
except FileNotFoundError:
return ""


def process(file_path):
"""Process a markdown file to fix image paths"""
try:
with open(file_path, 'r', encoding='utf-8') as f:
with open(file_path, "r", encoding="utf-8") as f:
content = f.read()

is_index_file = os.path.basename(file_path) == "_index.markdown"

if is_index_file:
# For _index.markdown files, leave image paths as is. As they are on the same level
return

# Pattern to match img src with relative paths that need fixing
# skips ../, /, or http
img_pattern = r'<img\s+([^>]*?)src="(?!\.\.\/|\/|https?:\/\/)([^"]+?)"([^>]*?)>'

# Replace with ../ added to the path
modified_content = re.sub(img_pattern, r'<img \1src="../\2"\3>', content, flags=re.IGNORECASE);
modified_content = re.sub(
img_pattern, r'<img \1src="../\2"\3>', content, flags=re.IGNORECASE
)
if modified_content != content:
with open(file_path, 'w', encoding='utf-8') as f:
with open(file_path, "w", encoding="utf-8") as f:
f.write(modified_content)

except Exception as e:
print(f"Error processing {file_path}: {e}")
2 changes: 2 additions & 0 deletions generator/_scripts/cfdoc_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import os
import json


def run(config):
config["syntax_path"] = config["project_directory"] + "/_generated/syntax_map.json"
config["syntax_map"] = json.load(open(config["syntax_path"], "r"))
Expand All @@ -31,6 +32,7 @@ def run(config):
for file in markdown_files:
processMetaData(file, config)


# parse meta data lines, remove existing header for later reconstruction
def parseHeader(lines):
header = {}
Expand Down
5 changes: 4 additions & 1 deletion generator/_scripts/cfdoc_patch_header_nav.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ def patch(current_branch):
for branch in data["docs"]:
print(
'<li><a href="%s">%s</a></li>'
% ("../../.." + branch["Link"], branch["Title"].replace("Version ", "")),
% (
"../../.." + branch["Link"],
branch["Title"].replace("Version ", ""),
),
file=f,
)
with open("_includes/lts_versions_list.html", "w") as f:
Expand Down
60 changes: 37 additions & 23 deletions generator/_scripts/cfdoc_references_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,81 +2,95 @@
import os
import sys


def load_references(references_file):
"""Parse the _references.md file and return a dictionary of references."""
references = {}
content = ""

refs_path = os.path.join(os.environ.get("WRKDIR"), references_file)
try:
if os.path.exists(refs_path):
with open(refs_path, 'r', encoding='utf-8') as file:
with open(refs_path, "r", encoding="utf-8") as file:
content = file.read()
else:
sys.stderr.write(f"Warning: References file {refs_path} not found. No references will be added.")
sys.stderr.write(
f"Warning: References file {refs_path} not found. No references will be added."
)
return ""
except Exception as e:
sys.stderr.write(f"Error reading references file {refs_path}: {str(e)}")
return ""

# Pattern to match reference definitions: [ref]: url "title"
pattern = r'\[(.*?)\]:\s+(.*?)(?:\s+"(.*?)")?\s*$'

for match in re.finditer(pattern, content, re.MULTILINE):
ref, url, title = match.groups()
if title is None:
title = ""
references[ref] = (url, title)

return references


def process(file_path, references):
"""Process a markdown file and replace reference links with direct links."""
with open(file_path, 'r', encoding='utf-8') as f:

with open(file_path, "r", encoding="utf-8") as f:
content = f.read()

# Pattern to match reference links: [`text`][reference]
pattern = r'\[(.*?)\]\[(.*?)\]'
pattern = r"\[(.*?)\]\[(.*?)\]"

def replace_link(match):
text, ref = match.groups()
ref = ref or text # if ref is empty use text as ref to support cases like [ref][]

ref = (
ref or text
) # if ref is empty use text as ref to support cases like [ref][]

if ref in references:
url, title = references[ref]
if title:
return f'[{text}]({url} "{title}")'
else:
return f'[{text}]({url})'
return f"[{text}]({url})"
else:
sys.stderr.write(f"References {ref} is not found in the _references.md. File: {file_path}")
sys.stderr.write(
f"References {ref} is not found in the _references.md. File: {file_path}"
)
return match.group(0)

new_content = re.sub(pattern, replace_link, content)

# finds functions except ones already processed inside []
functions_pattern = r'(?<!\[)\`([^\s]*?)\(\)\`(?!\])'
functions_pattern = r"(?<!\[)\`([^\s]*?)\(\)\`(?!\])"

def replace_function_link(match):
ref = match.group(1)
text = f'{ref}()'
text = f"{ref}()"
if ref in references:
url, title = references[ref]
if title:
return f'[{text}]({url} "{title}")'
else:
return f'[{text}]({url})'
return f"[{text}]({url})"
else:
sys.stderr.write(f"References {ref} is not found in the _references.md. File: {file_path}")
sys.stderr.write(
f"References {ref} is not found in the _references.md. File: {file_path}"
)
return match.group(0)

new_content = re.sub(functions_pattern, replace_function_link, new_content)
with open(file_path, 'w', encoding='utf-8') as f:

with open(file_path, "w", encoding="utf-8") as f:
f.write(new_content)


def run(config):
"""Replaces [text][reference] with markdown links retrieved from _references.md"""
markdown_files = config["markdown_files"]
references = load_references("documentation/generator/_references.md")

for file in markdown_files:
process(file, references)
16 changes: 10 additions & 6 deletions generator/_scripts/cfdoc_shortcodes_resolver.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
import re


def process(file):
try:
# Read the file content
with open(file, 'r', encoding='utf-8') as f:
with open(file, "r", encoding="utf-8") as f:
content = f.read()

# Replace the pattern {{%anything}} with {{% print `{{%$1}}` %}}
# The .*? ensures a non-greedy match for the content between the braces
updated_content = re.sub(r'\{\{%([^%]*?)\}\}', r'{{% print `{{%\1}}` %}}', content)

updated_content = re.sub(
r"\{\{%([^%]*?)\}\}", r"{{% print `{{%\1}}` %}}", content
)

# Write the updated content back to the file
with open(file, 'w', encoding='utf-8') as f:
with open(file, "w", encoding="utf-8") as f:
f.write(updated_content)

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


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