From 21491514b3642a321ce65f2a07428f63c4d9feb5 Mon Sep 17 00:00:00 2001 From: Benji Dial Date: Sun, 8 Nov 2020 11:11:49 -0500 Subject: manual viewer, "swap colors" system call --- tools/man-gen.py | 135 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 tools/man-gen.py (limited to 'tools/man-gen.py') diff --git a/tools/man-gen.py b/tools/man-gen.py new file mode 100644 index 0000000..30ade8a --- /dev/null +++ b/tools/man-gen.py @@ -0,0 +1,135 @@ +from sys import argv + +infn, outfn = argv[1:] + +inf = open(infn, 'r') + +title = infn[infn.rfind('/')+1:infn.rfind('.')] +lines = [] +links = [] + +coutl = bytes(0) +cllen = 0 +cword = '' +ccol = 0x0f + +def word_end(): + global cword, cllen, coutl + + if cword == '': + return + + if cllen + len(cword) > 80: + line_end() + + cllen += len(cword) + coutl += bytes(cword, 'ascii') + cword = '' + + if cllen == 80: + line_end() + +def line_end(): + global lines, cllen, coutl, ccol + + if len(lines) == 0 and cllen == 0: + return + coutl += bytes([0x20] * (80 - cllen)) + + lines.append(coutl) + coutl = bytes(0) + cllen = 0 + if ccol != 0x0f: + coutl += bytes([ccol | 0x80]) + +while True: + ch = inf.read(1) + if ch == '': + word_end() + line_end() + break + + elif ch == '\\': + word_end() + + cmd = '' + while ch != '{': + cmd += ch + ch = inf.read(1) + cmd = cmd[1:] + + content = '' + while ch != '}': + content += ch + ch = inf.read(1) + content = content[1:] + + if cmd == 'title': + title = content + elif cmd == 'link': + lto = content + cpos = content.rfind(':') + if cpos != -1: + lto = content[cpos+1:] + content = content[:cpos] + links.append((len(lines), cllen, len(content), lto)) + coutl += bytes([0x89]) + cword = content + word_end() + coutl += bytes([0x8f]) + elif cmd == 'color': + ncol = int(content, base=16) + if ccol != ncol: + ccol = ncol + coutl += bytes([ccol | 0x80]) + + elif ch == ' ': + word_end() + if cllen != 0: + coutl += bytes([0x20]) + cllen += 1 + + elif ch == '\n': + word_end() + line_end() + + else: + cword += ch + +inf.close() + +def dword(n): + return bytes([n % 256, (n // 256) % 256, (n // 65536) % 256, n // 16777216]) + +outf = open(outfn, 'wb') + +outf.write(dword(len(title))) +outf.write(bytes(title, 'ascii')) +outf.write(bytes([0])) + +outf.write(dword(len(links))) + +ltable = bytes(0) +for link in links: + line, scol, tlen, fpto = link + + ltable += dword(line) + ltable += bytes([scol, tlen]) + ltable += dword(len(fpto)) + ltable += bytes(fpto, 'ascii') + ltable += bytes([0]) + +outf.write(dword(len(ltable))) +outf.write(ltable) + +outf.write(dword(len(lines))) + +off = 4 + len(title) + 1 + 4 + 4 + len(ltable) + 4 + len(lines) * 4 +for line in lines: + outf.write(dword(off)) + off += len(line) + +for line in lines: + outf.write(line) + +outf.close() \ No newline at end of file -- cgit v1.2.3