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
commit210fa81ad1123aff13511978e773ee463af49f58 (patch)
treea5bc86790793a49239a9ba3171d2c68768c78212
parent0f3b07b81896eeee9fdc558927199fc64e114edc (diff)
parent9835a0a1a3f6bbf6151c4d025f07b2102a910377 (diff)
downloadpygments-210fa81ad1123aff13511978e773ee463af49f58.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))