Skip to content

Commit

Permalink
Optimized re_meta().
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfontein committed Jul 1, 2017
1 parent 6717091 commit a03d4ea
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion nikola/utils.py
Expand Up @@ -2030,12 +2030,17 @@ def read_from_config(self, site, basename, posts_per_classification_per_language
signal('{}_translations_config'.format(basename.lower())).send(args)


# Moved to global variable to avoid recompilation
# of regex every time re_meta() is called.
_DEFAULT_REST_METADATA_PARSING = re.compile('^\.\. (.*?): (.*)')


def re_meta(line, match=None):
"""Find metadata using regular expressions."""
if match:
reStr = re.compile('^\.\. {0}: (.*)'.format(re.escape(match)))
else:
reStr = re.compile('^\.\. (.*?): (.*)')
reStr = _DEFAULT_REST_METADATA_PARSING
result = reStr.findall(line.strip())
if match and result:
return (match, result[0])
Expand Down

0 comments on commit a03d4ea

Please sign in to comment.