| 1 | --[[ |
| 2 | SHARED LUA FILTER |
| 3 | This filter contains utility functions that are useful in several filters. |
| 4 | |
| 5 | USAGE |
| 6 | This script is loaded in other script by an RMARKDOWN_LUA_SHARED environment variable |
| 7 | set inside rmarkdown R package by `render()`. |
| 8 | |
| 9 | Use this line in other script to use this function |
| 10 | dofile(os.getenv 'RMARKDOWN_LUA_SHARED') |
| 11 | |
| 12 | To use this script directly, use `dofile(<path-to-script>)` |
| 13 | |
| 14 | NOTE |
| 15 | USAGE could change if Pandoc 2.1.2+ is supported only as PANDOC_SCRIPT_FILE is |
| 16 | available from other Lua Filters. In this case, path to `shared.lua` could be |
| 17 | determined from other filters relatively and loaded with `dofile()` |
| 18 | |
| 19 | See https://gist.github.com/cderv/ca24d2681b0a1c4b6f91e0b49d725f36 for an example |
| 20 | of `import("shared.lua")` |
| 21 | |
| 22 | LICENSE |
| 23 | MIT License |
| 24 | ]] |
| 25 | |
| 26 | |
| 27 | --[[ |
| 28 | This function tests the pandoc version againt a target version. |
| 29 | |
| 30 | USAGE |
| 31 | pandocAvailable {2,1} |
| 32 | NOTE |
| 33 | For Pandoc 2.7.3, PANDOC_VERSION >= "2.8" would be enough but before 2.7.3 |
| 34 | it is a table object |
| 35 | ]] |
| 36 | function pandocAvailable(target) |
| 37 | -- this function only work for Pandoc 2.1 and above. It returns false is not |
| 38 | if not PANDOC_VERSION then return false end |
| 39 | assert(target, "No target version specified in pandocAvailable.") |
| 40 | -- checking each version number |
| 41 | -- EPOCH.MAJOR.MINOR.PATCH (https://pvp.haskell.org) |
| 42 | for i = 1,4 do |
| 43 | -- some versions do not have all numbers |
| 44 | if not PANDOC_VERSION[i] or not target[i] then break end |
| 45 | if PANDOC_VERSION[i] < target[i] then return false end |
| 46 | if PANDOC_VERSION[i] > target[i] then return true end |
| 47 | end |
| 48 | return true |
| 49 | end |
| 50 | |
| 51 | -- for debuging purpose |
| 52 | function print_debug(label,obj,iter) |
| 53 | local debug_mode = os.getenv("DEBUG_PANDOC_LUA") == "TRUE" |
| 54 | obj = obj or nil |
| 55 | iter = iter or pairs |
| 56 | label = label or "" |
| 57 | label = "DEBUG (from custom-environment.lua): "..label |
| 58 | if (debug_mode) then |
| 59 | if not obj then |
| 60 | print(label.." nil") |
| 61 | elseif (type(obj) == "string") then |
| 62 | print(label.." "..obj) |
| 63 | elseif type(obj) == "table" then |
| 64 | for k,v in iter(obj) do |
| 65 | print(label.."id:"..k.. " val:"..v) |
| 66 | end |
| 67 | end |
| 68 | end |
| 69 | return nil |
| 70 | end |
| 71 | |
| 72 | |
| 73 | --- Returns the type of a metadata value. |
| 74 | -- Author: Albert Krewinkel |
| 75 | -- Source: https://github.com/pandoc/lua-filters/commit/3057acc52d1d6cfb7134c934a5039ce170bf78fa |
| 76 | -- |
| 77 | -- Backward compatible function as retrieving type on Metadata has changed. |
| 78 | -- `pandoc.utils.type()` not available before Pandoc 2.17 |
| 79 | -- |
| 80 | -- @param v a metadata value |
| 81 | -- @treturn string one of `Blocks`, `Inlines`, `List`, `Map`, `string`, `boolean` |
| 82 | function pandoc_type (v) |
| 83 | if pandoc.utils.type == nil then |
| 84 | local metatag = type(v) == 'table' and v.t and v.t:gsub('^Meta', '') |
| 85 | return metatag and metatag ~= 'Map' and metatag or type(v) |
| 86 | end |
| 87 | return pandoc.utils.type(v) |
| 88 | end |
| 89 |