Created
January 31, 2025 11:16
-
-
Save agoose77/ed6b9c1b2c32a10ad9e30912bb146323 to your computer and use it in GitHub Desktop.
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
# See docs at: https://mystmd.org/guide/frontmatter | |
version: 1 | |
project: | |
plugins: | |
- type: executable | |
path: table-plugin.py | |
site: | |
template: book-theme |
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
#!/usr/bin/env python3 | |
import argparse | |
import json | |
import sys | |
plugin = { | |
"name": "Strong to emphasis", | |
"transforms": [ | |
{ | |
"name": "transform-typography", | |
"doc": "An example transform that rewrites bold text as text with emphasis.", | |
"stage": "document", | |
} | |
], | |
} | |
def find_all_by_type(node: dict, type_: str): | |
"""Simple node visitor that matches a particular node type | |
:param parent: starting node | |
:param type_: type of the node to search for | |
""" | |
if node["type"] == type_: | |
yield node | |
if "children" not in node: | |
return | |
for next_node in node["children"]: | |
yield from find_all_by_type(next_node, type_) | |
def declare_result(content): | |
"""Declare result as JSON to stdout | |
:param content: content to declare as the result | |
""" | |
# Format result and write to stdout | |
json.dump(content, sys.stdout, indent=2) | |
# Successfully exit | |
raise SystemExit(0) | |
def to_text(node): | |
if "children" in node: | |
return "".join(to_text(c) for c in node["children"]) | |
elif "value" in node: | |
return node["value"] | |
else: | |
return "" | |
###### Custom part of the plugin | |
colours = { | |
"positively charged": "#CBE4F9", | |
"negatively charged": "#CDF5F6", | |
"polar uncharged": "#EFF9DA", | |
"hydrophobic": "#F9EBDF", | |
"hydrophobic and aromatic": "#F9D8D6", | |
"special": "#D6CDEA", | |
} | |
def find_colour(row): | |
for col in row["children"]: | |
body = to_text(col) | |
for pattern, colour in colours.items(): | |
if body.lower().startswith(pattern): | |
return colour | |
def run_transform(name, data): | |
"""Execute a transform with the given name and data | |
:param name: name of the transform to run | |
:param data: AST of the document upon which the transform is being run | |
""" | |
assert name == "transform-typography" | |
for container_node in find_all_by_type(data, "container"): | |
classes = {*container_node.get("class", "").split()} | |
if "amino-acids" not in classes: | |
continue | |
for table_node in find_all_by_type(container_node, "table"): | |
for row in table_node["children"]: | |
# Skip header rows | |
if any(child.get("header") for child in row["children"]): | |
continue | |
colour = find_colour(row) | |
if colour is not None: | |
row["style"] = {"background-color": colour} | |
return data | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
group = parser.add_mutually_exclusive_group() | |
group.add_argument("--role") | |
group.add_argument("--directive") | |
group.add_argument("--transform") | |
args = parser.parse_args() | |
if args.directive: | |
raise NotImplementedError | |
elif args.transform: | |
data = json.load(sys.stdin) | |
declare_result(run_transform(args.transform, data)) | |
elif args.role: | |
raise NotImplementedError | |
else: | |
declare_result(plugin) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment