summaryrefslogtreecommitdiff
path: root/pygments/formatters
diff options
context:
space:
mode:
authorWalter Dörwald <walter@livinglogic.de>2021-12-28 18:12:45 +0100
committerGitHub <noreply@github.com>2021-12-28 18:12:45 +0100
commit34bf90dcfc8fc32fca8b84e4c56b2805fc0dd82f (patch)
tree8a1bfef947a6b4b52434ecadea7b793acbe0f86c /pygments/formatters
parent7dea301f7f3baa2c5a585a90ab8d8c30332ef562 (diff)
downloadpygments-git-34bf90dcfc8fc32fca8b84e4c56b2805fc0dd82f.tar.gz
Add a tooltips option to HtmlFormatter. (#1822)
* Add a tooltips option to HtmlFormmater. * Rename option tooltips to debug_token_types. * Use explicit if block. * Fix check. * Document how to use the new debug_token_types option for the HTML formatter. * Remove output redirection from example call.
Diffstat (limited to 'pygments/formatters')
-rw-r--r--pygments/formatters/html.py19
1 files changed, 17 insertions, 2 deletions
diff --git a/pygments/formatters/html.py b/pygments/formatters/html.py
index f9588f78..ba2df3fa 100644
--- a/pygments/formatters/html.py
+++ b/pygments/formatters/html.py
@@ -349,6 +349,12 @@ class HtmlFormatter(Formatter):
.. versionadded:: 2.4
+ `debug_token_types`
+ Add ``title`` attributes to all token ``<span>`` tags that show the
+ name of the token.
+
+ .. versionadded:: 2.10
+
**Subclassing the HTML formatter**
@@ -419,6 +425,7 @@ class HtmlFormatter(Formatter):
self.filename = self._decodeifneeded(options.get('filename', ''))
self.wrapcode = get_bool_opt(options, 'wrapcode', False)
self.span_element_openers = {}
+ self.debug_token_types = get_bool_opt(options, 'debug_token_types', False)
if self.tagsfile:
if not ctags:
@@ -836,12 +843,20 @@ class HtmlFormatter(Formatter):
try:
cspan = self.span_element_openers[ttype]
except KeyError:
+ title = ' title="%s"' % '.'.join(ttype) if self.debug_token_types else ''
if nocls:
css_style = self._get_css_inline_styles(ttype)
- cspan = css_style and '<span style="%s">' % self.class2style[css_style][0] or ''
+ if css_style:
+ css_style = self.class2style[css_style][0]
+ cspan = '<span style="%s"%s>' % (css_style, title)
+ else:
+ cspan = ''
else:
css_class = self._get_css_classes(ttype)
- cspan = css_class and '<span class="%s">' % css_class or ''
+ if css_class:
+ cspan = '<span class="%s"%s>' % (css_class, title)
+ else:
+ cspan = ''
self.span_element_openers[ttype] = cspan
parts = self._translate_parts(value)