I want to write a simple markdown parser function that will take in a single line of markdown and be translated into the appropriate HTML. To keep it simple, I want to support only one feature of markdown in atx syntax: headers.
Headers are designated by (1-6) hashes followed by a space, followed by text. The number of hashes determines the header level of the HTML output. Examples
# Header will become <h1>Header</h1>
## Header will become <h2>Header</h2>
###### Header will become <h6>Header</h6>
Rules are as follows
Header content should only come after the initial hashtag(s) plus a space character.
Invalid headers should just be returned as the markdown that was recieved, no translation necessary.
Spaces before and after both the header content and the hashtag(s) should be ignored in the resulting output.
This is code I made.
import re
def markdown_parser(markdown):
results =''
pattern = re.compile("#+\s")
matches = pattern.search(markdown.strip())
if (matches != None):
tag = matches[0]
hashTagLen = len(tag) - 1
htmlTag = "h" + str(hashTagLen)
content = markdown.strip()[(hashTagLen + 1):]
results = "<" + htmlTag + ">" + content + "</" + htmlTag + ">"
else:
results = markdown
return results
When I run this code, exception has occurred as follows.
Unhandled Exception: '_sre.SRE_Match' object is not subscriptable
I'm not sure why this error has occurred.
When I run the script on my shell, it works well. But When I run it on unittest environment (import unittest), the error has occurred.
Please help me.
markdownmodule and have it do all the dirty work for you?matchesis not the items matched, but a match object, usematches.groupto interact with them, cf the docs :m = re.search('(?<=abc)def', 'abcdef'); m.group(0). thus in your case:matches[0]-->matches.group(0)