summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGES3
-rw-r--r--pygments/formatters/html.py23
-rw-r--r--pygments/token.py3
3 files changed, 15 insertions, 14 deletions
diff --git a/CHANGES b/CHANGES
index 2a09025e..07138589 100644
--- a/CHANGES
+++ b/CHANGES
@@ -7,6 +7,8 @@ Version 1.4
-----------
(in development)
+- Performance improvements in the HTML formatter (#523).
+
- With the ``noclasses`` option in the HTML formatter, some styles
present in the stylesheet were not added as inline styles.
@@ -14,6 +16,7 @@ Version 1.4
- More context-sensitive Gherkin lexer with support for more i18n translations.
+
Version 1.3.1
-------------
(bugfix release, released Mar 05, 2010)
diff --git a/pygments/formatters/html.py b/pygments/formatters/html.py
index be68c2bb..0b0e5cbe 100644
--- a/pygments/formatters/html.py
+++ b/pygments/formatters/html.py
@@ -21,14 +21,17 @@ from pygments.util import get_bool_opt, get_int_opt, get_list_opt, bytes
__all__ = ['HtmlFormatter']
-def escape_html(text):
+_escape_html_table = {
+ ord('&'): u'&',
+ ord('<'): u'&lt;',
+ ord('>'): u'&gt;',
+ ord('"'): u'&quot;',
+ ord("'"): u'&#39;',
+}
+
+def escape_html(text, table=_escape_html_table):
"""Escape &, <, > as well as single and double quotes for HTML."""
- return text.replace('&', '&amp;'). \
- replace('<', '&lt;'). \
- replace('>', '&gt;'). \
- replace('"', '&quot;'). \
- replace("'", '&#39;')
-
+ return text.translate(table)
def get_random_id():
"""Return a random id for javascript fields."""
@@ -371,14 +374,11 @@ class HtmlFormatter(Formatter):
except ValueError:
pass
- self._class_cache = {}
self._create_stylesheet()
def _get_css_class(self, ttype):
"""Return the css class of this token type prefixed with
the classprefix option."""
- if ttype in self._class_cache:
- return self._class_cache[ttype]
return self.classprefix + _get_ttype_class(ttype)
def _create_stylesheet(self):
@@ -640,6 +640,7 @@ class HtmlFormatter(Formatter):
# for <span style=""> lookup only
getcls = self.ttype2class.get
c2s = self.class2style
+ escape_table = _escape_html_table
lspan = ''
line = ''
@@ -654,7 +655,7 @@ class HtmlFormatter(Formatter):
cls = self._get_css_class(ttype)
cspan = cls and '<span class="%s">' % cls or ''
- parts = escape_html(value).split('\n')
+ parts = value.translate(escape_table).split('\n')
# for all but the last line
for part in parts[:-1]:
diff --git a/pygments/token.py b/pygments/token.py
index 4815ddfb..1f513923 100644
--- a/pygments/token.py
+++ b/pygments/token.py
@@ -40,9 +40,6 @@ class _TokenType(tuple):
new.parent = self
return new
- def __hash__(self):
- return hash(tuple(self))
-
def __repr__(self):
return 'Token' + (self and '.' or '') + '.'.join(self)