Commit 8747a672 authored by Miguel Angel Reina Ortega's avatar Miguel Angel Reina Ortega
Browse files

create markdown table from frontmatter

parent c0194d35
Loading
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -68,6 +68,7 @@ Build generateSpecWebSite docker image:
        - generateSpecWebSite/regexMatches.py
        - generateSpecWebSite/spec_on_pages.sh
        - generateSpecWebSite/toMkdocs.py
        - generateSpecWebSite/create_frontmatter_table.py
        - markdownTools/dockerfile
        - markdownTools/setup.py
        - markdownTools/requirements.txt
+100 −0
Original line number Diff line number Diff line
#!/usr/bin/env python3
#
#	create_frontmatter_table.py
#
#	Creates a markdown table from frontmatter data
#	- Removes headings
#	- Removes <br> tags
#	- Formats as a clean markdown table
#
#	(c) 2025 by Miguel Angel Reina Ortega
#	License: BSD 3-Clause License. See the LICENSE file for further details.

import sys
import yaml
import re

def remove_br_tags(text):
    """Remove <br> and <br/> tags from text."""
    if not text:
        return text
    # Replace <br>, <br/>, <br /> with space
    text = re.sub(r'<br\s*/?>', ' ', text, flags=re.IGNORECASE)
    # Clean up multiple spaces
    text = re.sub(r'\s+', ' ', text)
    return text.strip()

def create_table_from_frontmatter(frontmatter_file, output_file=None):
    """
    Create a markdown table from frontmatter data.
    
    Args:
        frontmatter_file: Path to the frontmatter.md file
        output_file: Optional path to output file (if None, prints to stdout)
    """
    
    # Read frontmatter
    with open(frontmatter_file, 'r', encoding='utf-8') as f:
        content = f.read()
    frontmatter_content = []
    # Split into lines and skip first line if it looks like a filename
    lines = content.split('\n')
    for line in lines:
        # Check if it's a filename line: ends with .md: or ends with : and no space before colon
        if line.strip().endswith('.md:'):
            # Skip first line
            continue
        else:
            frontmatter_content.append(line)
    
    # Parse YAML
    frontmatter = yaml.safe_load('\n'.join(frontmatter_content))
    if not frontmatter:
        frontmatter = {}
    
    # Generate markdown table (no header row)
    table_lines = []
    table_lines.append("")
    table_lines.append("|&nbsp; |&nbsp; |")
    table_lines.append(f"|:-|:-|")
    # Process each field dynamically
    for key, value in frontmatter.items():
        # Convert value to string and clean it
        if value is None:
            value_str = ""
        elif isinstance(value, (list, dict)):
            # For lists and dicts, convert to YAML string
            value_str = yaml.dump(value, default_flow_style=False).strip()
        else:
            value_str = str(value)
        
        # Remove <br> tags
        #value_str = remove_br_tags(value_str)
        
        # Escape pipe characters in values (they break markdown tables)
        value_str = value_str.replace('|', '\\|')
        
        # Add row to table
        table_lines.append(f"| **{key}** | {value_str} |")
    
    # Join all lines
    table_lines.append("")
    table_content = '\n'.join(table_lines)
    
    # Output
    if output_file:
        with open(output_file, 'w', encoding='utf-8') as f:
            f.write(table_content)
    else:
        print(table_content)

if __name__ == '__main__':
    if len(sys.argv) < 2:
        print("Usage: create_frontmatter_table.py <frontmatter_file> [output_file]")
        sys.exit(1)
    
    frontmatter_file = sys.argv[1]
    output_file = sys.argv[2] if len(sys.argv) > 2 else None
    
    create_table_from_frontmatter(frontmatter_file, output_file)
+17 −23
Original line number Diff line number Diff line
@@ -49,11 +49,24 @@ git checkout $7

####### GENERATE NAV SPEC #######
echo "------ Process MD Spec --------"
frontmatterClause=False
python3 /markdownTools/processMDSpec.py -fmo "./$8.md" -fmf frontmatter.md
if [ -f "frontmatter.md" ] && [ "$(cat frontmatter.md | tr -d ' \n\t')" != "{}" ]; then
  echo "------ Adding frontmatter to spec.md --------"
  python3 /generateSpecWebSite/create_frontmatter_table.py frontmatter.md spec.md
  echo "------ Content of spec.md --------"
  cat spec.md
  frontmatterClause=True
fi
python3 /markdownTools/processMDSpec.py "./$8.md" > combined.md
echo "------ Generate input for mkdocs --------"
#python3 $1 -it -ihp --title ${9^^} "./$8.md"
python3 /generateSpecWebSite/toMkdocs.py -it -ihp --title ${9^^} "./combined.md"
if [ $frontmatterClause == True ]; then
  cat ./combined.md >> ./spec.md
  python3 /generateSpecWebSite/toMkdocs.py -it -ihp --title ${9^^} "./spec.md"
else
  python3 /generateSpecWebSite/toMkdocs.py -it -ihp --title ${9^^} "./$8.md"
fi
ls ${9^^}
echo "------ Move to docs folder --------"
mv ${9^^}/* docs/
@@ -61,29 +74,10 @@ sed -i 's/'${9^^}'\///g' _nav.yml
cat _nav.yml
echo "  - 'Home': 'index.md'" >> mkdocs.yml
cat _nav.yml >> mkdocs.yml
if [ -f "frontmatter.md" ] && [ "$(cat frontmatter.md | tr -d ' \n\t')" != "{}" ]; then
  echo "------ Adding frontmatter to index.md --------"
  sed -i 's/^.*\.md://g' ./frontmatter.md
  #echo "---" >> ./frontmatter.md
  cat ./frontmatter.md
  cp ./frontmatter.md docs/index.md
else
cp docs/0.md docs/index.md
fi
repo_url="https://${3}/${4}.git"
echo ${repo_url}
sed -i 's/##PROJECT/'${9^^}'/g' mkdocs.yml
repo_url=$(echo ${repo_url} | sed 's/\//\\\//g')
sed -i 's/##REPO_URL/'"${repo_url}"'/g' mkdocs.yml
sed -i 's/##REPO_NAME/'${9^^}'/g' mkdocs.yml

sed --version

cat mkdocs.yml

## Create download tab if official baseline version
if [[ ${10} == v* ]]; then
    echo "Adding download tab for version ${10}..."
    echo "\nAdding download tab for version ${10}..."
    mkdir -p "docs/download"
    mv "baseline/$8_${10}.docx" docs/download
    spec_name=$(echo ${8/&/and})