aboutsummaryrefslogtreecommitdiff
path: root/man/make_man_page.py
blob: ed5238149606e20cefb27911c550d56551ac4d2a (plain)
  1. #!/usr/bin/env python
  2. # Creates a man page from a C file.
  3. # Lines beginning with /// are treated as Markdown.
  4. # Non-blank lines immediately following a /// line are treated
  5. # as function signatures or examples and included verbatim. The
  6. # immediately preceding markdown chunk is printed after the example
  7. # as a comment on it.
  8. # That's about it!
  9. import sys
  10. import re
  11. special_comment_re = re.compile('^\/\/\/ ?');
  12. blank_re = re.compile('^\s*$');
  13. mdlines = []
  14. chunk = []
  15. sig = []
  16. with open('../src/cmark.h', 'r') as cmarkh:
  17. state = 'default'
  18. for line in cmarkh:
  19. # state transition
  20. oldstate = state
  21. if special_comment_re.match(line):
  22. state = 'markdown'
  23. elif blank_re.match(line):
  24. state = 'default'
  25. elif state == 'markdown':
  26. state = 'signature'
  27. # handle line
  28. #if oldstate != state and len(mdlines) > 0 and mdlines[-1] != '\n':
  29. # mdlines.append('\n')
  30. if state == 'markdown':
  31. chunk.append(re.sub(special_comment_re, '', line))
  32. elif state == 'signature':
  33. sig.append(' ' + line)
  34. elif oldstate == 'signature' and state != 'signature':
  35. if len(mdlines) > 0 and mdlines[-1] != '\n':
  36. mdlines.append('\n')
  37. mdlines += sig # add sig, then prepended markdown comment
  38. if len(mdlines) > 0 and mdlines[-1] != '\n':
  39. mdlines.append('\n')
  40. mdlines += chunk
  41. chunk = []
  42. sig = []
  43. elif oldstate == 'markdown' and state != 'signature':
  44. if len(mdlines) > 0 and mdlines[-1] != '\n':
  45. mdlines.append('\n')
  46. mdlines += chunk # add markdown chunk
  47. chunk = []
  48. mdlines.append('\n')
  49. sys.stdout.write(''.join(mdlines))