Files
wehub-resource-sync a9cd7750f4
CI / unit-test (push) Has been cancelled
CI / detect-changes (push) Has been cancelled
CI / build (push) Has been cancelled
Publish docs via GitHub Pages / Deploy docs (push) Has been cancelled
CI / test-harness (push) Has been cancelled
CI / generate-e2e-matrix (push) Has been cancelled
CI / e2e (push) Has been cancelled
CI / build-ui (push) Has been cancelled
Release Drafter / update_release_draft (push) Has been cancelled
UI v2 Integration CI / E2E (Integration) (push) Has been cancelled
UI v2 CI / Lint, Format & Test (push) Has been cancelled
UI v2 CI / E2E (Mocked) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:37:56 +08:00

94 lines
3.0 KiB
Python

import os
import re
import subprocess
import sys
def on_pre_build(env):
"""Fetch SDK README files before the build starts."""
script = os.path.join(os.path.dirname(os.path.abspath(__file__)), "scripts", "fetch-sdk-docs.py")
if os.path.exists(script):
print("Fetching SDK documentation from GitHub...")
subprocess.run([sys.executable, script], check=False)
def define_env(env):
"Hook function"
@env.macro
def insert_content(key = None):
key = key or env.page.title
filename = env.variables['extra']['additional_content'][key]
return include_file(filename)
@env.macro
def include_file(filename):
prefix = env.variables['config']['docs_dir']
full_filename = os.path.join(prefix, filename)
with open(full_filename, 'r') as f:
lines = f.readlines()
return ''.join(lines)
"""
def copy_markdown_images(tmpRoot, markdown):
# root = os.path.dirname(os.path.dirname(self.page.url))
root = self.page.url
paths = []
p = re.compile("!\[.*\]\((.*)\)")
it = p.finditer(markdown)
for match in it:
path = match.group(1)
paths.append(path)
destinationPath = os.path.realpath(self.config['base_path'] + "/" +
root + "/gen_/" + path)
if not os.path.isfile(destinationPath):
print("Copying image: " + path + " to " + destinationPath)
os.makedirs(os.path.dirname(destinationPath), exist_ok=True)
shutil.copyfile(tmpRoot + "/" + path, destinationPath)
for path in paths:
markdown = markdown.replace(path, "gen_/" + path)
return markdown
"""
@env.macro
def snippet(file_path, section_name, num_sections=1):
p = re.compile("^#+ ")
m = p.search(section_name)
if m:
section_level = m.span()[1] - 1
root = env.variables['config']['docs_dir']
full_path = os.path.join(root, file_path)
content = ""
with open(full_path, 'r') as myfile:
content = myfile.read()
p = re.compile("^" + section_name + "$", re.MULTILINE)
start = p.search(content)
start_span = start.span()
p = re.compile("^#{1," + str(section_level) + "} ", re.MULTILINE)
result = ""
all = [x for x in p.finditer(content[start_span[1]:])]
print (len(all))
if len(all) == 0 or (num_sections-1) >= len(all):
result = content[start_span[0]:]
else:
end = all[num_sections-1]
end_index = end.span()[0]
result = content[start_span[0]:end_index + start_span[1]]
# If there are any images, find them, copy them
# result = copy_markdown_images(root, result)
return result
else:
return "Heading reference beginning in # is required"