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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{- |
Module : Text.Pandoc.Lua.Module.Utils
Copyright : Copyright © 2017-2024 Albert Krewinkel
License : GNU GPL, version 2 or above
Maintainer : Albert Krewinkel <[email protected]>
Stability : alpha
Utility module for Lua, exposing internal helper functions.
-}
module Text.Pandoc.Lua.Module.Utils
( documentedModule
, sha1
) where
import Control.Applicative ((<|>))
import Control.Monad ((<$!>))
import Crypto.Hash (hashWith, SHA1(SHA1))
import Data.Data (showConstr, toConstr)
import Data.Default (def)
import Data.Maybe (fromMaybe)
import Data.Version (Version, makeVersion)
import HsLua as Lua
import HsLua.Module.Version (peekVersionFuzzy, pushVersion)
import Text.Pandoc.Citeproc (getReferences, processCitations)
import Text.Pandoc.Definition
import Text.Pandoc.Error (PandocError)
import Text.Pandoc.Filter (applyJSONFilter)
import Text.Pandoc.Lua.Filter (runFilterFile')
import Text.Pandoc.Lua.Marshal.AST
import Text.Pandoc.Lua.Marshal.Reference
import Text.Pandoc.Lua.PandocLua (PandocLua (unPandocLua))
import qualified Data.Map as Map
import qualified Data.Text as T
import qualified Text.Pandoc.Builder as B
import qualified Text.Pandoc.Shared as Shared
import qualified Text.Pandoc.UTF8 as UTF8
import qualified Text.Pandoc.Writers.Shared as Shared
-- | Push the "pandoc.utils" module to the Lua stack.
documentedModule :: Module PandocError
documentedModule = Module
{ moduleName = "pandoc.utils"
, moduleDescription = T.unlines
[ "This module exposes internal pandoc functions and utility"
, "functions."
]
, moduleFields = []
, moduleOperations = []
, moduleTypeInitializers = []
, moduleFunctions =
[ blocks_to_inlines `since` v[2,2,3]
, citeproc `since` v[2,19,1]
, equals `since` v[2,5]
, from_simple_table `since` v[2,11]
, make_sections `since` v[2,8]
, normalize_date `since` v[2,0,6]
, references `since` v[2,17]
, run_json_filter `since` v[2,1,1]
, run_lua_filter `since` v[3,2,1]
, sha1 `since` v[2,0,6]
, stringify `since` v[2,0,6]
, to_roman_numeral `since` v[2,0,6]
, to_simple_table `since` v[2,11]
, type' `since` v[2,17]
, defun "Version"
### liftPure (id @Version)
<#> parameter peekVersionFuzzy
"version string, list of integers, or integer"
"v" "version description"
=#> functionResult pushVersion "Version" "new Version object"
#? "Creates a Version object."
]
}
where
v = makeVersion
blocks_to_inlines :: LuaError e => DocumentedFunction e
blocks_to_inlines = defun "blocks_to_inlines"
### (\blks mSep -> do
let sep = maybe Shared.defaultBlocksSeparator B.fromList mSep
return $ B.toList (Shared.blocksToInlinesWithSep sep blks))
<#> parameter (peekList peekBlock) "Blocks"
"blocks"
"List of [[Block]] elements to be flattened."
<#> opt (parameter (peekList peekInline) "Inlines" "sep"
("List of [[Inline]] elements inserted as separator between\n" <>
"two consecutive blocks; defaults to `{pandoc.LineBreak()}`."))
=#> functionResult pushInlines "Inlines" ""
#? T.unlines
[ "Squash a list of blocks into a list of inlines."
, ""
, "Usage"
, ""
, " local blocks = {"
, " pandoc.Para{ pandoc.Str 'Paragraph1' },"
, " pandoc.Para{ pandoc.Emph 'Paragraph2' }"
, " }"
, " local inlines = pandoc.utils.blocks_to_inlines(blocks)"
, " assert("
, " inlines == pandoc.Inlines {"
, " pandoc.Str 'Paragraph1',"
, " pandoc.Linebreak(),"
, " pandoc.Emph{ pandoc.Str 'Paragraph2' }"
, " }"
, " )"
]
citeproc :: DocumentedFunction PandocError
citeproc = defun "citeproc"
### unPandocLua . processCitations
<#> parameter peekPandoc "Pandoc" "doc" "document"
=#> functionResult pushPandoc "Pandoc" "processed document"
#? T.unlines
[ "Process the citations in the file, replacing them with "
, "rendered citations and adding a bibliography. "
, "See the manual section on citation rendering for details."
, ""
, "Usage:"
, ""
, " -- Lua filter that behaves like `--citeproc`"
, " function Pandoc (doc)"
, " return pandoc.utils.citeproc(doc)"
, " end"
]
equals :: LuaError e => DocumentedFunction e
equals = defun "equals"
### equal
<#> parameter pure "any" "element1" ""
<#> parameter pure "any" "element2" ""
=#> functionResult pushBool "boolean"
"Whether the two objects represent the same element"
#? T.unlines
[ "Test equality of AST elements. Elements in Lua are considered"
, "equal if and only if the objects obtained by unmarshaling are"
, "equal."
, ""
, "**This function is deprecated.** Use the normal Lua `==` equality"
, "operator instead."
]
-- | Converts an old/simple table into a normal table block element.
from_simple_table :: LuaError e => DocumentedFunction e
from_simple_table = defun "from_simple_table"
### liftPure
(\(SimpleTable capt aligns widths head' body) ->
Table
nullAttr
(Caption Nothing [Plain capt | not (null capt)])
(zipWith (\a w -> (a, toColWidth w)) aligns widths)
(TableHead nullAttr [blockListToRow head' | not (null head') ])
[TableBody nullAttr 0 [] $ map blockListToRow body | not (null body)]
(TableFoot nullAttr []))
<#> parameter peekSimpleTable "SimpleTable" "simple_tbl" ""
=#> functionResult pushBlock "Block" "table block element"
#? T.unlines
[ "Creates a [[Table]] block element from a [[SimpleTable]]. This is"
, "useful for dealing with legacy code which was written for pandoc"
, "versions older than 2.10."
, ""
, "Usage:"
, ""
, " local simple = pandoc.SimpleTable(table)"
, " -- modify, using pre pandoc 2.10 methods"
, " simple.caption = pandoc.SmallCaps(simple.caption)"
, " -- create normal table block again"
, " table = pandoc.utils.from_simple_table(simple)"
]
where
blockListToRow :: [[Block]] -> Row
blockListToRow = Row nullAttr . map (B.simpleCell . B.fromList)
toColWidth :: Double -> ColWidth
toColWidth 0 = ColWidthDefault
toColWidth w = ColWidth w
make_sections :: LuaError e => DocumentedFunction e
make_sections = defun "make_sections"
### liftPure3 Shared.makeSections
<#> parameter peekBool "boolean" "number_sections"
("whether section divs should get an additional `number`\n" <>
"attribute containing the section number.")
<#> parameter (\i -> (Nothing <$ peekNil i) <|> (Just <$!> peekIntegral i))
"integer|nil" "baselevel"
"shift top-level headings to this level"
<#> parameter (peekList peekBlock) "Blocks"
"blocks" "list of blocks to process"
=#> functionResult pushBlocks "Blocks"
"blocks with sections"
#? T.unlines
[ "Converts a list of [[Block]] elements into sections."
, "`Div`s will be created beginning at each `Header`"
, "and containing following content until the next `Header`"
, "of comparable level. If `number_sections` is true,"
, "a `number` attribute will be added to each `Header`"
, "containing the section number. If `base_level` is"
, "non-null, `Header` levels will be reorganized so"
, "that there are no gaps, and so that the base level"
, "is the level specified."
]
normalize_date :: DocumentedFunction e
normalize_date = defun "normalize_date"
### liftPure Shared.normalizeDate
<#> parameter peekText "string" "date" "the date string"
=#> functionResult (maybe pushnil pushText) "string or nil"
"normalized date, or nil if normalization failed."
#? T.unwords
[ "Parse a date and convert (if possible) to \"YYYY-MM-DD\" format. We"
, "limit years to the range 1601-9999 (ISO 8601 accepts greater than"
, "or equal to 1583, but MS Word only accepts dates starting 1601)."
, "Returns nil instead of a string if the conversion failed."
]
-- | List of references in CSL format.
references :: DocumentedFunction PandocError
references = defun "references"
### (unPandocLua . getReferences Nothing)
<#> parameter peekPandoc "Pandoc" "doc" "document"
=#> functionResult (pushPandocList pushReference) "table"
"lift of references."
#? T.unlines
[ "Get references defined inline in the metadata and via an external"
, "bibliography. Only references that are actually cited in the"
, "document (either with a genuine citation or with `nocite`) are"
, "returned. URL variables are converted to links."
, ""
, "The structure used represent reference values corresponds to that"
, "used in CSL JSON; the return value can be use as `references`"
, "metadata, which is one of the values used by pandoc and citeproc"
, "when generating bibliographies."
, ""
, "Usage:"
, ""
, " -- Include all cited references in document"
, " function Pandoc (doc)"
, " doc.meta.references = pandoc.utils.references(doc)"
, " doc.meta.bibliography = nil"
, " return doc"
, " end"
]
-- | Run a filter from a file.
run_lua_filter :: DocumentedFunction PandocError
run_lua_filter = defun "run_lua_filter"
### (\doc fp mbenv -> do
envIdx <- maybe copyOfGlobalTable pure mbenv
runFilterFile' envIdx fp doc)
<#> parameter peekPandoc "Pandoc" "doc" "the Pandoc document to filter"
<#> parameter peekString "string" "filter" "filepath of the filter to run"
<#> opt (parameter (typeChecked "table" istable pure) "table" "env"
"environment to load and run the filter in")
=#> functionResult pushPandoc "Pandoc" "filtered document"
#? ( "Filter the given doc by passing it through a Lua filter." <>
"\n\nThe filter will be run in the current Lua process." <>
"\n"
)
where
copynext :: LuaError e => StackIndex -> LuaE e StackIndex
copynext to =
Lua.next (nth 2) >>= \case
False -> pure to
True -> do
pushvalue (nth 2)
insert (nth 2)
rawset to
copynext to
copyOfGlobalTable :: LuaError e => LuaE e StackIndex
copyOfGlobalTable = do
newtable
pushglobaltable
pushnil
(copynext =<< absindex (nth 3)) <* pop 1 -- pop source table
-- | Process the document with a JSON filter.
run_json_filter :: DocumentedFunction PandocError
run_json_filter = defun "run_json_filter"
### (\doc filterPath margs -> do
args <- case margs of
Just xs -> return xs
Nothing -> do
Lua.getglobal "FORMAT"
(forcePeek ((:[]) <$!> peekString top) <* pop 1)
applyJSONFilter def args filterPath doc
)
<#> parameter peekPandoc "Pandoc" "doc" "the Pandoc document to filter"
<#> parameter peekString "string" "filter" "filter to run"
<#> opt (parameter (peekList peekString) "{string,...}" "args"
"list of arguments passed to the filter. Defaults to `{FORMAT}`.")
=#> functionResult pushPandoc "Pandoc" "filtered document"
#? "Filter the given doc by passing it through a JSON filter."
-- | Documented Lua function to compute the hash of a string.
sha1 :: DocumentedFunction e
sha1 = defun "sha1"
### liftPure (show . hashWith SHA1)
<#> parameter peekByteString "string" "input" ""
=#> functionResult pushString "string" "hexadecimal hash value"
#? "Computes the SHA1 hash of the given string input."
-- | Convert pandoc structure to a string with formatting removed.
-- Footnotes are skipped (since we don't want their contents in link
-- labels).
stringify :: LuaError e => DocumentedFunction e
stringify = defun "stringify"
### (\idx ->
forcePeek . retrieving "stringifyable element" $
choice
[ (fmap Shared.stringify . peekPandoc)
, (fmap Shared.stringify . peekInline)
, (fmap Shared.stringify . peekBlock)
, (fmap Shared.stringify . peekCitation)
, (fmap stringifyMetaValue . peekMetaValue)
, (fmap (const "") . peekAttr)
, (fmap (const "") . peekListAttributes)
] idx)
<#> parameter pure "AST element" "element" "some pandoc AST element"
=#> functionResult pushText "string"
"A plain string representation of the given element."
#? T.unlines
[ "Converts the given element (Pandoc, Meta, Block, or Inline) into"
, "a string with all formatting removed."
]
where
stringifyMetaValue :: MetaValue -> T.Text
stringifyMetaValue mv = case mv of
MetaBool b -> T.toLower $ T.pack (show b)
MetaString s -> s
MetaList xs -> mconcat $ map stringifyMetaValue xs
MetaMap m -> mconcat $ map (stringifyMetaValue . snd) (Map.toList m)
_ -> Shared.stringify mv
to_roman_numeral :: LuaError e => DocumentedFunction e
to_roman_numeral = defun "to_roman_numeral"
### liftPure Shared.toRomanNumeral
<#> parameter (peekIntegral @Int) "integer" "n"
"positive integer below 4000"
=#> functionResult pushText "string" "A roman numeral."
#? T.unlines
[ "Converts an integer < 4000 to uppercase roman numeral."
, ""
, "Usage:"
, ""
, " local to_roman_numeral = pandoc.utils.to_roman_numeral"
, " local pandoc_birth_year = to_roman_numeral(2006)"
, " -- pandoc_birth_year == 'MMVI'"
]
-- | Converts a table into an old/simple table.
to_simple_table :: DocumentedFunction PandocError
to_simple_table = defun "to_simple_table"
### (\case
Table _attr caption specs thead tbodies tfoot -> do
let (capt, aligns, widths, headers, rows) =
Shared.toLegacyTable caption specs thead tbodies tfoot
return $ SimpleTable capt aligns widths headers rows
blk -> Lua.failLua $ mconcat
[ "Expected Table, got ", showConstr (toConstr blk), "." ])
<#> parameter peekTable "Block" "tbl" "a table"
=#> functionResult pushSimpleTable "SimpleTable" "SimpleTable object"
#? T.unlines
[ "Converts a table into an old/simple table."
, ""
, "Usage:"
, ""
, " local simple = pandoc.utils.to_simple_table(table)"
, " -- modify, using pre pandoc 2.10 methods"
, " simple.caption = pandoc.SmallCaps(simple.caption)"
, " -- create normal table block again"
, " table = pandoc.utils.from_simple_table(simple)"
]
where
peekTable :: LuaError e => Peeker e Block
peekTable idx = peekBlock idx >>= \case
t@(Table {}) -> return t
b -> Lua.failPeek $ mconcat
[ "Expected Table, got "
, UTF8.fromString $ showConstr (toConstr b)
, "." ]
type' :: DocumentedFunction e
type' = defun "type"
### (\idx -> getmetafield idx "__name" >>= \case
TypeString -> fromMaybe mempty <$> tostring top
_ -> ltype idx >>= typename)
<#> parameter pure "any" "value" "any Lua value"
=#> functionResult pushByteString "string" "type of the given value"
#? T.unlines
[ "Pandoc-friendly version of Lua's default `type` function, returning"
, "type information similar to what is presented in the manual."
, ""
, "The function works by checking the metafield `__name`. If the"
, "argument has a string-valued metafield `__name`, then it returns"
, "that string. Otherwise it behaves just like the normal `type`"
, "function."
, ""
, "Usage:"
, ""
, " -- Prints one of 'string', 'boolean', 'Inlines', 'Blocks',"
, " -- 'table', and 'nil', corresponding to the Haskell constructors"
, " -- MetaString, MetaBool, MetaInlines, MetaBlocks, MetaMap,"
, " -- and an unset value, respectively."
, ""
, " function Meta (meta)"
, " print('type of metavalue `author`:', pandoc.utils.type(meta.author))"
, " end"
]
|