-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b1f6bc7
commit 6464710
Showing
6 changed files
with
95 additions
and
79 deletions.
There are no files selected for viewing
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
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
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
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
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
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 |
---|---|---|
@@ -1,90 +1,106 @@ | ||
# macros for mkdocs-macros-plugin | ||
import os | ||
import requests | ||
import importlib | ||
import os | ||
from datetime import datetime | ||
|
||
import requests | ||
|
||
_inline_code_styles = { | ||
".py": "python", | ||
".sh": "bash", | ||
".h": "cpp", | ||
".cpp": "cpp", | ||
".c": "c", | ||
".rs": "rs", | ||
".js": "js", | ||
".md": None | ||
".py": "python", | ||
".sh": "bash", | ||
".h": "cpp", | ||
".cpp": "cpp", | ||
".c": "c", | ||
".rs": "rs", | ||
".js": "js", | ||
".md": None, | ||
} | ||
|
||
# this is the overall record id, not a specific version | ||
_NEWORDER_ZENODO_ID = 4031821 | ||
|
||
|
||
def write_requirements() -> None: | ||
try: | ||
with open("docs/requirements.txt", "w") as fd: | ||
fd.write(f"""# DO NOT EDIT | ||
try: | ||
with open("docs/requirements.txt", "w") as fd: | ||
fd.write( | ||
f"""\ | ||
# DO NOT EDIT | ||
# auto-generated @ {datetime.now()} by docs/macros.py::write_requirements() | ||
# required by readthedocs.io | ||
""") | ||
fd.writelines(f"{dep}=={importlib.metadata.version(dep)}\n" for dep in [ | ||
"mkdocs", | ||
"mkdocs-macros-plugin", | ||
"mkdocs-material", | ||
"mkdocs-material-extensions", | ||
"mkdocs-video", | ||
"requests" | ||
]) | ||
# ignore any error, this should only run in a dev env anyway | ||
except: | ||
pass | ||
""" | ||
) | ||
fd.writelines( | ||
f"{dep}=={importlib.metadata.version(dep)}\n" | ||
for dep in [ | ||
"mkdocs", | ||
"mkdocs-macros-plugin", | ||
"mkdocs-material", | ||
"mkdocs-material-extensions", | ||
"mkdocs-video", | ||
"requests", | ||
] | ||
) | ||
# ignore any error, this should only run in a dev env anyway | ||
except: | ||
pass | ||
|
||
|
||
def define_env(env): | ||
|
||
@env.macro | ||
def insert_zenodo_field(*keys: str): | ||
""" This is the *released* version not the dev one """ | ||
try: | ||
response = requests.get('https://zenodo.org/api/records', params={'q': _NEWORDER_ZENODO_ID, 'access_token': os.getenv("ZENODO_PAT")}) | ||
response.raise_for_status() | ||
result = response.json()["hits"]["hits"][0] | ||
for k in keys: | ||
result = result[k] | ||
return result | ||
|
||
except Exception as e: | ||
return f"{e.__class__.__name__}:{e} while retrieving {keys}" | ||
|
||
|
||
@env.macro | ||
def include_snippet(filename, tag=None, show_filename=True): | ||
""" looks for code in <filename> between lines containing "!<tag>!" """ | ||
full_filename = os.path.join(env.project_dir, filename) | ||
|
||
_, file_type = os.path.splitext(filename) | ||
# default to literal "text" for inline code style | ||
code_style = _inline_code_styles.get(file_type, "text") | ||
|
||
with open(full_filename, 'r') as f: | ||
lines = f.readlines() | ||
|
||
if tag: | ||
tag = f"!{tag}!" | ||
span = [] | ||
for i, l in enumerate(lines): | ||
if tag in l: | ||
span.append(i) | ||
if len(span) != 2: | ||
return f"```ERROR {filename} ({code_style}) too few/many tags ({len(span)}) for '{tag}'```" | ||
lines = lines[span[0] + 1: span[1]] | ||
|
||
if show_filename: | ||
title = f'title="{filename}"' | ||
else: | ||
title = "" | ||
if code_style is not None: | ||
return f"```{code_style} {title}\n{''.join(lines)}```" | ||
else: | ||
return "".join(lines) | ||
@env.macro | ||
def insert_zenodo_field(*keys: str): | ||
"""This is the *released* version not the dev one""" | ||
try: | ||
response = requests.get( | ||
"https://zenodo.org/api/records", | ||
params={ | ||
"q": _NEWORDER_ZENODO_ID, | ||
"access_token": os.getenv("ZENODO_PAT"), | ||
}, | ||
) | ||
response.raise_for_status() | ||
# with open("zenodo-result.json", "w") as fd: | ||
# print(response.text) | ||
# fd.write(response.text) | ||
result = response.json()["hits"]["hits"][0] | ||
for k in keys: | ||
result = result[k] | ||
return result | ||
|
||
except Exception as e: | ||
return f"{e.__class__.__name__}:{e} while retrieving {keys}" | ||
|
||
@env.macro | ||
def include_snippet(filename, tag=None, show_filename=True): | ||
"""looks for code in <filename> between lines containing "!<tag>!" """ | ||
full_filename = os.path.join(env.project_dir, filename) | ||
|
||
_, file_type = os.path.splitext(filename) | ||
# default to literal "text" for inline code style | ||
code_style = _inline_code_styles.get(file_type, "text") | ||
|
||
with open(full_filename, "r") as f: | ||
lines = f.readlines() | ||
|
||
if tag: | ||
tag = f"!{tag}!" | ||
span = [] | ||
for i, l in enumerate(lines): | ||
if tag in l: | ||
span.append(i) | ||
if len(span) != 2: | ||
return f"```ERROR {filename} ({code_style}) too few/many tags ({len(span)}) for '{tag}'```" | ||
lines = lines[span[0] + 1 : span[1]] | ||
|
||
if show_filename: | ||
title = f'title="{filename}"' | ||
else: | ||
title = "" | ||
if code_style is not None: | ||
return f"```{code_style} {title}\n{''.join(lines)}```" | ||
else: | ||
return "".join(lines) | ||
|
||
|
||
# write_requirements() |