-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merging bib-generating logic with the docs-generating script, CSS/HTM…
…L tunings
- Loading branch information
Showing
6 changed files
with
151 additions
and
98 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,7 @@ jobs: | |
pip install -e . | ||
pip install -e examples | ||
python -We .github/workflows/docs.py . . | ||
python -We docs/generate_html.py . . | ||
- if: ${{ github.ref == 'refs/heads/main' && matrix.platform == 'ubuntu-latest' }} | ||
uses: JamesIves/[email protected] | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
""" | ||
Script to generate docs based on given input directory (first argument) | ||
and output directory (second argument), includes bibliography logic | ||
""" | ||
|
||
import json | ||
import os | ||
import re | ||
import subprocess | ||
import sys | ||
|
||
import nbformat | ||
|
||
# <TEMPORARILY COPIED FROM DEVOPS_TESTS> | ||
from git.cmd import Git | ||
|
||
|
||
def find_files(path_to_folder_from_project_root=".", file_extension=None): | ||
""" | ||
Returns all files in a current git repo. | ||
The list of returned files may be filtered with `file_extension` param. | ||
""" | ||
all_files = [ | ||
path | ||
for path in Git( | ||
Git(path_to_folder_from_project_root).rev_parse("--show-toplevel") | ||
) | ||
.ls_files() | ||
.split("\n") | ||
if os.path.isfile(path) | ||
] | ||
if file_extension is not None: | ||
return list(filter(lambda path: path.endswith(file_extension), all_files)) | ||
|
||
return all_files | ||
|
||
|
||
# </TEMPORARILY COPIED FROM DEVOPS_TESTS> | ||
|
||
|
||
def generate_badges_md_files(): | ||
"""extracts badge-containing cell from each notebook and writes it to an .md file""" | ||
for notebook_path in find_files("examples/PySDM_examples", ".ipynb"): | ||
with open(notebook_path, encoding="utf8") as fin: | ||
with open(notebook_path + ".badges.md", "w", encoding="utf8") as fout: | ||
fout.write(nbformat.read(fin, nbformat.NO_CONVERT).cells[0].source) | ||
|
||
|
||
def read_dois_from_json(code_path): | ||
"""loads bibliography data from .json file""" | ||
bibliography_json_path = f"{code_path}/docs/bibliography.json" | ||
dois_from_json = {} | ||
if os.path.exists(bibliography_json_path): | ||
with open(bibliography_json_path, "r", encoding="utf8") as fin: | ||
dois_from_json = json.load(fin) | ||
return dois_from_json | ||
|
||
|
||
def check_dois(dois_from_json): | ||
"""checks if all bib entries are referenced from code and vice versa""" | ||
found_dois = [] | ||
for extension in (".md", ".ipynb", ".py"): | ||
for full_path in find_files(file_extension=extension): | ||
with open(full_path, "r", encoding="utf-8") as fin: | ||
text = fin.read() | ||
dois = re.findall( | ||
r"\b(https://doi\.org/10[.][0-9]{4,}(?:[.][0-9]+)*/(?:(?![\"&\'<>^\\])\S)+)\b", | ||
text, | ||
) | ||
if dois: | ||
found_dois.extend((full_path, doi) for doi in dois) | ||
|
||
unique_dois_found = {doi for _, doi in found_dois} | ||
unique_dois_read = set(dois_from_json.keys()) | ||
|
||
for doi in unique_dois_found: | ||
assert doi in unique_dois_read, f"{doi} not found in the json file" | ||
for doi in unique_dois_read: | ||
assert doi in unique_dois_found, f"{doi} not referenced in the code" | ||
|
||
|
||
def create_references_html(dois_from_json, code_path): | ||
"""writes HTML file with the reference list""" | ||
with open( | ||
f"{code_path}/docs/templates/bibliography.html", "w", encoding="utf8" | ||
) as fout: | ||
fout.write("<ol>\n") | ||
for doi, data in sorted( | ||
dois_from_json.items(), key=lambda item: item[1]["label"].lower() | ||
): | ||
fout.write("<li>\n") | ||
fout.write( | ||
f'<a href="{doi}">{data["label"]}: "<em>{data["title"]}</em>"</a>\n' | ||
) | ||
fout.write('<ul style="list-style-type:square;font-size:smaller;">') | ||
for path in data["usages"]: | ||
fout.write( | ||
f'<li><a href="https://github.com/open-atmos/PySDM/tree/main/{path}">' | ||
) | ||
fout.write(f"{path}</a></li>") | ||
fout.write("</ul>\n") | ||
fout.write("</ol>\n") | ||
|
||
|
||
def run_pdoc(code_path, out_path): | ||
"""generates docs in HTML formay by executing pdoc""" | ||
subprocess.run( | ||
[ | ||
sys.executable, | ||
"-We", | ||
"-m", | ||
"pdoc", | ||
"-o", | ||
f"{out_path}/html", | ||
f"{code_path}/PySDM", | ||
f"{code_path}/examples/PySDM_examples", | ||
"-t", | ||
f"{code_path}/docs/templates", | ||
"--math", | ||
"--mermaid", | ||
], | ||
env={**os.environ, "PDOC_ALLOW_EXEC": "1"}, | ||
check=True, | ||
) | ||
|
||
|
||
def _main(): | ||
assert len(sys.argv) == 3, f"usage: {sys.argv[0]} code_path out_path" | ||
code_path, out_path = sys.argv[1], sys.argv[2] | ||
generate_badges_md_files() | ||
dois_from_json = read_dois_from_json(code_path) | ||
check_dois(dois_from_json) | ||
create_references_html(dois_from_json, code_path) | ||
run_pdoc(code_path, out_path) | ||
|
||
|
||
if __name__ == "__main__": | ||
_main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,7 @@ td, th { | |
display: table; | ||
width: 100%; | ||
} | ||
|
||
ul { | ||
margin-left: 2em; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters