|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Example usage: |
| 4 | +# scripts/add_new_page.py "My new HAT" |
| 5 | +# will create documentation/asciidoc/accessories/my-new-hat.adoc and documentation/asciidoc/accessories/my-new-hat/about.adoc |
| 6 | +# |
| 7 | +# scripts/add_new_page.py -b new-hat "My new HAT" |
| 8 | +# will create documentation/asciidoc/accessories/new-hat.adoc and documentation/asciidoc/accessories/new-hat/about.adoc |
| 9 | +# |
| 10 | +# scripts/add_new_page.py -b new-hat -s intro "My new HAT" |
| 11 | +# will create documentation/asciidoc/accessories/new-hat.adoc and documentation/asciidoc/accessories/new-hat/intro.adoc |
| 12 | +# |
| 13 | +# scripts/add_new_page.py -c services "Some extra Service" |
| 14 | +# will create documentation/asciidoc/services/some-extra-service.adoc and documentation/asciidoc/services/some-extra-service/about.adoc |
| 15 | +# |
| 16 | +# scripts/add_new_page.py -c services -b id -s signing_up "Raspberry Pi ID" |
| 17 | +# will create documentation/asciidoc/services/id.adoc and documentation/asciidoc/services/id/signing_up.adoc |
| 18 | + |
| 19 | +import argparse |
| 20 | +import json |
| 21 | +import os |
| 22 | +import random |
| 23 | +import re |
| 24 | +import shutil |
| 25 | +import sys |
| 26 | + |
| 27 | +DOCUMENTATION_DIR = 'documentation' |
| 28 | +INDEX_JSON = os.path.join(DOCUMENTATION_DIR, 'index.json') |
| 29 | +IMAGES_DIR = os.path.join(DOCUMENTATION_DIR, 'images') |
| 30 | +PLACEHOLDER_IMAGES_DIR = os.path.join(IMAGES_DIR, 'placeholder') |
| 31 | +PLACEHOLDER_SHAPES = ('circle', 'square', 'triangle') |
| 32 | + |
| 33 | +if __name__ == "__main__": |
| 34 | + parser = argparse.ArgumentParser() |
| 35 | + parser.add_argument('name') |
| 36 | + parser.add_argument('-c', '--category', default='accessories') |
| 37 | + parser.add_argument('-b', '--basename') |
| 38 | + parser.add_argument('-s', '--subfile', default='about') |
| 39 | + args = parser.parse_args() |
| 40 | + print(args) |
| 41 | + |
| 42 | + name = args.name |
| 43 | + category = args.category |
| 44 | + basename = args.basename |
| 45 | + if not basename: |
| 46 | + basename = re.sub('\W', '-', name.lower()) |
| 47 | + subfile = f"{args.subfile}.adoc" |
| 48 | + |
| 49 | + # Validate the category |
| 50 | + valid_categories = [] |
| 51 | + with open(INDEX_JSON, 'r') as json_fh: |
| 52 | + json_contents = json.load(json_fh) |
| 53 | + for tab in json_contents["tabs"]: |
| 54 | + if 'from_json' in tab: |
| 55 | + continue |
| 56 | + valid_categories.append(tab['path']) |
| 57 | + if category not in valid_categories: |
| 58 | + raise Exception(f"Invalid category {category}. Choose from {valid_categories}") |
| 59 | + category_dir = os.path.join(DOCUMENTATION_DIR, 'asciidoc', category) |
| 60 | + if not os.path.exists(category_dir): |
| 61 | + raise Exception(f"{category_dir} doesn't exist") |
| 62 | + |
| 63 | + # Check the new page hasn't already been added |
| 64 | + new_dir = os.path.join(category_dir, basename) |
| 65 | + if os.path.isdir(new_dir): |
| 66 | + raise Exception(f"{new_dir} already exists") |
| 67 | + new_file = f"{new_dir}.adoc" |
| 68 | + if os.path.isfile(new_file): |
| 69 | + raise Exception(f"{new_file} already exists") |
| 70 | + new_subfile = os.path.join(new_dir, subfile) |
| 71 | + if os.path.isfile(new_subfile): |
| 72 | + raise Exception(f"{new_subfile} already exists") |
| 73 | + |
| 74 | + print(f"Will create {new_file} and {new_subfile}") |
| 75 | + sys.exit(0) |
| 76 | + # Create the template AsciiDoc documentation |
| 77 | + os.mkdir(new_dir) |
| 78 | + with open(new_file, 'w') as fh: |
| 79 | + fh.write(f"include::{basename}/{subfile}[]\n") |
| 80 | + with open(new_subfile, 'w') as fh: |
| 81 | + fh.write(f"== About\n\nAll about {name} and why it's amazing") |
| 82 | + |
| 83 | + # Add the placeholder images |
| 84 | + shape = random.choice(PLACEHOLDER_SHAPES) |
| 85 | + shutil.copyfile(os.path.join(PLACEHOLDER_IMAGES_DIR, f"placeholder_{shape}-SMALL.png"), os.path.join(IMAGES_DIR, f"{basename}-SMALL.png")) |
| 86 | + shutil.copyfile(os.path.join(PLACEHOLDER_IMAGES_DIR, f"placeholder_{shape}.png"), os.path.join(IMAGES_DIR, 'full-sized', f"{basename}.png")) |
| 87 | + |
| 88 | + # And finally update the JSON which ties everything together |
| 89 | + with open(INDEX_JSON, 'r+') as json_fh: |
| 90 | + json_contents = json.load(json_fh) |
| 91 | + found = False |
| 92 | + for tab in json_contents["tabs"]: |
| 93 | + if tab['path'] == category: |
| 94 | + found = True |
| 95 | + tab["subitems"].append({ |
| 96 | + "title": name, |
| 97 | + "description": f"Description of {name}", |
| 98 | + "image": os.path.join('full-sized', f"{basename}.png"), |
| 99 | + "subpath": f"{basename}.adoc", |
| 100 | + }) |
| 101 | + break |
| 102 | + if not found: # this shouldn't ever happen, as we already validated the category earlier |
| 103 | + raise Exception(f"Couldn't find tab with path {category} in {INDEX_JSON}") |
| 104 | + json_fh.seek(0) |
| 105 | + json.dump(json_contents, json_fh, indent=4) |
| 106 | + |
| 107 | + print(f"Done. Use 'git status' and 'git diff' to see what has changed, and then add more content to\n {new_subfile}") |
| 108 | + |
0 commit comments