4 title => 'words cheat sheet admin',
11 grid: auto-flow / min-content repeat(10, auto);
16 border-spacing: 0 2px;
25 form > ul li > label {
29 form > ul li > label + * {
35 box-sizing: border-box;
38 font-family: monospace;
41 padding: .3rem .2rem; /* TODO: input */
58 background: rgba(0, 0, 0, .8);
59 border: 1px solid #CCC;
68 margin: 0 -1ex; /* inner gap */
78 -margin-left: 1em; /* flex gap */
84 display: inline-block;
89 document.addEventListener('DOMContentLoaded', () => {
90 var wpinput = document.getElementById('wptitle');
91 var wpbutton = wpinput.parentNode.appendChild(document.createElement('button'));
92 wpbutton.type = 'button';
93 wpbutton.append('Copy');
94 wpbutton.onclick = () => {
95 let wptitle = wpinput.value || document.getElementById('form').value;
96 let wplang = document.getElementById('lang').value.substr(0, 2); // crude iso-639-3→2
97 let wpapi = `https://${wplang}.wikipedia.org/w/api.php`;
98 let wppage = wpapi+'?action=parse&format=json&origin=*&prop=text&page='+wptitle;
99 fetch(wppage).then(res => res.json()).then(json => {
100 if (json.error) throw `error returned: ${json.error.info}`;
101 wpinput.value = json.parse.title;
102 let imginput = document.getElementById('source');
103 if (imginput.value) return;
104 let wpimages = json.parse.text['*'].match(/<img\s[^>]+>/g);
105 let wpselect = wpinput.parentNode.appendChild(document.createElement('ul'));
106 wpselect.className = 'popup';
107 wpimages.forEach(img => {
108 let selectitem = wpselect.appendChild(document.createElement('li'));
109 selectitem.insertAdjacentHTML('beforeend', img);
110 selectitem.onclick = e => {
111 let imgsrc = e.target.src
112 .replace(/^(?=\/\/)/, 'https:')
113 .replace(/\/thumb(\/.+)\/[^\/]+$/, '$1');
114 imginput.value = imgsrc;
119 }).catch(error => alert(error));
127 use List::Util qw( pairs pairkeys );
131 'DBI:Pg:dbname=sheet;host=localhost', 'sheetadmin', 'fairuse',
132 ) or die "database not configured\n";
133 require DBIx::Simple;
134 DBIx::Simple->new(@dbinfo[0..2], {
138 } or Abort('Database error', 501, $@);
143 form => 'Translation',
145 wptitle => 'Wikipedia',
146 source => 'Image URL',
147 thumb => 'Convert options',
149 cover => undef, # included with prio
152 my @prioenum = qw( essential basic common distinctive rare invisible );
153 my ($find) = map {{id => $_}} $fields{id} || $Request || ();
157 $row = $db->select(word => '*', $find)->hash
158 or Abort("Word not found", 404);
161 if (exists $get{copy}) {
162 $row = {%{$row}{ qw(prio lang cat) }};
164 elsif ($ENV{REQUEST_METHOD} eq 'POST') {{
166 $row = {%post{ pairkeys @wordcols }};
167 $_ = length ? $_ : undef for values %{$row};
170 my %res = (returning => '*');
171 my $query = $find ? $db->update(word => $row, $find, \%res) :
172 $db->insert(word => $row, \%res);
175 Alert("Entry could not be saved", $@);
179 my $imgpath = "data/word/org/$row->{id}.jpg";
181 ($row->{source} // '') ne ($replace->{source} // '') or return;
182 # copy changed remote url to local file
183 unlink $imgpath if -e $imgpath;
184 my $download = $row->{source} or return 1;
185 require LWP::UserAgent;
186 my $ua = LWP::UserAgent->new;
188 my $status = $ua->mirror($download, $imgpath);
190 or die "Download from <q>$download</q> failed: ".$status->status_line."\n";
192 !$@ or Alert(["Source image not found", $@]);
194 $reimage ||= $row->{thumb} ~~ $replace->{thumb}; # different convert
195 $reimage ||= $row->{cover} ~~ $replace->{cover}; # resize
196 $reimage++ if $fields{rethumb}; # force refresh
198 my $thumbpath = "data/word/eng/$row->{form}.jpg";
201 my $xyres = $row->{cover} ? '600x400' : '300x200';
202 my @cmds = @{ $row->{thumb} // [] };
205 -delete => '1--1', -background => 'white',
206 -gravity => @cmds ? 'northwest' : 'center',
208 -resize => "$xyres^", -extent => $xyres,
209 '-strip', -quality => '60%', -interlace => 'plane',
210 $imgpath => $thumbpath
215 IPC::Run::run(\@cmds, '<' => \undef, '>&' => \$output)
217 ($? & 127 ? "signal $?" : "error code ".($? >> 8))."\n";
219 "Thumbnail image not generated",
220 "Failed to convert source image.",
230 $row->{$_} = $get{$_} for keys %get;
233 my $title = $row->{id} ? "entry <small>#$row->{id}</small>" : 'new entry';
235 <h1>Words <:= $title :></h1>
239 <form action="?" method="post">
240 <input id="id" name="id" value="<:= $row->{id} // '' :>" type="hidden" />
244 for my $colinfo (pairs @wordcols) {
245 my ($col, $title) = @{$colinfo};
246 defined $title or next;
247 my $val = $row->{$col} // '';
248 $val = '{'.join(',', map {s/,/\\,/gr} @{$val}).'}' if ref $val eq 'ARRAY';
249 printf '<li><label for="%s">%s</label><p>', $col, $title;
250 printf '<span class=inline>';
251 if ($col eq 'prio') {
252 printf '<select id="%s" name="%1$s">', $col;
253 printf('<option value="%s"%s>%s</option>',
254 $_, $row->{$col} eq $_ && ' selected', $prioenum[$_]
255 ) for 0 .. $#prioenum;
260 '<input id="%1$s" name="%1$s" value="0" type="hidden" />',
261 '<input id="%s" name="%1$s" value="1" type="checkbox"%s>',
264 'cover', !!$row->{cover} && ' checked', 'Highlighted'
268 printf '<input id="%s" name="%1$s" value="%s" />', $col, Entity($val);
269 -e and printf '<img src="/%s" alt="%s" />', $_, $row->{form} for
270 $col eq 'source' ? "data/word/org/$row->{id}.jpg" :
271 $col eq 'thumb' ? "data/word/eng/$row->{form}.jpg" :
280 <input type="submit" value="Save" />
281 <input type="submit" value="New" formaction="/writer?copy=cat" />
293 my $parents = $db->select(word => '*', {id => $row->{cat}});
294 while (my $ref = $parents->hash) {
295 printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
297 say "<li><strong>$row->{form}</strong></li>";
298 my $children = $db->select(word => '*', {cat => $row->{id}});
299 while (my $ref = $children->hash) {
300 printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
303 <li><form action="/writer">
304 <input type="hidden" name="cat" value="<:= $row->{id} :>" />
305 <input type="hidden" name="lang" value="<:= $row->{lang} :>" />
306 <input type="submit" value="Add" />