mkdocs-paralex-plugin 0.1.0b0__py2.py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mkdocs_paralex_plugin/__init__.py +1 -0
- mkdocs_paralex_plugin/css/paralex_styles.css +133 -0
- mkdocs_paralex_plugin/paralex.png +0 -0
- mkdocs_paralex_plugin/plugin.py +354 -0
- mkdocs_paralex_plugin/templates/paradigm.md +63 -0
- mkdocs_paralex_plugin/templates/table.md +104 -0
- mkdocs_paralex_plugin-0.1.0b0.dist-info/LICENSE +674 -0
- mkdocs_paralex_plugin-0.1.0b0.dist-info/METADATA +92 -0
- mkdocs_paralex_plugin-0.1.0b0.dist-info/RECORD +12 -0
- mkdocs_paralex_plugin-0.1.0b0.dist-info/WHEEL +6 -0
- mkdocs_paralex_plugin-0.1.0b0.dist-info/entry_points.txt +2 -0
- mkdocs_paralex_plugin-0.1.0b0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
VERSION="0.1.0b"
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
|
|
2
|
+
tr td.table_id {
|
|
3
|
+
font-family: var(--md-code-font-family);
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
tr td.lexeme, tr td.cell {
|
|
8
|
+
font-variant: small-caps;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
tr td.phon_form .form:before, tr td.analysed_phon_form .form:before,
|
|
13
|
+
tr td.phon_form .form:after, tr td.analysed_phon_form .form:after {
|
|
14
|
+
content: "/"
|
|
15
|
+
}
|
|
16
|
+
tr td .defective {
|
|
17
|
+
font-style: italic;
|
|
18
|
+
color: var(--md-accent-fg-color);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
tr td span.paralex_tag {
|
|
22
|
+
border-radius: .8em;
|
|
23
|
+
padding: .5em;
|
|
24
|
+
line-height:2.5em;
|
|
25
|
+
font-size:90%;
|
|
26
|
+
background-color: var(--md-primary-fg-color);
|
|
27
|
+
color: var(--md-primary-bg-color);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
tr td span.paralex_tag a {
|
|
31
|
+
color: var(--md-primary-bg-color);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
.paradigm_table caption {
|
|
35
|
+
font-size: 110%;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
.paradigm_table {
|
|
39
|
+
border: .05rem solid var(--md-typeset-table-color);
|
|
40
|
+
border-radius: .1rem;
|
|
41
|
+
font-size: 80%;
|
|
42
|
+
margin: 10px;
|
|
43
|
+
display: inline-block;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
.paradigm_table th,
|
|
47
|
+
.paradigm_table td {
|
|
48
|
+
padding: 2px 5px;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
/** Indicate that rows (lexemes) are clickable **/
|
|
53
|
+
#lexemes.dataTable > tbody > tr > td.lexeme_id {
|
|
54
|
+
cursor: pointer;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
#lexemes.dataTable > tbody > tr > td.lexeme_id::before {
|
|
58
|
+
content: "+ ";
|
|
59
|
+
color: var(--md-primary-fg-color);
|
|
60
|
+
font-weight: bolder;
|
|
61
|
+
font-size: 110%;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
#lexemes.dataTable > tbody > tr > td.lexeme_id::before {
|
|
65
|
+
content: "+ ";
|
|
66
|
+
color: var(--md-primary-fg-color);
|
|
67
|
+
font-weight: bolder;
|
|
68
|
+
font-size: 110%;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
#lexemes.dataTable > tbody > tr.selected > td.lexeme_id::before {
|
|
72
|
+
color: var(--dt-row-selected-text);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
.dataTables_wrapper {
|
|
76
|
+
overflow: scroll;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
.md-typeset .paradigm_table caption,
|
|
80
|
+
.paradigm_table th {
|
|
81
|
+
font-variant: small-caps;
|
|
82
|
+
font-weight: bold;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
*[title] {
|
|
87
|
+
cursor: help;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
.md-typeset ul.form_sets {
|
|
91
|
+
margin: 0;
|
|
92
|
+
list-style-type: none;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
.md-typeset ul li.form_set {
|
|
96
|
+
margin: 0;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
.formtype ~ .formtype {
|
|
101
|
+
padding-left: .4em;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/* Styling search bar */
|
|
105
|
+
|
|
106
|
+
.dataTables_wrapper .dataTables_filter {
|
|
107
|
+
border: 2px solid rgba(0, 0, 0, 0.15);
|
|
108
|
+
border-radius: 3px;
|
|
109
|
+
padding: 5px;
|
|
110
|
+
background-color: transparent;
|
|
111
|
+
margin-left: 3px;
|
|
112
|
+
box-shadow: 0 1px 5px 3px var(--md-accent-fg-color--transparent);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
.dataTables_wrapper .dataTables_filter:focus {
|
|
117
|
+
border: 2px solid var(--md-primary-fg-color--light);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
.dataTables_wrapper .dataTables_filter input {
|
|
122
|
+
border: 0 !important;;
|
|
123
|
+
padding: 0 !important;
|
|
124
|
+
background-color: transparent;
|
|
125
|
+
margin-left: 10px !important;
|
|
126
|
+
width: 400px;
|
|
127
|
+
font-size: 100%;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
.dataTables_filter .fa.fa-search {
|
|
131
|
+
color: var(--md-accent-fg-color);
|
|
132
|
+
}
|
|
133
|
+
|
|
Binary file
|
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
import frictionless
|
|
2
|
+
from mkdocs.plugins import BasePlugin
|
|
3
|
+
from mkdocs.config.base import Config
|
|
4
|
+
from mkdocs.config import base, config_options as c
|
|
5
|
+
from mkdocs.structure.files import File
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from tempfile import TemporaryDirectory
|
|
8
|
+
import pandas as pd
|
|
9
|
+
from jinja2 import Environment, PackageLoader
|
|
10
|
+
import json
|
|
11
|
+
import re
|
|
12
|
+
import unidecode
|
|
13
|
+
from collections import defaultdict
|
|
14
|
+
from itertools import groupby
|
|
15
|
+
from tqdm import tqdm
|
|
16
|
+
from paralex import to_markdown
|
|
17
|
+
|
|
18
|
+
env = Environment(loader=PackageLoader("mkdocs_paralex_plugin"))
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class ParalexConfig(Config):
|
|
22
|
+
paralex_package_path = c.Optional(c.File(exists=True))
|
|
23
|
+
paradigm_pages = c.Type(bool, default=True)
|
|
24
|
+
layout_rows = c.ListOfItems(c.Type(str), default=[])
|
|
25
|
+
layout_columns = c.ListOfItems(c.Type(str), default=[])
|
|
26
|
+
layout_tables = c.ListOfItems(c.Type(str), default=[])
|
|
27
|
+
frequency_sample = c.Type(bool, default=True)
|
|
28
|
+
sample_size = c.Integer(default=None)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class ParalexSite(BasePlugin[ParalexConfig]):
|
|
32
|
+
|
|
33
|
+
def on_config(self, config):
|
|
34
|
+
"""
|
|
35
|
+
Note:
|
|
36
|
+
the config argument contains mkdocs global config
|
|
37
|
+
self.config contains the config for this plugin
|
|
38
|
+
|
|
39
|
+
"""
|
|
40
|
+
self.dir = Path(config.get("config_file_path")).parent
|
|
41
|
+
try:
|
|
42
|
+
self.package_path = self.config.get("paralex_package_path") or next(Path(self.dir).glob("*.package.json"))
|
|
43
|
+
except StopIteration:
|
|
44
|
+
raise ValueError("Can not find your .package.json file."
|
|
45
|
+
"Please either put it in the same directory as the config file, "
|
|
46
|
+
"or specify a path with the config option paralex_package_path.")
|
|
47
|
+
self.layout_rows = self.config.get("layout_rows")
|
|
48
|
+
self.layout_columns = self.config.get("layout_columns")
|
|
49
|
+
self.layout_tables = self.config.get("layout_tables")
|
|
50
|
+
self.frequency_sample = self.config.get("frequency_sample")
|
|
51
|
+
self.sample_size = self.config.get("sample_size")
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
assert (self.layout_rows or not self.layout_columns) and \
|
|
55
|
+
(self.layout_rows or not self.layout_tables)
|
|
56
|
+
self.paradigm_pages = self.config.get("paradigm_pages")
|
|
57
|
+
self.cleaners = []
|
|
58
|
+
|
|
59
|
+
with open(self.package_path, 'r', encoding="utf-8") as f:
|
|
60
|
+
self.package = json.load(f)
|
|
61
|
+
|
|
62
|
+
self.tmp_dir = TemporaryDirectory("data_tmp_")
|
|
63
|
+
self.cleaners.append(self.tmp_dir.cleanup)
|
|
64
|
+
|
|
65
|
+
# Add any css / js / etc.
|
|
66
|
+
config["extra_css"].insert(0,
|
|
67
|
+
"https://cdn.datatables.net/1.13.4/css/jquery.dataTables.min.css") # So that it has lower priority than user files.
|
|
68
|
+
config["extra_css"].insert(0, "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css")
|
|
69
|
+
config["extra_css"].insert(0, "css/paralex_styles.css") # So that it has lower priority than user files.
|
|
70
|
+
|
|
71
|
+
if not config.get("copyright", None):
|
|
72
|
+
footer = ""
|
|
73
|
+
if "contributors" in self.package:
|
|
74
|
+
footer += "by " + ", ".join([auth['title'] for auth in self.package["contributors"]]) + "."
|
|
75
|
+
if "licenses" in self.package:
|
|
76
|
+
footer += f" licenses: " + ", ".join(
|
|
77
|
+
[f"<a href='{ls['path']}'>{ls['name']}</a>" for ls in self.package["licenses"]]) + ". "
|
|
78
|
+
config["copyright"] = footer
|
|
79
|
+
config[
|
|
80
|
+
"copyright"] += "This lexicon follows the <a href='https://www.paralex-standard.org/'>paralex</a> standard."
|
|
81
|
+
if "theme" in config and "logo" not in config["theme"]:
|
|
82
|
+
config["theme"]["logo"] = "paralex.png"
|
|
83
|
+
self.add_paralex_logo = True
|
|
84
|
+
|
|
85
|
+
def on_pre_build(self, *args, config):
|
|
86
|
+
# Simlink readme to index if there is no index in the docs directory
|
|
87
|
+
doc_dir = Path(config.get("docs_dir"))
|
|
88
|
+
readmes = list((self.dir.glob("[Rr][eE][aA][dD][mM][eE].md")))
|
|
89
|
+
readme = readmes[0] if readmes else None
|
|
90
|
+
index = (doc_dir / "index.md")
|
|
91
|
+
if not index.exists() and readme is not None:
|
|
92
|
+
index.symlink_to(readme)
|
|
93
|
+
self.cleaners.append(index.unlink)
|
|
94
|
+
|
|
95
|
+
def on_files(self, files, *, config):
|
|
96
|
+
here = Path(__file__).parent
|
|
97
|
+
files.append(File("paralex_styles.css", str(here / "css"), config['site_dir'] + "/css/", False))
|
|
98
|
+
tables = {"forms": None,
|
|
99
|
+
"lexemes": None,
|
|
100
|
+
"features-values": None}
|
|
101
|
+
|
|
102
|
+
if self.add_paralex_logo:
|
|
103
|
+
dest = Path(self.tmp_dir.name) / "paralex.png"
|
|
104
|
+
src = Path(__file__).parent / "paralex.png"
|
|
105
|
+
dest.write_bytes(src.read_bytes())
|
|
106
|
+
files.append(File(dest.name, self.tmp_dir.name, config['site_dir'], False))
|
|
107
|
+
|
|
108
|
+
for resource in self.package["resources"]:
|
|
109
|
+
new_files = csv_to_html_table(resource, self.tmp_dir.name, with_paradigms=self.paradigm_pages)
|
|
110
|
+
name = resource["name"]
|
|
111
|
+
if name in tables:
|
|
112
|
+
tables[name] = resource
|
|
113
|
+
for filename in new_files:
|
|
114
|
+
files.append(File(filename, self.tmp_dir.name, config['site_dir'], False))
|
|
115
|
+
|
|
116
|
+
package_out = Path(self.tmp_dir.name) / "metadata.md"
|
|
117
|
+
to_markdown(frictionless.Package(self.package_path), package_out, title="Metadata")
|
|
118
|
+
files.append(File("metadata.md", self.tmp_dir.name, config['site_dir'], False))
|
|
119
|
+
|
|
120
|
+
if self.paradigm_pages:
|
|
121
|
+
if tables["features-values"] is None:
|
|
122
|
+
raise ValueError("Can not generate paradigms tables if there is no features table.")
|
|
123
|
+
|
|
124
|
+
if tables["lexemes"] is None:
|
|
125
|
+
raise ValueError("Can not generate paradigm tables if there is no lexemes table.")
|
|
126
|
+
|
|
127
|
+
(Path(self.tmp_dir.name) / "details").mkdir(exist_ok=True)
|
|
128
|
+
|
|
129
|
+
forms_df = read_pandas(tables["forms"]["path"])
|
|
130
|
+
features = read_pandas(tables["features-values"]["path"], index_col=0)
|
|
131
|
+
lexemes = read_pandas(tables["lexemes"]["path"])
|
|
132
|
+
|
|
133
|
+
# Restrict to a sample of lexemes
|
|
134
|
+
if self.sample_size is not None:
|
|
135
|
+
if self.frequency_sample and "frequency" in lexemes.columns:
|
|
136
|
+
lexemes = lexemes.sort_values("frequency", ascending=False)[:self.sample_size]
|
|
137
|
+
else:
|
|
138
|
+
lexemes = lexemes.sample(self.sample_size)
|
|
139
|
+
sampled = set(lexemes.lexeme_id)
|
|
140
|
+
forms_df = forms_df[forms_df.lexeme.isin(sampled)]
|
|
141
|
+
|
|
142
|
+
# Separate cells features according to layout
|
|
143
|
+
if not self.layout_rows:
|
|
144
|
+
self.layout_rows = features.index.tolist()
|
|
145
|
+
|
|
146
|
+
dim_to_f = defaultdict(list)
|
|
147
|
+
for i, f in features.feature.items():
|
|
148
|
+
dim_to_f[f].append(i)
|
|
149
|
+
feature_grouper = dict(**{v: "tables" for f in self.layout_tables for v in dim_to_f[f]},
|
|
150
|
+
**{v: "rows" for f in self.layout_rows for v in dim_to_f[f]},
|
|
151
|
+
**{v: "cols" for f in self.layout_columns for v in dim_to_f[f]},
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
used_dimensions = set(dim_to_f)
|
|
155
|
+
known_dimensions = set(self.layout_rows + self.layout_tables + self.layout_columns)
|
|
156
|
+
undef_dimensions = used_dimensions - known_dimensions
|
|
157
|
+
if undef_dimensions:
|
|
158
|
+
raise ValueError("Undefined features found. "
|
|
159
|
+
"Edit your config file to add "
|
|
160
|
+
"these to tables, rows or cols: {}".format(", ".join(undef_dimensions)))
|
|
161
|
+
|
|
162
|
+
cells = {cell: parse_cell(cell, feature_grouper) for cell in forms_df.cell.unique()}
|
|
163
|
+
forms_df.loc[:, ["tables", "rows", "cols"]] = forms_df.cell.apply(lambda x: cells[x])
|
|
164
|
+
|
|
165
|
+
if "canonical_order" in features.columns:
|
|
166
|
+
cell_sorter = {elt: cell_order(elt, features.canonical_order)
|
|
167
|
+
for c in cells for _, elt in cells[c].fillna("").items()}
|
|
168
|
+
else:
|
|
169
|
+
# TODO: default to something sensible here...
|
|
170
|
+
cell_sorter = {elt: 0 for c in cells for _, elt in cells[c].fillna("").items()}
|
|
171
|
+
|
|
172
|
+
with tqdm(total=lexemes.shape[0]) as pbar:
|
|
173
|
+
p_files = forms_df.groupby("lexeme") \
|
|
174
|
+
.apply(paradigm_to_html,
|
|
175
|
+
self.tmp_dir.name,
|
|
176
|
+
cell_sorter,
|
|
177
|
+
lexemes.set_index("lexeme_id"),
|
|
178
|
+
pbar)
|
|
179
|
+
for f in p_files:
|
|
180
|
+
files.append(File(f, self.tmp_dir.name, config['site_dir'], False))
|
|
181
|
+
|
|
182
|
+
return files
|
|
183
|
+
|
|
184
|
+
def on_post_build(self, *, config):
|
|
185
|
+
for clean_action in self.cleaners:
|
|
186
|
+
clean_action()
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def paradigm_to_html(paradigm, out_dir, sorter, lexemes, pbar):
|
|
190
|
+
table_dict = paradigm_to_json(paradigm, sorter)
|
|
191
|
+
lexeme = paradigm.name
|
|
192
|
+
# Load jinja template
|
|
193
|
+
template = env.get_template("paradigm.md")
|
|
194
|
+
|
|
195
|
+
table = template.render(tables=table_dict,
|
|
196
|
+
lexeme=lexeme,
|
|
197
|
+
lexeme_info=lexemes.loc[lexeme, :].to_dict()
|
|
198
|
+
)
|
|
199
|
+
filename = f"details/{slug(lexeme)}.txt"
|
|
200
|
+
table_out = Path(out_dir) / filename
|
|
201
|
+
with table_out.open("w", encoding="utf-8") as f:
|
|
202
|
+
f.write(table)
|
|
203
|
+
pbar.update(1)
|
|
204
|
+
return filename
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def paradigm_to_json(paradigm, sorter):
|
|
208
|
+
"""Go from long form to a json dict
|
|
209
|
+
|
|
210
|
+
tables: [ # list of tables (dict)
|
|
211
|
+
{ # A table
|
|
212
|
+
headers: [] # list of headers (str)
|
|
213
|
+
indexes: [] # list of indexes (str)
|
|
214
|
+
rows: [ # list of content rows
|
|
215
|
+
[ # one row is a list
|
|
216
|
+
{ # One original form entry, multiple can be at the same coordinates
|
|
217
|
+
phon_form: xyz # col/vals from original df
|
|
218
|
+
}
|
|
219
|
+
]
|
|
220
|
+
]
|
|
221
|
+
},
|
|
222
|
+
|
|
223
|
+
{} # another table...
|
|
224
|
+
]
|
|
225
|
+
"""
|
|
226
|
+
|
|
227
|
+
def one_paradigm_to_json(table):
|
|
228
|
+
table_data = {"headers": table.cols.unique().tolist(),
|
|
229
|
+
"indexes": [],
|
|
230
|
+
"rows": [],
|
|
231
|
+
"name": table.name
|
|
232
|
+
}
|
|
233
|
+
col_to_i = {c: i for i, c in enumerate(table_data["headers"])}
|
|
234
|
+
row_list = []
|
|
235
|
+
for idx, data in table.groupby("rows"): # groupby doesn't respect row order !
|
|
236
|
+
idx_hr = []
|
|
237
|
+
if idx:
|
|
238
|
+
idx_hr.append(idx)
|
|
239
|
+
r = [None for _ in col_to_i]
|
|
240
|
+
for col, entries in data.groupby("cols"):
|
|
241
|
+
r[col_to_i[col]] = entries.to_dict('records')
|
|
242
|
+
row_list.append((idx, ".".join(idx_hr), r))
|
|
243
|
+
|
|
244
|
+
# Sort the row order
|
|
245
|
+
row_list = sorted(row_list, key=lambda x: sorter[x[0]])
|
|
246
|
+
table_data["indexes"] = [x[1] for x in row_list]
|
|
247
|
+
table_data["rows"] = [x[2] for x in row_list]
|
|
248
|
+
return table_data
|
|
249
|
+
|
|
250
|
+
ts = paradigm.sort_values(by=["tables", "rows", "cols"],
|
|
251
|
+
key=lambda x: x.map(sorter)) \
|
|
252
|
+
.fillna("") \
|
|
253
|
+
.groupby("tables").apply(one_paradigm_to_json).to_list()
|
|
254
|
+
ts = sorted(ts, key=lambda t: sorter[t["name"]])
|
|
255
|
+
return ts
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
def cell_order(cell, order):
|
|
259
|
+
return tuple(order.get(f, default=0) for f in cell.split("."))
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
def parse_cell(cell, feature_sorter):
|
|
263
|
+
def sort_func(val):
|
|
264
|
+
r = feature_sorter.get(val)
|
|
265
|
+
if r is None:
|
|
266
|
+
print("Err at:",val)
|
|
267
|
+
print("sorter is:", feature_sorter)
|
|
268
|
+
return float("inf")
|
|
269
|
+
return r
|
|
270
|
+
features = sorted(cell.split("."), key=sort_func)
|
|
271
|
+
groups = groupby(features, sort_func)
|
|
272
|
+
return pd.Series({k: ".".join(g) for k, g in groups}, index=["tables", "rows", "cols"])
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
def slug(string):
|
|
276
|
+
slug = unidecode.unidecode(str(string))
|
|
277
|
+
return re.sub(r'[\W]+', '-', slug)
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
def read_pandas(path, **kwargs):
|
|
281
|
+
data = pd.read_csv(path,
|
|
282
|
+
na_values=['', 'NaN'],
|
|
283
|
+
keep_default_na=False,
|
|
284
|
+
**kwargs).fillna("")
|
|
285
|
+
return data
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
def get_id(name):
|
|
289
|
+
return "id_" + slug(name)
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
def table_to_json(resource, infos=None, with_paradigms=False):
|
|
293
|
+
## If paradigm tables, add links to tables from lexeme...
|
|
294
|
+
df = read_pandas(resource["path"])
|
|
295
|
+
table_dict = {}
|
|
296
|
+
fields = {f["name"]: f for f in resource["schema"]["fields"]}
|
|
297
|
+
pkey = set(resource["schema"].get("primaryKey", {})).pop()
|
|
298
|
+
|
|
299
|
+
# Add styling classes
|
|
300
|
+
for f in fields:
|
|
301
|
+
name = fields[f]["name"]
|
|
302
|
+
fields[f]["classes"] = [slug(name)]
|
|
303
|
+
if name == pkey:
|
|
304
|
+
fields[f]["classes"].append("table_id")
|
|
305
|
+
|
|
306
|
+
# prepare foreign key url
|
|
307
|
+
rels = {}
|
|
308
|
+
for relation in resource["schema"].get("foreignKeys", []):
|
|
309
|
+
fs = relation["fields"][0]
|
|
310
|
+
table = relation["reference"]["resource"]
|
|
311
|
+
id_col = relation["reference"]["fields"][0]
|
|
312
|
+
if id_col.endswith("_id"):
|
|
313
|
+
rels[fs] = f"{table}.html#" + "{}"
|
|
314
|
+
|
|
315
|
+
table_dict["columns"] = [fields.get(col, {"name": col}) for col in df.columns]
|
|
316
|
+
|
|
317
|
+
# Prepare rows
|
|
318
|
+
table_dict["rows"] = []
|
|
319
|
+
for id, row in df.iterrows():
|
|
320
|
+
row_data = {col: cell for col, cell in zip(df.columns, row)}
|
|
321
|
+
table_dict["rows"].append(row_data)
|
|
322
|
+
if pkey:
|
|
323
|
+
row_data["DT_RowId"] = get_id(row_data[pkey])
|
|
324
|
+
for col, url in rels.items():
|
|
325
|
+
row_data[col] = f"<a href={url.format(get_id(row_data[col]))}>{row_data[col]}</a>"
|
|
326
|
+
|
|
327
|
+
table_dict["infos"] = infos
|
|
328
|
+
|
|
329
|
+
if resource["name"] == "lexemes":
|
|
330
|
+
for row in table_dict["rows"]:
|
|
331
|
+
row["details"] = "details/" + slug(row["lexeme_id"]) + ".txt"
|
|
332
|
+
return table_dict
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
def csv_to_html_table(resource, out_dir, with_paradigms):
|
|
336
|
+
files = []
|
|
337
|
+
template = env.get_template("table.md")
|
|
338
|
+
|
|
339
|
+
table_data = table_to_json(resource, with_paradigms=with_paradigms)
|
|
340
|
+
json_out = Path(out_dir) / (resource["name"] + ".json")
|
|
341
|
+
with json_out.open("w", encoding="utf-8") as f:
|
|
342
|
+
json.dump({"data": table_data["rows"]}, f)
|
|
343
|
+
files.append(json_out.name)
|
|
344
|
+
|
|
345
|
+
table = template.render(columns=table_data["columns"],
|
|
346
|
+
current_page=resource["name"],
|
|
347
|
+
json_src=json_out.name
|
|
348
|
+
)
|
|
349
|
+
table_out = Path(out_dir) / (resource["name"] + ".md")
|
|
350
|
+
with table_out.open("w", encoding="utf-8") as f:
|
|
351
|
+
f.write(table)
|
|
352
|
+
files.append(table_out.name)
|
|
353
|
+
|
|
354
|
+
return files
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{% for table in tables %}
|
|
2
|
+
<table class="paradigm_table">
|
|
3
|
+
<thead>
|
|
4
|
+
<caption >
|
|
5
|
+
{{ table["name"] }}
|
|
6
|
+
</caption>
|
|
7
|
+
<tr>
|
|
8
|
+
<th></th>
|
|
9
|
+
{% for col in table["headers"] %}
|
|
10
|
+
<th>{{ col }}</th>
|
|
11
|
+
{% endfor %}
|
|
12
|
+
</tr>
|
|
13
|
+
</thead>
|
|
14
|
+
<tbody>
|
|
15
|
+
{% for row in table["rows"] %}
|
|
16
|
+
<tr>
|
|
17
|
+
<th>{{ table["indexes"][loop.index-1] }}</th>
|
|
18
|
+
{% for data in row %}
|
|
19
|
+
<td>
|
|
20
|
+
{% if data is not none %}
|
|
21
|
+
<ul class='form_sets list-unstyled'>
|
|
22
|
+
{% for form_group in data %}
|
|
23
|
+
<li class="form_set">
|
|
24
|
+
{% if "orth_form" in form_group %}
|
|
25
|
+
{% if form_group["orth_form"] == "#DEF#" %}
|
|
26
|
+
<span class="formtype orth_form defective">defective</span>
|
|
27
|
+
{% else %}
|
|
28
|
+
<span class="formtype orth_form">{{ form_group["orth_form"] }}</span>
|
|
29
|
+
{% endif %}
|
|
30
|
+
{% endif %}
|
|
31
|
+
{% if "segmented_orth_form" in form_group %}
|
|
32
|
+
{% if form_group["segmented_orth_form"] == "#DEF#" %}
|
|
33
|
+
<span class="formtype segmented_orth_form defective">defective</span>
|
|
34
|
+
{% else %}
|
|
35
|
+
<span class="formtype segmented_orth_form">{{ form_group["segmented_orth_form"] }}</span>
|
|
36
|
+
{% endif %}
|
|
37
|
+
{% endif %}
|
|
38
|
+
{% if "phon_form" in form_group %}
|
|
39
|
+
{% if form_group["phon_form"] == "#DEF#" %}
|
|
40
|
+
<span class="formtype phon_form defective">defective</span>
|
|
41
|
+
{% else %}
|
|
42
|
+
<span class="formtype phon_form">/{{ form_group["phon_form"] | replace(" ","") }}/</span>
|
|
43
|
+
{% endif %}
|
|
44
|
+
{% endif %}
|
|
45
|
+
{% if "segmented_phon_form" in form_group %}
|
|
46
|
+
{% if form_group["segmented_phon_form"] == "#DEF#" %}
|
|
47
|
+
<span class="formtype segmented_phon_form defective">defective</span>
|
|
48
|
+
{% else %}
|
|
49
|
+
<span class="formtype segmented_phon_form">/{{ form_group["segmented_phon_form"] | replace(" ","") }}/</span>
|
|
50
|
+
{% endif %}
|
|
51
|
+
{% endif %}
|
|
52
|
+
</li>
|
|
53
|
+
{% endfor %}
|
|
54
|
+
</ul>
|
|
55
|
+
{% endif %}
|
|
56
|
+
</td>
|
|
57
|
+
{% endfor %}
|
|
58
|
+
</tr>
|
|
59
|
+
|
|
60
|
+
{% endfor %}
|
|
61
|
+
</tbody>
|
|
62
|
+
</table>
|
|
63
|
+
{% endfor %}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
---
|
|
2
|
+
hide:
|
|
3
|
+
- navigation
|
|
4
|
+
- toc
|
|
5
|
+
- path
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
<table id="{{current_page}}" class='dataTable table table-hover display {{current_page}}'>
|
|
9
|
+
<thead>
|
|
10
|
+
<tr>
|
|
11
|
+
{% for column in columns %}
|
|
12
|
+
<th class='table-col-header'><div title="{{ column["description"] }}"> {{ column["name"] }} </div></th>
|
|
13
|
+
{% endfor %}
|
|
14
|
+
</thead>
|
|
15
|
+
</table>
|
|
16
|
+
|
|
17
|
+
{% block datatables %}
|
|
18
|
+
<script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.js" crossorigin="anonymous"></script>
|
|
19
|
+
<script type="text/javascript" src="https://cdn.datatables.net/1.13.4/js/jquery.dataTables.min.js"></script>
|
|
20
|
+
<script type="text/javascript" src="https://cdn.datatables.net/plug-ins/1.13.4/api/row().show().js"></script>
|
|
21
|
+
<script>
|
|
22
|
+
|
|
23
|
+
var details = details ? JSON.parse(details) : {};
|
|
24
|
+
|
|
25
|
+
function select_row(table) {
|
|
26
|
+
var id = "#" + location.hash.slice(1);
|
|
27
|
+
if (id != "#"){
|
|
28
|
+
let row = table.row(id);
|
|
29
|
+
row.show().draw(false);
|
|
30
|
+
$(id).toggleClass('selected');
|
|
31
|
+
row.scrollTo();
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
$(document).ready(function() {
|
|
36
|
+
var table = $('#{{current_page}}').DataTable({
|
|
37
|
+
ajax: "{{json_src}}",
|
|
38
|
+
paging: true,
|
|
39
|
+
deferRender: true,
|
|
40
|
+
language: {
|
|
41
|
+
search: ""
|
|
42
|
+
},
|
|
43
|
+
pageLength: 10,
|
|
44
|
+
search: {
|
|
45
|
+
"regex": false,
|
|
46
|
+
},
|
|
47
|
+
orderCellsTop: true,
|
|
48
|
+
columns: [
|
|
49
|
+
{% for c in columns %}
|
|
50
|
+
{ "defaultContent": "",
|
|
51
|
+
"data":"{{c["name"]}}",
|
|
52
|
+
"className":"{{ c['classes']|join(' ') }}",
|
|
53
|
+
{% if ('url' in c) and c['url'] is not none %}
|
|
54
|
+
"render": function ( data, type, row, meta ) {return '<a href="{{c['url']}}' + data + '">' + data + '</a>';}
|
|
55
|
+
{% elif c["name"].endswith('_tag') %}
|
|
56
|
+
"render": function ( data, type, row, meta ) {
|
|
57
|
+
if (data === '') {
|
|
58
|
+
return data;
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
return data.split("|").map(tag => '<span class="paralex_tag"><a href="tags.html#id_' + tag + '">' + tag + '</a></span>').join(" ");
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
{% elif "form" in c["name"] %}
|
|
65
|
+
"render": function ( data, type, row, meta ) {
|
|
66
|
+
if (data === '#DEF#') {
|
|
67
|
+
return '<span class="defective">defective</span>' ;
|
|
68
|
+
} else {
|
|
69
|
+
return '<span class="form">'+data+'</span>'
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
{% endif %}
|
|
73
|
+
},
|
|
74
|
+
{% endfor %}
|
|
75
|
+
],
|
|
76
|
+
initComplete: function() { select_row(this.api())}
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
$('#lexemes tbody').on('click', 'td.lexeme_id', function (event) {
|
|
80
|
+
var tr = $(this).closest('tr');
|
|
81
|
+
var row = table.row( tr );
|
|
82
|
+
if ( row.child.isShown() ) {
|
|
83
|
+
row.child.hide();
|
|
84
|
+
tr.removeClass('shown');
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
var xhttp = new XMLHttpRequest();
|
|
88
|
+
xhttp.onreadystatechange = function() {
|
|
89
|
+
if (this.readyState == 4 && this.status == 200) {
|
|
90
|
+
row.child(this.responseText, "detail-child").show();
|
|
91
|
+
tr.addClass('shown');
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
xhttp.open("GET", row.data()["details"], true);
|
|
95
|
+
xhttp.send();
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
$('[type=search]').each(function () {
|
|
99
|
+
$(this).attr("placeholder", "Search...");
|
|
100
|
+
$(this).before('<span class="fa fa-search"></span>');
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
</script>
|
|
104
|
+
{% endblock %}
|