feat: tex rendering via temml
This commit is contained in:
55
doerg/doerg-tex/index.js
Executable file
55
doerg/doerg-tex/index.js
Executable file
@@ -0,0 +1,55 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const commandLineArgs = require ("command-line-args")
|
||||
const temml = require ("temml")
|
||||
const fs = require ("node:fs")
|
||||
const path = require ("node:path")
|
||||
const { DecoderStream, EncoderStream } = require ("cbor-x")
|
||||
const { Transform } = require('node:stream')
|
||||
|
||||
const cli_spec =
|
||||
[ { name: "preamble" }
|
||||
]
|
||||
|
||||
function load_preambles (preamble) {
|
||||
const data = fs.readFileSync (preamble, "utf8") .toString ()
|
||||
return temml.definePreamble (data)
|
||||
}
|
||||
|
||||
let macros = {}
|
||||
|
||||
function is_render_command (cmd) {
|
||||
return cmd instanceof Array
|
||||
&& cmd.length === 1
|
||||
&& typeof cmd[0] === "string"
|
||||
}
|
||||
|
||||
function do_command (cmd) {
|
||||
try {
|
||||
if (typeof cmd === "string") {
|
||||
return temml.renderToString (cmd, {macros})
|
||||
} else if (is_render_command (cmd)) {
|
||||
return temml.renderToString (cmd[0], {displayMode: true,macros})
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
} catch (e) {
|
||||
return e
|
||||
}
|
||||
}
|
||||
|
||||
function main () {
|
||||
const options = commandLineArgs (cli_spec)
|
||||
console.error (options)
|
||||
macros = load_preambles (options.preamble)
|
||||
const decoder = new DecoderStream ()
|
||||
const encoder = new EncoderStream ()
|
||||
process.stdin.pipe (decoder)
|
||||
const command_responses = decoder.map (do_command)
|
||||
decoder
|
||||
.map (do_command)
|
||||
.pipe (encoder)
|
||||
.pipe (process.stdout)
|
||||
}
|
||||
|
||||
main ()
|
||||
Reference in New Issue
Block a user