Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support doc comments & complex quoting #15

Merged
merged 6 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions changelog.org
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
* v0.2.1
- support Nim doc comments and turn them into LaTeX comments
- support complex expressions in a quote, i.e. `x.foo.bar`.
*Note*: *Only* dot expressions are supported. Call or command syntax
are not allowed!
- *Potentially breaking* (unlikely): If you used the ~compile~ helper
with ~fullBody = true~, previously it injected a ~\begin/end
document~ around the given body. That is not really very useful, if
one wants to pass a full TeX file to compile including header and
everything.
* v0.2.0
- ~DEBUG_TEX~ can be set as an environment variable to overwrite the
verbosity setting of the LaTeX compiler
Expand Down
2 changes: 1 addition & 1 deletion latexdsl.nimble
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Package

version = "0.2.0"
version = "0.2.1"
author = "Vindaar"
description = "A DSL to write LaTeX in Nim. No idea who wants that."
license = "MIT"
Expand Down
38 changes: 37 additions & 1 deletion src/latexdsl/dsl_impl.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import macros, strutils, strformat, sequtils

Check warning on line 1 in src/latexdsl/dsl_impl.nim

View workflow job for this annotation

GitHub Actions / linux (version-1-6)

imported and not used: 'sequtils' [UnusedImport]

Check warning on line 1 in src/latexdsl/dsl_impl.nim

View workflow job for this annotation

GitHub Actions / linux (version-2-0)

imported and not used: 'sequtils' [UnusedImport]

Check warning on line 1 in src/latexdsl/dsl_impl.nim

View workflow job for this annotation

GitHub Actions / linux (version-2-0)

imported and not used: 'sequtils' [UnusedImport]

Check warning on line 1 in src/latexdsl/dsl_impl.nim

View workflow job for this annotation

GitHub Actions / linux (version-1-6)

imported and not used: 'sequtils' [UnusedImport]

Check warning on line 1 in src/latexdsl/dsl_impl.nim

View workflow job for this annotation

GitHub Actions / linux (devel)

imported and not used: 'sequtils' [UnusedImport]

Check warning on line 1 in src/latexdsl/dsl_impl.nim

View workflow job for this annotation

GitHub Actions / linux (devel)

imported and not used: 'sequtils' [UnusedImport]

Check warning on line 1 in src/latexdsl/dsl_impl.nim

View workflow job for this annotation

GitHub Actions / linux (version-2-0)

imported and not used: 'sequtils' [UnusedImport]

Check warning on line 1 in src/latexdsl/dsl_impl.nim

View workflow job for this annotation

GitHub Actions / linux (version-2-0)

imported and not used: 'sequtils' [UnusedImport]

Check warning on line 1 in src/latexdsl/dsl_impl.nim

View workflow job for this annotation

GitHub Actions / linux (devel)

imported and not used: 'sequtils' [UnusedImport]

Check warning on line 1 in src/latexdsl/dsl_impl.nim

View workflow job for this annotation

GitHub Actions / linux (devel)

imported and not used: 'sequtils' [UnusedImport]

Check warning on line 1 in src/latexdsl/dsl_impl.nim

View workflow job for this annotation

GitHub Actions / windows (version-1-6)

imported and not used: 'sequtils' [UnusedImport]

Check warning on line 1 in src/latexdsl/dsl_impl.nim

View workflow job for this annotation

GitHub Actions / windows (version-1-6)

imported and not used: 'sequtils' [UnusedImport]

Check warning on line 1 in src/latexdsl/dsl_impl.nim

View workflow job for this annotation

GitHub Actions / windows (version-2-0)

imported and not used: 'sequtils' [UnusedImport]

Check warning on line 1 in src/latexdsl/dsl_impl.nim

View workflow job for this annotation

GitHub Actions / windows (version-2-0)

imported and not used: 'sequtils' [UnusedImport]

Check warning on line 1 in src/latexdsl/dsl_impl.nim

View workflow job for this annotation

GitHub Actions / windows (devel)

imported and not used: 'sequtils' [UnusedImport]

Check warning on line 1 in src/latexdsl/dsl_impl.nim

View workflow job for this annotation

GitHub Actions / windows (devel)

imported and not used: 'sequtils' [UnusedImport]

Check warning on line 1 in src/latexdsl/dsl_impl.nim

View workflow job for this annotation

GitHub Actions / windows (version-2-0)

imported and not used: 'sequtils' [UnusedImport]

Check warning on line 1 in src/latexdsl/dsl_impl.nim

View workflow job for this annotation

GitHub Actions / windows (version-2-0)

imported and not used: 'sequtils' [UnusedImport]

Check warning on line 1 in src/latexdsl/dsl_impl.nim

View workflow job for this annotation

GitHub Actions / windows (devel)

imported and not used: 'sequtils' [UnusedImport]

Check warning on line 1 in src/latexdsl/dsl_impl.nim

View workflow job for this annotation

GitHub Actions / windows (devel)

imported and not used: 'sequtils' [UnusedImport]

import macrocache
const NoCommandChecks = CacheCounter"CommandCounterCheck"
Expand Down Expand Up @@ -218,13 +218,44 @@
of nnkIdent: result = toTex(n)
of nnkIntLit, nnkFloatLit: result = toTex(n)
of nnkAccQuoted: result = toTex(n)
of nnkCommentStmt: result = toTex(n)
#of nnkPar: result = toTex(n)
else:
error("Invalid kind " & $n.kind)

proc fromAccToDotExpr(n: NimNode): NimNode =
## Turns a accent quoted node back into a dot expression to evaluate
## the quoted Nim expression.
doAssert n.kind == nnkAccQuoted
if n.len == 1:
result = nnkCall.newTree(ident"$", n[0])
elif n.len mod 2 == 0:
error("Invalid term in accent quoted: " & $n.treerepr & ". Only supports " &
"dot expressions at the moment, e.g. `foo.bar.baz`.")
else:
var res: NimNode
for i in countup(0, n.len - 2, 2):
## This processes:
## AccQuoted
## Ident "cv" Iter 0, i = 0
## Ident "." Iter 0, i = 1
## Ident "cfg" Iter 0, i = 2; Iter 1, i = 0
## Ident "." Iter 1, i = 1
## Ident "pEmail" Iter 1, i = 2
## so we reassign to `res` below.
doAssert n[i+1].kind == nnkIdent and n[i+1].strVal == ".", "The node at index : " & $i & " is not a `.`. Invalid " &
"node in accent quote: " & $n.treerepr
if res == nil:
res = nnkDotExpr.newTree(n[i], n[i+2])
else:
# in this case `n[i]` already processed in `res`
res = nnkDotExpr.newTree(res, n[i+2])
result = res

proc toTex(n: NimNode): NimNode =
case n.kind
of nnkSym: result = n
of nnkAccQuoted: result = nnkCall.newTree(ident"$", n[0])
of nnkAccQuoted: result = fromAccToDotExpr(n)
of nnkIdent, nnkStrLit, nnkTripleStrLit, nnkRStrLit:
let nStr = n.strVal
result = if nStr == "\\\\": newLit r"\\" else: newLit nStr
Expand All @@ -237,6 +268,11 @@
result = n
else:
result = parseBody(n)
of nnkCommentStmt:
var cmt: string
for el in n.strVal.splitLines:
cmt.add "%" & el
result = newLit(cmt)
else: result = parseBody(n)

proc unsym(n: NimNode): NimNode =
Expand Down
4 changes: 3 additions & 1 deletion src/latexdsl/latex_compiler.nim
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ proc compile*(fname, body: string, tmpl = getStandaloneTmpl(),
of "xelatex": xelatexFontSettings()
of "lualatex": lualatexFontSettings()
else: ""
let body = body.replace(r"\begin{document}", fontSettings & "\n" & r"\begin{document}")
var body = body
if not fullBody:
body = body.replace(r"\begin{document}", fontSettings & "\n" & r"\begin{document}")
# 2. write the TeX file with injected body
writeTexFile(fname, body, tmpl, fullBody)
when nimvm:
Expand Down
4 changes: 2 additions & 2 deletions src/latexdsl/latex_helpers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ type
$v is string
DataFrameLike* = concept df
df.getKeys() is seq[string]
df.row(int) is ValueLike
df.len is int
df.row(system.int) is ValueLike
df.len is system.int

AlignmentKind* = enum
akNone = ""
Expand Down
Loading