directives.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. # -*- coding: utf-8 -*-
  2. from docutils import nodes
  3. from docutils.parsers.rst.directives.admonitions import BaseAdmonition
  4. from docutils.parsers.rst.directives import unchanged
  5. class HandbookAdmonition(BaseAdmonition):
  6. """RST directive
  7. """
  8. node_class = nodes.admonition
  9. # empty is no allowed
  10. has_content = True
  11. # needs at least a one word title
  12. required_arguments = 1
  13. option_spec = {
  14. 'name': unchanged,
  15. 'float': unchanged,
  16. }
  17. hba_cls = None
  18. hba_label = None
  19. # whether an admonition should be allowed to be displayed
  20. # in a toggle container (HTML only)
  21. toggle = True
  22. def run(self):
  23. # this uses the admonition code for RST parsing
  24. toggle = _make_std_nodes(
  25. self,
  26. super().run(),
  27. self.hba_cls,
  28. [self.hba_label])
  29. return [toggle]
  30. def _make_std_nodes(admonition, docnodes, cls, classes):
  31. # throw away the title, because we want to mark
  32. # it up as a 'header' further down
  33. del docnodes[0][0]
  34. # now put the entire admonition structure into a container
  35. # that we assign the necessary class to make it 'toggle-able'
  36. # in HTML
  37. # outer container
  38. return cls(
  39. 'handbookbox',
  40. # header line with 'Find out more' prefix
  41. nodes.paragraph(
  42. # place actual admonition title we removed
  43. # above
  44. 'title', admonition.arguments[0],
  45. # add (CSS) class
  46. classes=['header'],
  47. ),
  48. # place the rest of the admonition structure after the header,
  49. # but still inside the container
  50. *docnodes[0].children,
  51. # properly identify as 'findoutmore' to enable easy custom
  52. # styling, and also tag with 'toggle'. The later is actually
  53. # not 100% necessary, as 'findoutmore' could get that
  54. # functional assigned in CSS instead (maybe streamline later)
  55. classes=(['toggle'] if admonition.toggle else []) + classes,
  56. # propagate all other attributes
  57. **{k: v for k, v in docnodes[0].attributes.items() if k != 'classes'}
  58. )
  59. def _get_counted_boxstart(label, node):
  60. title = node.children[0].astext()
  61. # we have used the title for the colorbox header
  62. # already, do not duplicate in the body
  63. del node.children[0]
  64. float_args = ''
  65. if 'float' in node.attributes:
  66. flt = node.attributes['float']
  67. float_args = ', float, floatplacement={}'.format(flt) \
  68. if flt else ', float'
  69. return \
  70. "\\begin{{{label}}}" \
  71. "[{ref}before title={{\\thetcbcounter\\ }}{float_args}]" \
  72. "{{{title}}}\n".format(
  73. ref='label={{{r}}}, '.format(r=node.attributes['ids'][0])
  74. if len(node.attributes.get('ids', []))
  75. else '',
  76. label=label,
  77. title=title,
  78. float_args=float_args,
  79. )
  80. def _add_label(body, node):
  81. """If we can construct a latex label for a node, add to body"""
  82. parent_docname = node.parent.attributes.get('docname')
  83. node_id = [i for i in node.attributes.get('ids') or []]
  84. node_id = node_id[0] if len(node_id) else None
  85. # build the same form that sphinx does
  86. label = '\\detokenize{{{parent}:{id}}}'.format(
  87. parent=parent_docname, id=node_id) \
  88. if parent_docname and node_id else ''
  89. if label:
  90. body.append('\\label{{{}}}\n'.format(label))
  91. class gitusernote(nodes.Admonition, nodes.Element):
  92. """Custom "gitusernote" admonition."""
  93. def visit_gitusernote_html(self, node):
  94. self.visit_container(node)
  95. def depart_gitusernote_html(self, node):
  96. self.depart_container(node)
  97. def visit_gitusernote_latex(self, node):
  98. self.body.append(_get_counted_boxstart('gitusernote', node))
  99. _add_label(self.body, node)
  100. def depart_gitusernote_latex(self, node):
  101. self.body.append('\n\n\\end{gitusernote}\n')
  102. class GitUserNote(HandbookAdmonition):
  103. """
  104. An admonition mentioning things to look at as reference.
  105. """
  106. hba_cls = gitusernote
  107. hba_label = 'gitusernote'
  108. toggle = False
  109. class findoutmore(nodes.container):
  110. """Custom "findoutmore" container."""
  111. pass
  112. def visit_findoutmore_html(self, node):
  113. self.visit_container(node)
  114. def depart_findoutmore_html(self, node):
  115. self.depart_container(node)
  116. def visit_findoutmore_latex(self, node):
  117. self.body.append(_get_counted_boxstart('findoutmore', node))
  118. _add_label(self.body, node)
  119. def depart_findoutmore_latex(self, node):
  120. self.body.append('\n\n\\end{findoutmore}\n')
  121. class FindOutMore(HandbookAdmonition):
  122. """findoutmore RST directive
  123. The idea here is to use an admonition to parse the RST,
  124. but actually fully replace it afterwards with a custom
  125. node structure. This is done to be able to replace a
  126. rather verbose custom markup that was used before in the
  127. book. Eventually, it may be replaced (in here) with
  128. something completely different -- without having to change
  129. content and markup in the book sources.
  130. """
  131. hba_cls = findoutmore
  132. hba_label = 'findoutmore'
  133. class windowswit(nodes.container):
  134. """Custom "windowswit" container."""
  135. pass
  136. class WindowsWit(HandbookAdmonition):
  137. """windows-wit RST directive
  138. This is identical to the FindOutMore directive, and allows a custom markup
  139. for notes targeted at Windows users
  140. """
  141. hba_cls = windowswit
  142. hba_label = 'windows-wit'
  143. def visit_windowswit_html(self, node):
  144. self.visit_container(node)
  145. def depart_windowswit_html(self, node):
  146. self.depart_container(node)
  147. def visit_windowswit_latex(self, node):
  148. self.body.append(_get_counted_boxstart('windowswit', node))
  149. _add_label(self.body, node)
  150. def depart_windowswit_latex(self, node):
  151. self.body.append('\n\n\\end{windowswit}\n')
  152. class importantnote(nodes.container):
  153. pass
  154. def visit_importantnote_html(self, node):
  155. self.visit_container(node)
  156. def depart_importantnote_html(self, node):
  157. self.depart_container(node)
  158. def visit_importantnote_latex(self, node):
  159. self.body.append(_get_counted_boxstart('importantnote', node))
  160. _add_label(self.body, node)
  161. def depart_importantnote_latex(self, node):
  162. self.body.append('\n\n\\end{importantnote}\n')
  163. class ImportantNote(HandbookAdmonition):
  164. hba_cls = importantnote
  165. hba_label = 'importantnote'
  166. toggle = False
  167. class findoutmoreref(nodes.inline):
  168. pass
  169. def visit_findoutmoreref_html(self, node):
  170. self.visit_inline(node)
  171. self.body.append('Find-out-more ')
  172. def depart_findoutmoreref_html(self, node):
  173. self.depart_inline(node)
  174. def visit_findoutmoreref_latex(self, node):
  175. # \textit to imitate the href style
  176. self.body.append(
  177. '\\textit{{Find-out-more}}~{{\\findoutmoreiconinline}}\\textit{{\\ref{{{r}}}}} '.format(
  178. r=node.children[0].attributes['refid']))
  179. def depart_findoutmoreref_latex(self, node):
  180. pass
  181. class windowswitref(nodes.inline):
  182. pass
  183. def visit_windowswitref_html(self, node):
  184. self.visit_inline(node)
  185. self.body.append('Windows-wit ')
  186. def depart_windowswitref_html(self, node):
  187. self.depart_inline(node)
  188. def visit_windowswitref_latex(self, node):
  189. # \textit to imitate the href style
  190. self.body.append(
  191. '\\textit{{Windows-wit}}~{{\\windowswiticoninline}}\\textit{{\\ref{{{r}}}}} '.format(
  192. r=node.children[0].attributes['refid']))
  193. def depart_windowswitref_latex(self, node):
  194. pass
  195. def setup(app):
  196. app.add_node(
  197. gitusernote,
  198. html=(visit_gitusernote_html, depart_gitusernote_html),
  199. latex=(visit_gitusernote_latex, depart_gitusernote_latex),
  200. )
  201. app.add_directive('gitusernote', GitUserNote)
  202. app.add_node(
  203. findoutmore,
  204. html=(visit_findoutmore_html, depart_findoutmore_html),
  205. latex=(visit_findoutmore_latex, depart_findoutmore_latex),
  206. )
  207. app.add_directive('find-out-more', FindOutMore)
  208. app.add_node(
  209. windowswit,
  210. html=(visit_windowswit_html, depart_windowswit_html),
  211. latex=(visit_windowswit_latex, depart_windowswit_latex),
  212. )
  213. app.add_directive('windows-wit', WindowsWit)
  214. app.add_node(
  215. importantnote,
  216. html=(visit_importantnote_html, depart_importantnote_html),
  217. latex=(visit_importantnote_latex, depart_importantnote_latex),
  218. )
  219. app.add_directive('importantnote', ImportantNote)
  220. app.add_node(
  221. findoutmoreref,
  222. html=(visit_findoutmoreref_html, depart_findoutmoreref_html),
  223. latex=(visit_findoutmoreref_latex, depart_findoutmoreref_latex),
  224. )
  225. app.add_node(
  226. windowswitref,
  227. html=(visit_windowswitref_html, depart_windowswitref_html),
  228. latex=(visit_windowswitref_latex, depart_windowswitref_latex),
  229. )
  230. # vim: set expandtab shiftwidth=4 softtabstop=4 :