#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() self.builder.cleanup()
def warn(self, message, location=None, prefix='WARNING: '): 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 \ warntext = location and '%s: %s%s\n' % (location, prefix, message) or \
'%s%s\n' % (prefix, message) '%s%s\n' % (prefix, message)
if self.warningiserror: if self.warningiserror:

View File

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

View File

@ -336,12 +336,8 @@ class BuildEnvironment:
self.settings['warning_stream'] = WarningStream(func) self.settings['warning_stream'] = WarningStream(func)
def warn(self, docname, msg, lineno=None): def warn(self, docname, msg, lineno=None):
if docname: # strange argument order is due to backwards compatibility
if lineno is None: self._warnfunc(msg, (docname, lineno))
lineno = ''
self._warnfunc(msg, '%s:%s' % (self.doc2path(docname), lineno))
else:
self._warnfunc(msg)
def clear_doc(self, docname): def clear_doc(self, docname):
"""Remove all traces of a source file in the inventory.""" """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) lexer = lexers[lang] = get_lexer_by_name(lang)
except ClassNotFound: except ClassNotFound:
if warn: if warn:
warn('Pygments lexer name %s is not known' % lang) warn('Pygments lexer name %r is not known' % lang)
return self.unhighlighted(source) return self.unhighlighted(source)
else: else:
raise raise

View File

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

View File

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