-
Notifications
You must be signed in to change notification settings - Fork 4
/
syntax.py
90 lines (62 loc) · 2.1 KB
/
syntax.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# -*- coding: utf8 -*-
import pdb
import re
class Syntax(object):
def __init__(self):
pass
def pattern(self):
return 'plain-text'
def newline(self):
return True
class UrbanSyntax(Syntax):
def __init__(self):
pass
def pattern(self, text):
content = text.get_text().strip()
# content = text.get_text().encode('utf8').strip()
if not content:
return 'none'
if content.isdigit(): # page number
return 'none'
if 17.9 < text.height < 18.1:
return 'heading-3'
if 20.0 < text.height < 20.1:
return 'heading-1'
if 15.9 < text.height < 16.0:
return 'heading-2'
mo = re.search(r'^(一|二|三|四|五|六|七|八|九|十)、', content)
if mo:
return 'heading-3'
mo = re.search(r'^((|\()(一|二|三|四|五|六|七|八|九|十)()|\))', content)
if mo:
return 'heading-4'
mo = re.search(r'^\d+、', content)
if mo:
return 'ordered-list-item'
if text.x0 < 90.1: # special case for neihu page 2
return 'unordered-list-item'
return 'plain-text'
def newline(self, text):
# content = text.get_text().encode('utf8').strip()
content = text.get_text().strip()
if text.x0 < 90.1: # special case for neihu page 2
return True
mo = re.search('。$', content)
if mo:
return True
if text.x1 > 505.0: # reach the right margin
return False
return True
def purify(self, text):
# content = text.get_text().encode('utf8').strip()
content = text.get_text().strip()
mo = re.match(r'(一|二|三|四|五|六|七|八|九|十)、(.*)', content)
if mo:
return mo.group(2)
mo = re.match(r'((|\()(一|二|三|四|五|六|七|八|九|十)()|\))(.*)', content)
if mo:
return mo.group(4)
mo = re.match(r'^\d+、(.*)', content)
if mo:
return mo.group(1)
return content