Skip to content

Commit

Permalink
Split out create_build_adoc.py into a separate create_build_adoc_doxy…
Browse files Browse the repository at this point in the history
…gen.py for the pico-sdk pages
  • Loading branch information
lurch committed Mar 6, 2023
1 parent b0f4089 commit 321bbd0
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 25 deletions.
4 changes: 2 additions & 2 deletions build.ninja
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ rule create_toc
rule create_build_adoc
command = $scripts_dir/create_build_adoc.py $documentation_index $site_config $GITHUB_EDIT_TEMPLATE $in $inc_dir $out

rule create_build_adoc2
command = $scripts_dir/create_build_adoc.py $documentation_index $site_config $in $out_dir $out
rule create_build_adoc_doxygen
command = $scripts_dir/create_build_adoc_doxygen.py $documentation_index $site_config $in $out_dir $out

rule create_build_adoc_include
command = $scripts_dir/create_build_adoc_include.py $site_config $GITHUB_EDIT_TEMPLATE $in $out
Expand Down
4 changes: 2 additions & 2 deletions scripts/create_auto_ninjabuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,10 @@ def add_entire_directory(tab_dir, dir_path, pages_set, src_images, dest_images):

dest = os.path.join('$out_dir', page)
source = os.path.join('$src_dir', page)
extra_sources = ['$scripts_dir/create_build_adoc.py', '$documentation_index', '$site_config']
extra_sources = ['$scripts_dir/create_build_adoc_doxygen.py', '$documentation_index', '$site_config']
if source not in all_doc_sources:
all_doc_sources.append(source)
ninja.build(dest, 'create_build_adoc2', source, extra_sources)
ninja.build(dest, 'create_build_adoc_doxygen', source, extra_sources)
targets.append(dest)
if targets:
ninja.default(targets)
Expand Down
33 changes: 12 additions & 21 deletions scripts/create_build_adoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,12 @@ def check_no_markdown(filename):


if __name__ == "__main__":
if len(sys.argv) == 7:
index_json = sys.argv[1]
config_yaml = sys.argv[2]
github_edit = sys.argv[3]
src_adoc = sys.argv[4]
includes_dir = sys.argv[5]
build_adoc = sys.argv[6]
elif len(sys.argv) == 6:
index_json = sys.argv[1]
config_yaml = sys.argv[2]
github_edit = None
src_adoc = sys.argv[3]
includes_dir = sys.argv[4]
build_adoc = sys.argv[5]
index_json = sys.argv[1]
config_yaml = sys.argv[2]
github_edit = sys.argv[3]
src_adoc = sys.argv[4]
includes_dir = sys.argv[5]
build_adoc = sys.argv[6]

output_subdir = os.path.basename(os.path.dirname(build_adoc))
adoc_filename = os.path.basename(build_adoc)
Expand All @@ -63,13 +55,12 @@ def check_no_markdown(filename):
with open(config_yaml) as config_fh:
site_config = yaml.safe_load(config_fh)

if github_edit is not None:
with open(github_edit) as edit_fh:
edit_template = edit_fh.read()
template_vars = {
'github_edit_link': os.path.join(site_config['githuburl'], 'blob', site_config['githubbranch_edit'], src_adoc)
}
edit_text = re.sub('{{\s*(\w+)\s*}}', lambda m: template_vars[m.group(1)], edit_template)
with open(github_edit) as edit_fh:
edit_template = edit_fh.read()
template_vars = {
'github_edit_link': os.path.join(site_config['githuburl'], 'blob', site_config['githubbranch_edit'], src_adoc)
}
edit_text = re.sub('{{\s*(\w+)\s*}}', lambda m: template_vars[m.group(1)], edit_template)

new_contents = ''
seen_header = False
Expand Down
79 changes: 79 additions & 0 deletions scripts/create_build_adoc_doxygen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/usr/bin/env python3

import sys
import os
import json
import re
import yaml


def check_no_markdown(filename):
with open(filename) as fh:
asciidoc = fh.read()
if re.search('```\n.*?\n```', asciidoc):
raise Exception("{} uses triple-backticks for markup - please use four-hyphens instead".format(filename))
# strip out code blocks
asciidoc = re.sub('----\n.*?\n----', '', asciidoc, flags=re.DOTALL)
# strip out pass-through blocks
asciidoc = re.sub('\+\+\+\+\n.*?\n\+\+\+\+', '', asciidoc, flags=re.DOTALL)
if re.search('(?:^|\n)#+', asciidoc):
raise Exception("{} contains a Markdown-style header (i.e. '#' rather than '=')".format(filename))
if re.search(r'(\[.+?\]\(.+?\))', asciidoc):
raise Exception("{} contains a Markdown-style link (i.e. '[title](url)' rather than 'url[title]')".format(filename))


if __name__ == "__main__":
index_json = sys.argv[1]
config_yaml = sys.argv[2]
src_adoc = sys.argv[3]
includes_dir = sys.argv[4]
build_adoc = sys.argv[5]

output_subdir = os.path.basename(os.path.dirname(build_adoc))
adoc_filename = os.path.basename(build_adoc)

check_no_markdown(src_adoc)

index_title = None
with open(index_json) as json_fh:
data = json.load(json_fh)
for tab in data['tabs']:
if 'path' in tab and tab['path'] == output_subdir:
for subitem in tab['subitems']:
if 'subpath' in subitem and subitem['subpath'] == adoc_filename:
index_title = subitem['title']
break
if index_title is not None:
break
elif 'from_json' in tab:
index_title = tab['title']
break
if index_title is None:
raise Exception("Couldn't find title for {} in {}".format(os.path.join(output_subdir, adoc_filename), index_json))

with open(config_yaml) as config_fh:
site_config = yaml.safe_load(config_fh)

new_contents = ''
seen_header = False
with open(src_adoc) as in_fh:
for line in in_fh.readlines():
if line.startswith('== '):
if not seen_header:
seen_header = True
else:
m = re.match('^(include::)(.+)(\[\]\n?)$', line)
if m:
line = m.group(1) + os.path.join('{includedir}/{parentdir}', m.group(2)) + m.group(3)
new_contents += line

with open(build_adoc, 'w') as out_fh:
out_fh.write(""":parentdir: {}
:page-layout: docs
:includedir: {}
:doctitle: {}
:page-sub_title: {}
:sectanchors:
{}
""".format(output_subdir, includes_dir, '{} - {}'.format(site_config['title'], index_title), index_title, new_contents))

0 comments on commit 321bbd0

Please sign in to comment.