From 37e63b18d032317a3a6c74ac0896db65765f8246 Mon Sep 17 00:00:00 2001 From: Fenris Wolf Date: Wed, 30 Jul 2025 07:17:08 +0000 Subject: [PATCH] [ini] --- .gitignore | 1 + source/main.py | 236 +++++++++++++++++++++++++++++++++++++++++++++++++ tools/build | 15 ++++ tools/install | 10 +++ 4 files changed, 262 insertions(+) create mode 100644 .gitignore create mode 100644 source/main.py create mode 100755 tools/build create mode 100755 tools/install diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..84c048a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/build/ diff --git a/source/main.py b/source/main.py new file mode 100644 index 0000000..214d0f7 --- /dev/null +++ b/source/main.py @@ -0,0 +1,236 @@ +import sys as _sys +import argparse as _argparse + + +def string_coin( + template, + arguments +): + result = template + for (key, value, ) in arguments.items(): + result = result.replace("{{%s}}" % key, value) + return result + + +def css_render_color_hsl( + css_color_hsl +): + return string_coin( + "hsl({{hue}},{{saturation}},{{lightness}})", + { + "hue": ("%.4f" % (css_color_hsl["hue"] * 360)), + "saturation": ("%.4f%%" % (css_color_hsl["saturation"] * 100)), + "lightness": ("%.4f%%" % (css_color_hsl["lightness"] * 100)), + } + ) + + +def css_render_assignments( + css_assignments +): + return ( + "; ".join( + map( + lambda pair: string_coin( + "{{name}}: {{value}}", + { + "name": pair[0], + "value": pair[1], + } + ), + css_assignments.items() + ) + ) + ) + + +def css_render_rule( + css_rule +): + return string_coin( + "{{selector}} {{{assignments}}}", + { + "selector": css_rule["selector"], + "assignments": css_render_assignments(css_rule["assignments"]), + } + ) + + +def make_stylesheet( + css_rules +): + return ( + "\n".join( + map( + lambda css_rule: css_render_rule(css_rule), + css_rules + ) + ) + ) + + +def make_doc( + content, + data +): + return string_coin( + "\n{{html}}\n", + { + "html": string_coin( + "\n{{head}}\n{{body}}\n\n", + { + "head": string_coin( + "\n{{encoding}}\n{{viewport}}\n{{style}}\n{{macro_title}}\n", + { + "encoding": string_coin( + "", + { + "charset": data["encoding"], + } + ), + "viewport": string_coin( + "", + { + } + ), + "style": string_coin( + "", + { + "style": make_stylesheet( + [ + { + "selector": "html", + "assignments": { + "background-color": css_render_color_hsl( + { + "hue": data["hue"], + "saturation": 0, + "lightness": 0, + } + ), + "color": css_render_color_hsl( + { + "hue": data["hue"], + "saturation": 0, + "lightness": 1, + } + ), + } + }, + { + "selector": "body", + "assignments": { + "background-color": css_render_color_hsl( + { + "hue": data["hue"], + "saturation": 0, + "lightness": (1/8), + } + ), + "color": css_render_color_hsl( + { + "hue": data["hue"], + "saturation": 0, + "lightness": (7/8), + } + ), + "max-width": "960px", + "margin": "auto", + "padding": "32px", + } + }, + { + "selector": "a", + "assignments": { + "color": css_render_color_hsl( + { + "hue": data["hue"], + "saturation": (1/2), + "lightness": (2/4), + } + ), + "text-decoration": "none", + } + }, + { + "selector": "a:hover", + "assignments": { + "color": css_render_color_hsl( + { + "hue": data["hue"], + "saturation": (1/2), + "lightness": (3/4), + } + ), + } + }, + ] + ), + } + ), + "macro_title": ( + "" + if (data["title"] is None) else + string_coin( + "{{title}}\n", + { + "title": data["title"], + } + ) + ), + } + ), + "body": string_coin( + "{{content}}", + { + "content": content, + } + ), + } + ), + } + ) + + +def main( +): + ## args + argument_parser = _argparse.ArgumentParser() + argument_parser.add_argument( + "-t", + "--title", + type = str, + default = None, + ) + argument_parser.add_argument( + "-c", + "--hue", + type = int, + default = 150, + ) + argument_parser.add_argument( + "-e", + "--encoding", + type = str, + default = "utf-8", + ) + args = argument_parser.parse_args() + + ## exec + content_in = _sys.stdin.read( + ) + content_out = make_doc( + content_in, + { + "encoding": args.encoding, + "hue": (args.hue / 360), + "title": args.title, + } + ) + _sys.stdout.write( + content_out + ) + + +main() + diff --git a/tools/build b/tools/build new file mode 100755 index 0000000..98349a7 --- /dev/null +++ b/tools/build @@ -0,0 +1,15 @@ +#!/usr/bin/env sh + +## const + +dir_source=source +dir_build=build + + +## exec + +mkdir -p ${dir_build} +echo "#!/usr/bin/env python3" > ${dir_build}/html-docwrap +cat ${dir_source}/main.py >> ${dir_build}/html-docwrap +chmod +x ${dir_build}/html-docwrap + diff --git a/tools/install b/tools/install new file mode 100755 index 0000000..d38db5e --- /dev/null +++ b/tools/install @@ -0,0 +1,10 @@ +#!/usr/bin/env sh + +if $(test "$(whoami)" != "root") +then + echo "need to be root" > /dev/stderr + exit 1 +else + cp build/html-docwrap /usr/local/bin/ +fi +