diff options
author | Georg Brandl <georg@python.org> | 2010-10-22 10:02:10 +0200 |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2010-10-22 10:02:10 +0200 |
commit | 0859bada809ada0c232f532ed2d527410f3d32c8 (patch) | |
tree | 5242a9d9994fcbac7df4e09dcf488691048fb3f0 | |
parent | c1fe3491aed7bc7860f3f3b654f4129bae2bc79c (diff) | |
download | sphinx-0859bada809ada0c232f532ed2d527410f3d32c8.tar.gz |
#534: warning message instead of crash if invalid Pygments lexer name is used.
-rw-r--r-- | sphinx/application.py | 6 | ||||
-rw-r--r-- | sphinx/builders/html.py | 3 | ||||
-rw-r--r-- | sphinx/environment.py | 8 | ||||
-rw-r--r-- | sphinx/highlighting.py | 2 | ||||
-rw-r--r-- | sphinx/writers/html.py | 6 | ||||
-rw-r--r-- | sphinx/writers/latex.py | 20 |
6 files changed, 25 insertions, 20 deletions
diff --git a/sphinx/application.py b/sphinx/application.py index 11f887da..134f607a 100644 --- a/sphinx/application.py +++ b/sphinx/application.py @@ -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: diff --git a/sphinx/builders/html.py b/sphinx/builders/html.py index 951f516d..f6cb802c 100644 --- a/sphinx/builders/html.py +++ b/sphinx/builders/html.py @@ -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'] diff --git a/sphinx/environment.py b/sphinx/environment.py index 4919331b..77b43d6e 100644 --- a/sphinx/environment.py +++ b/sphinx/environment.py @@ -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.""" diff --git a/sphinx/highlighting.py b/sphinx/highlighting.py index 0dcbc021..ea91b254 100644 --- a/sphinx/highlighting.py +++ b/sphinx/highlighting.py @@ -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 diff --git a/sphinx/writers/html.py b/sphinx/writers/html.py index f206e479..9e8c9c2b 100644 --- a/sphinx/writers/html.py +++ b/sphinx/writers/html.py @@ -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') diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py index 0c71a79f..ee30b4eb 100644 --- a/sphinx/writers/latex.py +++ b/sphinx/writers/latex.py @@ -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 |