Skip to content

Commit

Permalink
Merge pull request #62 from viash-io/auto_generate_reference_index
Browse files Browse the repository at this point in the history
Auto generate reference index
  • Loading branch information
Grifs authored Aug 18, 2023
2 parents 240010c + 239c30b commit e96f7a7
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 614 deletions.
30 changes: 30 additions & 0 deletions _src/automation/generate_reference_main_index_page/config.vsh.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
functionality:
name: generate_reference_main_index_page
description: |
The script reads the viash cli & config schema json schemas and assembles it to the main reference page listing
arguments:
- name: "--cli"
type: file
direction: input
description: The cli json file to use as input
required: true
must_exist: true
- name: "--config"
type: file
direction: input
description: The config json file to use as input
required: true
must_exist: true
- name: "--output"
type: file
alternatives: "-o"
direction: output
default: 'reference/reference.yml'
description: Path to use to store the index page listing
resources:
- type: python_script
path: script.py
- type: file
path: ../config_pages_settings.yaml
platforms:
- type: native
100 changes: 100 additions & 0 deletions _src/automation/generate_reference_main_index_page/script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import json, re, yaml
from pathlib import Path

## VIASH START
par = {
'cli': 'reference/cli_schema_export.json',
'config': 'reference/config_schema_export.json',
'output': 'reference/cli'
}
meta = {
"resources_dir": "_src/automation/generate_reference_cli_pages"
}
## VIASH END

output = Path(par["output"])
# keyword_regex = r"\@\[(.*?)\]\((.*?)\)"

config_file = Path(meta['resources_dir'], 'config_pages_settings.yaml')

with open(config_file, 'r') as infile:
config_pages_settings = yaml.safe_load(infile)

ref_dict = {}
def add_entry(topic, name, href):
# print(f'topic: {topic}, name: {name}, href: {href} ')
if topic in ref_dict:
ref_dict[topic].append({ 'text': name, 'href': href + '.html' })
else:
ref_dict[topic] = [{ 'text': name, 'href': href + '.html' }]

def generate_reference_page():
""" Load the generated JSON files and create reference entries. """

# List the CLI commands
with open(par['cli'], 'r') as infile:
cli_json = json.load(infile)

for entry in cli_json:
if "bannerCommand" in entry:
name = f'Viash {entry["name"]}'.title()
filename = entry["name"]
add_entry("Viash CLI Commands", name, f'/reference/cli/{filename}')
else:
for subcommand in entry['subcommands']:
name = f'Viash {entry["name"]} {subcommand["name"]}'.title()
filename = f'{entry["name"]}_{subcommand["name"]}'
add_entry("Viash CLI Commands", name, f'/reference/cli/{filename}')

# Add some static pages :(
add_entry("Viash Config", "Config Overview", "/reference/config/index")
add_entry("Viash Config", "Functionality", "/reference/config/functionality/index")
add_entry("Viash Config", "Platforms", "/reference/config/platforms/index")
add_entry("Miscellaneous", "Project Config", "/reference/project/index")
add_entry("Miscellaneous", "Environment Variables", "/reference/config/environmentVariables")
add_entry("Miscellaneous", "Config Mods", "/reference/config_mods/index")
add_entry("Miscellaneous", "Viash Code Block", "/reference/viash_code_block/index")


# Add config pages highlighting field groups
with open(par['config'], 'r') as infile:
config_json = json.load(infile)

# Regex filters. Platforms are not simply contained in a main folder, grab index files. For other matches, skip the index files.
to_document = {
'\./platforms/\w*/index': "Platforms",
'\./functionality/arguments/(?!index$)\w*': "Argument Types",
'\./functionality/resources/(?!index$)\w*': "Resource Types",
'\./platforms/docker/setup/(?!index$)\w*': "Docker Setup Requirements"
}

for entry in config_json:
this_parameter = {}
# Get the __this__ parameter
for d in entry:
if d['name'] == '__this__':
this_parameter = d
break

topic = this_parameter['type']
title = re.sub(r"(\w)([A-Z])", r"\1 \2", topic).title() \
.replace("Java Script", "JavaScript") \
.replace("C Sharp", "C#") \
.replace(" Argument", "")
filename = config_pages_settings['structure'][topic]
for key, value in to_document.items():
if re.match(key, filename):
add_entry(value, title, f'/reference/config{filename.strip(".")}')
break

# Minor final touch on the output and save to file
output = []
for key, value in ref_dict.items():
output.append({ 'title': key, 'links': sorted(value, key=lambda k: k['text'] ) })

with open(par['output'], 'w') as outfile:
outfile.write(yaml.safe_dump(output))

if __name__ == "__main__":
generate_reference_page()

5 changes: 5 additions & 0 deletions _src/render_pages.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ echo "Creating config information"
viash export config_schema --output ./reference/config_schema_export.json --format json
viash run _src/automation/generate_reference_config_pages/config.vsh.yaml -- \
--input ./reference/config_schema_export.json

echo "Creating reference main page"
viash run ./_src/automation/generate_reference_main_index_page/config.vsh.yaml -- \
--cli ./reference/cli_schema_export.json \
--config ./reference/config_schema_export.json
9 changes: 0 additions & 9 deletions reference/_metadata.yml

This file was deleted.

9 changes: 0 additions & 9 deletions reference/reference.css

This file was deleted.

Loading

0 comments on commit e96f7a7

Please sign in to comment.