sphinxext.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. """
  2. pygments.sphinxext
  3. ~~~~~~~~~~~~~~~~~~
  4. Sphinx extension to generate automatic documentation of lexers,
  5. formatters and filters.
  6. :copyright: Copyright 2006-2022 by the Pygments team, see AUTHORS.
  7. :license: BSD, see LICENSE for details.
  8. """
  9. import sys
  10. from docutils import nodes
  11. from docutils.statemachine import ViewList
  12. from docutils.parsers.rst import Directive
  13. from sphinx.util.nodes import nested_parse_with_titles
  14. MODULEDOC = '''
  15. .. module:: %s
  16. %s
  17. %s
  18. '''
  19. LEXERDOC = '''
  20. .. class:: %s
  21. :Short names: %s
  22. :Filenames: %s
  23. :MIME types: %s
  24. %s
  25. '''
  26. FMTERDOC = '''
  27. .. class:: %s
  28. :Short names: %s
  29. :Filenames: %s
  30. %s
  31. '''
  32. FILTERDOC = '''
  33. .. class:: %s
  34. :Name: %s
  35. %s
  36. '''
  37. class PygmentsDoc(Directive):
  38. """
  39. A directive to collect all lexers/formatters/filters and generate
  40. autoclass directives for them.
  41. """
  42. has_content = False
  43. required_arguments = 1
  44. optional_arguments = 0
  45. final_argument_whitespace = False
  46. option_spec = {}
  47. def run(self):
  48. self.filenames = set()
  49. if self.arguments[0] == 'lexers':
  50. out = self.document_lexers()
  51. elif self.arguments[0] == 'formatters':
  52. out = self.document_formatters()
  53. elif self.arguments[0] == 'filters':
  54. out = self.document_filters()
  55. elif self.arguments[0] == 'lexers_overview':
  56. out = self.document_lexers_overview()
  57. else:
  58. raise Exception('invalid argument for "pygmentsdoc" directive')
  59. node = nodes.compound()
  60. vl = ViewList(out.split('\n'), source='')
  61. nested_parse_with_titles(self.state, vl, node)
  62. for fn in self.filenames:
  63. self.state.document.settings.record_dependencies.add(fn)
  64. return node.children
  65. def document_lexers_overview(self):
  66. """Generate a tabular overview of all lexers.
  67. The columns are the lexer name, the extensions handled by this lexer
  68. (or "None"), the aliases and a link to the lexer class."""
  69. from pip._vendor.pygments.lexers._mapping import LEXERS
  70. from pip._vendor.pygments.lexers import find_lexer_class
  71. out = []
  72. table = []
  73. def format_link(name, url):
  74. if url:
  75. return f'`{name} <{url}>`_'
  76. return name
  77. for classname, data in sorted(LEXERS.items(), key=lambda x: x[1][1].lower()):
  78. lexer_cls = find_lexer_class(data[1])
  79. extensions = lexer_cls.filenames + lexer_cls.alias_filenames
  80. table.append({
  81. 'name': format_link(data[1], lexer_cls.url),
  82. 'extensions': ', '.join(extensions).replace('*', '\\*').replace('_', '\\') or 'None',
  83. 'aliases': ', '.join(data[2]),
  84. 'class': f'{data[0]}.{classname}'
  85. })
  86. column_names = ['name', 'extensions', 'aliases', 'class']
  87. column_lengths = [max([len(row[column]) for row in table if row[column]])
  88. for column in column_names]
  89. def write_row(*columns):
  90. """Format a table row"""
  91. out = []
  92. for l, c in zip(column_lengths, columns):
  93. if c:
  94. out.append(c.ljust(l))
  95. else:
  96. out.append(' '*l)
  97. return ' '.join(out)
  98. def write_seperator():
  99. """Write a table separator row"""
  100. sep = ['='*c for c in column_lengths]
  101. return write_row(*sep)
  102. out.append(write_seperator())
  103. out.append(write_row('Name', 'Extension(s)', 'Short name(s)', 'Lexer class'))
  104. out.append(write_seperator())
  105. for row in table:
  106. out.append(write_row(
  107. row['name'],
  108. row['extensions'],
  109. row['aliases'],
  110. f':class:`~{row["class"]}`'))
  111. out.append(write_seperator())
  112. return '\n'.join(out)
  113. def document_lexers(self):
  114. from pip._vendor.pygments.lexers._mapping import LEXERS
  115. out = []
  116. modules = {}
  117. moduledocstrings = {}
  118. for classname, data in sorted(LEXERS.items(), key=lambda x: x[0]):
  119. module = data[0]
  120. mod = __import__(module, None, None, [classname])
  121. self.filenames.add(mod.__file__)
  122. cls = getattr(mod, classname)
  123. if not cls.__doc__:
  124. print("Warning: %s does not have a docstring." % classname)
  125. docstring = cls.__doc__
  126. if isinstance(docstring, bytes):
  127. docstring = docstring.decode('utf8')
  128. modules.setdefault(module, []).append((
  129. classname,
  130. ', '.join(data[2]) or 'None',
  131. ', '.join(data[3]).replace('*', '\\*').replace('_', '\\') or 'None',
  132. ', '.join(data[4]) or 'None',
  133. docstring))
  134. if module not in moduledocstrings:
  135. moddoc = mod.__doc__
  136. if isinstance(moddoc, bytes):
  137. moddoc = moddoc.decode('utf8')
  138. moduledocstrings[module] = moddoc
  139. for module, lexers in sorted(modules.items(), key=lambda x: x[0]):
  140. if moduledocstrings[module] is None:
  141. raise Exception("Missing docstring for %s" % (module,))
  142. heading = moduledocstrings[module].splitlines()[4].strip().rstrip('.')
  143. out.append(MODULEDOC % (module, heading, '-'*len(heading)))
  144. for data in lexers:
  145. out.append(LEXERDOC % data)
  146. return ''.join(out)
  147. def document_formatters(self):
  148. from pip._vendor.pygments.formatters import FORMATTERS
  149. out = []
  150. for classname, data in sorted(FORMATTERS.items(), key=lambda x: x[0]):
  151. module = data[0]
  152. mod = __import__(module, None, None, [classname])
  153. self.filenames.add(mod.__file__)
  154. cls = getattr(mod, classname)
  155. docstring = cls.__doc__
  156. if isinstance(docstring, bytes):
  157. docstring = docstring.decode('utf8')
  158. heading = cls.__name__
  159. out.append(FMTERDOC % (heading, ', '.join(data[2]) or 'None',
  160. ', '.join(data[3]).replace('*', '\\*') or 'None',
  161. docstring))
  162. return ''.join(out)
  163. def document_filters(self):
  164. from pip._vendor.pygments.filters import FILTERS
  165. out = []
  166. for name, cls in FILTERS.items():
  167. self.filenames.add(sys.modules[cls.__module__].__file__)
  168. docstring = cls.__doc__
  169. if isinstance(docstring, bytes):
  170. docstring = docstring.decode('utf8')
  171. out.append(FILTERDOC % (cls.__name__, name, docstring))
  172. return ''.join(out)
  173. def setup(app):
  174. app.add_directive('pygmentsdoc', PygmentsDoc)