From e454b3eb629cd182392ea2364e595a2cecbf76fc Mon Sep 17 00:00:00 2001 From: Ian Fijolek Date: Sat, 5 May 2018 22:33:14 +0100 Subject: [PATCH] Some tweaks of regex --- markdone-python/markdone_python/common.py | 2 +- markdone-python/markdone_python/note.py | 1 + .../markdone_python/rendering_extensions.py | 23 +++++++++++-------- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/markdone-python/markdone_python/common.py b/markdone-python/markdone_python/common.py index 5337ce9..551864a 100644 --- a/markdone-python/markdone_python/common.py +++ b/markdone-python/markdone_python/common.py @@ -2,7 +2,7 @@ import re section_pattern = re.compile(r'^#') -tag_pattern = re.compile(r'(\![\w]+\b)') +tag_pattern = re.compile(r'\![\w_-]+\b') task_pattern = re.compile(r'^[\s]*[*]?[\s]*\[[ xX]?\]') diff --git a/markdone-python/markdone_python/note.py b/markdone-python/markdone_python/note.py index cac0bc5..d2834cf 100644 --- a/markdone-python/markdone_python/note.py +++ b/markdone-python/markdone_python/note.py @@ -42,6 +42,7 @@ if __name__ == '__main__': ## Subheader Got some !tags !here +More !tags with !stuff-here,!and, !there * [] task1 !tag * [ ] task2 diff --git a/markdone-python/markdone_python/rendering_extensions.py b/markdone-python/markdone_python/rendering_extensions.py index 16afb31..bf906df 100644 --- a/markdone-python/markdone_python/rendering_extensions.py +++ b/markdone-python/markdone_python/rendering_extensions.py @@ -1,4 +1,7 @@ +import re + from markdown.extensions import Extension +from markdown.inlinepatterns import Pattern from markdown.inlinepatterns import SimpleTagPattern from markdown.inlinepatterns import SimpleTextPattern from markdown.util import etree @@ -6,19 +9,21 @@ from markdown.util import etree from markdone_python.common import tag_pattern -class SimpleTagPattern(Pattern): # pragma: no cover +class TagPattern(Pattern): """ - Return element of type `tag` with a text attribute of group(3) - of a Pattern. + Returns a !tag rendered as [tag] """ - def __init__(self, pattern, tag): - Pattern.__init__(self, pattern) - self.tag = tag + tag = 'em' + def __init__(self): + super().__init__(r'\!([\w_-]+\b)') def handleMatch(self, m): - el = util.etree.Element(self.tag) - el.text = m.group(3) + el = etree.Element(self.tag) + text = m.group(2) + # text = text.replace('!', '') + # el.text = '[{}]'.format(re.sub(r'[, ]+', ', ', text)) + el.text = text return el @@ -26,7 +31,7 @@ class MarkdoneExtension(Extension): def extendMarkdown(self, md, md_globals): md.inlinePatterns.add( 'markdone_tag', - SimpleTagPattern(tag_pattern.pattern, 'em'), + TagPattern(), '_begin', )