sphinxext.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. """
  2. pygments.sphinxext
  3. ~~~~~~~~~~~~~~~~~~
  4. Sphinx extension to generate automatic documentation of lexers,
  5. formatters and filters.
  6. :copyright: Copyright 2006-2023 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 pygments.lexers._mapping import LEXERS
  70. import pygments.lexers
  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 = pygments.lexers.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 pygments.lexers._mapping import LEXERS
  115. import pygments
  116. import inspect
  117. import pathlib
  118. out = []
  119. modules = {}
  120. moduledocstrings = {}
  121. for classname, data in sorted(LEXERS.items(), key=lambda x: x[0]):
  122. module = data[0]
  123. mod = __import__(module, None, None, [classname])
  124. self.filenames.add(mod.__file__)
  125. cls = getattr(mod, classname)
  126. if not cls.__doc__:
  127. print("Warning: %s does not have a docstring." % classname)
  128. docstring = cls.__doc__
  129. if isinstance(docstring, bytes):
  130. docstring = docstring.decode('utf8')
  131. example_file = getattr(cls, '_example', None)
  132. if example_file:
  133. p = pathlib.Path(inspect.getabsfile(pygments)).parent.parent /\
  134. 'tests' / 'examplefiles' / example_file
  135. content = p.read_text(encoding='utf-8')
  136. if not content:
  137. raise Exception(
  138. f"Empty example file '{example_file}' for lexer "
  139. f"{classname}")
  140. if data[2]:
  141. lexer_name = data[2][0]
  142. docstring += '\n\n .. admonition:: Example\n'
  143. docstring += f'\n .. code-block:: {lexer_name}\n\n'
  144. for line in content.splitlines():
  145. docstring += f' {line}\n'
  146. modules.setdefault(module, []).append((
  147. classname,
  148. ', '.join(data[2]) or 'None',
  149. ', '.join(data[3]).replace('*', '\\*').replace('_', '\\') or 'None',
  150. ', '.join(data[4]) or 'None',
  151. docstring))
  152. if module not in moduledocstrings:
  153. moddoc = mod.__doc__
  154. if isinstance(moddoc, bytes):
  155. moddoc = moddoc.decode('utf8')
  156. moduledocstrings[module] = moddoc
  157. for module, lexers in sorted(modules.items(), key=lambda x: x[0]):
  158. if moduledocstrings[module] is None:
  159. raise Exception("Missing docstring for %s" % (module,))
  160. heading = moduledocstrings[module].splitlines()[4].strip().rstrip('.')
  161. out.append(MODULEDOC % (module, heading, '-'*len(heading)))
  162. for data in lexers:
  163. out.append(LEXERDOC % data)
  164. return ''.join(out)
  165. def document_formatters(self):
  166. from pygments.formatters import FORMATTERS
  167. out = []
  168. for classname, data in sorted(FORMATTERS.items(), key=lambda x: x[0]):
  169. module = data[0]
  170. mod = __import__(module, None, None, [classname])
  171. self.filenames.add(mod.__file__)
  172. cls = getattr(mod, classname)
  173. docstring = cls.__doc__
  174. if isinstance(docstring, bytes):
  175. docstring = docstring.decode('utf8')
  176. heading = cls.__name__
  177. out.append(FMTERDOC % (heading, ', '.join(data[2]) or 'None',
  178. ', '.join(data[3]).replace('*', '\\*') or 'None',
  179. docstring))
  180. return ''.join(out)
  181. def document_filters(self):
  182. from pygments.filters import FILTERS
  183. out = []
  184. for name, cls in FILTERS.items():
  185. self.filenames.add(sys.modules[cls.__module__].__file__)
  186. docstring = cls.__doc__
  187. if isinstance(docstring, bytes):
  188. docstring = docstring.decode('utf8')
  189. out.append(FILTERDOC % (cls.__name__, name, docstring))
  190. return ''.join(out)
  191. def setup(app):
  192. app.add_directive('pygmentsdoc', PygmentsDoc)