summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2016-01-04 11:17:59 +0100
committerGeorg Brandl <georg@python.org>2016-01-04 11:17:59 +0100
commitf74d0d301d6556c2bdd5341ba063eae9d697d03f (patch)
treef0b20dd20e67704009f9afd18a552a347732ab7b
parentff3a623a6babadace111a4dab1718061000dc6d6 (diff)
parentb0a128e1c5d3c3c3b86fbc78123d292a430f22c3 (diff)
downloadpygments-git-f74d0d301d6556c2bdd5341ba063eae9d697d03f.tar.gz
Merged in atodorov/pygments-main/htmlformatter_with_title (pull request #527)
Add filename parameter to HtmlFormatter
-rw-r--r--pygments/formatters/html.py10
-rw-r--r--tests/test_html_formatter.py8
2 files changed, 18 insertions, 0 deletions
diff --git a/pygments/formatters/html.py b/pygments/formatters/html.py
index b03a4bd5..12f2e83e 100644
--- a/pygments/formatters/html.py
+++ b/pygments/formatters/html.py
@@ -321,6 +321,12 @@ class HtmlFormatter(Formatter):
.. versionadded:: 1.6
+ `filename`
+ A string used to generate a filename when rendering <pre> blocks,
+ for example if displaying source code.
+
+ .. versionadded:: 2.1
+
**Subclassing the HTML formatter**
@@ -388,6 +394,7 @@ class HtmlFormatter(Formatter):
self.noclobber_cssfile = get_bool_opt(options, 'noclobber_cssfile', False)
self.tagsfile = self._decodeifneeded(options.get('tagsfile', ''))
self.tagurlformat = self._decodeifneeded(options.get('tagurlformat', ''))
+ self.filename = self._decodeifneeded(options.get('filename', ''))
if self.tagsfile:
if not ctags:
@@ -692,6 +699,9 @@ class HtmlFormatter(Formatter):
style.append('line-height: 125%')
style = '; '.join(style)
+ if self.filename:
+ yield 0, ('<span class="filename">' + self.filename + '</span>')
+
yield 0, ('<pre' + (style and ' style="%s"' % style) + '>')
for tup in inner:
yield tup
diff --git a/tests/test_html_formatter.py b/tests/test_html_formatter.py
index a82aaaf7..567de51f 100644
--- a/tests/test_html_formatter.py
+++ b/tests/test_html_formatter.py
@@ -192,3 +192,11 @@ class HtmlFormatterTest(unittest.TestCase):
fmt.format(tokensource, outfile)
self.assertTrue('<a href="test_html_formatter.py#L-165">test_ctags</a>'
in outfile.getvalue())
+
+ def test_filename(self):
+ optdict = dict(filename="test.py")
+ outfile = StringIO()
+ fmt = HtmlFormatter(**optdict)
+ fmt.format(tokensource, outfile)
+ html = outfile.getvalue()
+ self.assertTrue(re.search("<span class=\"filename\">test.py</span><pre>", html))