7
7
8
8
"""
9
9
from __future__ import unicode_literals
10
+
10
11
import re
11
12
12
13
JTABLE_SEPARATOR = 'JTABLE_SEPARATOR'
13
14
JTABLE_TEMPLATE = 'JTABLE_TEMPLATE'
14
15
DEFAULT_SEPARATOR = ','
15
16
16
- ai_regex = re .compile (r"ai ?\= ?\" ?(1) ?\"" )
17
- th_regex = re .compile (r"th ?\= ?\" ?(0) ?\"" )
18
- cap_regex = re .compile ("caption ?\= ?\" (.+?)\" " )
19
- sep_regex = re .compile ("separator ?\= ?\" (.+?)\" " )
20
- main_regex = re .compile (r"(\[jtable(.*?)\]([\s\S]*?)\[\/jtable\])" )
17
+ AUTO_INCREMENT_REGEX = re .compile (r"ai ?\= ?\" ?(1) ?\"" )
18
+ TABLE_HEADER_REGEX = re .compile (r"th ?\= ?\" ?(0) ?\"" )
19
+ CAPTION_REGEX = re .compile ("caption ?\= ?\" (.+?)\" " )
20
+ SEPARATOR_REGEX = re .compile ("separator ?\= ?\" (.+?)\" " )
21
+ MAIN_REGEX = re .compile (r"(\[jtable(.*?)\]([\s\S]*?)\[\/jtable\])" )
21
22
22
23
DEFAULT_TEMPATE = """
23
24
<div class="justtable">
24
25
<table>
25
- {% if caption %}
26
+ {%- if caption %}
26
27
<caption> {{ caption }} </caption>
27
- {% endif %}
28
- {% if th != 0 %}
28
+ {%- endif %}
29
+ {%- if th != 0 %}
29
30
<thead>
30
31
<tr>
31
- {% if ai == 1 %}
32
+ {%- if ai == 1 %}
32
33
<th> No. </th>
33
- {% endif %}
34
- {% for head in heads %}
34
+ {%- endif %}
35
+ {%- for head in heads %}
35
36
<th>{{ head }}</th>
36
- {% endfor %}
37
+ {%- endfor %}
37
38
</tr>
38
39
</thead>
39
- {% endif %}
40
+ {%- endif %}
40
41
<tbody>
41
- {% for body in bodies %}
42
+ {%- for body in bodies %}
42
43
<tr>
43
- {% if ai == 1 %}
44
+ {%- if ai == 1 %}
44
45
<td> {{ loop.index }} </td>
45
- {% endif %}
46
- {% for entry in body %}
46
+ {%- endif %}
47
+ {%- for entry in body %}
47
48
<td>{{ entry }}</td>
48
- {% endfor %}
49
+ {%- endfor %}
49
50
</tr>
50
- {% endfor %}
51
+ {%- endfor %}
51
52
</tbody>
52
53
</table>
53
54
</div>
54
55
"""
55
56
56
57
57
58
def generate_table (generator ):
58
- """Replace table tag in the article content."""
59
59
from jinja2 import Template
60
60
61
- # Always good idea to give plugin users to change
62
- # column separator if they prefer another
63
61
if JTABLE_SEPARATOR in generator .settings :
64
62
separator = generator .settings [JTABLE_SEPARATOR ]
65
63
else :
66
64
separator = DEFAULT_SEPARATOR
67
65
68
- # Many people using bootstrap, and prefer to
69
- # style tables using bootstrap classes.
70
- # Good idea to give ability to change template
71
66
if JTABLE_TEMPLATE in generator .settings :
72
67
table_template = generator .settings [JTABLE_TEMPLATE ]
73
68
else :
@@ -76,44 +71,38 @@ def generate_table(generator):
76
71
template = Template (table_template )
77
72
78
73
for article in generator .articles + generator .drafts :
79
- for match in main_regex .findall (article ._content ):
74
+ for match in MAIN_REGEX .findall (article ._content ):
75
+ all_match_str , props , table_data = match
80
76
param = {"ai" : 0 , "th" : 1 , "caption" : "" , "sep" : separator }
81
- if ai_regex .search (match [1 ]):
77
+
78
+ if AUTO_INCREMENT_REGEX .search (props ):
82
79
param ['ai' ] = 1
83
- if cap_regex .search (match [ 1 ] ):
84
- param ['caption' ] = cap_regex .findall (match [ 1 ] )[0 ]
85
- if th_regex .search (match [ 1 ] ):
80
+ if CAPTION_REGEX .search (props ):
81
+ param ['caption' ] = CAPTION_REGEX .findall (props )[0 ]
82
+ if TABLE_HEADER_REGEX .search (props ):
86
83
param ["th" ] = 0
87
- if sep_regex .search (match [1 ]):
88
- # Giving ability to use custom column separator to specific tables
89
- param ["sep" ] = sep_regex .findall (match [1 ])[0 ]
90
- data = match [2 ].strip ().split ('\n ' )
91
- if len (data ) > 2 or len (data ) == 1 and param ['th' ] == 0 :
92
- if param ['th' ] != 0 :
93
- heads = data [0 ].split (param ["sep" ])
94
- begin = 1
95
- else :
96
- heads = None
97
- begin = 0
84
+ if SEPARATOR_REGEX .search (props ):
85
+ param ["sep" ] = SEPARATOR_REGEX .findall (props )[0 ]
86
+
87
+ table_data_list = table_data .strip ().split ('\n ' )
98
88
99
- if begin = = 1 :
100
- # If we have header, we already know how much columns
101
- # we have and no need to split more to columns.
102
- bodies = [n .split (param ["sep" ], len (heads ) - 1 ) for n in data [ begin :]]
89
+ if len ( table_data_list ) > = 1 :
90
+ heads = table_data_list [ 0 ]. split ( param [ "sep" ]) if param [ 'th' ] else None
91
+ if heads :
92
+ bodies = [n .split (param ["sep" ], len (heads ) - 1 ) for n in table_data_list [ 1 :]]
103
93
else :
104
- bodies = [n .split (param ["sep" ]) for n in data [ begin :] ]
94
+ bodies = [n .split (param ["sep" ]) for n in table_data_list ]
105
95
106
- # Create a context to render with
107
96
context = generator .context .copy ()
108
97
context .update ({
109
98
'heads' : heads ,
110
99
'bodies' : bodies ,
111
100
})
112
101
context .update (param )
113
102
114
- # Render the template
115
- replacement = template . render ( context )
116
- article ._content = article ._content .replace ('' .join (match [ 0 ] ), replacement )
103
+ replacement = template . render ( context ). replace ( " \n \n " , " \n " )
104
+ print ( replacement )
105
+ article ._content = article ._content .replace ('' .join (all_match_str ), replacement )
117
106
118
107
119
108
def register ():
0 commit comments