#534: warning message instead of crash if invalid Pygments lexer name is used.

This commit is contained in:
Georg Brandl 2010-10-22 10:02:10 +02:00
parent c768b68340
commit 357f8472c7
6 changed files with 25 additions and 20 deletions

View File

@ -213,6 +213,12 @@ class Sphinx(object):
self.builder.cleanup()
def warn(self, message, location=None, prefix='WARNING: '):
if isinstance(location, tuple):
docname, lineno = location
if docname:
location = '%s:%s' % (self.env.doc2path(docname), lineno or '')
else:
location = None
warntext = location and '%s: %s%s\n' % (location, prefix, message) or \
'%s%s\n' % (prefix, message)
if self.warningiserror:

View File

@ -87,6 +87,8 @@ class StandaloneHTMLBuilder(Builder):
self.tags_hash = ''
# section numbers for headings in the currently visited document
self.secnumbers = {}
# currently written docname
self.current_docname = None
self.init_templates()
self.init_highlighter()
@ -396,6 +398,7 @@ class StandaloneHTMLBuilder(Builder):
self.imgpath = relative_uri(self.get_target_uri(docname), '_images')
self.post_process_images(doctree)
self.dlpath = relative_uri(self.get_target_uri(docname), '_downloads')
self.current_docname = docname
self.docwriter.write(doctree, destination)
self.docwriter.assemble_parts()
body = self.docwriter.parts['fragment']

View File

@ -336,12 +336,8 @@ class BuildEnvironment:
self.settings['warning_stream'] = WarningStream(func)
def warn(self, docname, msg, lineno=None):
if docname:
if lineno is None:
lineno = ''
self._warnfunc(msg, '%s:%s' % (self.doc2path(docname), lineno))
else:
self._warnfunc(msg)
# strange argument order is due to backwards compatibility
self._warnfunc(msg, (docname, lineno))
def clear_doc(self, docname):
"""Remove all traces of a source file in the inventory."""

View File

@ -207,7 +207,7 @@ class PygmentsBridge(object):
lexer = lexers[lang] = get_lexer_by_name(lang)
except ClassNotFound:
if warn:
warn('Pygments lexer name %s is not known' % lang)
warn('Pygments lexer name %r is not known' % lang)
return self.unhighlighted(source)
else:
raise

View File

@ -232,8 +232,10 @@ class HTMLTranslator(BaseTranslator):
lang = node['language']
if node.has_key('linenos'):
linenos = node['linenos']
highlighted = self.highlighter.highlight_block(node.rawsource,
lang, linenos)
def warner(msg):
self.builder.warn(msg, (self.builder.current_docname, node.line))
highlighted = self.highlighter.highlight_block(
node.rawsource, lang, linenos, warn=warner)
starttag = self.starttag(node, 'div', suffix='',
CLASS='highlight-%s' % lang)
self.body.append(starttag + highlighted + '</div>\n')

View File

@ -429,10 +429,8 @@ class LaTeXTranslator(nodes.NodeVisitor):
elif self.this_is_the_title:
if len(node.children) != 1 and not isinstance(node.children[0],
nodes.Text):
self.builder.warn(
'document title is not a single Text node',
'%s:%s' % (self.builder.env.doc2path(self.curfilestack[-1]),
node.line or ''))
self.builder.warn('document title is not a single Text node',
(self.curfilestack[-1], node.line))
if not self.elements['title']:
# text needs to be escaped since it is inserted into
# the output literally
@ -465,8 +463,7 @@ class LaTeXTranslator(nodes.NodeVisitor):
self.builder.warn(
'encountered title node not in section, topic, table, '
'admonition or sidebar',
'%s:%s' % (self.builder.env.doc2path(self.curfilestack[-1]),
node.line or ''))
(self.curfilestack[-1], node.line or ''))
self.body.append('\\textbf{')
self.context.append('}\n')
self.in_title = 1
@ -1107,10 +1104,8 @@ class LaTeXTranslator(nodes.NodeVisitor):
self.body.append('\\grammartoken{')
self.context.append('}')
else:
self.builder.warn(
'unusable reference target found: %s' % uri,
'%s:%s' % (self.builder.env.doc2path(self.curfilestack[-1]),
node.line or ''))
self.builder.warn('unusable reference target found: %s' % uri,
(self.curfilestack[-1], node.line))
self.context.append('')
def depart_reference(self, node):
self.body.append(self.context.pop())
@ -1215,7 +1210,10 @@ class LaTeXTranslator(nodes.NodeVisitor):
lang = node['language']
if node.has_key('linenos'):
linenos = node['linenos']
hlcode = self.highlighter.highlight_block(code, lang, linenos)
def warner(msg):
self.builder.warn(msg, (self.curfilestack[-1], node.line))
hlcode = self.highlighter.highlight_block(code, lang, linenos,
warn=warner)
# workaround for Unicode issue
hlcode = hlcode.replace(u'', u'@texteuro[]')
# must use original Verbatim environment and "tabular" environment