rst2md.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #!/usr/bin/env python
  2. # reStructuredText (RST) to GitHub-flavored Markdown converter
  3. import re, sys
  4. from docutils import core, nodes, writers
  5. def is_github_ref(node):
  6. return re.match('https://github.com/.*/(issues|pull)/.*', node['refuri'])
  7. class Translator(nodes.NodeVisitor):
  8. def __init__(self, document):
  9. nodes.NodeVisitor.__init__(self, document)
  10. self.output = ''
  11. self.indent = 0
  12. self.preserve_newlines = False
  13. def write(self, text):
  14. self.output += text.replace('\n', '\n' + ' ' * self.indent)
  15. def visit_document(self, node):
  16. pass
  17. def depart_document(self, node):
  18. pass
  19. def visit_section(self, node):
  20. pass
  21. def depart_section(self, node):
  22. # Skip all sections except the first one.
  23. raise nodes.StopTraversal
  24. def visit_title(self, node):
  25. self.version = re.match(r'(\d+\.\d+\.\d+).*', node.children[0]).group(1)
  26. raise nodes.SkipChildren
  27. def visit_title_reference(self, node):
  28. raise Exception(node)
  29. def depart_title(self, node):
  30. pass
  31. def visit_Text(self, node):
  32. if not self.preserve_newlines:
  33. node = node.replace('\n', ' ')
  34. self.write(node)
  35. def depart_Text(self, node):
  36. pass
  37. def visit_bullet_list(self, node):
  38. pass
  39. def depart_bullet_list(self, node):
  40. pass
  41. def visit_list_item(self, node):
  42. self.write('* ')
  43. self.indent += 2
  44. def depart_list_item(self, node):
  45. self.indent -= 2
  46. self.write('\n\n')
  47. def visit_paragraph(self, node):
  48. self.write('\n\n')
  49. def depart_paragraph(self, node):
  50. pass
  51. def visit_reference(self, node):
  52. if not is_github_ref(node):
  53. self.write('[')
  54. def depart_reference(self, node):
  55. if not is_github_ref(node):
  56. self.write('](' + node['refuri'] + ')')
  57. def visit_target(self, node):
  58. pass
  59. def depart_target(self, node):
  60. pass
  61. def visit_literal(self, node):
  62. self.write('`')
  63. def depart_literal(self, node):
  64. self.write('`')
  65. def visit_literal_block(self, node):
  66. self.write('\n\n```')
  67. if 'c++' in node['classes']:
  68. self.write('c++')
  69. self.write('\n')
  70. self.preserve_newlines = True
  71. def depart_literal_block(self, node):
  72. self.write('\n```\n')
  73. self.preserve_newlines = False
  74. def visit_inline(self, node):
  75. pass
  76. def depart_inline(self, node):
  77. pass
  78. def visit_image(self, node):
  79. self.write('![](' + node['uri'] + ')')
  80. def depart_image(self, node):
  81. pass
  82. def write_row(self, row, widths):
  83. for i, entry in enumerate(row):
  84. text = entry[0][0] if len(entry) > 0 else ''
  85. if i != 0:
  86. self.write('|')
  87. self.write('{:{}}'.format(text, widths[i]))
  88. self.write('\n')
  89. def visit_table(self, node):
  90. table = node.children[0]
  91. colspecs = table[:-2]
  92. thead = table[-2]
  93. tbody = table[-1]
  94. widths = [int(cs['colwidth']) for cs in colspecs]
  95. sep = '|'.join(['-' * w for w in widths]) + '\n'
  96. self.write('\n\n')
  97. self.write_row(thead[0], widths)
  98. self.write(sep)
  99. for row in tbody:
  100. self.write_row(row, widths)
  101. raise nodes.SkipChildren
  102. def depart_table(self, node):
  103. pass
  104. class MDWriter(writers.Writer):
  105. """GitHub-flavored markdown writer"""
  106. supported = ('md',)
  107. """Formats this writer supports."""
  108. def translate(self):
  109. translator = Translator(self.document)
  110. self.document.walkabout(translator)
  111. self.output = (translator.output, translator.version)
  112. def convert(rst_path):
  113. """Converts RST file to Markdown."""
  114. return core.publish_file(source_path=rst_path, writer=MDWriter())
  115. if __name__ == '__main__':
  116. convert(sys.argv[1])