1
1
import re
2
+ import hamlpy
2
3
3
4
# Valid characters for dictionary key
4
5
re_key = re .compile (r'[a-zA-Z0-9-_]+' )
5
6
re_nums = re .compile (r'[0-9\.]+' )
6
-
7
+ re_whitespace = re .compile (r'([ \t]+)' )
8
+ re_leading_spaces = re .compile (r'^\s+' , re .MULTILINE )
9
+ re_line = re .compile (r'.*' )
7
10
re_sq = re .compile (r'(.*?)(?<!\\)(?:\')' )
8
11
re_dq = re .compile (r'(.*?)(?<!\\)(?:")' )
9
12
@@ -106,9 +109,14 @@ def parse(self):
106
109
while self .ptr < self .length - 1 :
107
110
key = self .__parse_key ()
108
111
109
- # Tuple/List parsing
110
112
self .consume_whitespace ()
111
- if self .s [self .ptr ] in ('(' , '[' ):
113
+ # Multi-line HAML
114
+ if self .s [self .ptr ] == '\n ' :
115
+ self .ptr += 1
116
+ val = self .__parse_haml ()
117
+ self .consume_whitespace ()
118
+ # Tuple/List parsing
119
+ elif self .s [self .ptr ] in ('(' , '[' ):
112
120
tl_parser = AttributeTupleAndListParser (self .s [self .ptr :])
113
121
val = tl_parser .parse ()
114
122
self .ptr += tl_parser .ptr
@@ -118,6 +126,22 @@ def parse(self):
118
126
119
127
self .dict [key ]= val
120
128
return self .dict
129
+
130
+ def __parse_haml (self ):
131
+ def whitespace_length ():
132
+ r = re_whitespace .match (self .s , pos = self .ptr )
133
+ return len (r .group (0 ))
134
+
135
+ initial_indentation = whitespace_length ()
136
+ lines = []
137
+ while whitespace_length () >= initial_indentation :
138
+ line = re_line .match (self .s , pos = self .ptr ).group (0 )
139
+ lines .append (line )
140
+ self .ptr += len (line )+ 1
141
+
142
+ h = hamlpy .Compiler ()
143
+ html = h .process_lines (lines )
144
+ return re .sub (re_leading_spaces , ' ' , html ).replace ('\n ' , '' ).strip ()
121
145
122
146
def __parse_key (self ):
123
147
'''Parse key variable and consume up to the colon'''
0 commit comments