From 6bd9bc97da887bd112c0e501fd0b4b924602d48e Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Wed, 30 Oct 2024 14:22:23 -0500 Subject: [PATCH] mvp --- .gitignore | 4 +++ LICENSE | 21 +++++++++++++++ config.nims | 10 +++++++ nimble.lock | 26 ++++++++++++++++++ src/typstgen.nim | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ typstgen.nimble | 15 +++++++++++ 6 files changed, 144 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 config.nims create mode 100644 nimble.lock create mode 100644 src/typstgen.nim create mode 100644 typstgen.nimble diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..baed4d3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +nimbledeps +nimble.develop +nimble.paths +bin/ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..7048b81 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Daylin Morgan + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/config.nims b/config.nims new file mode 100644 index 0000000..48f2499 --- /dev/null +++ b/config.nims @@ -0,0 +1,10 @@ +task build, "build": + switch("outdir", "bin") + setCommand "c", "src/typstgen.nim" + + +# begin Nimble config (version 2) +--noNimblePath +when withDir(thisDir(), system.fileExists("nimble.paths")): + include "nimble.paths" +# end Nimble config diff --git a/nimble.lock b/nimble.lock new file mode 100644 index 0000000..9a28c05 --- /dev/null +++ b/nimble.lock @@ -0,0 +1,26 @@ +{ + "version": 2, + "packages": { + "hwylterm": { + "version": "0.1.0", + "vcsRevision": "7385a93110f95e1d67d803780efa014fdc960779", + "url": "https://github.com/daylinmorgan/hwylterm", + "downloadMethod": "git", + "dependencies": [], + "checksums": { + "sha1": "bf170216cff989ea17abfb97946b9f0804c0e50b" + } + }, + "yaml": { + "version": "2.1.1", + "vcsRevision": "48a90e36e82bd97457dae87e86efe423dcd3bb40", + "url": "https://github.com/flyx/NimYAML", + "downloadMethod": "git", + "dependencies": [], + "checksums": { + "sha1": "302727fcd74c79d0697a4e909d26455d61a5b979" + } + } + }, + "tasks": {} +} diff --git a/src/typstgen.nim b/src/typstgen.nim new file mode 100644 index 0000000..a86e815 --- /dev/null +++ b/src/typstgen.nim @@ -0,0 +1,68 @@ +import std/[streams, tables, strutils, os] +import yaml, hwylterm + + +type + Config = object + components {.defaultVal: @[]}: seq[Table[string, string]] + templates {.defaultVal: initTable[string, string]()}: Table[string, string] + + +proc typstGen(configPath: string) = + + var config: Config + var s = newFileStream(configPath) + load(s, config) + s.close() + + var output: string + + if not config.templates.hasKey("raw"): + config.templates["raw"] = "$text" + + + func componentToFmtArgs(t: Table[string, string]): seq[string] = + for k, v in t.pairs: + result.add k + result.add v + + for component in config.components: + var kind = "figure" + var component = component + discard component.pop("kind", kind) + output &= config.templates[kind] % componentToFmtArgs(component) + + echo output + + +when isMainModule: + import hwylterm/[cli, parseopt3] + proc writeHelp() = + echo newHwylCli( + "[bold]typstgen[/] [[[faint]-h[/]]", + """ + typstgen --config typstgen.yml""", + [ + ("h", "help", "show this help"), + ("c","config", "path to config file"), + ] + ) + var p = initOptParser() + var configPath = "typstgen.yml" + for kind, key, val in p.getopt(): + case kind + of cmdError: quit($bb"[red]cli error[/]: " & p.message) + of cmdEnd: assert false + of cmdArgument: quit($bb"[red] unexpected argument[/]: " & key) + of cmdShortOption, cmdLongOption: + case key + of "help", "h": + writeHelp(); quit 0 + of "config", "c": + configPath = val + + if not fileExists(configPath): + quit($bbfmt("file: [b] {configPath} does not exist")) + typstGen(configPath) + + diff --git a/typstgen.nimble b/typstgen.nimble new file mode 100644 index 0000000..a9ef3b7 --- /dev/null +++ b/typstgen.nimble @@ -0,0 +1,15 @@ +# Package + +version = "0.1.0" +author = "Daylin Morgan" +description = "generating typst files" +license = "MIT" +srcDir = "src" +bin = @["typstgen"] + + +# Dependencies + +requires "nim >= 2.2.0" +requires "yaml >= 2.1.1" +requires "https://github.com/daylinmorgan/hwylterm#HEAD"