aboutsummaryrefslogtreecommitdiff
path: root/man/make_man_page.py
blob: 19e12716be51293f41702bb7c542c44af1d9aef0 (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. if len(sys.argv) > 1:
  12. sourcefile = sys.argv[1]
  13. else:
  14. print("Usage: make_man_page.py sourcefile")
  15. exit(1)
  16. special_comment_re = re.compile('^\/\/\/ ?')
  17. blank_re = re.compile('^\s*$')
  18. macro_re = re.compile('CMARK_EXPORT *')
  19. mdlines = []
  20. chunk = []
  21. sig = []
  22. with open(sourcefile, 'r') as cmarkh:
  23. state = 'default'
  24. for line in cmarkh:
  25. # state transition
  26. oldstate = state
  27. if special_comment_re.match(line):
  28. state = 'markdown'
  29. elif blank_re.match(line):
  30. state = 'default'
  31. elif state == 'markdown':
  32. state = 'signature'
  33. # handle line
  34. #if oldstate != state and len(mdlines) > 0 and mdlines[-1] != '\n':
  35. # mdlines.append('\n')
  36. if state == 'markdown':
  37. chunk.append(re.sub(special_comment_re, '', line))
  38. elif state == 'signature':
  39. sig.append(' ' + re.sub(macro_re, '', line))
  40. elif oldstate == 'signature' and state != 'signature':
  41. if len(mdlines) > 0 and mdlines[-1] != '\n':
  42. mdlines.append('\n')
  43. mdlines += sig # add sig, then prepended markdown comment
  44. if len(mdlines) > 0 and mdlines[-1] != '\n':
  45. mdlines.append('\n')
  46. mdlines += chunk
  47. chunk = []
  48. sig = []
  49. elif oldstate == 'markdown' and state != 'signature':
  50. if len(mdlines) > 0 and mdlines[-1] != '\n':
  51. mdlines.append('\n')
  52. mdlines += chunk # add markdown chunk
  53. chunk = []
  54. mdlines.append('\n')
  55. sys.stdout.write(''.join(mdlines))