PageRenderTime 58ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/documentation/sphinx/ext/XXXsphinx_clatexXXX/writer.py

https://github.com/bruceravel/demeter
Python | 217 lines | 210 code | 3 blank | 4 comment | 0 complexity | 0c446442f0afdfb3b0e39c909c897c50 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. """
  3. LaTeX writer for Sphinx which allows to use arbitrary LaTeX class.
  4. """
  5. import sys
  6. from os import path
  7. from docutils import nodes
  8. from sphinx import highlighting
  9. import sphinx.writers.latex
  10. from sphinx.util.osutil import ustrftime
  11. from sphinx.ext.mathbase import latex_visit_math, latex_visit_displaymath, latex_visit_eqref
  12. from .directives import *
  13. HEADER = r'''%%%% File generated by Sphinx clatex builder
  14. %(documentclass)s
  15. %%%% Added by Sphinx:
  16. \usepackage[%(hyperref_args)s]{hyperref}
  17. %(longtable)s
  18. %(tabulary)s
  19. %(multirow)s
  20. %(makeidx)s
  21. %%%% Sphinx: addition's end.
  22. %(preamble)s
  23. '''
  24. BEGIN_DOC = \
  25. r'''
  26. \begin{document}
  27. %(begin_doc)s
  28. '''
  29. FOOTER = \
  30. r'''
  31. %(end_doc)s
  32. \end{document}
  33. '''
  34. class CustomLaTeXWriter(sphinx.writers.latex.LaTeXWriter):
  35. def translate(self):
  36. visitor = CustomLaTeXTranslator(self.document, self.builder)
  37. self.document.walkabout(visitor)
  38. self.output = visitor.astext()
  39. class CustomLaTeXTranslator(sphinx.writers.latex.LaTeXTranslator, nodes.NodeVisitor, object):
  40. default_elements = {
  41. 'preamble': '',
  42. 'begin_doc': '',
  43. 'end_doc': '',
  44. 'longtable': '',
  45. 'tabulary': '\\usepackage{tabulary}',
  46. 'multirow': '\\usepackage{multirow}',
  47. 'hyperref_args': '',
  48. 'makeidx': '',
  49. 'documentclass': '\documentclass{book}',
  50. 'shorthandoff': '',
  51. 'transition': '\n\n\\bigskip\\hrule{}\\bigskip\n\n',
  52. 'figure_align': 'htbp',
  53. }
  54. def visit_document(self, node):
  55. self.footnotestack.append(self.collect_footnotes(node))
  56. self.curfilestack.append(node.get('docname', ''))
  57. if self.first_document == 1:
  58. # the first document is all the regular content ...
  59. self.body.append(BEGIN_DOC % self.elements)
  60. self.first_document = 0
  61. elif self.first_document == 0:
  62. # ... and all others are the appendices
  63. self.body.append(u'\n\\appendix\n')
  64. self.first_document = -1
  65. if 'docname' in node:
  66. self.body.append(self.hypertarget(':doc'))
  67. # "- 1" because the level is increased before the title is visited
  68. self.sectionlevel = self.top_sectionlevel - 1
  69. # todo: I should find a real solution
  70. def visit_transition(self, node):
  71. try:
  72. self.body.append(self.elements['transition'])
  73. except Exception, e:
  74. print('ERROR: %s' % e)
  75. def __init__(self, document, builder):
  76. # super(type(self), self).__init__(document, builder)
  77. nodes.NodeVisitor.__init__(self, document)
  78. self.builder = builder
  79. self.body = []
  80. self.sectionnames = builder.app.config.clatex_sectionnames
  81. self.elements = self.default_elements.copy()
  82. if type(builder.config.clatex_makeidx) == bool:
  83. if builder.config.clatex_makeidx:
  84. makeidx = '\\usepackage{makeidx}\n\\makeindex'
  85. else:
  86. makeidx = ''
  87. else:
  88. makeidx = builder.config.clatex_makeidx
  89. self.elements.update({
  90. 'author': document.settings.author,
  91. 'title': document.settings.title,
  92. 'documentclass': builder.config.clatex_documentclass,
  93. 'preamble': builder.config.clatex_preamble,
  94. 'begin_doc': builder.config.clatex_begin_doc,
  95. 'end_doc': builder.config.clatex_end_doc,
  96. 'hyperref_args': builder.config.clatex_hyperref_args,
  97. 'makeidx': makeidx,
  98. })
  99. self.highlighter = highlighting.PygmentsBridge('latex',
  100. builder.config.pygments_style, builder.config.trim_doctest_flags)
  101. self.context = []
  102. self.descstack = []
  103. self.bibitems = []
  104. self.table = None
  105. self.next_table_colspec = None
  106. # stack of [language, linenothreshold] settings per file
  107. # the first item here is the default and must not be changed
  108. # the second item is the default for the master file and can be changed
  109. # by .. highlight:: directive in the master file
  110. self.hlsettingstack = 2 * [[builder.config.highlight_language,
  111. sys.maxint]]
  112. self.footnotestack = []
  113. self.curfilestack = []
  114. self.handled_abbrs = set()
  115. if builder.config.latex_use_parts:
  116. self.top_sectionlevel = 0
  117. else:
  118. if builder.config.clatex_use_chapters:
  119. self.top_sectionlevel = 1
  120. else:
  121. self.top_sectionlevel = 2
  122. self.next_section_ids = set()
  123. self.next_figure_ids = set()
  124. self.next_table_ids = set()
  125. # flags
  126. self.verbatim = None
  127. self.in_title = 0
  128. self.in_production_list = 0
  129. self.in_footnote = 0
  130. self.in_caption = 0
  131. self.first_document = 1
  132. self.this_is_the_title = 1
  133. self.literal_whitespace = 0
  134. self.no_contractions = 0
  135. self.compact_list = 0
  136. self.first_param = 0
  137. self.previous_spanning_row = 0
  138. self.previous_spanning_column = 0
  139. self.remember_multirow = {}
  140. self.remember_multirowcol = {}
  141. def astext(self):
  142. HEADER = self.builder.app.config.clatex_header
  143. if self.builder.config.clatex_highlighter:
  144. return (HEADER % self.elements +
  145. self.highlighter.get_stylesheet() +
  146. u''.join(self.body) +
  147. FOOTER % self.elements)
  148. else:
  149. return (HEADER % self.elements +
  150. u''.join(self.body) +
  151. FOOTER % self.elements)
  152. def visit_environment(self, node):
  153. visit_environment_latex(self, node)
  154. def depart_environment(self, node):
  155. depart_environment_latex(self, node)
  156. def visit_textcolor(self, node):
  157. visit_textcolor_latex(self, node)
  158. def depart_textcolor(self, node):
  159. depart_textcolor_latex(self, node)
  160. def visit_demeter(self, node):
  161. visit_demeter_latex(self, node)
  162. def depart_demeter(self, node):
  163. depart_demeter_latex(self, node)
  164. def visit_endpar(self, node):
  165. visit_endpar_latex(self, node)
  166. def depart_endpar(self, node):
  167. depart_endpar_latex(self, node)
  168. def visit_math(self, node):
  169. latex_visit_math(self, node)
  170. def depart_math(self, node):
  171. pass
  172. def visit_displaymath(self, node):
  173. latex_visit_displaymath(self, node)
  174. def depart_displaymath(self, node):
  175. pass
  176. def visit_eqref(self, node):
  177. latex_visit_eqref(self, node)
  178. def depart_eqref(self, node):
  179. pass
  180. def visit_align(self, node):
  181. visit_align_latex(self, node)
  182. def depart_align(self, node):
  183. depart_align_latex(self, node)
  184. def visit_ifhtml(self, node):
  185. pass
  186. def depart_ifhtml(self, node):
  187. pass
  188. def visit_iflatex(self, node):
  189. pass
  190. def depart_iflatex(self, node):
  191. pass