summaryrefslogtreecommitdiff
path: root/macros/luatex/latex/ideavault/ideavault-lua.lua
blob: 6d4de1e383a76eee942d52b19b880f162b4e9d2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
-- ideavault-lua.lua
-- Copyright 2024 Tomasz M. Czarkowski
--
-- This work may be distributed and/or modified under the
-- conditions of the LaTeX Project Public License, either version 1.3c
-- of this license or any later version.
-- The latest version of this license is in
--   https://www.latex-project.org/lppl.txt
-- and version 1.3c or later is part of all distributions of LaTeX
-- version 2008 or later.
--
-- This work has the LPPL maintenance status `maintained'.
--
-- The Current Maintainer of this work is Tomasz M. Czarkowski
--
-- This work consists of the files ideavault.sty and ideavault-lua.lua

IdeaVaultClass = {}
IdeaVaultClass.__index = IdeaVaultClass

setmetatable(IdeaVaultClass, {
  __call = function (cls, ...)
    return cls.new(...)
  end,
})

function IdeaVaultClass.new()
  local self = setmetatable({}, IdeaVaultClass)
  self.all = {}
  return self
end

function IdeaVaultClass:addPrefix(prefix)
  if (self.all[prefix] == nil)
  then
    self.all[prefix] = {}
  end
end

function IdeaVaultClass:set(prefix, key, value)
  self:addPrefix(prefix)
  self.all[prefix][key] = value
end

function IdeaVaultClass:contains(prefix, key)
  if (self.all[prefix] == nil or self.all[prefix][key] == nil)
  then
    return false
  end
  return true
end

function IdeaVaultClass:get(prefix, key)
  if (self.all[prefix] == nil)
  then
    die("Prefix: '" .. prefix .. "' does not exist!")
  end
  if (self.all[prefix][key] == nil)
  then
    die("Key: '" .. key .. "' not found in prefix: '" .. prefix .. "'!")
  end

  return self.all[prefix][key]
end

function IdeaVaultClass:getAllWithPrefix(prefix)
  ideas = {}
  pr = self.all[prefix]
  for _, value in pairs(pr)
  do
    table.insert(ideas, value)
  end
  table.sort(ideas, function(a,b)
    if b:isGreaterThan(a) then return true end
  end)
  return ideas
end

function IdeaVaultClass:getAllWithTag(prefix, tag)
  ideas = {}
  pr = self.all[prefix]
  for _, value in pairs(pr)
  do
    if (value:hasTag(tag))
    then
      table.insert(ideas, value)
    end
  end
  table.sort(ideas, function(a,b)
    if b:isGreaterThan(a) then return true end
  end)
  return ideas
end

function IdeaVaultClass:printIdea(prefix, key, style)
  idea = self:get(prefix, key)
  idea:printSelf(style)
end

function IdeaVaultClass:printAllWithTag(prefix, tag, style)
  ideas = self:getAllWithTag(prefix, tag)
  for _, idea in ipairs(ideas)
  do
    idea:printSelf(style)
  end
end

function IdeaVaultClass:printAllWithPrefix(prefix, style)
  ideas = self:getAllWithPrefix(prefix)
  for _, idea in ipairs(ideas)
  do
    idea:printSelf(style)
  end
end

IdeaClass = {}
IdeaClass.__index = IdeaClass

setmetatable(IdeaClass, {
  __call = function (cls, ...)
    return cls.new(...)
  end,
})

function IdeaClass.new(title, content, dependencies, tags, value)
  local self = setmetatable({}, IdeaClass)
  self.title = title
  self.content = content
  self.dependencies = dependencies or {}
  self.tags = tags or {}
  self.value = value or 0
  return self
end

function IdeaClass:getTitle()
  return self.title
end

function IdeaClass:getContent()
  return self.content
end

function IdeaClass:getTags()
  return self.tags
end

function IdeaClass:getDependencies()
  return self.dependencies
end

function IdeaClass:getValue()
  return self.value
end

function IdeaClass:isGreaterThan(idea)
  if self:getValue() > idea:getValue()
  then
    return true
  else
    if self:getValue() == idea:getValue()
    then
      if self:getTitle() > idea:getTitle()
      then
        return true
      else
        return false
      end
    else
      return false
    end
  end
end

function IdeaClass:hasTag(tag)
  for _, value in pairs(self:getTags())
  do
    if (value == tag)
    then
      return true
    end
  end
  return false
end

bookmark_counter = 0

function IdeaClass:printSelf(style)
  style = style or ""
  texio.write("Printing idea. Title: '" .. self:getTitle() .. "', style: '" .. style .. "'.\n")
  
  local frame = false
  local center = false
  local bookmark = false
  local large = false
  local needSpace = false
  local emph = false
  local quiet = false
  for c in style:gmatch"."
  do
    if (c == "f") then 
      frame = true 
    elseif (c == "c") then 
      center = true 
    elseif (c == "b") then 
      bookmark = true 
    elseif (c == "l") then 
      large = true 
    elseif (c == "s") then 
      needSpace = true 
    elseif (c == "e") then 
      emph = true 
    elseif (c == "q") then 
      quiet = true 
    else
      die("Unknown style: '" .. c .. "'")
    end
  end
  if (needSpace) 
  then
    tex.sprint("\\needspace{5cm}%")
  end
  if (bookmark) 
  then
    tex.sprint("\\hypertarget{" .. bookmark_counter .. "}{}%")
    tex.sprint("\\bookmark[dest=" .. bookmark_counter .. ", level=\\bookDepth]{" .. self:getTitle() .. "}%")
    --tex.sprint("\\subpdfbookmark{" .. self:getTitle() .. "}{" .. bookmark_counter .. "}%")
    bookmark_counter = bookmark_counter + 1
    tex.sprint("\\bookDown%")
  end
  if (frame) 
  then
    tex.sprint("\\begin{mdframed}%")
  end
  if (center) 
  then
    tex.sprint("\\begin{center}%")
  end
  if (not quiet)
  then
    if (large) 
    then
      tex.sprint("{\\Large%")
    end
    if (emph) 
    then
      tex.sprint("\\emph{,,")
    end
    tex.sprint(self:getTitle())
    if (emph) 
    then
      tex.sprint("''}%")
    end
    if (large) 
    then
      tex.sprint("}%")
    end
  end
  if (center) 
  then
    tex.sprint("\\end{center}%")
  end
  tex.sprint(self:getContent() .. "%")
  if (not quiet)
  then
    self:printDependencies()
  end
  if (frame) 
  then
    tex.sprint("\\end{mdframed}%")
  end
  if (bookmark) 
  then
    tex.sprint("\\bookUp%")
  end
end

function IdeaClass:printDependencies()
  for _, dependency in ipairs(self:getDependencies())
  do
    dependency:resolve():printSelf("lcf")
  end
end

DependencyClass = {}
DependencyClass.__index = DependencyClass

setmetatable(DependencyClass, {
  __call = function (cls, ...)
    return cls.new(...)
  end,
})

function DependencyClass.new(prefix, title)
  local self = setmetatable({}, DependencyClass)
  self.prefix = prefix or "default"
  self.title = title or ""
  if (not ideaVault:contains(self.prefix, self.title))
  then
    die("Broken dependency! Title: '" .. self.title .. "'. Prefix: '" .. self.prefix .. "'.")
  end
  return self
end

function DependencyClass:getPrefix()
  return self.prefix
end

function DependencyClass:getTitle()
  return self.title
end

function DependencyClass:resolve()
  return ideaVault:get(self:getPrefix(), self:getTitle())
end

function DependencyClass:isGreaterThan(idea)
  if self:getTitle() > idea:getTitle()
  then
    return true
  else
    return false
  end
end
ideaVault = IdeaVaultClass()

function createIdea(prefix, key, content, dependencies, tags, value)
  t = "["
  for _, a in pairs(tags)
  do
    t = t .. a .. ", "
  end
  t = t .. "]"
  texio.write("Creating idea. Title: '" .. key .. "'. Prefix: '" .. prefix .. "', tags: " .. t .. ".\n")
  deps = {}
  for _, val in pairs(dependencies)
  do
    d = DependencyClass(val[1], val[2])
    table.insert(deps, d)
  end
  table.sort(deps, function(a,b)
    if b:isGreaterThan(a) then return true end
  end)
  idea = IdeaClass(key, content, deps, tags, value)
  ideaVault:set(prefix, key, idea)
end

function die(reason)
  reason = reason or "nil"
  texio.write_nl("Critical error: " .. reason)
  tex.error("Critical error: " .. reason)
end

function startsWith(text, prefix)
    return text:find(prefix, 1, true) == 1
end