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";
180 if (($row->{source} // '') ne ($replace->{source} // '')) {
181 # copy changed remote url to local file
182 unlink $imgpath if -e $imgpath;
183 if (my $download = $row->{source}) {
184 require LWP::UserAgent;
185 my $ua = LWP::UserAgent->new;
187 my $status = $ua->mirror($download, $imgpath);
188 $status->is_success or Alert([
189 "Source image not found",
190 "Download from <q>$download</q> failed: ".$status->status_line,
194 elsif ($row->{thumb} ~~ $replace->{thumb}) {
195 # image and conversion unaltered
199 my $thumbpath = "data/word/eng/$row->{form}.jpg";
202 my $xyres = $row->{cover} ? '600x400' : '300x200';
203 my @cmds = @{ $row->{thumb} // [] };
206 -delete => '1--1', -background => 'white',
207 -gravity => @cmds ? 'northwest' : 'center',
209 -resize => "$xyres^", -extent => $xyres,
210 '-strip', -quality => '60%', -interlace => 'plane',
211 $imgpath => $thumbpath
216 IPC::Run::run(\@cmds, '<' => \undef, '>&' => \$output)
218 ($? & 127 ? "signal $?" : "error code ".($? >> 8))."\n";
220 "Thumbnail image not generated",
221 "Failed to convert source image.",
231 $row->{$_} = $get{$_} for keys %get;
234 my $title = $row->{id} ? "entry <small>#$row->{id}</small>" : 'new entry';
236 <h1>Words <:= $title :></h1>
240 <form action="?" method="post">
241 <input id="id" name="id" value="<:= $row->{id} // '' :>" type="hidden" />
245 for my $colinfo (pairs @wordcols) {
246 my ($col, $title) = @{$colinfo};
247 defined $title or next;
248 my $val = $row->{$col} // '';
249 $val = '{'.join(',', map {s/,/\\,/gr} @{$val}).'}' if ref $val eq 'ARRAY';
250 printf '<li><label for="%s">%s</label><p>', $col, $title;
251 printf '<span class=inline>';
252 if ($col eq 'prio') {
253 printf '<select id="%s" name="%1$s">', $col;
254 printf('<option value="%s"%s>%s</option>',
255 $_, $row->{$col} eq $_ && ' selected', $prioenum[$_]
256 ) for 0 .. $#prioenum;
261 '<input id="%1$s" name="%1$s" value="0" type="hidden" />',
262 '<input id="%s" name="%1$s" value="1" type="checkbox"%s>',
265 'cover', !!$row->{cover} && ' checked', 'Highlighted'
269 printf '<input id="%s" name="%1$s" value="%s" />', $col, Entity($val);
270 -e and printf '<img src="/%s" alt="%s" />', $_, $row->{form} for
271 $col eq 'source' ? "data/word/org/$row->{id}.jpg" :
272 $col eq 'thumb' ? "data/word/eng/$row->{form}.jpg" :
281 <input type="submit" value="Save" />
282 <input type="submit" value="New" formaction="/writer?copy=cat" />
294 my $parents = $db->select(word => '*', {id => $row->{cat}});
295 while (my $ref = $parents->hash) {
296 printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
298 say "<li><strong>$row->{form}</strong></li>";
299 my $children = $db->select(word => '*', {cat => $row->{id}});
300 while (my $ref = $children->hash) {
301 printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
304 <li><form action="/writer">
305 <input type="hidden" name="cat" value="<:= $row->{id} :>" />
306 <input type="hidden" name="lang" value="<:= $row->{lang} :>" />
307 <input type="submit" value="Add" />