-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_parser.py
38 lines (29 loc) · 963 Bytes
/
text_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = 'Jerry Bai'
import re
from util import blocks
class Parser:
"""
Text parser, using rules and handlers to format plain text
"""
def __init__(self, handler):
self.handler = handler
self.rules = []
self.filters = []
def addRules(self, rule):
self.rules.append(rule)
def addFilter(self, pattern, name):
def filter(block, handler):
return re.sub(pattern, handler.sub(name), block)
self.filters.append(filter)
def parse(self, file):
self.handler.start('document')
for block in blocks(file):
for filter in self.filters:
block = filter(block, self.handler)
for rule in self.rules:
if rule.match(block):
is_match = rule.mark(block, self.handler)
if is_match: break
self.handler.end('document')