|
1 |
| -# -*- coding: utf-8 -*- |
2 |
| -""" |
3 |
| -Table embedding plugin for Pelican |
4 |
| -================================= |
5 |
| -
|
6 |
| -This plugin allows you to create easily table. |
7 |
| -
|
8 |
| -""" |
9 |
| -from __future__ import unicode_literals |
10 |
| - |
11 |
| -import re |
12 |
| - |
13 |
| -JTABLE_SEPARATOR = 'JTABLE_SEPARATOR' |
14 |
| -JTABLE_TEMPLATE = 'JTABLE_TEMPLATE' |
15 |
| -DEFAULT_SEPARATOR = ',' |
16 |
| - |
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\])") |
22 |
| - |
23 |
| -DEFAULT_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 |
| - |
58 |
| -def 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) |
102 |
| - |
103 |
| - replacement = template.render(context) |
104 |
| - article._content = article._content.replace(''.join(all_match_str), replacement) |
105 |
| - |
106 |
| - |
107 |
| -def register(): |
108 |
| - """Plugin registration.""" |
109 |
| - from pelican import signals |
110 |
| - |
111 |
| - signals.article_generator_finalized.connect(generate_table) |
| 1 | +"""just_table. |
| 2 | +
|
| 3 | +Table embedding plugin for Pelican |
| 4 | +================================= |
| 5 | +
|
| 6 | +This plugin allows you to create easily table. |
| 7 | +
|
| 8 | +""" |
| 9 | +import itertools |
| 10 | +import re |
| 11 | + |
| 12 | +from jinja2 import Template |
| 13 | +from pelican import signals |
| 14 | +from pelican.generators import ArticlesGenerator |
| 15 | +from pelican.generators import PagesGenerator |
| 16 | + |
| 17 | +JTABLE_SEPARATOR = "JTABLE_SEPARATOR" |
| 18 | +JTABLE_TEMPLATE = "JTABLE_TEMPLATE" |
| 19 | +DEFAULT_SEPARATOR = "," |
| 20 | + |
| 21 | +AUTO_INCREMENT_REGEX = re.compile(r"ai ?\= ?\" ?(1) ?\"") |
| 22 | +CAPTION_REGEX = re.compile('caption ?= ?"(.+?)"') |
| 23 | +SEPARATOR_REGEX = re.compile('separator ?= ?"(.+?)"') |
| 24 | +TABLE_HEADER_REGEX = re.compile(r"th ?\= ?\" ?(0) ?\"") |
| 25 | +MAIN_REGEX = re.compile(r"(\[jtable(.*?)\]([\s\S]*?)\[\/jtable\])") |
| 26 | + |
| 27 | +DEFAULT_TEMPATE = """ |
| 28 | +<div class="justtable"> |
| 29 | + <table> |
| 30 | + {%- if caption %} |
| 31 | + <caption> {{ caption }} </caption> |
| 32 | + {%- endif %} |
| 33 | + {%- if th != 0 %} |
| 34 | + <thead> |
| 35 | + <tr> |
| 36 | + {%- if ai == 1 %} |
| 37 | + <th> No. </th> |
| 38 | + {%- endif %} |
| 39 | + {%- for head in heads %} |
| 40 | + <th>{{ head }}</th> |
| 41 | + {%- endfor %} |
| 42 | + </tr> |
| 43 | + </thead> |
| 44 | + {%- endif %} |
| 45 | + <tbody> |
| 46 | + {%- for body in bodies %} |
| 47 | + <tr> |
| 48 | + {%- if ai == 1 %} |
| 49 | + <td> {{ loop.index }} </td> |
| 50 | + {%- endif %} |
| 51 | + {%- for entry in body %} |
| 52 | + <td>{{ entry }}</td> |
| 53 | + {%- endfor %} |
| 54 | + </tr> |
| 55 | + {%- endfor %} |
| 56 | + </tbody> |
| 57 | + </table> |
| 58 | +</div> |
| 59 | +""" |
| 60 | + |
| 61 | + |
| 62 | +def just_table(generator, kira_object): |
| 63 | + """Generate table.""" |
| 64 | + if JTABLE_SEPARATOR in generator.settings: |
| 65 | + separator = generator.settings[JTABLE_SEPARATOR] |
| 66 | + else: |
| 67 | + separator = DEFAULT_SEPARATOR |
| 68 | + |
| 69 | + if JTABLE_TEMPLATE in generator.settings: |
| 70 | + table_template = generator.settings[JTABLE_TEMPLATE] |
| 71 | + else: |
| 72 | + table_template = DEFAULT_TEMPATE |
| 73 | + |
| 74 | + template = Template(table_template) |
| 75 | + |
| 76 | + for match in MAIN_REGEX.findall(kira_object._content): |
| 77 | + all_match_str, props, table_data = match |
| 78 | + param = {"ai": 0, "th": 1, "caption": "", "sep": separator} |
| 79 | + |
| 80 | + if AUTO_INCREMENT_REGEX.search(props): |
| 81 | + param["ai"] = 1 |
| 82 | + if CAPTION_REGEX.search(props): |
| 83 | + param["caption"] = CAPTION_REGEX.findall(props)[0] |
| 84 | + if TABLE_HEADER_REGEX.search(props): |
| 85 | + param["th"] = 0 |
| 86 | + if SEPARATOR_REGEX.search(props): |
| 87 | + param["sep"] = SEPARATOR_REGEX.findall(props)[0] |
| 88 | + |
| 89 | + table_data_list = table_data.strip().split("\n") |
| 90 | + |
| 91 | + if len(table_data_list) >= 1: |
| 92 | + heads = table_data_list[0].split( |
| 93 | + param["sep"]) if param["th"] else None |
| 94 | + if heads: |
| 95 | + bodies = [ |
| 96 | + n.split(param["sep"], len(heads) - 1) |
| 97 | + for n in table_data_list[1:] |
| 98 | + ] |
| 99 | + else: |
| 100 | + bodies = [n.split(param["sep"]) for n in table_data_list] |
| 101 | + |
| 102 | + context = generator.context.copy() |
| 103 | + context.update({"heads": heads, "bodies": bodies}) |
| 104 | + context.update(param) |
| 105 | + |
| 106 | + replacement = template.render(context) |
| 107 | + kira_object._content = kira_object._content.replace( |
| 108 | + "".join(all_match_str), replacement |
| 109 | + ) |
| 110 | + |
| 111 | + |
| 112 | +def run_plugin(generators): |
| 113 | + """Run generators on both pages and articles.""" |
| 114 | + for generator in generators: |
| 115 | + # [INFO] Set plugin for articles and pages: |
| 116 | + # https://github.com/oumpy/hp_management/blob/928b67170a40259599d636cda2b223b8c4350340/myplugins/skiptags.py |
| 117 | + if isinstance(generator, ArticlesGenerator): |
| 118 | + # [INFO] Articles types: |
| 119 | + # https://github.com/getpelican/pelican/blob/d43b786b300358e8a4cbae4afc4052199a7af762/pelican/generators.py#L288-L296 |
| 120 | + for article in itertools.chain( |
| 121 | + generator.articles, generator.translations, |
| 122 | + generator.drafts, generator.drafts_translations): |
| 123 | + just_table(generator, article) |
| 124 | + elif isinstance(generator, PagesGenerator): |
| 125 | + # [INFO] Pages types: |
| 126 | + # https://github.com/getpelican/pelican/blob/d43b786b300358e8a4cbae4afc4052199a7af762/pelican/generators.py#L702-L707 |
| 127 | + for page in itertools.chain( |
| 128 | + generator.pages, generator.translations, generator.hidden_pages, |
| 129 | + generator.hidden_translations, generator.draft_pages, generator.draft_translations): |
| 130 | + just_table(generator, page) |
| 131 | + |
| 132 | + |
| 133 | +def register(): |
| 134 | + """Plugin registration.""" |
| 135 | + signals.all_generators_finalized.connect(run_plugin) |
0 commit comments