Parser Resource
import re
import Queue
from reSource0 import mainConversion
from insertImg import insertImg
 
from google.appengine.api.datastore_types import Text
 
#最後修改日期6/14  17:29 
#@auther : Peterk
 
'''
in : full page
out: changed result
'''
 
checkType = lambda a,b : type(a) is type(b)
 
#------------  sub-function ---------------
 
class horizon(mainConversion):
    '''
    Format:
    ------
    '''
    def __init__(self):
        self.pattren = r'-{6}'
        self.expand  = r'<hr size="1" align="center" noshade width="90%" color="000000">'        
 
class volume(mainConversion):
    '''
    Format:
    **content**
    '''
    def __init__(self):
        self.pattren = r'(?P<start>.*?)(?:[\*]{2})(?P<text>[^\*]*)(?:[\*]{2})(?P<end>.*)'
        self.expand  = r'\g<start><b>\g<text></b>\g<end>'
 
class midLine(mainConversion):
    '''
    Format:
    --content--
    '''
    def __init__(self):
        self.pattren = r'(?P<start>.*?)(?:[-]{2})(?P<text>[^-]*)(?:[-]{2})(?P<end>.*)'
        self.expand = r'\g<start><s>\g<text></s>\g<end>'
 
class sizetext(mainConversion):
    '''
    input : [[size=150%]150% of current size]
    output : <span style="font-size:150%;">150% of current size</span>
    '''
    def __init__(self):
        self.expand = '\g<start><span style="font-size:\g<sizeValue>;">\g<sizeTxt></span>\g<end>'
        self.pattren = r'(?P<start>.*?)(?:\[\[size=)(?P<sizeValue>[^\[\]]*)(?:\])(?P<sizeTxt>[^\[\]]*)(?:\])(?P<end>.*)'
 
class changeLink(mainConversion):
    '''
    Format:
    [[http://www.example.com/ | targerLink]]
    '''
    def __init__(self):
        self.pattren = r'(?P<start>.*?)(?:\[\[)[\s]*(?P<link>(http[s]?:)\/{2}([\S]+(\/|\.)?))[\s]*[\|](?P<linkTxt>.*)[\s]*(?:\]\])(?P<end>.*)'
        self.expand = r'\g<start><a href="\g<link>">\g<linkTxt></a>\g<end>'
 
class subscript(mainConversion):
        '''
        input : ,,content,,
        output:<sub>content</sub>
        '''
    def __init__(self):
        self.pattren =r'(?P<start>.*?)(?:,{2})(?P<subStr>[^,]*)(?:,{2})(?P<end>.*)'
        self.expand = r'\g<start><sub>\g<subStr></sub>\g<end>'
 
class supscript(mainConversion):
        '''
        input :^^content^^
        output :<sup>content</sup>
        '''
    def __init__(self):
        self.pattren =r'(?P<start>.*?)(?:\^{2})(?P<supStr>[^\^]*)(?:\^{2})(?P<end>.*)'
        self.expand = r'\g<start><sup>\g<supStr></sup>\g<end>'
 
class changeBigText(mainConversion):
    '''
    Format:
    <h>content</h>
    '''
    def __init__(self):
        self.pattren = r'(?P<start>.*?)(?P<len>[\+]+)[\s]*(?P<txt>.*)'
    def conver(self,loadString):
            #full document
            strlist = self.split(loadString)
            res = ''
            #every sentence
            for line in strlist:
                    matchOjt = re.search(self.pattren,line)
                    if matchOjt :
                            tLen = str(len(matchOjt.group('len')))
                            txt = matchOjt.group('txt')
                            res += matchOjt.group('start') + "<h" + tLen + ">" + txt + "</h" + tLen +">"
                    else:
                            res += line
                    res += '\n'
                    #sentence to document
            return res
        #return full processed document
 
class form(mainConversion):
    '''
    Format:
    || testTxt1 | testTxt1 | testTxt1 ||
    '''
    def __init__(self):
        #self.compi = re.search(r'(\|\|)\n(\|\|)',self.loads)#change line flag : <tr>
        self.startTag = '<table border=1>'
        self.nLTag = '<tr>'
        self.endTag = '</table>'
    def conver(self,loadString):
        #full document
        strlist = self.split(loadString)
        res = ''
        count = 0
        #every sentence
        for line in strlist:
            matchForm = re.search(r'(?<=(\|\|))(?P<con>.*)(?=(\|\|))',line)#add <table border=1></table>
            #process every line and add tag : <td></td>
            if matchForm:
                matchFormStr = re.findall(r'[^\|]*',matchForm.group('con'))#get || (content) ||
                if count is not strlist.index(line) -1 :
                    res += self.startTag
                for i in matchFormStr :
                    if i:
                        res += '<td>' + i + '</td>' #result : <td>testTxt1</td><td>testTxt2</td><td>testTxt3</td>
                if count == strlist.index(line)-1 : # if find next line and add tag:'<tr>'
                    res += '<tr>'
                else:
                    res += self.endTag
                count = strlist.index(line)
            else:
                res += line
            res += '\n'
        return res
 
class HTMLList(mainConversion):
 
    start = 1
    end = 2
    same = 3
    def conver(self,content):
        if type(content) is not str and type(content) is not unicode and type(content) is not Text:
            raise TypeError('error tpye is' +str(type(content)))
        queue = Queue.Queue()
        current_deep = 0
        for line in content.split('\n'):
            m = self.compile.search(line)
            if m:
                this_deep = len(m.group('space')) + 1
                if this_deep < current_deep:
                    for i in range(current_deep - this_deep):
                        current_deep -= 1
                        queue.put(self.end)
                if this_deep == current_deep:
                    queue.put(self.same)
                elif this_deep > current_deep:
                    for i in range(this_deep - current_deep):
                        current_deep += 1
                        queue.put(self.start)
                queue.put(m)
            else:
                for i in range(current_deep):
                    current_deep -= 1
                    queue.put(self.end)
                queue.put(line)
        return self.mixContent(queue)
 
    def mixContent(self,queue):
        content = ''
        while not queue.empty():
            item = queue.get()
            #if not item:
            #       raise ValueError('is none.')
            if item is self.start:
                content += '\n' + self.htmlStartTag + '\n'
            elif item is self.end:
                content += '</li>\n' + self.htmlEndTag + '\n'
            elif item is self.same:
                content += '</li>\n'
            elif type(item) is str or type(item) is unicode:
                content += item + '\n'
            else:
                content += '<li>' + item.group('content')
        return content
 
class BulletList(HTMLList):
    def __init__(self):
        self.compile = re.compile(r'(?P<space> *)(?:\* )(?P<content>.*)')
        self.htmlStartTag = "<ul>"
        self.htmlEndTag = "</ul>"
 
class NumberList(HTMLList):
    def __init__(self):
        self.compile = re.compile(r'(?P<space> *)(?:# )(?P<content>.*)')
        self.htmlStartTag = "<ol>"
        self.htmlEndTag = "</ol>"
 
class br(mainConversion):
    def conver(self,content):
        return content.replace('\n','<br />')
 
#--------------------after4/27 new----------------
class teletypeTxt(mainConversion):
    '''
    input : {{teletype text}}
    output : <tt>teletype text</tt>
    '''
    def __init__(self):
        self.expand = '\g<start><tt>\g<txt><\tt>\g<end>'
        self.pattren = r'(?P<start>.*?)(?:\{{2})(?P<txt>[^\{]*)(?:}{2})(?P<end>.*)'
 
class italicText(mainConversion):
    '''
    input : //italic text//
    output :<em>italic text</em>
    '''
    def __init__(self):
        self.expand = r'\g<start><em>\g<txt></em>\g<end>'
        self.pattren = r'(?P<start>.*?)(?:/{2})(?P<txt>[^/]*)(?:/{2})(?P<end>.*)'
 
class underline(mainConversion):
    '''
    #
    input : __underline__
    output :<u>italic text</u>
    '''
    def __init__(self):
        self.expand = r'\g<start><u>\g<txt></u>\g<end>'
        self.pattren = r'(?P<start>.*?)(?:_{2})(?P<txt>[^_]*)(?:_{2})(?P<end>.*)'
'''
class
 
    input : @@raw text@@
    output:<span style="white-space: pre-wrap;">raw text</span>
    '''
 
'''
WIKIDOT自己寫而HTML沒有的語法:
    [((bibcite label))]
    [[toc]]
    [[footnote]] footnote text [[/footnote]]
'''
#/--------------------after4/27 new----------------    
 
res = [
    horizon(),
    midLine(),
    italicText(),
    volume(),
    underline(),
    subscript(),
    supscript(),
    teletypeTxt(),
    form(),
    changeBigText(),
    changeLink(),
    sizetext(),
    BulletList(),
    NumberList()
    ]