diff --git a/sphinx/application.py b/sphinx/application.py index 11f887da9..134f607ac 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 951f516d6..f6cb802cc 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 4919331b2..77b43d6ef 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 0dcbc0219..ea91b2549 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 f206e479a..9e8c9c2bf 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 + '\n') diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py index 0c71a79fd..ee30b4ebc 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