-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable_generator.py
More file actions
62 lines (40 loc) · 2 KB
/
table_generator.py
File metadata and controls
62 lines (40 loc) · 2 KB
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
import sublime
import sublime_plugin
from .csv_converter import csv2plaintext, csv2markdown, csv2latex
class TableGenerator(sublime_plugin.TextCommand):
def run(self, edit, alignment, converter):
assert len(self.view.sel()) == 1, sublime.error_message(
'expected only one selection')
selection = self.view.sel()[0]
selection = (selection if self.view.substr(selection)
else sublime.Region(0, self.view.size()))
rawdata = self.view.substr(selection)
try:
output = converter(rawdata,
alignment=alignment if alignment else None,
raw_string=True) + '\n'
except Exception as e:
sublime.error_message(str(e))
self.view.replace(edit, selection, output)
def input(self, args):
if 'alignment' not in args:
return AlignmentInputHandler()
def input_description(self):
return "Alignment Specifiers of Columns"
class PlainTableGeneratorCommand(TableGenerator):
def run(self, edit, alignment):
super(PlainTableGeneratorCommand, self).run(edit, alignment,
converter=csv2plaintext)
class MarkdownTableGeneratorCommand(TableGenerator):
def run(self, edit, alignment):
super(MarkdownTableGeneratorCommand, self).run(edit, alignment,
converter=csv2markdown)
class LatexTableGeneratorCommand(TableGenerator):
def run(self, edit, alignment):
super(LatexTableGeneratorCommand, self).run(edit, alignment,
converter=csv2latex)
class AlignmentInputHandler(sublime_plugin.TextInputHandler):
def placeholder(self):
return "'clr': render the 3-column table with 'center-left-right' alignment"
def validate(self, alignment):
return len(alignment) == 0 or (len(alignment) > 0 and set(alignment).issubset({'c', 'l', 'r'}))