update_documentation.lua 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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].text
  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].text .. '\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('| Name | Type | Default | Description |\n')
  52. io.write('|------|------|---------|-------------|\n')
  53. for _, arg in ipairs(tags.arg) do
  54. local name = arg.name
  55. name = '`' .. name .. '`'
  56. local description = arg.description or ''
  57. local type = arg.type:gsub('|', ' or ')
  58. local default = ''
  59. if arg.optional then
  60. type = type .. ' (optional)'
  61. default = arg.default or default
  62. end
  63. local line = '| ' .. name .. ' | ' .. type .. ' | ' .. default .. ' | ' .. description .. ' |'
  64. io.write(line .. '\n')
  65. end
  66. io.write('\n')
  67. end
  68. end
  69. end