Skip to content

Instantly share code, notes, and snippets.

@sbp
Created September 1, 2011 15:19
Show Gist options
  • Save sbp/1186399 to your computer and use it in GitHub Desktop.
Save sbp/1186399 to your computer and use it in GitHub Desktop.
Emphasis style hypertext, lightweight format script
import re
p_emphasis = ur'(?<!\\)\*(.+?)\*'
p_paragraph = ur'\A(?=\S)|(?<=\A\n)(?=\S)|(?<=\n\n)(?=\S)'
p_link = ur'(?s)(?<!\\)\*([^*]+)\*(\W?)(?:[^\S\n]+|\n(?!\n))+(\S*[./]\S+)'
r_link = re.compile(p_link)
r_emphasis = re.compile(p_emphasis)
r_paragraph = re.compile(p_paragraph)
def s_link(match):
text, punctuation, link = match.groups()
if not ':' in link: link = 'http://' + link
return '<a href="%s">%s</a>%s' % (link, text, punctuation)
def hypertext(text):
text = text.replace('<', '&lt;')
text = r_link.sub(s_link, text)
text = r_emphasis.sub(r'<strong>\g<1></strong>', text)
text = r_paragraph.sub('<p>', text)
text = text.replace(' \\n\n', '<br>\n')
return text.replace('\\*', '*')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment