update_documentation.lua 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. local docroc = require 'tools/docroc'
  2. io.output('doc/README.md')
  3. io.write('RxLua\n===\n\n')
  4. local comments = docroc.process('rx.lua')
  5. -- Generate table of contents
  6. for _, comment in ipairs(comments) do
  7. local tags = comment.tags
  8. if tags.class then
  9. local class = tags.class[1].name
  10. io.write('- [' .. class .. '](#' .. class:lower() .. ')\n')
  11. else
  12. local context = comment.context:match('function.-([:%.].+)')
  13. if tags.arg then
  14. context = context:gsub('%b()', function(signature)
  15. local args = {}
  16. for _, arg in ipairs(tags.arg) do
  17. table.insert(args, arg.name)
  18. end
  19. return '(' .. table.concat(args, ', ') .. ')'
  20. end)
  21. end
  22. local name = comment.context:match('function.-[:%.]([^%(]+)')
  23. io.write(' - [' .. name .. '](#' .. context:gsub('[^%w%s]+', ''):gsub(' ', '-'):lower() .. ')\n')
  24. end
  25. end
  26. io.write('\n')
  27. -- Generate content
  28. for _, comment in ipairs(comments) do
  29. local tags = comment.tags
  30. if tags.class then
  31. io.write('# ' .. tags.class[1].name .. '\n\n')
  32. if tags.description then
  33. io.write(tags.description[1].text .. '\n\n')
  34. end
  35. else
  36. local context = comment.context:match('function.-([:%.].+)')
  37. if tags.arg then
  38. context = context:gsub('%b()', function(signature)
  39. local args = {}
  40. for _, arg in ipairs(tags.arg) do
  41. table.insert(args, arg.name)
  42. end
  43. return '(' .. table.concat(args, ', ') .. ')'
  44. end)
  45. end
  46. io.write(('---\n\n#### `%s`\n\n'):format(context))
  47. if tags.description then
  48. io.write(('%s\n\n'):format(tags.description[1].text))
  49. end
  50. if tags.arg then
  51. io.write('Arguments:\n\n')
  52. for _, arg in ipairs(tags.arg) do
  53. local name = arg.name
  54. if arg.optional then
  55. if arg.default then
  56. name = '[' .. name .. '=' .. arg.default .. ']'
  57. else
  58. name = '[' .. name .. ']'
  59. end
  60. end
  61. name = '`' .. name .. '`'
  62. local description = arg.description and (' - ' .. arg.description) or ''
  63. local type = ' (`' .. arg.type .. '`)'
  64. local line = '- ' .. name .. type .. description
  65. io.write(line .. '\n')
  66. end
  67. io.write('\n')
  68. end
  69. if tags.returns then
  70. io.write('Returns:\n\n')
  71. for _, result in ipairs(tags.returns) do
  72. io.write('- `' .. result.type .. '`\n')
  73. end
  74. io.write('\n')
  75. end
  76. end
  77. end