| 1 |
% Kale Ewasiuk ([email protected]) |
| 2 |
% 2025-02-11 |
| 3 |
% Copyright (C) 2021-2025 Kale Ewasiuk |
| 4 |
% |
| 5 |
% Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 |
% of this software and associated documentation files (the "Software"), to deal |
| 7 |
% in the Software without restriction, including without limitation the rights |
| 8 |
% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 |
% copies of the Software, and to permit persons to whom the Software is |
| 10 |
% furnished to do so, subject to the following conditions: |
| 11 |
% |
| 12 |
% The above copyright notice and this permission notice shall be included in |
| 13 |
% all copies or substantial portions of the Software. |
| 14 |
% |
| 15 |
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF |
| 16 |
% ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
| 17 |
% TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 18 |
% PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 19 |
% SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR |
| 20 |
% ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 21 |
% ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 |
% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE |
| 23 |
% OR OTHER DEALINGS IN THE SOFTWARE. |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
\NeedsTeXFormat{LaTeX2e} |
| 28 |
\ProvidesPackage{yamlvars}[2025-02-11] |
| 29 |
|
| 30 |
\RequirePackage{luacode} |
| 31 |
\RequirePackage{etoolbox} |
| 32 |
\RequirePackage{luakeys} |
| 33 |
\RequirePackage{penlightplus} % no option passed here, but extras must be used, error will be thrown if not |
| 34 |
|
| 35 |
%\IfFileExists{tinyyaml.lua}{}{\PackageError{yamlvars}{This package requires installation of the 'tinyyaml' package, please install it and try again}{}} |
| 36 |
|
| 37 |
|
| 38 |
\luadirect{YAMLvars = require('YAMLvars')} |
| 39 |
|
| 40 |
\luadirect{ |
| 41 |
if YAMLvars.yaml == nil then |
| 42 |
tex.sprint('\\PackageError{yamlvars}{"tinyyaml.lua" not found. Install the "lua-tinyyaml" package from CTAN or include "tinyyaml.lua" in your project}{}') |
| 43 |
end |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
\DeclareOption{useyv}{ |
| 50 |
\NewDocumentCommand{\yv}{m}{\csuse{yv#1}} |
| 51 |
\luadirect{YAMLvars.setts.prc = 'yvdef'} |
| 52 |
\luadirect{YAMLvars.settsdefault.prc = 'yvdef'} |
| 53 |
} |
| 54 |
\DeclareOption{parseCLI}{ |
| 55 |
\AtBeginDocument{\luadirect{YAMLvars.doYAMLfiles(YAMLvars.getYAMLcli())}} |
| 56 |
} |
| 57 |
\DeclareOption{debug}{ |
| 58 |
\luadirect{YAMLvars.debug = true} |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
\newcommand{\yamlvarsdebugon}{\luadirect{YAMLvars.debug = true}} |
| 71 |
\newcommand{\yamlvarsdebugoff}{\luadirect{YAMLvars.debug = false}} |
| 72 |
\newcommand{\lowercasevarYVon}{\luadirect{YAMLvars.lowvasevarall = true}} |
| 73 |
\newcommand{\lowercasevarYVoff}{\luadirect{YAMLvars.lowvasevarall = false}} |
| 74 |
\newcommand{\AllowUndeclaredYV}{\luadirect{YAMLvars.setts.undeclared = true}} |
| 75 |
\newcommand{\ForbidUndeclaredYV}{\luadirect{YAMLvars.setts.undeclared = false}} |
| 76 |
\newcommand{\TextualYV}{\luadirect{ |
| 77 |
YAMLvars.allowUndeclared = true |
| 78 |
YAMLvars.xfmDefault = {'lb2nl','addxspace'} |
| 79 |
}} |
| 80 |
\DeclareOption{allowundeclared}{ |
| 81 |
\luadirect{YAMLvars.settsdefault.undeclared = true} |
| 82 |
} |
| 83 |
\DeclareOption{overwritedefs}{ |
| 84 |
\luadirect{YAMLvars.settsdefault.overwrite = true} |
| 85 |
} |
| 86 |
%%%%%%%%%%%%%%%%%%%%%% |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
\DeclareOption*{\PackageWarning{YAMLvars}{Unknown option: '\CurrentOption'}{}} |
| 95 |
\ProcessOptions\relax |
| 96 |
|
| 97 |
\luadirect{YAMLvars.setts2default()} |
| 98 |
|
| 99 |
|
| 100 |
\NewDocumentCommand{\YAMLvarsSetKeyVals}{}{\luadirect{YAMLvars.callkeyvals()}} |
| 101 |
|
| 102 |
|
| 103 |
\newcommand{\declareYAMLvarsFile}[1]{\luadirect{YAMLvars.declareYAMLvarsFile(\luastring{#1})}} |
| 104 |
\NewDocumentCommand{\parseYAMLvarsFile}{m}{\luadirect{ |
| 105 |
YAMLvars.parseYAMLvarsFile(\luastring{#1}) |
| 106 |
}} |
| 107 |
|
| 108 |
|
| 109 |
\newenvironment{declareYAMLvars}{\luadirect{penlight.tex.startrecording()}}{\luadirect{penlight.tex.stoprecording()} |
| 110 |
\luadirect{YAMLvars.declareYAMLvarsStr(penlight.tex.recordedbuf)} |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
\NewDocumentEnvironment{parseYAMLvars}{}% |
| 115 |
{\luadirect{ |
| 116 |
penlight.tex.startrecording() |
| 117 |
}}% |
| 118 |
{\luadirect{ |
| 119 |
penlight.tex.stoprecording() |
| 120 |
}} |
| 121 |
\AfterEndEnvironment{parseYAMLvars}{\luadirect{YAMLvars.parseYAMLvarsStr(penlight.tex.recordedbuf)}\YAMLvarsSetKeyVals} |
| 122 |
% necessary hack to get rohead and lohead to work.. |
| 123 |
% .. https://tex.stackexchange.com/questions/637018/setting-koma-heading-within-lua/637021?noredirect=1#comment1587387_637021 |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
\newcommand{\resetYAMLvarsspec}{\luadirect{YAMLvars.varspec = {}}} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
\newenvironment{parseYAMLpdfdata}{\luadirect{penlight.tex.startrecording()}}{ |
| 132 |
\luadirect{penlight.tex.stoprecording() |
| 133 |
YAMLvars.yaml2PDFmetadata(penlight.tex.recordedbuf) |
| 134 |
penlight.tex.writePDFmetadata() |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
\NewDocumentCommand{\setYAMLvars}{s m }{\luadirect{ |
| 142 |
YAMLvars.updatesettskv(\luastring{#2}, \luastring{#1}) |
| 143 |
}} |
| 144 |
|
| 145 |
\NewDocumentCommand{\setdefYAMLvars}{ m }{\luadirect{ |
| 146 |
YAMLvars.updatesettskv(\luastring{#1}, pl.tex._xFalse, true) |
| 147 |
}} % set the default settings |
| 148 |
|
| 149 |
% \IfBooleanTF{#1}{\luadirect{YAMLvars.setts = pl.tablex.deepcopy(YAMLvars.settsdefault)}}{} % if * return to default first |
| 150 |
% \luadirect{pl.tablex.update(YAMLvars.setts, luakeys.parse(\luastring{#2}))} |
| 151 |
% \luadirect{YAMLvars.setts.xfm = {'lb2nl', 'addxspace'}} |
| 152 |
|