forked from schemaorg/schemaorg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apimarkdown.py
58 lines (46 loc) · 1.83 KB
/
apimarkdown.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import logging
logging.basicConfig(level=logging.INFO) # dev_appserver.py --log_level debug .
log = logging.getLogger(__name__)
import markdown2
from markdown2 import Markdown
import re
import threading
WIKILINKPATTERN = r'\[\[([\w0-9_ -]+)\]\]'
class MarkdownTool():
def __init__ (self):
#from markdown.extensions.wikilinks import WikiLinkExtension
#self._md = markdown2.Markdown(extensions=[WikiLinkExtension(base_url='/', end_url='', html_class='localLink')])
self._md = Markdown()
self.wclass = "localLink"
self.wpre = "/"
self.wpost = ""
self.parselock = threading.Lock()
def setPre(self,pre="/"):
self.wpre = pre
def setPost(self,post=""):
self.wpost = post
def parse(self,source,preservePara=False):
if not source or len(source) == 0:
return ""
source = source.strip()
source = source.replace("\\n","\n")
try:
self.parselock.acquire()
ret = self._md.convert(source)
finally:
self.parselock.release()
if not preservePara:
#Remove wrapping <p> </p>\n that Markdown2 adds by default
if len(ret) > 7 and ret.startswith("<p>") and ret.endswith("</p>\n"):
ret = ret[3:len(ret)-5]
ret = ret.replace("<p>","")
ret = ret.replace("</p>","<br/><br/>")
if ret.endswith("<br/><br/>"):
ret = ret[:len(ret)-10]
return self.parseWiklinks(ret)
def parseWiklinks(self,source):
return re.sub(WIKILINKPATTERN, self.wikilinkReplace, source)
def wikilinkReplace(self,match):
t = match.group(1)
return '<a class="%s" href="%s%s%s">%s</a>' % (self.wclass,self.wpre,t,self.wpost,t)
Markdown = MarkdownTool()