Skip to content
Snippets Groups Projects
Commit 3bb17562 authored by Martyn Welch's avatar Martyn Welch Committed by Emanuele Aina
Browse files

Convert HotDoc formatting for Hugo


The existing metadata and some inline codes need converting to work with
Hugo:

 - Remove authors as we don't have an equivalent for that in Hugo
 - Add in fields that the existing metadata doesn't have
 - Include aliases to allow us to point to the new pages from a
   redirect on designs.apertis.org
 - Ensure we have a title (based on filename if one not present)
 - Convert hotdoc code blocks to Hugo equivalent
 - HotDoc allowed a `[][link]` format that Hugo doesn't understand
 - HotDoc allowed relative links without text, which Hugo doesn't handle
 - The location of the images and other media has changed
 - The format used for internal page references has is different

Additionally, fix up touched links to other Apertis pages to use the
Hugo "ref" shortcode, which reduces the risk of leaving hanging links.

All frontmatter created in toml format to stay consistent with existing
documents.

Converted using the following python script:
```

import os
import re
import subprocess
import sys
import toml
import yaml

def url_munge(match):
    url = match.group('url').strip('.')
    if url[0] != "/":
        url = "/%s" % url
    url = url.replace("media", "images")
    link = ("![](%s)" % url)

    return link

def link_munge(match):
    link = match.group('link')
    new = link.replace("(", "")
    new = new.replace(")", "")
    new = new.replace("`", "")
    new = new.replace("?", "")
    new = new.replace(":", "")
    new = new.replace(",", "")
    new = new.replace("–", "")
    new = new.replace("“", "")
    new = new.replace("”", "")
    new = new.replace(".", "")
    new = new.replace(" ", "-")
    new = new.lower()
    new = (" [%s]( {{< ref \"#%s\" >}} )" % (link, new))

    return new

def link_munge_2(match):
    link = match.group('link')
    new = link.replace("#", "")
    new = new.replace("-", " ")
    new = new.replace(".md", "")
    if not "http" in link:
        link = " {{< ref \"%s\" >}} " % link

    new = (" [%s](%s)" % (new, link))

    return new

for filename in os.listdir("."):
    if ".md" not in filename:
        continue

    print("%s: " % filename)
    with open(filename, 'r+') as file:
        contents = file.read()
        if contents[0:3] == "+++":
            # We have toml
            data = toml.loads(contents.split("+++")[1])
            doc = "+++".join(contents.split("+++")[2:])
        elif contents[0:3] == "---":
            # We have yaml
            data = yaml.load(contents.split("---")[1])
            doc = "---".join(contents.split("---")[2:])
        else:
            # No frontmatter
            data = {}
            doc = contents

        if not "title" in data.keys():
            data["title"] = filename.split(".")[0].replace('_', ' ').capitalize()

        data["weight"] = 100
        data["aliases"] = ["/old-developer/latest/%s" % filename.replace(".md", ".html")]

        # Add aliases
        latest = data['aliases'][0]

        for version in ['v2019', 'v2020', 'v2021pre', 'v2022dev0']:
            versioned = latest.replace("/latest/", "/%s/" % version)
            data['aliases'].append(versioned)

        data["outputs"] = ["html", "pdf-in"]

        # Remove authors
        if "authors" in data.keys():
            del data["authors"]

        # Switch HotDoc code blocks to hugo code blocks
        pattern = re.compile("^---", re.MULTILINE)
        doc = pattern.sub("```", doc)

        # Hotdoc uses `[][Internal Title]` for internal links whilst Hugo uses `[](#internal-title)`
        pattern = re.compile("\[\]\[(?P<link>.*?)\]")
        doc = pattern.sub(link_munge, doc)

        # Hotdoc allows empty links like `[](url)`
        pattern = re.compile("[^!]\[\]\((?P<link>.*?)\)")
        doc = pattern.sub(link_munge_2, doc)

        # Media links not working
        pattern = re.compile("!\[\]\((?P<url>.*?)\)")
        doc = pattern.sub(url_munge, doc)

        file.seek(0)
        file.truncate()

        file.write("+++\n")
        file.write(toml.dumps(data))
        file.write("+++")
        file.write(doc)
```

Signed-off-by: default avatarMartyn Welch <martyn.welch@collabora.com>
parent 453977a1
No related branches found
No related tags found
1 merge request!97T6572: Move developer docs to main website
Showing
with 273 additions and 306 deletions
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment