Commits

Anonymous committed c31afca

bla

Comments (0)

Files changed (1)

+# -*- coding: utf-8 -*-
+
+import re
+import tidy
+
+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>
+"""
+text = unicode(text, 'latin1')
+
+class SimpleMarkupText(object):
+	
+	def __init__(self, input):
+		self.input = sanitize(input)
+		return self.output
+	
+	def sanitize(self):
+		text = text.replace('<', '&lt;')
+		text = text.replace('>', '&gt;')
+		text = text.replace('\n', '<br />')
+		return text
+
+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 = output.encode('utf8')
+	output = str(tidy.parseString(output, char_encoding='utf8', indent=True, output_xhtml=True, show_body_only=True))
+	output = unicode(output, 'utf8')
+	return output
+		
+	
+# print simple_markup(text).encode('utf8')
+# simple_markup(text)