diff options
author | Georg Brandl <georg@python.org> | 2010-08-18 17:37:49 +0200 |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2010-08-18 17:37:49 +0200 |
commit | e051d8dc8f02e49cefbaaa030c14c9531951a678 (patch) | |
tree | 700197537ed269bca0f0e8cef851c00c82989c51 /pygments/formatters/html.py | |
parent | eb116405a7d23456589371322e52f8430a8cde0d (diff) | |
download | pygments-e051d8dc8f02e49cefbaaa030c14c9531951a678.tar.gz |
Performance improvements in the HTML formatter (#523).
Diffstat (limited to 'pygments/formatters/html.py')
-rw-r--r-- | pygments/formatters/html.py | 23 |
1 files changed, 12 insertions, 11 deletions
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'<', + ord('>'): u'>', + ord('"'): u'"', + ord("'"): u''', +} + +def escape_html(text, table=_escape_html_table): """Escape &, <, > as well as single and double quotes for HTML.""" - return text.replace('&', '&'). \ - replace('<', '<'). \ - replace('>', '>'). \ - replace('"', '"'). \ - replace("'", ''') - + 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]: |