configurationblock.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #Copyright (c) 2010 Fabien Potencier
  2. #
  3. #Permission is hereby granted, free of charge, to any person obtaining a copy
  4. #of this software and associated documentation files (the "Software"), to deal
  5. #in the Software without restriction, including without limitation the rights
  6. #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. #copies of the Software, and to permit persons to whom the Software is furnished
  8. #to do so, subject to the following conditions:
  9. #
  10. #The above copyright notice and this permission notice shall be included in all
  11. #copies or substantial portions of the Software.
  12. #
  13. #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. #THE SOFTWARE.
  20. from docutils.parsers.rst import Directive, directives
  21. from docutils import nodes
  22. from string import upper
  23. class configurationblock(nodes.General, nodes.Element):
  24. pass
  25. class ConfigurationBlock(Directive):
  26. has_content = True
  27. required_arguments = 0
  28. optional_arguments = 0
  29. final_argument_whitespace = True
  30. option_spec = {}
  31. formats = {
  32. 'html': 'HTML',
  33. 'xml': 'XML',
  34. 'php': 'PHP',
  35. 'yaml': 'YAML',
  36. 'jinja': 'Twig',
  37. 'html+jinja': 'Twig',
  38. 'jinja+html': 'Twig',
  39. 'php+html': 'PHP',
  40. 'html+php': 'PHP',
  41. 'ini': 'INI',
  42. 'php-annotations': 'Annotations',
  43. }
  44. def run(self):
  45. env = self.state.document.settings.env
  46. node = nodes.Element()
  47. node.document = self.state.document
  48. self.state.nested_parse(self.content, self.content_offset, node)
  49. entries = []
  50. for i, child in enumerate(node):
  51. if isinstance(child, nodes.literal_block):
  52. # add a title (the language name) before each block
  53. #targetid = "configuration-block-%d" % env.new_serialno('configuration-block')
  54. #targetnode = nodes.target('', '', ids=[targetid])
  55. #targetnode.append(child)
  56. innernode = nodes.emphasis(self.formats[child['language']], self.formats[child['language']])
  57. para = nodes.paragraph()
  58. para += [innernode, child]
  59. entry = nodes.list_item('')
  60. entry.append(para)
  61. entries.append(entry)
  62. resultnode = configurationblock()
  63. resultnode.append(nodes.bullet_list('', *entries))
  64. return [resultnode]
  65. def visit_configurationblock_html(self, node):
  66. self.body.append(self.starttag(node, 'div', CLASS='configuration-block'))
  67. def depart_configurationblock_html(self, node):
  68. self.body.append('</div>\n')
  69. def visit_configurationblock_latex(self, node):
  70. pass
  71. def depart_configurationblock_latex(self, node):
  72. pass
  73. def setup(app):
  74. app.add_node(configurationblock,
  75. html=(visit_configurationblock_html, depart_configurationblock_html),
  76. latex=(visit_configurationblock_latex, depart_configurationblock_latex))
  77. app.add_directive('configuration-block', ConfigurationBlock)