-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathpre_build.py
More file actions
204 lines (159 loc) · 6.72 KB
/
Copy pathpre_build.py
File metadata and controls
204 lines (159 loc) · 6.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# %%
import os
import glob
from pathlib import Path
import jsonc as json
import logging
# Suppress warnings from json-schema-for-humans about unresolvable URLs
logging.getLogger().setLevel(logging.ERROR)
# change working directory to the location of this script
os.chdir(os.path.dirname(os.path.abspath(__file__)))
def build_json_examples():
"""Build markdown files from json examples."""
# glob recursively to find all json files
input_directory = 'examples'
output_directory = 'examples'
os.makedirs(output_directory, exist_ok=True)
# iterate over all folders in the examples directory
example_types = [
p for p in os.listdir(input_directory)
if os.path.isdir(os.path.join(input_directory, p))
]
index_md = """# NGFF metadata JSON Examples
This section contains JSON examples for various metadata layouts.
"""
for example in example_types:
json_files = glob.glob(os.path.join(input_directory, example, '*.json'), recursive=True)
index_md += f"\n## {example}\n"
# add header
markdown_content = f"""# {example} Examples
This document contains JSON examples for {example} metadata layouts.
"""
# append each json file content
for json_file in json_files:
print(f'Processing {json_file}...')
crossref = f"examples:{example}:{Path(json_file).stem}"
index_md += f"- [{Path(json_file).stem}](#{crossref})\n"
json_file_name = Path(json_file).stem
# Create the Markdown content
markdown_content += f"""## {os.path.splitext(json_file_name)[0]}
({crossref})=
```{{literalinclude}} {Path(os.path.relpath(json_file, Path(json_file).parent)).as_posix()}
:linenos:
:language: json
```
"""
with open(os.path.join(Path(json_file).parent, f'{example}.md'), 'w') as md_file:
md_file.write(markdown_content)
with open(Path(output_directory) / "index.md", 'w') as index_file:
index_file.write(index_md)
def build_json_schemas():
from json_schema_for_humans.generate import generate_from_filename
from json_schema_for_humans.generation_configuration import GenerationConfiguration
import json
schema_source_dir = 'schemas'
output_directory = 'schemas'
os.makedirs(output_directory, exist_ok=True)
schema_files = glob.glob(os.path.join(schema_source_dir, '*.schema'), recursive=True)
index_markdown = """# NGFF metadata JSON Schemas
This section contains JSON schemas for various metadata layouts.
Find below links to auto-generated markdown pages or interactive HTML pages for each schema.
| Schema | Markdown | HTML |
|--------|----------|------|
"""
for schema_file in schema_files:
if 'strict' in schema_file:
continue # skip strict schemas
print(f'Processing {schema_file}...')
output_path_md = os.path.join(output_directory, f"{Path(schema_file).stem}" + ".md")
output_path_html = os.path.join(output_directory, f"{Path(schema_file).stem}" + ".html")
os.makedirs(os.path.dirname(output_path_md), exist_ok=True)
os.makedirs(os.path.dirname(output_path_html), exist_ok=True)
# Generate the documentation
try:
config_md = GenerationConfiguration(
template_name='md',
with_footer=True,
show_toc=True,
link_to_reused_ref=True,
)
generate_from_filename(
os.path.abspath(schema_file),
result_file_name=os.path.abspath(output_path_md),
config=config_md
)
# insert mySt cross-reference at top of markdown files
with open(output_path_md, 'r', encoding='utf-8') as md_file:
md_content = md_file.read()
crossref = f"schemas:{Path(schema_file).stem}"
md_content = f"""---
author: ""
---
({crossref})=\n\n{md_content}
"""
with open(output_path_md, 'w', encoding='utf-8') as md_file:
md_file.write(md_content)
link_markdown = f"[{Path(schema_file).stem}](#{crossref})"
except Exception as e:
print(f"Error generating markdown for {schema_file}: {e}")
link_markdown = ""
try:
config_html = GenerationConfiguration(
template_name='js',
with_footer=True,
show_toc=False,
link_to_reused_ref=True,
)
generate_from_filename(
os.path.abspath(schema_file),
result_file_name=os.path.abspath(output_path_html),
config=config_html
)
link_html = f"[{Path(schema_file).stem}]({output_path_html})"
except Exception as e:
print(f"Error generating HTML for {schema_file}: {e}")
link_html = ""
index_markdown += f"| {Path(schema_file).stem} | {link_markdown} | {link_html} |\n"
with open(Path(output_directory) / "index.md", 'w') as index_file:
index_file.write(index_markdown)
def build_footer():
"""Build footer file."""
from datetime import datetime
year = datetime.now().year
footer_content = f"""
<div>
Copyright © 2020-{year}
<a href="https://www.openmicroscopy.org/"><abbr title="Open Microscopy Environment">OME</abbr></a><sup>®</sup>.
OME trademark rules apply.
</div>
"""
with open('footer.md', 'w') as footer_file:
footer_file.write(footer_content)
def render_authors_md():
from jinja2 import Template
import yaml
# Load authors data
with open('authors.yml', 'r') as f:
data = yaml.safe_load(f)
authors = data['project']['authors']
markdown_authors = """"""
# The invertocat icon was downloaded from https://brand.github.com/foundations/logo
# and colored in grey according to the guidelines under the "Color" section.
orcid_icon = "https://orcid.org/assets/vectors/orcid.logo.icon.svg"
github_icon = "https://github.githubassets.com/favicons/favicon.svg"
for idx, author in enumerate(authors):
name = author['name']
if "orcid" in author:
name += f" [<img src=\"{orcid_icon}\" alt=\"ORCID iD\" height=12 width=12 style=\"vertical-align: middle;\"/>](https://orcid.org/{author['orcid']})"
if "github" in author:
name += f" [<img src=\"{github_icon}\" alt=\"GitHub\" height=12 width=12 style=\"vertical-align: middle;\"/>](https://github.com/{author['github']})"
if idx == len(authors) - 1:
markdown_authors += name
else:
markdown_authors += name + ", "
with open ("_authors.md", "w") as f:
f.write(markdown_authors)
build_json_examples()
build_json_schemas()
build_footer()
render_authors_md()