import re __author__ = "Erik Smartt " __copyright__ = "Copyright 2003-2005, Erik Smartt" __license__ = """This work is licensed under the Creative Commons Attribution-ShareAlike 2.0 Attribution-ShareAlike 2.0 You are free: * to copy, distribute, display, and perform the work * to make derivative works * to make commercial use of the work Under the following conditions: - Attribution. You must give the original author credit. - Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one. * For any reuse or distribution, you must make clear to others the license terms of this work. * Any of these conditions can be waived if you get permission from the copyright holder. Your fair use and other rights are in no way affected by the above. http://creativecommons.org/licenses/by-sa/2.0/legalcode """ __doc__ = """A plain-text to html conversion utility. There are many like it, but this one is mine. """ ## --------------------- re_freelinkswithanchor = re.compile('(\[\[)(.*?)\#(.*?)(\]\])') re_namedextlinks = re.compile("(\[)(http://.*?)([\|\ ])(.*?)(\])") re_freelinks = re.compile('(\[\[)(.*?)(\]\])') re_anchor = re.compile("(\[anchor::)(.*?)(\])") re_img = re.compile("(\[img\ )(http://)(.*?)(\])") re_extlinks = re.compile("(\ )+(http|https|ftp|mail|gopher)(://.*?)(\ )+") re_bold = re.compile("(''')(.*?)(''')") re_italic = re.compile("('')(.*?)('')") re_h6 = re.compile("(\ ======)(.*?)(======)") re_h5 = re.compile("(\ =====)(.*?)(=====)") re_h4 = re.compile("(\ ====)(.*?)(====)") re_h3 = re.compile("(\ ===)(.*?)(===)") re_h2 = re.compile("(\ ==)(.*?)(==)") re_h1 = re.compile("(\ =)(.*?)(=)") re_hr = re.compile("----") #re_newline = re.compile(r'\n', re.IGNORECASE) #re_preOpen = re.compile("\[PRE\]") #re_preClose = re.compile("\[\/PRE\]") #re_codeOpen = re.compile("\[CODE\]") #re_codeClose = re.compile("\[\/CODE\]") BASE_URL = "" ## --------------------- def cb_preformat(args): """ Implements a callback mechanism for PyBlosxom. @param args: a dict with 'parser' string and a list 'story' @type args: dict """ if args['parser'] == 'wiki2html': config = args['request'].getConfiguration() return parse(''.join(args['story'])) # -- def _format_freelink(mo): global BASE_URL key = mo.group(2).strip() repkey = key.replace(" ", "_") return "%s" % (BASE_URL, repkey, key) # -- def _format_freelinkwithanchor(mo): global BASE_URL key = mo.group(2).strip() repkey = key.replace(" ", "_") anchor = mo.group(3).strip() return "%s" % (BASE_URL, repkey, anchor, key) # -- def parse(s, baseurl=None): result = s # Step 1, change < >'s to encoded chars. `cgi.escape()` should do the same thing result = result.replace('<', '<') result = result.replace('>', '>') result = result.replace(' & ', ' & ') # For an example of other wiki markup, take a look at: # http://ciaweb.net/free/textwiki.php?page=SamplePage # and: # http://www.usemod.com/cgi-bin/mb.pl?WikiMarkupStandard result = result.replace("[CODE]", "") result = result.replace("[/CODE]", "") result = result.replace("[PRE]", "
")
	result = result.replace("[/PRE]", "
") #result = re_preClose.sub("", result) #result = re_preClose.sub("", result) # Handle code #result = re_codeOpen.sub("", result) #result = re_codeClose.sub("", result) # Handle free-links with anchors result = re_freelinkswithanchor.sub(_format_freelinkwithanchor, result) # Handle named external-links result = re_namedextlinks.sub(lambda mo:"%s" % (mo.group(2), mo.group(4)), result) # Handle free-links result = re_freelinks.sub(_format_freelink, result) # Handle named anchors result = re_anchor.sub(lambda mo:"" % (mo.group(2)), result) # Handle linked media result = re_img.sub(lambda mo:"" % (mo.group(2), mo.group(3)), result) # Handle external-links result = re_extlinks.sub(lambda mo:" <%s%s> " % (mo.group(2), mo.group(3), mo.group(2), mo.group(3)), result) # Handle bold (emphasis) result = re_bold.sub(lambda mo:"%s" % (mo.group(2)), result) # Handle italics (emphasis) result = re_italic.sub(lambda mo:"%s" % (mo.group(2)), result) # Handle h6 result = re_h6.sub(lambda mo:"%s" % (mo.group(2)), result) # Handle h5 result = re_h5.sub(lambda mo:"%s" % (mo.group(2)), result) # Handle h4 result = re_h4.sub(lambda mo:"%s" % (mo.group(2)), result) # Handle h3 result = re_h3.sub(lambda mo:"%s" % (mo.group(2)), result) # Handle h2 result = re_h2.sub(lambda mo:"%s" % (mo.group(2)), result) # Handle h1 result = re_h1.sub(lambda mo:"%s" % (mo.group(2)), result) # Handle hr result = re_hr.sub("
", result) # Change line breaks: replace '\n' with
's #result = re_newline.sub('
', result) result = result.replace('\n', '
') # Now that we've done all the regex stuff, go through line-by-line # for additional tweaks. #lines = result.split() #for line in lines: #pass return result ## --------------------- if __name__ == "__main__": import unittest ## --------------------- class ModTest(unittest.TestCase): def testAnchor(self): self.assertEqual(parse("[anchor::foo]"), "") def testBold(self): self.assertEqual(parse("'''bold'''"), "bold") def testCodeOpen(self): self.assertEqual(parse("[PRE]"), "
")
	
		def testCodeClose(self):
			self.assertEqual(parse("[/PRE]"), "
") def testExternalLink(self): self.assertEqual(parse(" http://foo.com/ "), " <http://foo.com/> ") def testFreeLink(self): self.assertEqual(parse("[[foo]]"), "foo") self.assertEqual(parse("[[foo bar]]"), "foo bar") def testFreeLinkwithAnchor(self): self.assertEqual(parse("[[foo#manchoo]]"), "foo") self.assertEqual(parse("[[foo bar#manchoo]]"), "foo bar") def testH1(self): self.assertEqual(parse(" =header="), "header") def testH2(self): self.assertEqual(parse(" ==header=="), "header") def testH3(self): self.assertEqual(parse(" ===header==="), "header") def testH4(self): self.assertEqual(parse(" ====header===="), "header") def testH5(self): self.assertEqual(parse(" =====header====="), "header") def testH6(self): self.assertEqual(parse(" ======header======"), "header") def testHR(self): self.assertEqual(parse("----"), "
") def testItalic(self): self.assertEqual(parse("''italics''"), "italics") def testLT(self): self.assertEqual(parse("<"), "<") def testNamedLink(self): self.assertEqual(parse("[http://foo.com|foo]"), "foo") self.assertEqual(parse("[http://foo.com foo]"), "foo") def testNewline(self): self.assertEqual(parse("\n"), "
") def testRT(self): self.assertEqual(parse(">"), ">") def testAmp(self): self.assertEqual(parse("one & two © "), "one & two © ") unittest.main()