configurationblock.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # -*- coding: utf-8 -*-
  2. """
  3. :copyright: (c) 2010-2012 Fabien Potencier
  4. :license: MIT, see LICENSE for more details.
  5. """
  6. from docutils.parsers.rst import Directive, directives
  7. from docutils import nodes
  8. from string import upper
  9. class configurationblock(nodes.General, nodes.Element):
  10. pass
  11. class ConfigurationBlock(Directive):
  12. has_content = True
  13. required_arguments = 0
  14. optional_arguments = 0
  15. final_argument_whitespace = True
  16. option_spec = {}
  17. formats = {
  18. 'html': 'HTML',
  19. 'xml': 'XML',
  20. 'php': 'PHP',
  21. 'yaml': 'YAML',
  22. 'jinja': 'Twig',
  23. 'html+jinja': 'Twig',
  24. 'jinja+html': 'Twig',
  25. 'php+html': 'PHP',
  26. 'html+php': 'PHP',
  27. 'ini': 'INI',
  28. 'php-annotations': 'Annotations',
  29. }
  30. def run(self):
  31. env = self.state.document.settings.env
  32. node = nodes.Element()
  33. node.document = self.state.document
  34. self.state.nested_parse(self.content, self.content_offset, node)
  35. entries = []
  36. for i, child in enumerate(node):
  37. if isinstance(child, nodes.literal_block):
  38. # add a title (the language name) before each block
  39. #targetid = "configuration-block-%d" % env.new_serialno('configuration-block')
  40. #targetnode = nodes.target('', '', ids=[targetid])
  41. #targetnode.append(child)
  42. innernode = nodes.emphasis(self.formats[child['language']], self.formats[child['language']])
  43. para = nodes.paragraph()
  44. para += [innernode, child]
  45. entry = nodes.list_item('')
  46. entry.append(para)
  47. entries.append(entry)
  48. resultnode = configurationblock()
  49. resultnode.append(nodes.bullet_list('', *entries))
  50. return [resultnode]
  51. def visit_configurationblock_html(self, node):
  52. self.body.append(self.starttag(node, 'div', CLASS='configuration-block'))
  53. def depart_configurationblock_html(self, node):
  54. self.body.append('</div>\n')
  55. def visit_configurationblock_latex(self, node):
  56. pass
  57. def depart_configurationblock_latex(self, node):
  58. pass
  59. def setup(app):
  60. app.add_node(configurationblock,
  61. html=(visit_configurationblock_html, depart_configurationblock_html),
  62. latex=(visit_configurationblock_latex, depart_configurationblock_latex))
  63. app.add_directive('configuration-block', ConfigurationBlock)