Commits

Anonymous committed 1d143ac

savestate

  • Participants
  • Parent commits e58958a

Comments (0)

Files changed (2)

File simple_markup01.py

-# -*- coding: utf-8 -*-
-
-import re
-import tidy
-
-text = """
-ich bin __lustiger blabla__ text__ der=-- zum --testen gut is.
-ich hab **auch //mehere** zeilen//.
-
-ich kann auch /einfache/ _formats_ auf einzelne wörter machen
-aber/ich/kann/nicht/sowas/machen
-
-quote:
-blub die bla
-
-quote:"blub"
-blargh
-:quote
-
-code:"c++"
-int a = 1;
-int b = 2;
-if a > b:
-<script>
-:code
-
-code:"java"
-blargh
-
-quote:"jo dude"
-ich war ärster
-und zweiter auch
-:quote
-
-das is //auch// zum testen gut.
-<script type="javascript"></script>
-"""
-
-def simple_markup(text):
-	def clean(text):
-		text = text.replace('<', '&lt;')
-		text = text.replace('>', '&gt;')
-		text = text.replace('\n', '<br />')
-		return text
-		
-	def simple_clean(string):
-		string = string.replace('<', '&lt;')
-		string = string.replace('>', '&gt;')
-		return string
-	
-	def split_blocks(text):
-		blocks = text.split('\n\n')
-		return blocks
-	
-	def render_db(string):
-		db_re = re.compile(r'\*\*(?P<content>.*?)\*\*')
-		string = db_re.sub('<b>\g<content></b>', string)
-		return string
-	
-	def render_di(string):
-		di_re = re.compile(r'//(?P<content>.*?)//')
-		string = di_re.sub('<i>\g<content></i>', string)
-		return string
-	
-	def render_ds(string):
-		ds_re = re.compile(r'--(?P<content>.*?)--')
-		string = ds_re.sub('<s>\g<content></s>', string)
-		return string
-
-	def render_du(string):
-		du_re = re.compile(r'__(?P<content>.*?)__')
-		string = du_re.sub('<u>\g<content></u>', string)
-		return string
-
-	def render_b(string):
-		b_re = re.compile(r' \*(?P<content>[\w\d]+?)\* ')
-		string = b_re.sub(' <b>\g<content></b> ', string)
-		return string
-
-	def render_i(string):
-		i_re = re.compile(r' /(?P<content>[\w\d]+?)/ ')
-		string = i_re.sub(' <i>\g<content></i> ', string)
-		return string
-
-	def render_s(string):
-		s_re = re.compile(r' -(?P<content>[\w\d]+?)- ')
-		string = s_re.sub(' <s>\g<content></s> ', string)
-		return string
-
-	def render_u(string):
-		u_re = re.compile(r' _(?P<content>[\w\d]+?)_ ')
-		string = u_re.sub(' <u>\g<content></u> ', string)
-		return string
-	
-	def render_block(string):
-		code_re = re.compile(r'(?P<type>code|quote):("(?P<meta>.*?)")?\n(?P<content>.*?)\n:\1', re.DOTALL | re.MULTILINE)
-		match = code_re.match(string)
-
-		if match:
-			if match.group('type') == 'quote':
-				if match.group('meta') and match.group('content'):
-					string = '<div class="quote-header">' + clean(match.group('meta')) + ':</div><blockquote><p>' + clean(match.group('content')) + '</p></blockquote>'
-
-				elif match.groups('content'):
-					string = '<blockquote><p>' + clean(match.group('content')) + '</p></blockquote>'
-		
-			else:
-				if match.group('meta') and match.group('content'):
-					string = '<div>' + clean(match.group('meta')) + ':</div><pre>' + simple_clean(match.group('content')) + '</pre>'
-
-				elif match.groups('content'):
-					string = '<pre>' + simple_clean(match.group('content')) + '</pre>'
-
-		else:
-			string = '<p>' + clean(string) + '</p>'
-
-		return string
-	
-	blocks = split_blocks(text)
-	output = ''
-
-	# print blocks
-	for block in blocks:
-		if block[:6] == 'quote:' or block[:5] == 'code:':
-			block = render_block(block)
-			output += block
-		else:
-			block = clean(block.strip())
-			block = render_db(block)
-			block = render_di(block)
-			block = render_ds(block)
-			block = render_du(block)
-			block = render_b(block)
-			block = render_i(block)
-			block = render_s(block)
-			block = render_u(block)
-			output += '<p>' + block + '</p>'
-
-	# return output
-	output = str(tidy.parseString(output, char_encoding='raw', indent=True, output_xhtml=True, show_body_only=True))
-	return output
-		
-	
-# print simple_markup(text)
-# simple_markup(text)
+# -*- coding: utf-8 -*-
+from simple_markup import simple_markup
+import unittest
+
+# blocksplitting
+
+bs_s1 = u"""
+Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
+Sed sed nisi.
+"""
+bs_r1 = u"""<p>
+  Lorem ipsum dolor sit amet, consectetuer adipiscing elit.<br />
+  Sed sed nisi.
+</p>
+"""
+
+bs_s2 = u"""Fusce lobortis dui nec metus.
+Nam vitae purus vel tortor pharetra posuere."""
+bs_r2 = u"""<p>
+  Fusce lobortis dui nec metus.<br />
+  Nam vitae purus vel tortor pharetra posuere.
+</p>
+"""
+
+bs_s3 = u"""
+Fusce lobortis dui nec metus.
+
+Nam vitae purus vel tortor pharetra posuere.
+"""
+bs_r3 = u"""<p>
+  Fusce lobortis dui nec metus.
+</p>
+<p>
+  Nam vitae purus vel tortor pharetra posuere.
+</p>
+"""
+
+# blockquote
+
+# code
+c_s1 = u"""
+code:c
+def test_blockspliting(self):
+	pairs = (
+		(bs_s1, bs_r1),
+		(bs_s2, bs_r2),
+		(bs_s3, bs_r3),
+	)
+	for s, r in pairs:
+		self.failUnlessEqual(simple_markup(s), r)
+:code
+"""
+
+
+class SMTestCase(unittest.TestCase):
+	
+	def test_blockspliting(self):
+		pairs = (
+			(bs_s1, bs_r1),
+			(bs_s2, bs_r2),
+			(bs_s3, bs_r3),
+		)
+		for s, r in pairs:
+			self.failUnlessEqual(simple_markup(s), r)
+	
+	def test_bold(self):
+		pairs = (
+			(u'ich _bün_ fetter Text', u'<p>\n  ich _bün_ fetter Text\n</p>\n'),
+			(u'_das_', u'<p>\n  <u>das</u>\n</p>\n'),
+			(u'fetter _text_', u'<p>\n  fetter <u>text</u>\n</p>\n'),
+			(u'_däs_', u'<p>\n  _däs_\n</p>\n'),
+		)
+		for s, r in pairs:
+			self.failUnlessEqual(simple_markup(s), r)
+	
+	def test_longbold(self):
+		pairs = (
+			(u'__ich bün fetter Text__', u'<p>\n  <u>ich bün fetter Text</u>\n</p>\n'),
+			(u'__das üst ebenfalls__ fötter text', u'<p>\n  <u>das üst ebenfalls</u> fötter text\n</p>\n'),
+			(u'das is __aüch fetter text__', u'<p>\n  das is <u>aüch fetter text</u>\n</p>\n'),
+			(u'auch __däs ist ziemlich__ fätter text', u'<p>\n  auch <u>däs ist ziemlich</u> fätter text\n</p>\n'),
+		)
+		for s, r in pairs:
+			self.failUnlessEqual(simple_markup(s), r)
+			
+	def test_quote(self):
+		pairs = (
+			()
+		)
+
+if __name__ == '__main__': unittest.main() 
+
+# text = """
+# ich bin __lustiger blabla__ text__ der=-- zum --testen gut is.
+# ich hab **auch //mehere** zeilen//.
+# 
+# am ende is _fett_
+# 
+# ich kann auch /einfache/ _formats_ auf einzelne wörter machen
+# aber/ich/kann/nicht/sowas/machen
+# 
+# quote:
+# blub die bla
+# 
+# quote:"blub"
+# blargh
+# :quote
+# 
+# code:"c"
+# int a = 1;
+# int b = 2;
+# if a > b:
+# <script>
+# :code
+# 
+# code:"java"
+# blargh
+# 
+# quote:"jo dude"
+# ich war ärster
+# und zweiter auch
+# :quote
+# 
+# das is //auch// zum testen gut.
+# <script type="javascript"></script>
+# """
+#