Parser Resource2
import re
 
import Queue
from reSource0 import mainConversion
 
'''
HTMLList(mainConversion) struct:
 
start
m1
start
m2
end
m3
end
(#layer)
start
m4
same (#same layer)
m5
end
.
.
.
.
.
.
 
mixContent()
'''
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:
            raise TypeError('tpye must is str.')
        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>"
 
def test():
    a = NumberList()
    content = '''# 123
# 456
 # 789
# 123456
123
# 789
 # abc
  # 798
# 987
 
'''
    return a.conver(content)