Skip to content

Commit 2dc6f63

Browse files
committed
Fix table behaviour
1 parent d057adc commit 2dc6f63

File tree

1 file changed

+39
-50
lines changed

1 file changed

+39
-50
lines changed

just_table.py

+39-50
Original file line numberDiff line numberDiff line change
@@ -7,67 +7,62 @@
77
88
"""
99
from __future__ import unicode_literals
10+
1011
import re
1112

1213
JTABLE_SEPARATOR = 'JTABLE_SEPARATOR'
1314
JTABLE_TEMPLATE = 'JTABLE_TEMPLATE'
1415
DEFAULT_SEPARATOR = ','
1516

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\])")
2122

2223
DEFAULT_TEMPATE = """
2324
<div class="justtable">
2425
<table>
25-
{% if caption %}
26+
{%- if caption %}
2627
<caption> {{ caption }} </caption>
27-
{% endif %}
28-
{% if th != 0 %}
28+
{%- endif %}
29+
{%- if th != 0 %}
2930
<thead>
3031
<tr>
31-
{% if ai == 1 %}
32+
{%- if ai == 1 %}
3233
<th> No. </th>
33-
{% endif %}
34-
{% for head in heads %}
34+
{%- endif %}
35+
{%- for head in heads %}
3536
<th>{{ head }}</th>
36-
{% endfor %}
37+
{%- endfor %}
3738
</tr>
3839
</thead>
39-
{% endif %}
40+
{%- endif %}
4041
<tbody>
41-
{% for body in bodies %}
42+
{%- for body in bodies %}
4243
<tr>
43-
{% if ai == 1 %}
44+
{%- if ai == 1 %}
4445
<td> {{ loop.index }} </td>
45-
{% endif %}
46-
{% for entry in body %}
46+
{%- endif %}
47+
{%- for entry in body %}
4748
<td>{{ entry }}</td>
48-
{% endfor %}
49+
{%- endfor %}
4950
</tr>
50-
{% endfor %}
51+
{%- endfor %}
5152
</tbody>
5253
</table>
5354
</div>
5455
"""
5556

5657

5758
def generate_table(generator):
58-
"""Replace table tag in the article content."""
5959
from jinja2 import Template
6060

61-
# Always good idea to give plugin users to change
62-
# column separator if they prefer another
6361
if JTABLE_SEPARATOR in generator.settings:
6462
separator = generator.settings[JTABLE_SEPARATOR]
6563
else:
6664
separator = DEFAULT_SEPARATOR
6765

68-
# Many people using bootstrap, and prefer to
69-
# style tables using bootstrap classes.
70-
# Good idea to give ability to change template
7166
if JTABLE_TEMPLATE in generator.settings:
7267
table_template = generator.settings[JTABLE_TEMPLATE]
7368
else:
@@ -76,44 +71,38 @@ def generate_table(generator):
7671
template = Template(table_template)
7772

7873
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
8076
param = {"ai": 0, "th": 1, "caption": "", "sep": separator}
81-
if ai_regex.search(match[1]):
77+
78+
if AUTO_INCREMENT_REGEX.search(props):
8279
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):
8683
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')
9888

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:]]
10393
else:
104-
bodies = [n.split(param["sep"]) for n in data[begin:]]
94+
bodies = [n.split(param["sep"]) for n in table_data_list]
10595

106-
# Create a context to render with
10796
context = generator.context.copy()
10897
context.update({
10998
'heads': heads,
11099
'bodies': bodies,
111100
})
112101
context.update(param)
113102

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)
117106

118107

119108
def register():

0 commit comments

Comments
 (0)