~w-vollprecht/uberwriter/uberwriter_development

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import re
from gi.repository import Gtk

import logging
logger = logging.getLogger('uberwriter')
logger.critical("RASDAOD JKJD ASJD SJ")

class FixTable():

    def __init__(self, TextBuffer):
        self.TextBuffer = TextBuffer

    @staticmethod
    def create_seperator(widths, char):
        """
            Generate a line of + and - as sepeartor

            Example:
            >>> create_separarator([2, 4], '-')
            '+----+------+'
        """
        line = []

        for w in widths:
            line.append("+" + char * (w + 2))

        line.append("+")
        return ''.join(line)

    @staticmethod
    def create_line(columns, widths):
        """
            Crea una fila de la tabla separando los campos con un '|'.

            El argumento `columns` es una lista con las celdas que se
            quieren imprimir y el argumento `widths` tiene el ancho
            de cada columna. Si la columna es mas ancha que el texto
            a imprimir se agregan espacios vacíos.

            Example
                >>> create_line(['nombre', 'apellido'], [7, 10])
                '| nombre  | apellido   |'
        """

        line = zip(columns, widths)
        result = []

        for text, width in line:
            spaces = " " * (width - len(text))
            text = "| " + text + spaces + " "
            result.append(text)

        result.append("|")
        return ''.join(result)

    @staticmethod
    def create_table(content):
        """Imprime una tabla en formato restructuredText.

        El argumento `content` tiene que ser una lista
        de celdas.

        Example:

            >>> print create_table([['software', 'vesion'], ['python', '3.2'],
                ['vim', '7.3']])
+----------+--------+
| software | vesion |
+==========+========+
| python   | 3.2    |
+----------+--------+
| vim      | 7.3    |
+----------+--------+
        """

        # obtiene las columnas de toda la tabla.
        columns = zip(*content)
        # calcula el tamano maximo que debe tener cada columna.
        # replace with len()
        widths = [max([len(x) for x in i]) for i in columns]

        result = []

        result.append(FixTable.create_seperator(widths, '-'))
        print(content, widths)
        result.append(FixTable.create_line(content[0], widths))
        result.append(FixTable.create_seperator(widths, '='))

        for line in content[1:]:
            result.append(FixTable.create_line(line, widths))
            result.append(FixTable.create_seperator(widths, '-'))

        return '\n'.join(result)

    @staticmethod
    def are_in_a_table(current_line):
        "Line in a table?"
        return "|" in current_line or "+" in current_line

    @staticmethod
    def are_in_a_paragraph(current_line):
        "Line in a paragraph?"
        return len(current_line.strip()) >= 1

    def get_table_bounds(self, are_in_callback):
        """
            Gets the row number where the table begins and ends.
            are_in_callback argument must be a function
            indicating whether a particular line belongs or not
            to the table to be measured (or create).
            Returns two values ​​as a tuple
        """
        top = 0

        buf = self.TextBuffer
        start_iter = buf.get_start_iter()
        end_iter = buf.get_end_iter()

        text = self.TextBuffer.get_text(start_iter, end_iter, False).split('\n')
        logger.debug(text)
        length = len(text)
        bottom = length - 1

        insert_mark = self.TextBuffer.get_insert()
        insert_iter = self.TextBuffer.get_iter_at_mark(insert_mark)
        current_row_index = insert_iter.get_line()

        for a in range(current_row_index, top, -1):
            if not are_in_callback(text[a]):
                top = a + 1
                break

        for b in range(current_row_index, length):
            if not are_in_callback(text[b]):
                bottom = b - 1
                break

        return top, bottom

    @staticmethod
    def remove_spaces(string):
        """Remove unnecessary spaces"""
        return re.sub("\s\s+", " ", string)

    @staticmethod
    def create_separators_removing_spaces(string):
        return re.sub("\s\s+", "|", string)

    @staticmethod
    def extract_cells_as_list(string):
        "Extrae el texto de una fila de tabla y lo retorna como una lista."
        string = FixTable.remove_spaces(string)
        return [item.strip() for item in string.split('|') if item]

    @staticmethod
    def extract_table(text, top, bottom):
        full_table_text = text[top:bottom]
        # selecciona solamente las lineas que tienen celdas con texto.
        only_text_lines = [x for x in full_table_text if '|' in x]
        # extrae las celdas y descarta los separadores innecesarios.
        return [FixTable.extract_cells_as_list(x) for x in only_text_lines]

    @staticmethod
    def extract_words_as_lists(text, top, bottom):
        "Genera una lista de palabras para crear una lista."

        lines = text[top:bottom + 1]
        return [FixTable.create_separators_removing_spaces(line).split('|') for line in lines]

    def fix_table(self):
        """
            Fix Table, so all columns have the same width (again)

            `initial_row` is a int idicationg the current row index
        """

        cursor_mark = self.TextBuffer.get_insert()
        cursor_iter = self.TextBuffer.get_iter_at_mark(cursor_mark)
        cursor_iter.set_line(cursor_iter.get_line())

        end_line = cursor_iter.copy()
        end_line.forward_to_line_end()


        line_text = self.TextBuffer.get_text(cursor_iter, end_line, False)
        if FixTable.are_in_a_table(line_text):

            # obtiene el indice donde comienza y termina la tabla.
            r1, r2 = self.get_table_bounds(FixTable.are_in_a_table)

            logger.debug('asdasd ')

            # extrae de la tabla solo las celdas de texto
            buf = self.TextBuffer
            start_iter = buf.get_start_iter()
            end_iter = buf.get_end_iter()

            text = self.TextBuffer.get_text(start_iter, end_iter, False).split('\n')

            table_as_list = FixTable.extract_table(text, r1, r2)
            logger.debug(table_as_list)
            # genera una nueva tabla tipo restructured text y la dibuja en el buffer.
            table_content = FixTable.create_table(table_as_list)
            logger.debug(table_content)
            # Insert table back into Buffer ... 
            self.TextBuffer.insert(start_iter, table_content, -1)
        else:
            logger.debug("Not in a table")
            print("Not in table")