Browse Source

Optimize some regex'ing.

Compile the regexs on plugin load instead of every time a resource
is processed.
main
Elliott Sales de Andrade 13 years ago
committed by Lakshmi Vyasarajan
parent
commit
10f1967ebe
3 changed files with 20 additions and 19 deletions
  1. +9
    -9
      hyde/ext/plugins/css.py
  2. +4
    -4
      hyde/ext/plugins/meta.py
  3. +7
    -6
      hyde/ext/plugins/text.py

+ 9
- 9
hyde/ext/plugins/css.py View File

@@ -17,6 +17,10 @@ class LessCSSPlugin(CLTransformer):


def __init__(self, site): def __init__(self, site):
super(LessCSSPlugin, self).__init__(site) super(LessCSSPlugin, self).__init__(site)
self.import_finder = \
re.compile('^\\s*@import\s+(?:\'|\")([^\'\"]*)(?:\'|\")\s*\;\s*$',
re.MULTILINE)



@property @property
def executable_name(self): def executable_name(self):
@@ -51,10 +55,6 @@ class LessCSSPlugin(CLTransformer):
not self._should_replace_imports(resource): not self._should_replace_imports(resource):
return text return text


import_finder = re.compile(
'^\\s*@import\s+(?:\'|\")([^\'\"]*)(?:\'|\")\s*\;\s*$',
re.MULTILINE)

def import_to_include(match): def import_to_include(match):
if not match.lastindex: if not match.lastindex:
return '' return ''
@@ -68,7 +68,7 @@ class LessCSSPlugin(CLTransformer):
"Cannot import from path [%s]" % afile.path) "Cannot import from path [%s]" % afile.path)
ref.is_processable = False ref.is_processable = False
return self.template.get_include_statement(ref.relative_path) 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 return text




@@ -120,6 +120,9 @@ class StylusPlugin(CLTransformer):


def __init__(self, site): def __init__(self, site):
super(StylusPlugin, self).__init__(site) super(StylusPlugin, self).__init__(site)
self.import_finder = \
re.compile('^\\s*@import\s+(?:\'|\")([^\'\"]*)(?:\'|\")\s*\;?\s*$',
re.MULTILINE)


def begin_site(self): def begin_site(self):
""" """
@@ -138,9 +141,6 @@ class StylusPlugin(CLTransformer):


if not resource.source_file.kind == 'styl': if not resource.source_file.kind == 'styl':
return return
import_finder = re.compile(
'^\\s*@import\s+(?:\'|\")([^\'\"]*)(?:\'|\")\s*\;?\s*$',
re.MULTILINE)


def import_to_include(match): def import_to_include(match):
""" """
@@ -170,7 +170,7 @@ class StylusPlugin(CLTransformer):
"\n" "\n"
return '@import "' + path + '"\n' return '@import "' + path + '"\n'


text = import_finder.sub(import_to_include, text)
text = self.import_finder.sub(import_to_include, text)
return text return text


@property @property


+ 4
- 4
hyde/ext/plugins/meta.py View File

@@ -48,6 +48,9 @@ class MetaPlugin(Plugin):


def __init__(self, site): def __init__(self, site):
super(MetaPlugin, self).__init__(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): def begin_site(self):
""" """
@@ -77,10 +80,7 @@ class MetaPlugin(Plugin):
Once loaded, remove the meta area from the text. Once loaded, remove the meta area from the text.
""" """
self.logger.debug("Trying to load metadata from resource [%s]" % resource) 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: if not match:
self.logger.debug("No metadata found in resource [%s]" % resource) self.logger.debug("No metadata found in resource [%s]" % resource)
data = {} data = {}


+ 7
- 6
hyde/ext/plugins/text.py View File

@@ -178,14 +178,17 @@ class SyntextPlugin(TextyPlugin):
return super(SyntextPlugin, self).text_to_tag(match, start) return super(SyntextPlugin, self).text_to_tag(match, start)




import re

class TextlinksPlugin(Plugin): class TextlinksPlugin(Plugin):
""" """
The plugin class for syntax text replacement. The plugin class for syntax text replacement.
""" """
def __init__(self, site): def __init__(self, site):
super(TextlinksPlugin, self).__init__(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): def begin_text_resource(self, resource, text):
""" """
@@ -198,13 +201,11 @@ class TextlinksPlugin(Plugin):
""" """
if not resource.uses_template: if not resource.uses_template:
return text return text
content_link = re.compile('\[\[([^\]^!][^\]]*)\]\]', re.UNICODE|re.MULTILINE)
media_link = re.compile('\[\[\!\!([^\]]*)\]\]', re.UNICODE|re.MULTILINE)
def replace_content(match): def replace_content(match):
return self.template.get_content_url_statement(match.groups(1)[0]) return self.template.get_content_url_statement(match.groups(1)[0])
def replace_media(match): def replace_media(match):
return self.template.get_media_url_statement(match.groups(1)[0]) 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 return text



Loading…
Cancel
Save