-
Notifications
You must be signed in to change notification settings - Fork 3
/
decode.py
96 lines (81 loc) · 2.74 KB
/
decode.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
91
92
93
94
95
96
import sublime, sublime_plugin, re
class FoundationGridDecodeCommand(sublime_plugin.TextCommand):
BREAKPOINTS = {
'sg': 'small',
'mg': 'medium',
'lg': 'large',
'xg': 'xlarge',
'so': 'small-offset',
'mo': 'medium-offset',
'lo': 'large-offset',
'xo': 'xlarge-offset',
'sc': 'small-centered',
'mc': 'medium-centered',
'lc': 'large-centered',
'xc': 'xlarge-centered',
'sp': 'small-pu',
'mp': 'medium-pu',
'lp': 'large-pu',
'xp': 'xlarge-pu',
}
# Regular expressions for finding grid classes, columns, and rows
re_class = r"[a-z]{2}-?[\d]*(?:\(.*\))?"
re_column = r"(?:"+re_class+")+"
re_row = r"(?:"+re_column+",*)+"
def parse_row(self, input, depth):
row_indent = '\t' * (depth * 2)
column_indent = row_indent + '\t'
columns = re.findall(self.re_column, input)
# Opening tag for row
output = '%s<div class="row">\n' % row_indent
# Processing columns
for column in columns:
# Given the string sg1(sg2)
# sg1 is the base_column, and (sg2) is the nested row
base_column = column.split('(', 1)[0]
nested_row = re.findall(r'\(.*\)', column)
# Opening tag for column
output += '%s<div class="' % column_indent
# Processing breakpoint classes
sizes = re.findall(self.re_class, base_column)
for size in sizes:
bp, number = size[:2], size[2:]
if number[0] == '0':
number = number[1:]
if 'p' in bp:
# Pull (negative number)
if '-' in number:
output += '%sll-%s' % (self.BREAKPOINTS[bp], number[1:])
# Push (positive number)
else:
output += '%ssh-%s' % (self.BREAKPOINTS[bp], number)
else:
output += '%s-%s' % (self.BREAKPOINTS[bp], number)
output += ' '
output += 'columns">\n'
# Nested rows
if nested_row:
# Pull the first result out of the re search, and strip the parentheses from the string
output += self.parse_row(nested_row[0][1:-1], depth + 1)
else:
# Insert a blank line if there's no nested row
output += '%s\n' % column_indent
# Closing tag for column
output += '%s</div>\n' % column_indent
# Closing tag for row
output += '%s</div>\n' % row_indent
return output
def decode(self, input):
output = ''
rows = re.findall(self.re_row, input)
# Processing rows
for row in rows:
output += self.parse_row(row, 0)
return output
def run(self, edit):
selections = self.view.sel()
for selection in selections:
edit = self.view.begin_edit('foundation-grid')
string = self.view.substr(selection)
self.view.replace(edit, selection, self.decode(string))
self.view.end_edit(edit)