/External.LCA_RESTRICTED/Languages/IronPython/27/Doc/sphinx/ext/todo.py

https://github.com/kumaryu/IronLanguages-main
Python | 157 lines | 93 code | 35 blank | 29 comment | 12 complexity | 6916ca4452c7d442f27d14fe9c6612e8 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. """
  3. sphinx.ext.todo
  4. ~~~~~~~~~~~~~~~
  5. Allow todos to be inserted into your documentation. Inclusion of todos can
  6. be switched of by a configuration variable. The todolist directive collects
  7. all todos of your project and lists them along with a backlink to the
  8. original location.
  9. :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
  10. :license: BSD, see LICENSE for details.
  11. """
  12. from docutils import nodes
  13. from sphinx.environment import NoUri
  14. from sphinx.util.compat import Directive, make_admonition
  15. class todo_node(nodes.Admonition, nodes.Element): pass
  16. class todolist(nodes.General, nodes.Element): pass
  17. class Todo(Directive):
  18. """
  19. A todo entry, displayed (if configured) in the form of an admonition.
  20. """
  21. has_content = True
  22. required_arguments = 0
  23. optional_arguments = 0
  24. final_argument_whitespace = False
  25. option_spec = {}
  26. def run(self):
  27. env = self.state.document.settings.env
  28. targetid = "todo-%s" % env.index_num
  29. env.index_num += 1
  30. targetnode = nodes.target('', '', ids=[targetid])
  31. ad = make_admonition(todo_node, self.name, [_('Todo')], self.options,
  32. self.content, self.lineno, self.content_offset,
  33. self.block_text, self.state, self.state_machine)
  34. # Attach a list of all todos to the environment,
  35. # the todolist works with the collected todo nodes
  36. if not hasattr(env, 'todo_all_todos'):
  37. env.todo_all_todos = []
  38. env.todo_all_todos.append({
  39. 'docname': env.docname,
  40. 'lineno': self.lineno,
  41. 'todo': ad[0].deepcopy(),
  42. 'target': targetnode,
  43. })
  44. return [targetnode] + ad
  45. class TodoList(Directive):
  46. """
  47. A list of all todo entries.
  48. """
  49. has_content = False
  50. required_arguments = 0
  51. optional_arguments = 0
  52. final_argument_whitespace = False
  53. option_spec = {}
  54. def run(self):
  55. # Simply insert an empty todolist node which will be replaced later
  56. # when process_todo_nodes is called
  57. return [todolist('')]
  58. def process_todo_nodes(app, doctree, fromdocname):
  59. if not app.config['todo_include_todos']:
  60. for node in doctree.traverse(todo_node):
  61. node.parent.remove(node)
  62. # Replace all todolist nodes with a list of the collected todos.
  63. # Augment each todo with a backlink to the original location.
  64. env = app.builder.env
  65. if not hasattr(env, 'todo_all_todos'):
  66. env.todo_all_todos = []
  67. for node in doctree.traverse(todolist):
  68. if not app.config['todo_include_todos']:
  69. node.replace_self([])
  70. continue
  71. content = []
  72. for todo_info in env.todo_all_todos:
  73. para = nodes.paragraph()
  74. filename = env.doc2path(todo_info['docname'], base=None)
  75. description = (
  76. _('(The original entry is located in %s, line %d and '
  77. 'can be found ') % (filename, todo_info['lineno']))
  78. para += nodes.Text(description, description)
  79. # Create a reference
  80. newnode = nodes.reference('', '')
  81. innernode = nodes.emphasis(_('here'), _('here'))
  82. newnode['refdocname'] = todo_info['docname']
  83. try:
  84. newnode['refuri'] = app.builder.get_relative_uri(
  85. fromdocname, todo_info['docname'])
  86. newnode['refuri'] += '#' + todo_info['target']['refid']
  87. except NoUri:
  88. # ignore if no URI can be determined, e.g. for LaTeX output
  89. pass
  90. newnode.append(innernode)
  91. para += newnode
  92. para += nodes.Text('.)', '.)')
  93. # (Recursively) resolve references in the todo content
  94. todo_entry = todo_info['todo']
  95. env.resolve_references(todo_entry, todo_info['docname'],
  96. app.builder)
  97. # Insert into the todolist
  98. content.append(todo_entry)
  99. content.append(para)
  100. node.replace_self(content)
  101. def purge_todos(app, env, docname):
  102. if not hasattr(env, 'todo_all_todos'):
  103. return
  104. env.todo_all_todos = [todo for todo in env.todo_all_todos
  105. if todo['docname'] != docname]
  106. def visit_todo_node(self, node):
  107. self.visit_admonition(node)
  108. def depart_todo_node(self, node):
  109. self.depart_admonition(node)
  110. def setup(app):
  111. app.add_config_value('todo_include_todos', False, False)
  112. app.add_node(todolist)
  113. app.add_node(todo_node,
  114. html=(visit_todo_node, depart_todo_node),
  115. latex=(visit_todo_node, depart_todo_node),
  116. text=(visit_todo_node, depart_todo_node))
  117. app.add_directive('todo', Todo)
  118. app.add_directive('todolist', TodoList)
  119. app.connect('doctree-resolved', process_todo_nodes)
  120. app.connect('env-purge-doc', purge_todos)