| 1 |
-- ideavault-lua.lua |
| 2 |
-- Copyright 2024-2025 Tomasz M. Czarkowski |
| 3 |
-- |
| 4 |
-- This work may be distributed and/or modified under the |
| 5 |
-- conditions of the LaTeX Project Public License, either version 1.3c |
| 6 |
-- of this license or any later version. |
| 7 |
-- The latest version of this license is in |
| 8 |
-- https://www.latex-project.org/lppl.txt |
| 9 |
-- and version 1.3c or later is part of all distributions of LaTeX |
| 10 |
-- version 2008 or later. |
| 11 |
-- |
| 12 |
-- This work has the LPPL maintenance status `maintained'. |
| 13 |
-- |
| 14 |
-- The Current Maintainer of this work is Tomasz M. Czarkowski |
| 15 |
-- |
| 16 |
-- This work consists of the files ideavault.sty and ideavault-lua.lua |
| 17 |
|
| 18 |
|
| 19 |
logErrorEnabled = true |
| 20 |
logWarnEnabled = true |
| 21 |
logDebugEnabled = false |
| 22 |
|
| 23 |
function logError(message) |
| 24 |
if (logErrorEnabled) then |
| 25 |
texio.write_nl("[ERROR] " .. message) |
| 26 |
end |
| 27 |
end |
| 28 |
|
| 29 |
function logWarn(message) |
| 30 |
if (logWarnEnabled) then |
| 31 |
texio.write_nl("[WARN] " .. message) |
| 32 |
end |
| 33 |
end |
| 34 |
|
| 35 |
function logDebug(message) |
| 36 |
if (logDebugEnabled) then |
| 37 |
texio.write_nl("[DEBUG] " .. message) |
| 38 |
end |
| 39 |
end |
| 40 |
|
| 41 |
IdeaVaultClass = {} |
| 42 |
IdeaVaultClass.__index = IdeaVaultClass |
| 43 |
|
| 44 |
setmetatable(IdeaVaultClass, { |
| 45 |
__call = function (cls, ...) |
| 46 |
return cls.new(...) |
| 47 |
end, |
| 48 |
}) |
| 49 |
|
| 50 |
function IdeaVaultClass.new() |
| 51 |
local self = setmetatable({}, IdeaVaultClass) |
| 52 |
self.all = {} |
| 53 |
return self |
| 54 |
end |
| 55 |
|
| 56 |
function IdeaVaultClass:addPrefix(prefix) |
| 57 |
if (self.all[prefix] == nil) |
| 58 |
then |
| 59 |
self.all[prefix] = {} |
| 60 |
end |
| 61 |
end |
| 62 |
|
| 63 |
function IdeaVaultClass:set(prefix, key, value) |
| 64 |
self:addPrefix(prefix) |
| 65 |
self.all[prefix][key] = value |
| 66 |
end |
| 67 |
|
| 68 |
function IdeaVaultClass:contains(prefix, key) |
| 69 |
if (self.all[prefix] == nil or self.all[prefix][key] == nil) |
| 70 |
then |
| 71 |
return false |
| 72 |
end |
| 73 |
return true |
| 74 |
end |
| 75 |
|
| 76 |
function IdeaVaultClass:get(prefix, key) |
| 77 |
if (self.all[prefix] == nil) |
| 78 |
then |
| 79 |
die("Prefix: '" .. prefix .. "' does not exist!") |
| 80 |
end |
| 81 |
if (self.all[prefix][key] == nil) |
| 82 |
then |
| 83 |
die("Key: '" .. key .. "' not found in prefix: '" .. prefix .. "'!") |
| 84 |
end |
| 85 |
|
| 86 |
return self.all[prefix][key] |
| 87 |
end |
| 88 |
|
| 89 |
function IdeaVaultClass:getAllWithPrefix(prefix) |
| 90 |
ideas = {} |
| 91 |
pr = self.all[prefix] |
| 92 |
for _, value in pairs(pr) |
| 93 |
do |
| 94 |
table.insert(ideas, value) |
| 95 |
end |
| 96 |
table.sort(ideas, function(a,b) |
| 97 |
if b:isGreaterThan(a) then return true end |
| 98 |
end) |
| 99 |
return ideas |
| 100 |
end |
| 101 |
|
| 102 |
function IdeaVaultClass:getAllWithTag(prefix, tag) |
| 103 |
ideas = {} |
| 104 |
pr = self.all[prefix] |
| 105 |
for _, value in pairs(pr) |
| 106 |
do |
| 107 |
if (value:hasTag(tag)) |
| 108 |
then |
| 109 |
table.insert(ideas, value) |
| 110 |
end |
| 111 |
end |
| 112 |
table.sort(ideas, function(a,b) |
| 113 |
if b:isGreaterThan(a) then return true end |
| 114 |
end) |
| 115 |
return ideas |
| 116 |
end |
| 117 |
|
| 118 |
function IdeaVaultClass:printIdea(prefix, key, style) |
| 119 |
idea = self:get(prefix, key) |
| 120 |
idea:printSelf(style) |
| 121 |
end |
| 122 |
|
| 123 |
function IdeaVaultClass:printAllWithTag(prefix, tag, style) |
| 124 |
ideas = self:getAllWithTag(prefix, tag) |
| 125 |
for _, idea in ipairs(ideas) |
| 126 |
do |
| 127 |
idea:printSelf(style) |
| 128 |
end |
| 129 |
end |
| 130 |
|
| 131 |
function IdeaVaultClass:printAllWithPrefix(prefix, style) |
| 132 |
ideas = self:getAllWithPrefix(prefix) |
| 133 |
for _, idea in ipairs(ideas) |
| 134 |
do |
| 135 |
idea:printSelf(style) |
| 136 |
end |
| 137 |
end |
| 138 |
|
| 139 |
AutoCounter = {} |
| 140 |
AutoCounter.__index = AutoCounter |
| 141 |
|
| 142 |
setmetatable(AutoCounter, { |
| 143 |
__call = function (cls, ...) |
| 144 |
return cls.new(...) |
| 145 |
end, |
| 146 |
}) |
| 147 |
|
| 148 |
function AutoCounter.new(value) |
| 149 |
local self = setmetatable({}, AutoCounter) |
| 150 |
self.value = value or 0 |
| 151 |
return self |
| 152 |
end |
| 153 |
|
| 154 |
function AutoCounter:next() |
| 155 |
self.value = self.value + 1 |
| 156 |
return self.value |
| 157 |
end |
| 158 |
|
| 159 |
globalCounter = AutoCounter() |
| 160 |
|
| 161 |
IdeaClass = {} |
| 162 |
IdeaClass.__index = IdeaClass |
| 163 |
|
| 164 |
setmetatable(IdeaClass, { |
| 165 |
__call = function (cls, ...) |
| 166 |
return cls.new(...) |
| 167 |
end, |
| 168 |
}) |
| 169 |
|
| 170 |
function IdeaClass.new(title, content, dependencies, tags, value) |
| 171 |
local self = setmetatable({}, IdeaClass) |
| 172 |
self.title = title |
| 173 |
self.content = content |
| 174 |
self.dependencies = dependencies or {} |
| 175 |
self.tags = tags or {} |
| 176 |
if (value == "auto") |
| 177 |
then |
| 178 |
self.value = globalCounter:next() |
| 179 |
else |
| 180 |
self.value = value or 0 |
| 181 |
end |
| 182 |
return self |
| 183 |
end |
| 184 |
|
| 185 |
function IdeaClass:getTitle() |
| 186 |
return self.title |
| 187 |
end |
| 188 |
|
| 189 |
function IdeaClass:getContent() |
| 190 |
return self.content |
| 191 |
end |
| 192 |
|
| 193 |
function IdeaClass:getTags() |
| 194 |
return self.tags |
| 195 |
end |
| 196 |
|
| 197 |
function IdeaClass:getDependencies() |
| 198 |
return self.dependencies |
| 199 |
end |
| 200 |
|
| 201 |
function IdeaClass:getValue() |
| 202 |
return self.value |
| 203 |
end |
| 204 |
|
| 205 |
function IdeaClass:isGreaterThan(idea) |
| 206 |
if self:getValue() > idea:getValue() |
| 207 |
then |
| 208 |
return true |
| 209 |
else |
| 210 |
if self:getValue() == idea:getValue() |
| 211 |
then |
| 212 |
if self:getTitle() > idea:getTitle() |
| 213 |
then |
| 214 |
return true |
| 215 |
else |
| 216 |
return false |
| 217 |
end |
| 218 |
else |
| 219 |
return false |
| 220 |
end |
| 221 |
end |
| 222 |
end |
| 223 |
|
| 224 |
function IdeaClass:hasTag(tag) |
| 225 |
for _, value in pairs(self:getTags()) |
| 226 |
do |
| 227 |
if (value == tag) |
| 228 |
then |
| 229 |
return true |
| 230 |
end |
| 231 |
end |
| 232 |
return false |
| 233 |
end |
| 234 |
|
| 235 |
bookmark_counter = 0 |
| 236 |
|
| 237 |
function IdeaClass:printSelf(style) |
| 238 |
style = style or "" |
| 239 |
logDebug("Printing idea. Title: '" .. self:getTitle() .. "', style: '" .. style .. "'.\n") |
| 240 |
|
| 241 |
local frame = false |
| 242 |
local center = false |
| 243 |
local bookmark = false |
| 244 |
local large = false |
| 245 |
local Large = false |
| 246 |
local huge = false |
| 247 |
local Huge = false |
| 248 |
local needSpace = false |
| 249 |
local emph = false |
| 250 |
local quiet = false |
| 251 |
local preNewPage = false |
| 252 |
local postNewPage = false |
| 253 |
for c in style:gmatch"." |
| 254 |
do |
| 255 |
if (c == "f") then |
| 256 |
frame = true |
| 257 |
elseif (c == "c") then |
| 258 |
center = true |
| 259 |
elseif (c == "b") then |
| 260 |
bookmark = true |
| 261 |
elseif (c == "l") then |
| 262 |
large = true |
| 263 |
elseif (c == "L") then |
| 264 |
Large = true |
| 265 |
elseif (c == "h") then |
| 266 |
huge = true |
| 267 |
elseif (c == "H") then |
| 268 |
Huge = true |
| 269 |
elseif (c == "s") then |
| 270 |
needSpace = true |
| 271 |
elseif (c == "e") then |
| 272 |
emph = true |
| 273 |
elseif (c == "q") then |
| 274 |
quiet = true |
| 275 |
elseif (c == "p") then |
| 276 |
preNewPage = true |
| 277 |
elseif (c == "P") then |
| 278 |
postNewPage = true |
| 279 |
else |
| 280 |
die("Unknown style: '" .. c .. "'") |
| 281 |
end |
| 282 |
end |
| 283 |
if (preNewPage) |
| 284 |
then |
| 285 |
tex.sprint("\\newpage%") |
| 286 |
end |
| 287 |
if (needSpace) |
| 288 |
then |
| 289 |
tex.sprint("\\needspace{5cm}%") |
| 290 |
end |
| 291 |
if (frame) |
| 292 |
then |
| 293 |
tex.sprint("\\begin{mdframed}%") |
| 294 |
end |
| 295 |
if (bookmark) |
| 296 |
then |
| 297 |
tex.sprint("\\hypertarget{" .. bookmark_counter .. "}{}%") |
| 298 |
tex.sprint("\\bookmark[dest=" .. bookmark_counter .. ", level=\\bookDepth]{" .. self:getTitle() .. "}%") |
| 299 |
--tex.sprint("\\subpdfbookmark{" .. self:getTitle() .. "}{" .. bookmark_counter .. "}%") |
| 300 |
bookmark_counter = bookmark_counter + 1 |
| 301 |
tex.sprint("\\bookDown%") |
| 302 |
end |
| 303 |
if (center) |
| 304 |
then |
| 305 |
tex.sprint("\\begin{center}%") |
| 306 |
end |
| 307 |
if (not quiet) |
| 308 |
then |
| 309 |
tex.sprint("{%") |
| 310 |
if (large) |
| 311 |
then |
| 312 |
tex.sprint("\\Large%") |
| 313 |
end |
| 314 |
if (Large) |
| 315 |
then |
| 316 |
tex.sprint("\\LARGE%") |
| 317 |
end |
| 318 |
if (huge) |
| 319 |
then |
| 320 |
tex.sprint("\\huge%") |
| 321 |
end |
| 322 |
if (Huge) |
| 323 |
then |
| 324 |
tex.sprint("\\Huge%") |
| 325 |
end |
| 326 |
if (emph) |
| 327 |
then |
| 328 |
tex.sprint("\\emph{,,") |
| 329 |
end |
| 330 |
tex.sprint(self:getTitle()) |
| 331 |
tex.sprint("\\nopagebreak%") |
| 332 |
if (emph) |
| 333 |
then |
| 334 |
tex.sprint("''}%") |
| 335 |
end |
| 336 |
tex.sprint("}%") |
| 337 |
end |
| 338 |
if (center) |
| 339 |
then |
| 340 |
tex.sprint("\\end{center}%") |
| 341 |
end |
| 342 |
tex.sprint(self:getContent() .. "%") |
| 343 |
if (not quiet) |
| 344 |
then |
| 345 |
self:printDependencies() |
| 346 |
end |
| 347 |
if (frame) |
| 348 |
then |
| 349 |
tex.sprint("\\end{mdframed}%") |
| 350 |
end |
| 351 |
if (bookmark) |
| 352 |
then |
| 353 |
tex.sprint("\\bookUp%") |
| 354 |
end |
| 355 |
if (postNewPage) |
| 356 |
then |
| 357 |
tex.sprint("\\newpage%") |
| 358 |
end |
| 359 |
end |
| 360 |
|
| 361 |
function IdeaClass:printDependencies() |
| 362 |
for _, dependency in ipairs(self:getDependencies()) |
| 363 |
do |
| 364 |
dependency:resolve():printSelf("lcf") |
| 365 |
end |
| 366 |
end |
| 367 |
|
| 368 |
DependencyClass = {} |
| 369 |
DependencyClass.__index = DependencyClass |
| 370 |
|
| 371 |
setmetatable(DependencyClass, { |
| 372 |
__call = function (cls, ...) |
| 373 |
return cls.new(...) |
| 374 |
end, |
| 375 |
}) |
| 376 |
|
| 377 |
function DependencyClass.new(prefix, title) |
| 378 |
local self = setmetatable({}, DependencyClass) |
| 379 |
self.prefix = prefix or "default" |
| 380 |
self.title = title or "" |
| 381 |
if (not ideaVault:contains(self.prefix, self.title)) |
| 382 |
then |
| 383 |
die("Broken dependency! Title: '" .. self.title .. "'. Prefix: '" .. self.prefix .. "'.") |
| 384 |
end |
| 385 |
return self |
| 386 |
end |
| 387 |
|
| 388 |
function DependencyClass:getPrefix() |
| 389 |
return self.prefix |
| 390 |
end |
| 391 |
|
| 392 |
function DependencyClass:getTitle() |
| 393 |
return self.title |
| 394 |
end |
| 395 |
|
| 396 |
function DependencyClass:resolve() |
| 397 |
return ideaVault:get(self:getPrefix(), self:getTitle()) |
| 398 |
end |
| 399 |
|
| 400 |
function DependencyClass:isGreaterThan(idea) |
| 401 |
if self:getTitle() > idea:getTitle() |
| 402 |
then |
| 403 |
return true |
| 404 |
else |
| 405 |
return false |
| 406 |
end |
| 407 |
end |
| 408 |
|
| 409 |
ideaVault = IdeaVaultClass() |
| 410 |
|
| 411 |
function createIdea(prefix, key, content, dependencies, tags, value) |
| 412 |
t = "[" |
| 413 |
for _, a in pairs(tags) |
| 414 |
do |
| 415 |
t = t .. a .. ", " |
| 416 |
end |
| 417 |
t = t .. "]" |
| 418 |
logDebug("Creating idea. Title: '" .. key .. "'. Prefix: '" .. prefix .. "', tags: " .. t .. ".\n") |
| 419 |
deps = {} |
| 420 |
for _, val in pairs(dependencies) |
| 421 |
do |
| 422 |
d = DependencyClass(val[1], val[2]) |
| 423 |
table.insert(deps, d) |
| 424 |
end |
| 425 |
table.sort(deps, function(a,b) |
| 426 |
if b:isGreaterThan(a) then return true end |
| 427 |
end) |
| 428 |
idea = IdeaClass(key, content, deps, tags, value) |
| 429 |
ideaVault:set(prefix, key, idea) |
| 430 |
end |
| 431 |
|
| 432 |
function die(reason) |
| 433 |
reason = reason or "nil" |
| 434 |
logError("Critical error: " .. reason) |
| 435 |
tex.error("Critical error: " .. reason) |
| 436 |
end |
| 437 |
|
| 438 |
function startsWith(text, prefix) |
| 439 |
return text:find(prefix, 1, true) == 1 |
| 440 |
end |