summaryrefslogtreecommitdiff
path: root/lib/yaml/marker.py
blob: 6ec12e63980894af50c1756cfcba7a4f06bc83ef (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

class Marker:

    def __init__(self, source, data, index, row, column):
        self.source = source
        self.data = data
        self.index = index
        self.row = row
        self.column = column

    def get_snippet(self, max_length=79):
        if not isinstance(self.data, basestring):
            return None
        head = ''
        start = self.index
        while start > 0 and self.data[start-1] not in '\r\n':
            start -= 1
            if self.index-start > max_length/2-1:
                head = ' ... '
                start += 5
                break
        tail = ''
        end = self.index
        while end < len(self.data) and self.data[end] not in '\r\n':
            end += 1
            if end-self.index > max_length/2-1:
                tail = ' ... '
                end -= 5
                break
        snippet = self.data[start:end]
        if isinstance(snippet, unicode):
            snippet = snippet.encode('utf-8')
        return head + snippet + tail + '\n'  \
                + ' '*(self.index-start+len(head)) + '^' + '\n'