summaryrefslogtreecommitdiff
path: root/_extensions/dragonstyle/lipsum/lipsum.lua
blob: 57e53e820b45c65808ed1628e419cf01d33166a4 (plain)
  1. local lipsum
  2. math.randomseed(os.time())
  3. -- reads a file
  4. local function read_file(path)
  5. local file = io.open(path, "rb")
  6. if not file then return nil end
  7. local content = file:read "*a"
  8. file:close()
  9. return content
  10. end
  11. -- read lipsum data
  12. function readLipsum()
  13. if lipsum == nil then
  14. local file = quarto.utils.resolve_path("lipsum.json")
  15. local fileContents = read_file(file)
  16. if fileContents ~= nil then
  17. local json = quarto.json.decode(fileContents)
  18. lipsum = json
  19. else
  20. quarto.log.error("Unable to read lipsum data file.")
  21. lipsum = {}
  22. end
  23. end
  24. return lipsum
  25. end
  26. local rangePattern = '(%d+)%-(%d+)'
  27. local barePattern = '^(%d+)$'
  28. return {
  29. ['lipsum'] = function(args, kwargs, meta)
  30. local paraStart = 1
  31. local paraEnd = 5
  32. if args[1] ~= nil then
  33. -- a range is specified, like 1-5, 2-3, 5-1
  34. local range = pandoc.utils.stringify(args[1])
  35. local _,_,startRange,endRange = range:find(rangePattern)
  36. if startRange and endRange then
  37. local startNumber = tonumber(startRange)
  38. if startNumber ~= nil then
  39. paraStart = startNumber
  40. end
  41. local endNumber = tonumber(endRange)
  42. if endNumber ~= nil then
  43. paraEnd = endNumber
  44. end
  45. else
  46. -- a number of paragraphs is specified, like 10
  47. local _,_,bareVal = range:find(barePattern)
  48. if bareVal then
  49. paraStart = math.random(1, 17)
  50. local endNumber = tonumber(bareVal)
  51. if endNumber ~= nil then
  52. paraEnd = paraStart + endNumber - 1
  53. end
  54. end
  55. end
  56. end
  57. local paras = readLipsum();
  58. local outputParas = {}
  59. local count = paraEnd - paraStart + 1
  60. if paraStart > paraEnd then
  61. count = paraStart - paraEnd + 1
  62. end
  63. for i=1,count do
  64. local paraIdx = i + (paraStart - 1)
  65. if paraStart > paraEnd then
  66. paraIdx = (paraStart + 1) - i
  67. end
  68. local outIdx = ((paraIdx-1)%(#paras-1))+1
  69. outputParas[i] = pandoc.Para(paras[outIdx])
  70. end
  71. return outputParas
  72. end
  73. }