update_documentation.lua 2.4 KB

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