Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] use mkdocs macro doc to render tables #504

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
activities/.DS_Store
protocols/.DS_Store

__pycache__

.idea/

node_modules
Expand Down
232 changes: 1 addition & 231 deletions docs/schema.md

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions macros/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from .macros import (
schema_table,
)
from .main import define_env

__all__ = [
"define_env",
"schema_table",
]
93 changes: 93 additions & 0 deletions macros/macros.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
from pathlib import Path

import ruamel.yaml
from jinja2 import Environment, FileSystemLoader, select_autoescape

yaml = ruamel.yaml.YAML()
yaml.indent(mapping=2, sequence=4, offset=2)

ROOT = Path(__file__).parents[1]

TEMPLATES_DIR = ROOT / "templates"

SCHEMA_DIR = ROOT / "linkml-schema"


def return_jinja_env() -> Environment:
return Environment(
loader=FileSystemLoader(TEMPLATES_DIR),
autoescape=select_autoescape(),
lstrip_blocks=True,
trim_blocks=True,
)


def schema_table() -> str:

target_classes = [
"Protocol",
"Activity",
"Item",
"AdditionalProperty",
"OverrideProperty",
"UnitOption",
"ResponseOption",
"Choice",
"ComputeSpecification",
"MessageSpecification",
"AdditionalNoteObj",
"ResponseActivity",
"Response",
"Participant",
"SoftwareAgent",
]

input_file = SCHEMA_DIR / "reproschema.yaml"

reproschema = yaml.load(input_file)

env = return_jinja_env()
template = env.get_template("table.jinja")

content = []
for this_class in target_classes:

class_dict = reproschema["classes"][this_class]
class_dict["uri"] = class_dict["class_uri"].replace(
"reproschema:", reproschema["id"]
)

slots = []
for this_slot in class_dict["slots"]:

slot_dict = reproschema["slots"][this_slot]

slot_dict["name"] = this_slot

if "title" not in slot_dict:
slot_dict["title"] = "**TODO**"
if "description" not in slot_dict:
slot_dict["description"] = "**TODO**"

prefix = slot_dict["slot_uri"].split(":")[0]
value = reproschema["id"]
if prefix in reproschema["prefixes"]:
value = reproschema["prefixes"][prefix]

slot_dict["uri"] = slot_dict["slot_uri"].replace(f"{prefix}:", value)

slots.append(slot_dict)

class_dict["slots"] = slots

content.append(template.render(this_class=class_dict))

return "\n".join(content)


def main():
print(schema_table())


if __name__ == "__main__":
main()
32 changes: 32 additions & 0 deletions macros/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""This package is used to build elements from data into
MarkDown format for the specification text.

Functions decorated in "define_env()" are callable throughout the
specification and are run/rendered with the mkdocs plugin "macros".
"""

import os
import sys

code_path = os.path.abspath(os.path.join(os.path.dirname(__file__)))
sys.path.append(code_path)

import macros # noqa E402


def define_env(env):
"""Define variables, macros and filters for the mkdocs-macros plugin.

Parameters
----------
env : :obj:`macros.plugin.MacrosPlugin`
An object in which to inject macros, variables, and filters.

Notes
-----
"variables" are the dictionary that contains the environment variables
"macro" is a decorator function, to declare a macro.

Macro aliases must start with "MACROS___"
"""
env.macro(macros.schema_table, "MACROS___schema_table")
4 changes: 4 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,16 @@ markdown_extensions:
- pymdownx.snippets:
auto_append:
- includes/abbreviations.md
- tables
- toc:
anchorlink: true

watch:
- includes
- linkml-schema

plugins:
- search
- tags
- macros:
module_name: macros/main
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
rdflib==5.0.0
mkdocs-macros-plugin
mkdocs-material
pymdown-extensions
rdflib==5.0.0
ruamel.yaml
13 changes: 13 additions & 0 deletions templates/table.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@


### {{ this_class.title }}

{{ this_class.description }}

URI: [{{ this_class.uri }}]({{ this_class.uri }})

| Property | Title | Description | URI |
| :------- | :---- | :---------- | :-- |
{% for slot in this_class.slots %}
| {{ slot.name }} | {{ slot.title }} | {{ slot.description }} | [{{ slot.uri }}]({{ slot.uri }}) |
{% endfor %}
Loading