aboutsummaryrefslogtreecommitdiff
path: root/mammoth/document_matchers.py
blob: 6429911cb1e891f129a19e06ab6ab2f3b017a3af (plain)
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
import collections

import cobble


def paragraph(style_id=None, style_name=None, numbering=None):
    return ParagraphMatcher(style_id, style_name, numbering)


ParagraphMatcher = collections.namedtuple("ParagraphMatcher", ["style_id", "style_name", "numbering"])
ParagraphMatcher.element_type = "paragraph"


def run(style_id=None, style_name=None):
    return RunMatcher(style_id, style_name)


RunMatcher = collections.namedtuple("RunMatcher", ["style_id", "style_name"])
RunMatcher.element_type = "run"


class bold(object):
    element_type = "bold"


class italic(object):
    element_type = "italic"


class underline(object):
    element_type = "underline"


class strikethrough(object):
    element_type = "strikethrough"


class small_caps(object):
    element_type = "small_caps"


class comment_reference(object):
    element_type = "comment_reference"


BreakMatcher = collections.namedtuple("BreakMatcher", ["break_type"])
BreakMatcher.element_type = "break"


line_break = BreakMatcher("line")
page_break = BreakMatcher("page")
column_break = BreakMatcher("column")


def equal_to(value):
    return StringMatcher(_operator_equal_to, value)


def _operator_equal_to(first, second):
    return first.upper() == second.upper()
    
    
def starts_with(value):
    return StringMatcher(_operator_starts_with, value)

def _operator_starts_with(first, second):
    return second.upper().startswith(first.upper())


@cobble.data
class StringMatcher(object):
    operator = cobble.field()
    value = cobble.field()
    
    def matches(self, other):
        return self.operator(self.value, other)