Add buttons for editing or adding header and footer

This commit is contained in:
Igor Pecovnik
2025-05-10 18:03:59 +02:00
committed by Igor
parent dee17650a9
commit 9aff3edd94

View File

@@ -27,54 +27,40 @@ def extract_module_options_from_sh_files(directory):
return module_options
# Setup paths
SCRIPT_DIR = Path(__file__).resolve().parent
CONFIG_PATH = SCRIPT_DIR.parent / 'lib' / 'armbian-config' / 'config.jobs.json'
IMAGES_DIR = SCRIPT_DIR / 'include' / 'images'
MARKDOWN_DIR = SCRIPT_DIR / 'include' / 'markdown'
DOCS_DIR = Path('docs')
# Add this line to load it at runtime
module_options = extract_module_options_from_sh_files(str(SCRIPT_DIR.parent / 'lib' / 'armbian-config'))
# Load JSON
if not CONFIG_PATH.exists():
print("Error: The configuration file 'config.jobs.json' was not found.")
print("Please run 'config_assemble.sh -p' or '-t' first.")
sys.exit(1)
with open(CONFIG_PATH, 'r') as f:
data = json.load(f)
# Functions
def format_arch_labels(arch_string):
colors = {
"x86-amd64": ("#d0ebff", "#003865"), # blue
"arm64": ("#d3f9d8", "#1b5e20"), # green
"armhf": ("#fff3bf", "#7c4d00"), # yellow
"riscv64": ("#f3d9fa", "#6a1b9a"), # purple
"x86-amd64": ("#d0ebff", "#003865"),
"arm64": ("#d3f9d8", "#1b5e20"),
"armhf": ("#fff3bf", "#7c4d00"),
"riscv64": ("#f3d9fa", "#6a1b9a"),
}
label_template = '<span style="background-color:{bg}; color:{fg}; padding:3px 6px; border-radius:4px; font-size:90%;">{arch}</span>'
arches = arch_string.strip().split()
html_labels = []
for arch in arches:
bg, fg = colors.get(arch, ("#e0e0e0", "#333333")) # fallback to gray
html_labels.append(label_template.format(bg=bg, fg=fg, arch=arch))
return " ".join(html_labels)
return " ".join(label_template.format(bg=colors.get(arch, ("#e0e0e0", "#333333"))[0], fg=colors.get(arch, ("#e0e0e0", "#333333"))[1], arch=arch) for arch in arches)
def generate_anchor_links(item, level=0, parent_path=""):
links = []
current_id = item['id'].lower()
current_path = f"{parent_path}-{current_id}" if parent_path else current_id
indent = ' ' * level
links.append(f"{indent}- [{item.get('short', item.get('description', ''))}](#{current_id})")
if 'sub' in item:
for sub_item in item['sub']:
links.extend(generate_anchor_links(sub_item, level + 1, current_path))
links.extend(generate_anchor_links(sub_item, level + 1))
return links
def insert_images_and_header(item):
@@ -108,30 +94,25 @@ def create_markdown_user(item, level=1, show_meta=True, force_title=False, skip_
md.extend(insert_images_and_header(item))
if show_meta and level == 1:
if item.get('status'):
if item.get('status'):
md.append(f"__Status:__ {item['status']} ")
if item.get('module'):
module=item['module']
if module:
if module in module_options:
architecture = module_options[module].get('arch')
#formatted_arch = format_arch_labels(arch_list)
formatted_arch = format_arch_labels(architecture)
#formatted_arch = ' '.join(f"`{arch}`" for arch in architecture.split())
if formatted_arch:
md.append(f"__Architecture:__ {formatted_arch} ")
maintainer = module_options[module].get('maintainer')
if maintainer:
md.append(f"__Maintainer:__ {maintainer} ")
doc_link = module_options[module].get('doc_link')
if doc_link:
md.append(f"__Documentation:__ [Link]({doc_link}) ")
module = item['module']
if module in module_options:
architecture = module_options[module].get('arch')
formatted_arch = format_arch_labels(architecture)
if formatted_arch:
md.append(f"__Architecture:__ {formatted_arch} ")
maintainer = module_options[module].get('maintainer')
if maintainer:
md.append(f"__Maintainer:__ {maintainer} ")
doc_link = module_options[module].get('doc_link')
if doc_link:
md.append(f"__Documentation:__ [Link]({doc_link}) ")
if item.get('command') and not skip_commands:
cmd = item['command'][0] if isinstance(item['command'], list) else item['command']
fence = "custombash"
title = ""
md.append(f"\n~~~ {fence}{title}\narmbian-config --cmd {item['id']}\n~~~\n")
md.append(f"\n~~~ custombash\narmbian-config --cmd {item['id']}\n~~~\n")
footer_file = Path(__file__).parent / 'include' / 'markdown' / f"{item['id']}-footer.md"
if footer_file.is_file():
@@ -156,28 +137,32 @@ def create_markdown_user(item, level=1, show_meta=True, force_title=False, skip_
if sub_item.get('short') and sub_item.get('description') and sub_item.get('short') != sub_item.get('description'):
md.append(f"\n{sub_item.get('description')}\n")
md.extend(insert_images_and_header(sub_item))
# Insert unified edit line for header/footer only once
base_name = sub_item['id']
edit_parts = []
for section in ['footer', 'header']:
section_filename = f"{base_name}-{section}.md"
section_file = Path(__file__).parent / 'include' / 'markdown' / section_filename
rel_path = f"tools/include/markdown/{section_filename}"
edit_mode = "edit" if section_file.is_file() else "new"
url = f"https://github.com/armbian/configng/{edit_mode}/main/{rel_path}"
edit_parts.append(f"[{section}]({url})")
md.append(f"__Edit:__ {' '.join(edit_parts)} ")
if sub_item.get('status'):
md.append(f"__Status:__ {sub_item['status']} ")
if sub_item.get('module'):
module = sub_item['module']
if module in module_options:
architecture = module_options[module].get('arch')
formatted_arch = format_arch_labels(architecture)
#formatted_arch = ' '.join(f"`{arch}`" for arch in architecture.split())
if formatted_arch:
md.append(f"__Architecture:__ {formatted_arch} ")
if sub_item.get('module'):
module = sub_item['module']
if module in module_options:
maintainer = module_options[module].get('maintainer')
if maintainer:
md.append(f"__Maintainer:__ {maintainer} ")
if sub_item.get('module'):
module = sub_item['module']
if module in module_options:
doc_link = module_options[module].get('doc_link')
if doc_link:
md.append(f"__Documentation:__ [Link]({doc_link}) ")
module = sub_item.get('module')
if module in module_options:
arch = module_options[module].get('arch')
if arch:
md.append(f"__Architecture:__ {format_arch_labels(arch)} ")
maintainer = module_options[module].get('maintainer')
if maintainer:
md.append(f"__Maintainer:__ {maintainer} ")
doc_link = module_options[module].get('doc_link')
if doc_link:
md.append(f"__Documentation:__ [Link]({doc_link}) ")
first_sub = False
if sub_item.get('command'):
@@ -199,36 +184,22 @@ def create_markdown_user(item, level=1, show_meta=True, force_title=False, skip_
return '\n'.join(md)
# Rest of the code unchanged
def write_technical_markdown_files(data):
DOCS_DIR.mkdir(exist_ok=True)
for item in data['menu']:
item_dir = DOCS_DIR / item['id']
item_dir.mkdir(exist_ok=True)
anchors = "\n".join(generate_anchor_links(item)) + "\n\n"
technical_md = create_markdown_technical(item)
(item_dir / f"{item['id']}.technical.md").write_text('---\ncomments: true\n---\n\n' + anchors + technical_md)
if 'sub' in item:
for sub_item in item['sub']:
sub_anchors = "\n".join(generate_anchor_links(sub_item)) + "\n\n"
sub_technical_md = create_markdown_technical(sub_item)
(item_dir / f"{sub_item['id']}.technical.md").write_text('---\ncomments: true\n---\n\n' + sub_technical_md)
def write_user_markdown_files(data):
DOCS_DIR.mkdir(exist_ok=True)
for item in data['menu']:
item_dir = DOCS_DIR / item['id']
item_dir.mkdir(exist_ok=True)
user_md = create_markdown_user(item)
(item_dir / f"{item['id']}.md").write_text('---\ncomments: true\n---\n\n' + user_md)
if 'sub' in item:
for sub_item in item['sub']:
sub_user_md = create_markdown_user(sub_item)
@@ -248,7 +219,6 @@ def main():
print("Technical Markdown files created in 'docs' directory.")
else:
print("Usage: config-markdown [-u|-t]")
print("Options:\n -u Generate user documentation\n -t Generate technical documentation")
if __name__ == "__main__":
main()