Some tweaks of regex

This commit is contained in:
IamTheFij 2018-05-05 22:33:14 +01:00
parent e808570f76
commit e454b3eb62
3 changed files with 16 additions and 10 deletions

View File

@ -2,7 +2,7 @@ import re
section_pattern = re.compile(r'^#') 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]?\]') task_pattern = re.compile(r'^[\s]*[*]?[\s]*\[[ xX]?\]')

View File

@ -42,6 +42,7 @@ if __name__ == '__main__':
## Subheader ## Subheader
Got some !tags !here Got some !tags !here
More !tags with !stuff-here,!and, !there
* [] task1 !tag * [] task1 !tag
* [ ] task2 * [ ] task2

View File

@ -1,4 +1,7 @@
import re
from markdown.extensions import Extension from markdown.extensions import Extension
from markdown.inlinepatterns import Pattern
from markdown.inlinepatterns import SimpleTagPattern from markdown.inlinepatterns import SimpleTagPattern
from markdown.inlinepatterns import SimpleTextPattern from markdown.inlinepatterns import SimpleTextPattern
from markdown.util import etree from markdown.util import etree
@ -6,19 +9,21 @@ from markdown.util import etree
from markdone_python.common import tag_pattern 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) Returns a !tag rendered as <em>[tag]</em>
of a Pattern.
""" """
def __init__(self, pattern, tag): tag = 'em'
Pattern.__init__(self, pattern) def __init__(self):
self.tag = tag super().__init__(r'\!([\w_-]+\b)')
def handleMatch(self, m): def handleMatch(self, m):
el = util.etree.Element(self.tag) el = etree.Element(self.tag)
el.text = m.group(3) text = m.group(2)
# text = text.replace('!', '')
# el.text = '[{}]'.format(re.sub(r'[, ]+', ', ', text))
el.text = text
return el return el
@ -26,7 +31,7 @@ class MarkdoneExtension(Extension):
def extendMarkdown(self, md, md_globals): def extendMarkdown(self, md, md_globals):
md.inlinePatterns.add( md.inlinePatterns.add(
'markdone_tag', 'markdone_tag',
SimpleTagPattern(tag_pattern.pattern, 'em'), TagPattern(),
'_begin', '_begin',
) )