This commit is contained in:
Daylin Morgan 2024-10-30 14:22:23 -05:00
parent b14564021b
commit 6bd9bc97da
Signed by: daylin
GPG key ID: 950D13E9719334AD
6 changed files with 144 additions and 0 deletions

4
.gitignore vendored Normal file
View file

@ -0,0 +1,4 @@
nimbledeps
nimble.develop
nimble.paths
bin/

21
LICENSE Normal file
View file

@ -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.

10
config.nims Normal file
View file

@ -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

26
nimble.lock Normal file
View file

@ -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": {}
}

68
src/typstgen.nim Normal file
View file

@ -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)

15
typstgen.nimble Normal file
View file

@ -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"