docroc.lua 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. -- docroc - Lua documentation generator
  2. -- https://github.com/bjornbytes/docroc
  3. -- License - MIT, see LICENSE for details.
  4. local rocdoc = {}
  5. function rocdoc.process(filename)
  6. local file = io.open(filename, 'r')
  7. local text = file:read('*a')
  8. file:close()
  9. local comments = {}
  10. text:gsub('%s*%-%-%-(.-)\n([%w\n][^\n%-]*)', function(chunk, context)
  11. chunk = chunk:gsub('^%s*%-*%s*', ''):gsub('\n%s*%-*%s*', ' ')
  12. chunk = chunk:gsub('^[^@]', '@description %1')
  13. context = context:match('[^\n]+')
  14. local tags = {}
  15. chunk:gsub('@(%w+)%s?([^@]*)', function(name, body)
  16. body = body:gsub('(%s+)$', '')
  17. local processor = rocdoc.processors[name]
  18. local tag = processor and processor(body) or {}
  19. tag.tag = name
  20. tag.raw = body
  21. tags[name] = tags[name] or {}
  22. table.insert(tags[name], tag)
  23. table.insert(tags, tag)
  24. end)
  25. table.insert(comments, {
  26. tags = tags,
  27. context = context
  28. })
  29. end)
  30. return comments
  31. end
  32. rocdoc.processors = {
  33. description = function(body)
  34. return {
  35. text = body
  36. }
  37. end,
  38. arg = function(body)
  39. local name = body:match('^%s*(%w+)') or body:match('^%s*%b{}%s*(%w+)')
  40. local description = body:match('%-%s*(.*)$')
  41. local optional, default
  42. local type = body:match('^%s*(%b{})'):sub(2, -2):gsub('(%=)(.*)', function(_, value)
  43. optional = true
  44. default = value
  45. if #default == 0 then default = nil end
  46. return ''
  47. end)
  48. return {
  49. type = type,
  50. name = name,
  51. description = description,
  52. optional = optional,
  53. default = default
  54. }
  55. end,
  56. returns = function(body)
  57. local type
  58. body:gsub('^%s*(%b{})', function(match)
  59. type = match:sub(2, -2)
  60. return ''
  61. end)
  62. local description = body:match('^%s*(.*)')
  63. return {
  64. type = type,
  65. description = description
  66. }
  67. end,
  68. class = function(body)
  69. return {
  70. name = body
  71. }
  72. end
  73. }
  74. return rocdoc