import re def t_SUB(scanner, token): return "SUB", "<sub>"+token[1:]+"</sub>" def t_MD(scanner, token): return "MD", token def t_EVAL(scanner, token): return "EVAL", token[:-1]+str(eval(token[:-2])) def t_MATH(scanner, token): return "MATH", token[1:] def scan1(text): scanner1 = re.Scanner([ (r">[^\n]+", t_MATH), (r"[^>]+", t_MD), (r"\^[^ ]+", t_SUB), (r"\s+", None), ]) parse1 = "" tokens, remainder = scanner1.scan(text) for token in tokens: if token[0]=="MATH": parse1+=scan2(token[1])+"" else: parse1+=token[1]+"" return parse1 def scan2(text): scanner2 = re.Scanner([ (r"\^[^ ]+", t_SUB), #(r'[^=]+==',t_EVAL) ]) parse2 = "" tokens, remainder = scanner2.scan(text) for token in tokens: parse2 +=">"+token[1]+" " return parse2 md="""Math in markdown ================ >3**7== Then we multiply by 4 because there is 4 legs, >3*4==""" print scan1(md)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question