docroc.lua 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. -- docroc v0.1.0 - Lua documentation generator
  2. -- https://github.com/bjornbytes/docroc
  3. -- License - MIT, see LICENSE for details.
  4. local docroc = {}
  5. function docroc.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 = docroc.processors[name]
  18. local tag = processor and processor(body) or {}
  19. tag.tag = name
  20. tag.text = 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. docroc.processors = {
  33. arg = function(body)
  34. local name = body:match('^%s*(%w+)') or body:match('^%s*%b{}%s*(%w+)')
  35. local description = body:match('%-%s*(.*)$')
  36. local optional, default
  37. local type = body:match('^%s*(%b{})'):sub(2, -2):gsub('(%=)(.*)', function(_, value)
  38. optional = true
  39. default = value
  40. if #default == 0 then default = nil end
  41. return ''
  42. end)
  43. return {
  44. type = type,
  45. name = name,
  46. description = description,
  47. optional = optional,
  48. default = default
  49. }
  50. end,
  51. returns = function(body)
  52. local type
  53. body:gsub('^%s*(%b{})', function(match)
  54. type = match:sub(2, -2)
  55. return ''
  56. end)
  57. local description = body:match('^%s*(.*)')
  58. return {
  59. type = type,
  60. description = description
  61. }
  62. end
  63. }
  64. return docroc