From 10f1967ebe9cf0fc273297025d6ca9c35b87c5a2 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Sat, 24 Mar 2012 21:12:48 -0400 Subject: [PATCH] Optimize some regex'ing. Compile the regexs on plugin load instead of every time a resource is processed. --- hyde/ext/plugins/css.py | 18 +++++++++--------- hyde/ext/plugins/meta.py | 8 ++++---- hyde/ext/plugins/text.py | 13 +++++++------ 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/hyde/ext/plugins/css.py b/hyde/ext/plugins/css.py index 562700c..7f5db10 100644 --- a/hyde/ext/plugins/css.py +++ b/hyde/ext/plugins/css.py @@ -17,6 +17,10 @@ class LessCSSPlugin(CLTransformer): def __init__(self, site): super(LessCSSPlugin, self).__init__(site) + self.import_finder = \ + re.compile('^\\s*@import\s+(?:\'|\")([^\'\"]*)(?:\'|\")\s*\;\s*$', + re.MULTILINE) + @property def executable_name(self): @@ -51,10 +55,6 @@ class LessCSSPlugin(CLTransformer): not self._should_replace_imports(resource): return text - import_finder = re.compile( - '^\\s*@import\s+(?:\'|\")([^\'\"]*)(?:\'|\")\s*\;\s*$', - re.MULTILINE) - def import_to_include(match): if not match.lastindex: return '' @@ -68,7 +68,7 @@ class LessCSSPlugin(CLTransformer): "Cannot import from path [%s]" % afile.path) ref.is_processable = False return self.template.get_include_statement(ref.relative_path) - text = import_finder.sub(import_to_include, text) + text = self.import_finder.sub(import_to_include, text) return text @@ -120,6 +120,9 @@ class StylusPlugin(CLTransformer): def __init__(self, site): super(StylusPlugin, self).__init__(site) + self.import_finder = \ + re.compile('^\\s*@import\s+(?:\'|\")([^\'\"]*)(?:\'|\")\s*\;?\s*$', + re.MULTILINE) def begin_site(self): """ @@ -138,9 +141,6 @@ class StylusPlugin(CLTransformer): if not resource.source_file.kind == 'styl': return - import_finder = re.compile( - '^\\s*@import\s+(?:\'|\")([^\'\"]*)(?:\'|\")\s*\;?\s*$', - re.MULTILINE) def import_to_include(match): """ @@ -170,7 +170,7 @@ class StylusPlugin(CLTransformer): "\n" return '@import "' + path + '"\n' - text = import_finder.sub(import_to_include, text) + text = self.import_finder.sub(import_to_include, text) return text @property diff --git a/hyde/ext/plugins/meta.py b/hyde/ext/plugins/meta.py index 9a7afb2..29923bf 100644 --- a/hyde/ext/plugins/meta.py +++ b/hyde/ext/plugins/meta.py @@ -48,6 +48,9 @@ class MetaPlugin(Plugin): def __init__(self, site): super(MetaPlugin, self).__init__(site) + self.yaml_finder = re.compile( + r"^\s*(?:---|===)\s*\n((?:.|\n)+?)\n\s*(?:---|===)\s*\n*", + re.MULTILINE) def begin_site(self): """ @@ -77,10 +80,7 @@ class MetaPlugin(Plugin): Once loaded, remove the meta area from the text. """ self.logger.debug("Trying to load metadata from resource [%s]" % resource) - yaml_finder = re.compile( - r"^\s*(?:---|===)\s*\n((?:.|\n)+?)\n\s*(?:---|===)\s*\n*", - re.MULTILINE) - match = re.match(yaml_finder, text) + match = re.match(self.yaml_finder, text) if not match: self.logger.debug("No metadata found in resource [%s]" % resource) data = {} diff --git a/hyde/ext/plugins/text.py b/hyde/ext/plugins/text.py index f5970a9..a61123e 100644 --- a/hyde/ext/plugins/text.py +++ b/hyde/ext/plugins/text.py @@ -178,14 +178,17 @@ class SyntextPlugin(TextyPlugin): return super(SyntextPlugin, self).text_to_tag(match, start) -import re - class TextlinksPlugin(Plugin): """ The plugin class for syntax text replacement. """ def __init__(self, site): super(TextlinksPlugin, self).__init__(site) + import re + self.content_link = re.compile('\[\[([^\]^!][^\]]*)\]\]', + re.UNICODE|re.MULTILINE) + self.media_link = re.compile('\[\[\!\!([^\]]*)\]\]', + re.UNICODE|re.MULTILINE) def begin_text_resource(self, resource, text): """ @@ -198,13 +201,11 @@ class TextlinksPlugin(Plugin): """ if not resource.uses_template: return text - content_link = re.compile('\[\[([^\]^!][^\]]*)\]\]', re.UNICODE|re.MULTILINE) - media_link = re.compile('\[\[\!\!([^\]]*)\]\]', re.UNICODE|re.MULTILINE) def replace_content(match): return self.template.get_content_url_statement(match.groups(1)[0]) def replace_media(match): return self.template.get_media_url_statement(match.groups(1)[0]) - text = content_link.sub(replace_content, text) - text = media_link.sub(replace_media, text) + text = self.content_link.sub(replace_content, text) + text = self.media_link.sub(replace_media, text) return text