Skip to content

Commit

Permalink
Merge pull request #4 from burakkose/fix-two-element
Browse files Browse the repository at this point in the history
Fix table behaviour
  • Loading branch information
burakkose committed Mar 31, 2017
2 parents d057adc + 2dc6f63 commit 7100e98
Showing 1 changed file with 37 additions and 49 deletions.
86 changes: 37 additions & 49 deletions just_table.py
Expand Up @@ -7,67 +7,62 @@
"""
from __future__ import unicode_literals

import re

JTABLE_SEPARATOR = 'JTABLE_SEPARATOR'
JTABLE_TEMPLATE = 'JTABLE_TEMPLATE'
DEFAULT_SEPARATOR = ','

ai_regex = re.compile(r"ai ?\= ?\" ?(1) ?\"")
th_regex = re.compile(r"th ?\= ?\" ?(0) ?\"")
cap_regex = re.compile("caption ?\= ?\"(.+?)\"")
sep_regex = re.compile("separator ?\= ?\"(.+?)\"")
main_regex = re.compile(r"(\[jtable(.*?)\]([\s\S]*?)\[\/jtable\])")
AUTO_INCREMENT_REGEX = re.compile(r"ai ?\= ?\" ?(1) ?\"")
TABLE_HEADER_REGEX = re.compile(r"th ?\= ?\" ?(0) ?\"")
CAPTION_REGEX = re.compile("caption ?\= ?\"(.+?)\"")
SEPARATOR_REGEX = re.compile("separator ?\= ?\"(.+?)\"")
MAIN_REGEX = re.compile(r"(\[jtable(.*?)\]([\s\S]*?)\[\/jtable\])")

DEFAULT_TEMPATE = """
<div class="justtable">
<table>
{% if caption %}
{%- if caption %}
<caption> {{ caption }} </caption>
{% endif %}
{% if th != 0 %}
{%- endif %}
{%- if th != 0 %}
<thead>
<tr>
{% if ai == 1 %}
{%- if ai == 1 %}
<th> No. </th>
{% endif %}
{% for head in heads %}
{%- endif %}
{%- for head in heads %}
<th>{{ head }}</th>
{% endfor %}
{%- endfor %}
</tr>
</thead>
{% endif %}
{%- endif %}
<tbody>
{% for body in bodies %}
{%- for body in bodies %}
<tr>
{% if ai == 1 %}
{%- if ai == 1 %}
<td> {{ loop.index }} </td>
{% endif %}
{% for entry in body %}
{%- endif %}
{%- for entry in body %}
<td>{{ entry }}</td>
{% endfor %}
{%- endfor %}
</tr>
{% endfor %}
{%- endfor %}
</tbody>
</table>
</div>
"""


def generate_table(generator):
"""Replace table tag in the article content."""
from jinja2 import Template

# Always good idea to give plugin users to change
# column separator if they prefer another
if JTABLE_SEPARATOR in generator.settings:
separator = generator.settings[JTABLE_SEPARATOR]
else:
separator = DEFAULT_SEPARATOR

# Many people using bootstrap, and prefer to
# style tables using bootstrap classes.
# Good idea to give ability to change template
if JTABLE_TEMPLATE in generator.settings:
table_template = generator.settings[JTABLE_TEMPLATE]
else:
Expand All @@ -76,44 +71,37 @@ def generate_table(generator):
template = Template(table_template)

for article in generator.articles + generator.drafts:
for match in main_regex.findall(article._content):
for match in MAIN_REGEX.findall(article._content):
all_match_str, props, table_data = match
param = {"ai": 0, "th": 1, "caption": "", "sep": separator}
if ai_regex.search(match[1]):

if AUTO_INCREMENT_REGEX.search(props):
param['ai'] = 1
if cap_regex.search(match[1]):
param['caption'] = cap_regex.findall(match[1])[0]
if th_regex.search(match[1]):
if CAPTION_REGEX.search(props):
param['caption'] = CAPTION_REGEX.findall(props)[0]
if TABLE_HEADER_REGEX.search(props):
param["th"] = 0
if sep_regex.search(match[1]):
# Giving ability to use custom column separator to specific tables
param["sep"] = sep_regex.findall(match[1])[0]
data = match[2].strip().split('\n')
if len(data) > 2 or len(data) == 1 and param['th'] == 0:
if param['th'] != 0:
heads = data[0].split(param["sep"])
begin = 1
else:
heads = None
begin = 0
if SEPARATOR_REGEX.search(props):
param["sep"] = SEPARATOR_REGEX.findall(props)[0]

table_data_list = table_data.strip().split('\n')

if begin == 1:
# If we have header, we already know how much columns
# we have and no need to split more to columns.
bodies = [n.split(param["sep"], len(heads) - 1) for n in data[begin:]]
if len(table_data_list) >= 1:
heads = table_data_list[0].split(param["sep"]) if param['th'] else None
if heads:
bodies = [n.split(param["sep"], len(heads) - 1) for n in table_data_list[1:]]
else:
bodies = [n.split(param["sep"]) for n in data[begin:]]
bodies = [n.split(param["sep"]) for n in table_data_list]

# Create a context to render with
context = generator.context.copy()
context.update({
'heads': heads,
'bodies': bodies,
})
context.update(param)

# Render the template
replacement = template.render(context)
article._content = article._content.replace(''.join(match[0]), replacement)
article._content = article._content.replace(''.join(all_match_str), replacement)


def register():
Expand Down

0 comments on commit 7100e98

Please sign in to comment.