diff options
| author | Vosjedev <vosje@vosjedev.net> | 2026-02-08 16:58:37 +0100 |
|---|---|---|
| committer | Vosjedev <vosje@vosjedev.net> | 2026-02-08 16:58:37 +0100 |
| commit | c6f203b9f035f54cecbb20450a309cb0596ccfff (patch) | |
| tree | 85eb24b747f2e3bb0919d66b892e8cacd166ad08 /tools | |
| download | website-c6f203b9f035f54cecbb20450a309cb0596ccfff.tar.gz website-c6f203b9f035f54cecbb20450a309cb0596ccfff.tar.bz2 website-c6f203b9f035f54cecbb20450a309cb0596ccfff.tar.xz | |
WTF am I doing atp
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/__pycache__/discord_md.cpython-313.pyc | bin | 0 -> 709 bytes | |||
| -rw-r--r-- | tools/dc_md/__pycache__/simples.cpython-313.pyc | bin | 0 -> 5618 bytes | |||
| -rw-r--r-- | tools/dc_md/simples.py | 88 | ||||
| -rw-r--r-- | tools/discord_md.py | 29 | ||||
| -rw-r--r-- | tools/gen.py | 80 |
5 files changed, 197 insertions, 0 deletions
diff --git a/tools/__pycache__/discord_md.cpython-313.pyc b/tools/__pycache__/discord_md.cpython-313.pyc Binary files differnew file mode 100644 index 0000000..3650153 --- /dev/null +++ b/tools/__pycache__/discord_md.cpython-313.pyc diff --git a/tools/dc_md/__pycache__/simples.cpython-313.pyc b/tools/dc_md/__pycache__/simples.cpython-313.pyc Binary files differnew file mode 100644 index 0000000..f90b48b --- /dev/null +++ b/tools/dc_md/__pycache__/simples.cpython-313.pyc diff --git a/tools/dc_md/simples.py b/tools/dc_md/simples.py new file mode 100644 index 0000000..ffae2fb --- /dev/null +++ b/tools/dc_md/simples.py @@ -0,0 +1,88 @@ + +# literaly the example at https://python-markdown.github.io/extensions/api/#inlineprocessors +# but modified to use ~~text~~ instead of --text-- + +from markdown.inlinepatterns import InlineProcessor +from markdown.blockprocessors import BlockProcessor +from markdown import util +from markdown.extensions import Extension +import xml.etree.ElementTree as etree +import re + + +class DelInlineProcessor(InlineProcessor): + def handleMatch(self, m, data): + el = etree.Element('del') + el.text = m.group(1) + return el, m.start(0), m.end(0) + +class DelExtension(Extension): + def extendMarkdown(self, md): + DEL_PATTERN = r'~~(.*?)~~' # like ~~del~~ + md.inlinePatterns.register(DelInlineProcessor(DEL_PATTERN, md), 'del', 175) + + +# once again, but this time for __underlined__ +# works a bit weird with italic-underlined like ___this___. use __*this*__ instead. + +class UInlineProcessor(InlineProcessor): + def handleMatch(self, m, data): + el = etree.Element('u') + el.text = m.group(1) + return el, m.start(0), m.end(0) + +class UExtension(Extension): + def extendMarkdown(self, md): + U_PATTERN = r'__(.*?)__' # like __underline__ + md.inlinePatterns.register(UInlineProcessor(U_PATTERN, md), 'u', 175) + + +# for small text +# taken from https://github.com/Python-Markdown/markdown/blob/6347c57c8f309dcb0aaaa67b60d0f0039ff44484/markdown/blockprocessors.py#L284 +# and modified to use `-# ` instead of `> ` +class SmallTextProcessor(BlockProcessor): + """ Process small text. """ + + RE = re.compile(r'(^|\n)[ ]{0,3}-\#[ ]?(.*)') + + def test(self, parent: etree.Element, block: str) -> bool: + return bool(self.RE.search(block)) and not util.nearing_recursion_limit() + + def run(self, parent: etree.Element, blocks: list[str]) -> None: + block = blocks.pop(0) + m = self.RE.search(block) + if m: + before = block[:m.start()] # Lines before small + # Pass lines before small in recursively for parsing first. + self.parser.parseBlocks(parent, [before]) + # Remove `-# ` from beginning of each line. + block = '\n'.join( + [self.clean(line) for line in block[m.start():].split('\n')] + ) + sibling = self.lastChild(parent) + if sibling is not None and sibling.tag == "small": + # Previous block was a small so set that as this blocks parent + quote = sibling + else: + # This is a new small. Create a new parent element. + quote = etree.SubElement(parent, 'small') + # Recursively parse block with small as parent. + # change parser state so smalls embedded in lists use `p` tags + self.parser.state.set('small') + self.parser.parseChunk(quote, block) + self.parser.state.reset() + + def clean(self, line: str) -> str: + """ Remove `-#` from beginning of a line. """ + m = self.RE.match(line) + if line.strip() == "-#": + return "" + elif m: + return m.group(2) + else: + return line + + +class SmallExtension(Extension): + def extendMarkdown(self, md): + md.parser.blockprocessors.register(SmallTextProcessor(md.parser), 'small', 175) diff --git a/tools/discord_md.py b/tools/discord_md.py new file mode 100644 index 0000000..127a9ad --- /dev/null +++ b/tools/discord_md.py @@ -0,0 +1,29 @@ + +import markdown + +# markdown extensions + +## new code: +from dc_md.simples import ( + DelExtension, + UExtension, + SmallExtension + ) + +md=markdown.Markdown(extensions=[ + 'meta', + 'toc', + 'extra', + 'codehilite', + 'nl2br', + 'wikilinks', + DelExtension(), + UExtension(), + SmallExtension() + ]) + + +if __name__=='__main__': + import sys + print(convert(sys.stdin.read()).convert()) + diff --git a/tools/gen.py b/tools/gen.py new file mode 100644 index 0000000..e2f65e1 --- /dev/null +++ b/tools/gen.py @@ -0,0 +1,80 @@ +import os, shutil +from discord_md import md +import json +from collections import UserDict + +class IgnoreUnfoundPlaceholders(UserDict): + """Wrapper around dicts for using in str.format() + It basically tries to lookup the key it gets, and if the key doesn't exist, + it returns ``{keyname}`` where ``keyname`` is the key it got + """ + def __missing__(self, key): + return "{%s}"%str(key) + +# with open("conf.json", "r") as fd: +# conf=json.load(fd) + +with open("html/footer.html","r") as fd: + footer=fd.read() + +with open("html/header.html","r") as fd: + header=fd.read() + +with open("html/pagetemplate.html","r") as fd: + template=fd.read() + +template=template.format_map( + IgnoreUnfoundPlaceholders(header=header, footer=footer) +) + +if os.path.isdir("out"): + shutil.rmtree("out") + +out=os.path.abspath('out') +os.makedirs(out) +os.symlink(os.path.abspath("static"), os.path.join(out,"static"), True) +#os.chdir("content") + +def processdir(path): + print("processing",path) + os.chdir(path) + parent=os.path.dirname(path) + for file in os.listdir(): + if file.endswith(".md"): + + with open(file, "r") as fd: + print("| reading",file) + text=md.convert(fd.read()) + + meta:dict=md.Meta + print("Meta:",meta) + section=meta.get("section",[""])[0] + + html=template.format( + title=meta.get("title",["<No title>"])[0] + (" - "+section if section else ''), + section=section, + content=text, + toc=md.toc if not meta.get("notoc") else "" + ) + + newname=file.removesuffix(".md")+".html" + newname=newname.replace("README","index") + + target=os.path.join(out, section) + if not os.path.isdir(target): + os.makedirs(target) + + with open(os.path.join(out, section, newname), "x") as fd: + print("| writing") + fd.write(html) + + elif os.path.isdir(file): + processdir(os.path.join(path,file)) + + else: + shutil.copy2(file, os.path.join(out, os.path.basename(path))) + + os.chdir(parent) + +processdir("./content") + |
