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 + * {
34 form > ul li > p input {
35 box-sizing: border-box;
38 font-family: monospace;
55 background: rgba(0, 0, 0, .8);
56 border: 1px solid #CCC;
65 margin: 0 -1ex; /* inner gap */
75 -margin-left: 1em; /* flex gap */
81 display: inline-block;
86 document.addEventListener('DOMContentLoaded', () => {
87 var wpinput = document.getElementById('wptitle');
88 var wpbutton = wpinput.parentNode.appendChild(document.createElement('button'));
89 wpbutton.type = 'button';
90 wpbutton.append('Copy');
91 wpbutton.onclick = () => {
92 let wptitle = wpinput.value || document.getElementById('form').value;
93 let wplang = document.getElementById('lang').value.substr(0, 2); // crude iso-639-3→2
94 let wpapi = `https://${wplang}.wikipedia.org/w/api.php`;
95 let wppage = wpapi+'?action=parse&format=json&origin=*&prop=text&page='+wptitle;
96 fetch(wppage).then(res => res.json()).then(json => {
97 if (json.error) throw `error returned: ${json.error.info}`;
98 wpinput.value = json.parse.title;
99 let imginput = document.getElementById('source');
100 if (imginput.value) return;
101 let wpimages = json.parse.text['*'].match(/<img\s[^>]+>/g);
102 let wpselect = wpinput.parentNode.appendChild(document.createElement('ul'));
103 wpselect.className = 'popup';
104 wpimages.forEach(img => {
105 let selectitem = wpselect.appendChild(document.createElement('li'));
106 selectitem.insertAdjacentHTML('beforeend', img);
107 selectitem.onclick = e => {
108 let imgsrc = e.target.src
109 .replace(/^(?=\/\/)/, 'https:')
110 .replace(/\/thumb(\/.+)\/[^\/]+$/, '$1');
111 imginput.value = imgsrc;
116 }).catch(error => alert(error));
124 use List::Util qw( pairs pairkeys );
128 'DBI:Pg:dbname=sheet;host=localhost', 'sheetadmin', 'fairuse',
129 ) or die "database not configured\n";
130 require DBIx::Simple;
131 DBIx::Simple->new(@dbinfo[0..2], {
135 } or Abort('Database error', 501, $@);
140 form => 'Translation',
142 wptitle => 'Wikipedia',
143 source => 'Image URL',
144 thumb => 'Convert options',
148 my ($find) = map {{id => $_}} $fields{id} || $Request || ();
152 $row = $db->select(word => '*', $find)->hash
153 or Abort("Word not found", 404);
156 if (exists $get{copy}) {
157 $row = {%{$row}{ qw(prio lang cat) }};
159 elsif ($ENV{REQUEST_METHOD} eq 'POST') {{
161 $row = {%post{ pairkeys @wordcols }};
162 $_ = length ? $_ : undef for values %{$row};
165 my %res = (returning => '*');
166 my $query = $find ? $db->update(word => $row, $find, \%res) :
167 $db->insert(word => $row, \%res);
170 Alert("Entry could not be saved", $@);
174 my $imgpath = "data/word/org/$row->{id}.jpg";
175 if (($row->{source} // '') ne ($replace->{source} // '')) {
176 # copy changed remote url to local file
177 unlink $imgpath if -e $imgpath;
178 if (my $download = $row->{source}) {
179 require LWP::UserAgent;
180 my $ua = LWP::UserAgent->new;
182 my $status = $ua->mirror($download, $imgpath);
183 $status->is_success or Alert([
184 "Source image not found",
185 "Download from <q>$download</q> failed: ".$status->status_line,
189 elsif ($row->{thumb} ~~ $replace->{thumb}) {
190 # image and conversion unaltered
194 my $thumbpath = "data/word/eng/$row->{form}.jpg";
197 my @cmds = @{ $row->{thumb} // [] };
200 -delete => '1--1', -background => 'white',
201 -gravity => @cmds ? 'northwest' : 'center',
203 -resize => '300x200^', -extent => '300x200',
204 '-strip', -quality => '60%', -interlace => 'plane',
205 $imgpath => $thumbpath
207 my $status = system @cmds;
208 $status == 0 or Alert([
209 "Thumbnail image not generated",
210 "Failed to convert source image, error code ".($status >> 8),
220 $row->{$_} = $get{$_} for keys %get;
223 my $title = $row->{id} ? "entry <small>#$row->{id}</small>" : 'new entry';
225 <h1>Words <:= $title :></h1>
229 <form action="?" method="post">
230 <input id="id" name="id" value="<:= $row->{id} // '' :>" type="hidden" />
234 for my $col (pairs @wordcols) {
235 my $val = $row->{$col->key} // '';
236 $val = '{'.join(',', map {s/,/\\,/gr} @{$val}).'}' if ref $val eq 'ARRAY';
237 printf '<li><label for="%s">%s</label><p><span class=inline>'
238 . '<input id="%1$s" name="%1$s" value="%s" />',
239 $col->key, $col->value, Entity($val);
240 -e and printf '<img src="/%s" alt="%s" />', $_, $row->{form} for
241 $col->key eq 'source' ? "data/word/org/$row->{id}.jpg" :
242 $col->key eq 'thumb' ? "data/word/eng/$row->{form}.jpg" :
244 say '</span></p></li>';
249 <input type="submit" value="Save" />
250 <input type="submit" value="New" formaction="/writer?copy=cat" />
262 my $parents = $db->select(word => '*', {id => $row->{cat}});
263 while (my $ref = $parents->hash) {
264 printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
266 say "<li><strong>$row->{form}</strong></li>";
267 my $children = $db->select(word => '*', {cat => $row->{id}});
268 while (my $ref = $children->hash) {
269 printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
272 <li><form action="/writer">
273 <input type="hidden" name="cat" value="<:= $row->{id} :>" />
274 <input type="hidden" name="lang" value="<:= $row->{lang} :>" />
275 <input type="submit" value="Add" />