Pastery

just_table.py pages support +

  1# -*- coding: utf-8 -*-  2"""  3Table embedding plugin for Pelican  4=================================  5  6This plugin allows you to create easily table.  7  8"""  9from __future__ import unicode_literals 10 11import re 12 13JTABLE_SEPARATOR = 'JTABLE_SEPARATOR' 14JTABLE_TEMPLATE = 'JTABLE_TEMPLATE' 15DEFAULT_SEPARATOR = ',' 16 17AUTO_INCREMENT_REGEX = re.compile(r"ai ?\= ?\" ?(1) ?\"") 18TABLE_HEADER_REGEX = re.compile(r"th ?\= ?\" ?(0) ?\"") 19CAPTION_REGEX = re.compile("caption ?\= ?\"(.+?)\"") 20SEPARATOR_REGEX = re.compile("separator ?\= ?\"(.+?)\"") 21MAIN_REGEX = re.compile(r"(\[jtable(.*?)\]([\s\S]*?)\[\/jtable\])") 22 23DEFAULT_TEMPATE = """ 24<div class="justtable"> 25    <table> 26        {%- if caption %} 27        <caption> {{ caption }} </caption> 28        {%- endif %} 29        {%- if th != 0 %} 30        <thead> 31        <tr> 32            {%- if ai == 1 %} 33            <th> No. </th> 34            {%- endif %} 35            {%- for head in heads %} 36            <th>{{ head }}</th> 37            {%- endfor %} 38        </tr> 39        </thead> 40        {%- endif %} 41        <tbody> 42            {%- for body in bodies %} 43            <tr> 44                {%- if ai == 1 %} 45                <td> {{ loop.index }} </td> 46                {%- endif %} 47                {%- for entry in body %} 48                <td>{{ entry }}</td> 49                {%- endfor %} 50            </tr> 51            {%- endfor %} 52        </tbody> 53    </table> 54</div> 55""" 56 57 58def generate_table(generator): 59    from jinja2 import Template 60 61    if JTABLE_SEPARATOR in generator.settings: 62        separator = generator.settings[JTABLE_SEPARATOR] 63    else: 64        separator = DEFAULT_SEPARATOR 65 66    if JTABLE_TEMPLATE in generator.settings: 67        table_template = generator.settings[JTABLE_TEMPLATE] 68    else: 69        table_template = DEFAULT_TEMPATE 70 71    template = Template(table_template) 72 73    for article in generator.articles + generator.drafts: 74        for match in MAIN_REGEX.findall(article._content): 75            all_match_str, props, table_data = match 76            param = {"ai": 0, "th": 1, "caption": "", "sep": separator} 77 78            if AUTO_INCREMENT_REGEX.search(props): 79                param['ai'] = 1 80            if CAPTION_REGEX.search(props): 81                param['caption'] = CAPTION_REGEX.findall(props)[0] 82            if TABLE_HEADER_REGEX.search(props): 83                param["th"] = 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') 88 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:]] 93                else: 94                    bodies = [n.split(param["sep"]) for n in table_data_list] 95 96                context = generator.context.copy() 97                context.update({ 98                    'heads': heads, 99                    'bodies': bodies,100                })101                context.update(param)102103                replacement = template.render(context)104                article._content = article._content.replace(''.join(all_match_str), replacement)105106107def generate_table_pages(generator):108    from jinja2 import Template109110    if JTABLE_SEPARATOR in generator.settings:111        separator = generator.settings[JTABLE_SEPARATOR]112    else:113        separator = DEFAULT_SEPARATOR114115    if JTABLE_TEMPLATE in generator.settings:116        table_template = generator.settings[JTABLE_TEMPLATE]117    else:118        table_template = DEFAULT_TEMPATE119120    template = Template(table_template)121122    for page in generator.pages:123        for match in MAIN_REGEX.findall(page._content):124            all_match_str, props, table_data = match125            param = {"ai": 0, "th": 1, "caption": "", "sep": separator}126127            if AUTO_INCREMENT_REGEX.search(props):128                param['ai'] = 1129            if CAPTION_REGEX.search(props):130                param['caption'] = CAPTION_REGEX.findall(props)[0]131            if TABLE_HEADER_REGEX.search(props):132                param["th"] = 0133            if SEPARATOR_REGEX.search(props):134                param["sep"] = SEPARATOR_REGEX.findall(props)[0]135136            table_data_list = table_data.strip().split('\n')137138            if len(table_data_list) >= 1:139                heads = table_data_list[0].split(param["sep"]) if param['th'] else None140                if heads:141                    bodies = [n.split(param["sep"], len(heads) - 1) for n in table_data_list[1:]]142                else:143                    bodies = [n.split(param["sep"]) for n in table_data_list]144145                context = generator.context.copy()146                context.update({147                    'heads': heads,148                    'bodies': bodies,149                })150                context.update(param)151152                replacement = template.render(context)153                page._content = page._content.replace(''.join(all_match_str), replacement)154155156def register():157    """Plugin registration."""158    from pelican import signals159160    signals.article_generator_finalized.connect(generate_table)161    signals.page_generator_finalized.connect(generate_table_pages)
New paste