mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Implement image handling for Texinfo builder
This commit is contained in:
@@ -21,7 +21,7 @@ from sphinx.locale import _
|
|||||||
from sphinx.builders import Builder
|
from sphinx.builders import Builder
|
||||||
from sphinx.environment import NoUri
|
from sphinx.environment import NoUri
|
||||||
from sphinx.util.nodes import inline_all_toctrees
|
from sphinx.util.nodes import inline_all_toctrees
|
||||||
from sphinx.util.osutil import SEP
|
from sphinx.util.osutil import SEP, copyfile
|
||||||
from sphinx.util.console import bold, darkgreen
|
from sphinx.util.console import bold, darkgreen
|
||||||
from sphinx.writers.texinfo import TexinfoWriter
|
from sphinx.writers.texinfo import TexinfoWriter
|
||||||
|
|
||||||
@@ -86,7 +86,8 @@ class TexinfoBuilder(Builder):
|
|||||||
"""
|
"""
|
||||||
name = 'texinfo'
|
name = 'texinfo'
|
||||||
format = 'texinfo'
|
format = 'texinfo'
|
||||||
supported_image_types = []
|
supported_image_types = ['application/pdf', 'image/png',
|
||||||
|
'image/gif', 'image/jpeg']
|
||||||
|
|
||||||
def init(self):
|
def init(self):
|
||||||
self.docnames = []
|
self.docnames = []
|
||||||
@@ -154,6 +155,7 @@ class TexinfoBuilder(Builder):
|
|||||||
nodes.Text('@printindex ge\n',
|
nodes.Text('@printindex ge\n',
|
||||||
'@printindex ge\n'),
|
'@printindex ge\n'),
|
||||||
format="texinfo")))
|
format="texinfo")))
|
||||||
|
self.post_process_images(doctree)
|
||||||
docwriter = TexinfoWriter(self)
|
docwriter = TexinfoWriter(self)
|
||||||
settings = OptionParser(
|
settings = OptionParser(
|
||||||
defaults=self.env.settings,
|
defaults=self.env.settings,
|
||||||
@@ -213,6 +215,15 @@ class TexinfoBuilder(Builder):
|
|||||||
return largetree
|
return largetree
|
||||||
|
|
||||||
def finish(self):
|
def finish(self):
|
||||||
|
# copy image files
|
||||||
|
if self.images:
|
||||||
|
self.info(bold('copying images...'), nonl=1)
|
||||||
|
for src, dest in self.images.iteritems():
|
||||||
|
self.info(' '+src, nonl=1)
|
||||||
|
copyfile(path.join(self.srcdir, src),
|
||||||
|
path.join(self.outdir, dest))
|
||||||
|
self.info()
|
||||||
|
|
||||||
self.info(bold('copying Texinfo support files... '), nonl=True)
|
self.info(bold('copying Texinfo support files... '), nonl=True)
|
||||||
# copy Makefile
|
# copy Makefile
|
||||||
fn = path.join(self.outdir, 'Makefile')
|
fn = path.join(self.outdir, 'Makefile')
|
||||||
|
|||||||
@@ -9,6 +9,9 @@
|
|||||||
:license: BSD, see LICENSE for details.
|
:license: BSD, see LICENSE for details.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import re
|
||||||
|
from os import path
|
||||||
|
|
||||||
from docutils import nodes, writers
|
from docutils import nodes, writers
|
||||||
|
|
||||||
from sphinx import addnodes
|
from sphinx import addnodes
|
||||||
@@ -27,6 +30,7 @@ Generated by Sphinx
|
|||||||
@defindex ge
|
@defindex ge
|
||||||
@paragraphindent %(paragraphindent)s
|
@paragraphindent %(paragraphindent)s
|
||||||
@exampleindent %(exampleindent)s
|
@exampleindent %(exampleindent)s
|
||||||
|
@afourlatex
|
||||||
%(direntry)s
|
%(direntry)s
|
||||||
@c %%**end of header
|
@c %%**end of header
|
||||||
|
|
||||||
@@ -96,7 +100,6 @@ def escape_id(s):
|
|||||||
s = ' '.join(s.split()).strip()
|
s = ' '.join(s.split()).strip()
|
||||||
return s
|
return s
|
||||||
|
|
||||||
|
|
||||||
class TexinfoWriter(writers.Writer):
|
class TexinfoWriter(writers.Writer):
|
||||||
"""Texinfo writer for generating Texinfo documents."""
|
"""Texinfo writer for generating Texinfo documents."""
|
||||||
supported = ('texinfo', 'texi')
|
supported = ('texinfo', 'texi')
|
||||||
@@ -147,6 +150,8 @@ class TexinfoWriter(writers.Writer):
|
|||||||
|
|
||||||
class TexinfoTranslator(nodes.NodeVisitor):
|
class TexinfoTranslator(nodes.NodeVisitor):
|
||||||
|
|
||||||
|
ignore_missing_images = False
|
||||||
|
|
||||||
default_elements = {
|
default_elements = {
|
||||||
'filename': '',
|
'filename': '',
|
||||||
'title': '',
|
'title': '',
|
||||||
@@ -359,6 +364,21 @@ class TexinfoTranslator(nodes.NodeVisitor):
|
|||||||
self.add_text('\n@end menu\n\n')
|
self.add_text('\n@end menu\n\n')
|
||||||
|
|
||||||
|
|
||||||
|
def tex_image_length(self, width_str):
|
||||||
|
match = re.match('(\d*\.?\d*)\s*(\S*)', width_str)
|
||||||
|
if not match:
|
||||||
|
# fallback
|
||||||
|
return width_str
|
||||||
|
res = width_str
|
||||||
|
amount, unit = match.groups()[:2]
|
||||||
|
if not unit or unit == "px":
|
||||||
|
# pixels: let TeX alone
|
||||||
|
return ''
|
||||||
|
elif unit == "%":
|
||||||
|
# a4paper: textwidth=418.25368pt
|
||||||
|
res = "%d.0pt" % (float(amount) * 4.1825368)
|
||||||
|
return res
|
||||||
|
|
||||||
## xref handling
|
## xref handling
|
||||||
|
|
||||||
def get_short_id(self, id):
|
def get_short_id(self, id):
|
||||||
@@ -928,8 +948,24 @@ class TexinfoTranslator(nodes.NodeVisitor):
|
|||||||
self.add_text('}\n')
|
self.add_text('}\n')
|
||||||
|
|
||||||
def visit_image(self, node):
|
def visit_image(self, node):
|
||||||
self.add_text('@w{[image]}')
|
if node['uri'] in self.builder.images:
|
||||||
raise nodes.SkipNode
|
uri = self.builder.images[node['uri']]
|
||||||
|
else:
|
||||||
|
# missing image!
|
||||||
|
if self.ignore_missing_images:
|
||||||
|
return
|
||||||
|
uri = node['uri']
|
||||||
|
if uri.find('://') != -1:
|
||||||
|
# ignore remote images
|
||||||
|
return
|
||||||
|
name, ext = path.splitext(uri)
|
||||||
|
attrs = node.attributes
|
||||||
|
# ignored in non-tex output
|
||||||
|
width = self.tex_image_length(attrs.get('width', ''))
|
||||||
|
height = self.tex_image_length(attrs.get('height', ''))
|
||||||
|
alt = escape_arg(attrs.get('alt', ''))
|
||||||
|
self.add_text('\n\n@image{%s,%s,%s,%s,%s}\n\n' %
|
||||||
|
(name, width, height, alt, ext[1:]))
|
||||||
def depart_image(self, node):
|
def depart_image(self, node):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,8 @@ import sys
|
|||||||
from StringIO import StringIO
|
from StringIO import StringIO
|
||||||
from subprocess import Popen, PIPE
|
from subprocess import Popen, PIPE
|
||||||
|
|
||||||
|
from sphinx.writers.texinfo import TexinfoTranslator
|
||||||
|
|
||||||
from util import *
|
from util import *
|
||||||
from test_build_html import ENV_WARNINGS
|
from test_build_html import ENV_WARNINGS
|
||||||
|
|
||||||
@@ -25,7 +27,9 @@ def teardown_module():
|
|||||||
|
|
||||||
texinfo_warnfile = StringIO()
|
texinfo_warnfile = StringIO()
|
||||||
|
|
||||||
TEXINFO_WARNINGS = ENV_WARNINGS
|
TEXINFO_WARNINGS = ENV_WARNINGS + """\
|
||||||
|
None:None: WARNING: no matching candidate for image URI u'foo.\\*'
|
||||||
|
"""
|
||||||
|
|
||||||
if sys.version_info >= (3, 0):
|
if sys.version_info >= (3, 0):
|
||||||
TEXINFO_WARNINGS = remove_unicode_literals(TEXINFO_WARNINGS)
|
TEXINFO_WARNINGS = remove_unicode_literals(TEXINFO_WARNINGS)
|
||||||
@@ -33,6 +37,7 @@ if sys.version_info >= (3, 0):
|
|||||||
|
|
||||||
@with_app(buildername='texinfo', warning=texinfo_warnfile, cleanenv=True)
|
@with_app(buildername='texinfo', warning=texinfo_warnfile, cleanenv=True)
|
||||||
def test_texinfo(app):
|
def test_texinfo(app):
|
||||||
|
TexinfoTranslator.ignore_missing_images = True
|
||||||
app.builder.build_all()
|
app.builder.build_all()
|
||||||
texinfo_warnings = texinfo_warnfile.getvalue().replace(os.sep, '/')
|
texinfo_warnings = texinfo_warnfile.getvalue().replace(os.sep, '/')
|
||||||
texinfo_warnings_exp = TEXINFO_WARNINGS % {'root': app.srcdir}
|
texinfo_warnings_exp = TEXINFO_WARNINGS % {'root': app.srcdir}
|
||||||
|
|||||||
Reference in New Issue
Block a user