Skip to content

Commit

Permalink
Make smarty extension use its own InlineProcessor
Browse files Browse the repository at this point in the history
  • Loading branch information
mitya57 committed May 26, 2014
1 parent 54527a0 commit 47ec0cb
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions markdown/extensions/smarty.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
from __future__ import unicode_literals
from . import Extension
from ..inlinepatterns import HtmlPattern
from ..odict import OrderedDict
from ..treeprocessors import InlineProcessor
from ..util import parseBoolValue

# Constants for quote education.
Expand Down Expand Up @@ -145,20 +147,20 @@ def _addPatterns(self, md, patterns, serie):
for ind, pattern in enumerate(patterns):
pattern += (md,)
pattern = SubstituteTextPattern(*pattern)
after = ('>smarty-%s-%d' % (serie, ind - 1) if ind else '>entity')
after = ('>smarty-%s-%d' % (serie, ind - 1) if ind else '_begin')
name = 'smarty-%s-%d' % (serie, ind)
md.inlinePatterns.add(name, pattern, after)
self.inlinePatterns.add(name, pattern, after)

def educateDashes(self, md):
emDashesPattern = SubstituteTextPattern(r'(?<!-)---(?!-)', ('&mdash;',), md)
enDashesPattern = SubstituteTextPattern(r'(?<!-)--(?!-)', ('&ndash;',), md)
md.inlinePatterns.add('smarty-em-dashes', emDashesPattern, '>entity')
md.inlinePatterns.add('smarty-en-dashes', enDashesPattern,
self.inlinePatterns.add('smarty-em-dashes', emDashesPattern, '_begin')
self.inlinePatterns.add('smarty-en-dashes', enDashesPattern,
'>smarty-em-dashes')

def educateEllipses(self, md):
ellipsesPattern = SubstituteTextPattern(r'(?<!\.)\.{3}(?!\.)', ('&hellip;',), md)
md.inlinePatterns.add('smarty-ellipses', ellipsesPattern, '>entity')
self.inlinePatterns.add('smarty-ellipses', ellipsesPattern, '_begin')

def educateQuotes(self, md):
patterns = (
Expand All @@ -179,12 +181,16 @@ def educateQuotes(self, md):

def extendMarkdown(self, md, md_globals):
configs = self.getConfigs()
self.inlinePatterns = OrderedDict()
if configs['smart_quotes']:
self.educateQuotes(md)
if configs['smart_dashes']:
self.educateDashes(md)
if configs['smart_ellipses']:
self.educateEllipses(md)
inlineProcessor = InlineProcessor(md)
inlineProcessor.inlinePatterns = self.inlinePatterns
md.treeprocessors.add('smarty', inlineProcessor, '_end')
md.ESCAPED_CHARS.extend(['"', "'"])

def makeExtension(configs=None):
Expand Down

0 comments on commit 47ec0cb

Please sign in to comment.