Julian Diaz avatar Julian Diaz committed 302ce2c

Fix some problems with included tikz files

Comments (0)

Files changed (1)

sphinxcontrib/tikz.py

 from sphinx.util.compat import Directive
 
 _Win_ = sys.platform[0:3] == 'win'
-fromFile = False
 
 class TikzExtError(SphinxError):
     category = 'Tikz extension error'
             try:
                 fp = codecs.open(filename, 'r', 'utf-8')
                 try:
-                    node['tikz'] = fp.read()
+                    node['tikz'] = '\n' + fp.read() + '\n'
                 finally:
                     fp.close()
-                    fromFile = True
             except (IOError, OSError):
                 return [self.state.document.reporter.warning(
                     'External Tikz file %r not found or reading '
 
 DOC_BODY = r'''
 \begin{document}
-\begin{tikzpicture}
 %s
-\end{tikzpicture}
 \end{document}
 '''
 
-def render_tikz(self,tikz,libs='',stringsubst=False):
+def render_tikz(self,node,libs='',stringsubst=False):
+    tikz = node['tikz']
     hashkey = tikz.encode('utf-8')
     fname = 'tikz-%s.png' % (sha(hashkey).hexdigest())
     relfn = posixpath.join(self.builder.imgpath, fname)
     latex += self.builder.config.tikz_latex_preamble
     if stringsubst:
         tikz = tikz % {'wd': curdir}
+    if node['include'] == '':
+        tikz = '\\begin{tikzpicture}\n' + tikz + '\n\\end{tikzpicture}'
     latex += DOC_BODY % tikz
     if isinstance(latex, unicode):
         latex = latex.encode('utf-8')
     libs = self.builder.config.tikz_tikzlibraries
     libs = libs.replace(' ', '').replace('\t', '').strip(', ')
     try:
-        fname = render_tikz(self,node['tikz'],libs);
+        fname = render_tikz(self,node,libs);
     except TikzExtError, exc:
         info = str(exc)[str(exc).find('!'):-1]
         sm = nodes.system_message(info, type='WARNING', level=2,
     libs = libs.replace(' ', '').replace('\t', '').strip(', ')
 
     try:
-        fname = render_tikz(self,node['tikz'],libs,node['stringsubst'])
+        fname = render_tikz(self,node,libs,node['stringsubst'])
     except TikzExtError, exc:
         info = str(exc)[str(exc).find('!'):-1]
         sm = nodes.system_message(info, type='WARNING', level=2,
     raise nodes.SkipNode
 
 def latex_visit_tikz(self, node):
-    if fromFile:
+    if node['include'] != '':
         begTikzPic = ''
         endTikzPic = ''
+        node['tikz']=node['tikz'].replace('\r\n','\n')
     else:
         begTikzPic = '\\begin{tikzpicture}'
         endTikzPic = '\\end{tikzpicture}'
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.