# Path: zim/parser.py
# def prepare_text(text, tabstop=4):
# 	'''Ensures text is properly formatted.
# 	  - Fixes missing line end
# 	  - Expands spaces to tabs
# 	@param text: the intput text
# 	@param tabstop: the number of spaces to represent a tab
# 	@returns: the fixed text
# 	'''
# 	# HACK this char is recognized as line end by splitlines()
# 	# but not matched by \n in a regex. Hope there are no other
# 	# exceptions like it (crosses fingers)
# 	text = text.replace(u'\u2028', '\n')
# 
# 	# Fix line end
# 	if not text.endswith('\n'):
# 		text = text + '\n'
# 
# 	# Fix tabs
# 	spaces = ' ' * tabstop
# 	pattern = '(?m)^(\t*)((?:%s)+)' % spaces
# 	text = re.sub(
# 		pattern,
# 		lambda m: m.group(1) + '\t' * (len(m.group(2)) / tabstop),
# 		text
# 	)
# 	# Specify "(?m)" instead of re.M since "flags" keyword is not
# 	# supported in python 2.6
# 
# 	return text
# 
# class Rule(object):
# 	'''Class that defines a single parser rule. Typically used
# 	to define a regex pattern for one specific wiki format string
# 	and the processing to be done when this formatting is encountered
# 	in the text.
# 
# 	@ivar tag: L{Builder} tag for result of this rule. Used by the
# 	default process method.
# 	@ivar pattern: the regular expression for this parser as string
# 	@ivar process: function (or object) to process matched text, or C{None}
# 	The function should take a L{Builder} object as first argument,
# 	followed by one or more parameters for matched groups in the
# 	regular expression. If the regex pattern has no capturing groups
# 	this function is called with the whole match.
# 	The default function will use the C{tag} and C{descent}
# 	attributes
# 	@ivar decent: optional function (or object) to recursively parse the
# 	text matched by this rule. Called in the same way as C{process}.
# 	'''
# 
# 	def __init__(self, tag, pattern, process=None, descent=None):
# 		'''Constructor
# 		@param tag: L{Builder} tag for result of this rule. Used by the
# 		default process method.
# 		@param pattern: regex pattern as string
# 		@param process: optional function to process matched text
# 		@param descent: optional function to recursively parse matched text
# 		'''
# 		assert tag is not None or process is not None, 'Need at least a tag or a process method'
# 		self._re = None
# 		self.tag = tag
# 		self.pattern = pattern
# 		self.descent = descent
# 		self.process = process or self._process
# 
# 	def __repr__(self):
# 		return '<%s: %s: %s>' % (self.__class__.__name__, self.tag, self.pattern)
# 
# 	def __or__(self, other):
# 		'''Allow new parsers to be constructed by combining parser
# 		objects with the "|" operator.
# 		'''
# 		return Parser(self, other)
# 
# 	def _process(self, builder, text):
# 		# default action for matched text
# 		if self.descent:
# 			builder.start(self.tag)
# 			self.descent(builder, *text)
# 			builder.end(self.tag)
# 		else:
# 			builder.append(self.tag, None, text)


import re
import zim.parser
from zim.parser import prepare_text, Rule
from zim.formats import *
from zim.parsing import url_re




info = {
	'name': 'plain',
	'desc': 'Plain text',
	'mimetype': 'text/plain',
	'extension': 'txt',
	'native': False,
	'import': True,
	'export': True,
}


class Parser(ParserClass):

	# TODO parse constructs like *bold* and /italic/ same as in email,
	# but do not remove the "*" and "/", just display text 1:1

	# TODO also try at least to parse bullet and checkbox lists
	# common base class with wiki format

	# TODO parse markdown style headers

	def parse(self, input, partial=False):
		if not isinstance(input, basestring):
			input = ''.join(input)

		if not partial:

# Path: zim/parser.py
# def prepare_text(text, tabstop=4):
# 	'''Ensures text is properly formatted.
# 	  - Fixes missing line end
# 	  - Expands spaces to tabs
# 	@param text: the intput text
# 	@param tabstop: the number of spaces to represent a tab
# 	@returns: the fixed text
# 	'''
# 	# HACK this char is recognized as line end by splitlines()
# 	# but not matched by \n in a regex. Hope there are no other
# 	# exceptions like it (crosses fingers)
# 	text = text.replace(u'\u2028', '\n')
# 
# 	# Fix line end
# 	if not text.endswith('\n'):
# 		text = text + '\n'
# 
# 	# Fix tabs
# 	spaces = ' ' * tabstop
# 	pattern = '(?m)^(\t*)((?:%s)+)' % spaces
# 	text = re.sub(
# 		pattern,
# 		lambda m: m.group(1) + '\t' * (len(m.group(2)) / tabstop),
# 		text
# 	)
# 	# Specify "(?m)" instead of re.M since "flags" keyword is not
# 	# supported in python 2.6
# 
# 	return text
# 
# class Rule(object):
# 	'''Class that defines a single parser rule. Typically used
# 	to define a regex pattern for one specific wiki format string
# 	and the processing to be done when this formatting is encountered
# 	in the text.
# 
# 	@ivar tag: L{Builder} tag for result of this rule. Used by the
# 	default process method.
# 	@ivar pattern: the regular expression for this parser as string
# 	@ivar process: function (or object) to process matched text, or C{None}
# 	The function should take a L{Builder} object as first argument,
# 	followed by one or more parameters for matched groups in the
# 	regular expression. If the regex pattern has no capturing groups
# 	this function is called with the whole match.
# 	The default function will use the C{tag} and C{descent}
# 	attributes
# 	@ivar decent: optional function (or object) to recursively parse the
# 	text matched by this rule. Called in the same way as C{process}.
# 	'''
# 
# 	def __init__(self, tag, pattern, process=None, descent=None):
# 		'''Constructor
# 		@param tag: L{Builder} tag for result of this rule. Used by the
# 		default process method.
# 		@param pattern: regex pattern as string
# 		@param process: optional function to process matched text
# 		@param descent: optional function to recursively parse matched text
# 		'''
# 		assert tag is not None or process is not None, 'Need at least a tag or a process method'
# 		self._re = None
# 		self.tag = tag
# 		self.pattern = pattern
# 		self.descent = descent
# 		self.process = process or self._process
# 
# 	def __repr__(self):
# 		return '<%s: %s: %s>' % (self.__class__.__name__, self.tag, self.pattern)
# 
# 	def __or__(self, other):
# 		'''Allow new parsers to be constructed by combining parser
# 		objects with the "|" operator.
# 		'''
# 		return Parser(self, other)
# 
# 	def _process(self, builder, text):
# 		# default action for matched text
# 		if self.descent:
# 			builder.start(self.tag)
# 			self.descent(builder, *text)
# 			builder.end(self.tag)
# 		else:
# 			builder.append(self.tag, None, text)


# Path: zim/formats/plain.py
import re
import zim.parser
from zim.parser import prepare_text, Rule
from zim.formats import *
from zim.parsing import url_re





info = {
	'name': 'plain',
	'desc': 'Plain text',
	'mimetype': 'text/plain',
	'extension': 'txt',
	'native': False,
	'import': True,
	'export': True,
}


class Parser(ParserClass):

	# TODO parse constructs like *bold* and /italic/ same as in email,
	# but do not remove the "*" and "/", just display text 1:1

	# TODO also try at least to parse bullet and checkbox lists
	# common base class with wiki format

	# TODO parse markdown style headers

	def parse(self, input, partial=False):
		if not isinstance(input, basestring):
			input = ''.join(input)

		if not partial:

			input = prepare_text(input)