4 title => 'words cheat sheet admin',
13 font-family: monospace;
17 display: inline-block;
18 box-sizing: border-box;
22 vertical-align: text-top;
43 background: rgba(0, 0, 0, .8);
44 border: 1px solid #CCC;
55 section > ul strong, section form {
61 document.addEventListener('DOMContentLoaded', () => {
62 var wpinput = document.getElementById('wptitle');
63 var wpbutton = wpinput.parentNode.appendChild(document.createElement('button'));
64 wpbutton.type = 'button';
65 wpbutton.append('Copy');
66 wpbutton.onclick = () => {
67 let wptitle = wpinput.value || document.getElementById('form').value;
68 let wppage = 'https://en.wikipedia.org/w/api.php?action=parse&format=json&origin=*&prop=text&page='+wptitle;
69 fetch(wppage).then(res => res.json()).then(json => {
70 if (json.error) throw `error returned: ${json.error.info}`;
71 wpinput.value = json.parse.title;
72 let imginput = document.getElementById('source');
73 if (imginput.value) return;
74 let wpimages = json.parse.text['*'].match(/<img\s[^>]+>/g);
75 let wpselect = wpinput.parentNode.appendChild(document.createElement('ul'));
76 wpselect.className = 'popup';
77 wpimages.forEach(img => {
78 let selectitem = wpselect.appendChild(document.createElement('li'));
79 selectitem.insertAdjacentHTML('beforeend', img);
80 selectitem.onclick = e => {
81 let imgsrc = e.target.src
82 .replace(/^(?=\/\/)/, 'https:')
83 .replace(/\/thumb(\/.+)\/[^\/]+$/, '$1');
84 imginput.value = imgsrc;
89 }).catch(error => alert(error));
97 use List::Util qw( pairs pairkeys );
101 'DBI:Pg:dbname=sheet;host=localhost', 'sheetadmin', 'fairuse',
102 ) or die "database not configured\n";
103 require DBIx::Simple;
104 DBIx::Simple->new(@dbinfo[0..2], {
108 } or Abort('Database error', 501, $@);
111 form => 'Translation',
112 wptitle => 'Wikipedia',
116 source => 'Image URL',
117 thumb => 'Convert options',
119 my ($find) = map {{id => $_}} $fields{id} || $Request || ();
123 $row = $db->select(word => '*', $find)->hash
124 or Abort("Word not found", 404);
127 if (exists $get{copy}) {
128 $row = {%{$row}{ qw(lang cat) }};
130 elsif ($ENV{REQUEST_METHOD} eq 'POST') {
132 $row = {%post{ pairkeys @wordcols }};
133 $_ = length ? $_ : undef for values %{$row};
136 my %res = (returning => '*');
137 my $query = $find ? $db->update(word => $row, $find, \%res) :
138 $db->insert(word => $row, \%res);
140 } or Alert("Entry could not be saved", $@);
142 my $imgpath = "data/word/org/$row->{id}.jpg";
143 if (($row->{source} // '') ne ($replace->{source} // '')) {
144 # copy changed remote url to local file
145 unlink $imgpath if -e $imgpath;
146 if (my $download = $row->{source}) {
147 require LWP::UserAgent;
148 my $ua = LWP::UserAgent->new;
150 my $status = $ua->mirror($download, $imgpath);
151 $status->is_success or Alert([
152 "Source image not found",
153 "Download from <q>$download</q> failed: ".$status->status_line,
157 elsif ($row->{thumb} ~~ $replace->{thumb}) {
158 # image and conversion unaltered
162 my $thumbpath = "data/word/eng/$row->{form}.jpg";
165 my @cmds = @{ $row->{thumb} // [] };
168 -delete => '1--1', -background => 'white',
169 -gravity => @cmds ? 'northwest' : 'center',
171 -resize => '300x200^', -extent => '300x200',
172 '-strip', -quality => '60%', -interlace => 'plane',
173 $imgpath => $thumbpath
175 my $status = system @cmds;
176 $status == 0 or Alert([
177 "Thumbnail image not generated",
178 "Failed to convert source image, error code ".($status >> 8),
187 $row->{$_} = $get{$_} for keys %get;
190 my $title = $row->{id} ? "entry <small>#$row->{id}</small>" : 'new entry';
192 <h1>Words <:= $title :></h1>
194 <form action="?" method="post">
195 <input id="id" name="id" value="<:= $row->{id} // '' :>" type="hidden" />
199 for my $col (pairs @wordcols) {
200 my $val = $row->{$col->key} // '';
201 $val = '{'.join(',', map {s/,/\\,/gr} @{$val}).'}' if ref $val eq 'ARRAY';
202 printf '<dt><label for="%s">%s</label></dt>'
203 . '<dd><input id="%1$s" name="%1$s" value="%s" />',
204 $col->key, $col->value, Entity($val);
205 -e and printf ' <img src="/%s" alt="%s" />', $_, $row->{form} for
206 $col->key eq 'source' ? "data/word/org/$row->{id}.jpg" :
207 $col->key eq 'thumb' ? "data/word/eng/$row->{form}.jpg" :
214 <input type="submit" value="Save" />
215 <input type="submit" value="New" formaction="/writer?copy=cat" />
227 my $parents = $db->select(word => '*', {id => $row->{cat}});
228 while (my $ref = $parents->hash) {
229 printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
231 say "<li><strong>$row->{form}</strong></li>";
232 my $children = $db->select(word => '*', {cat => $row->{id}});
233 while (my $ref = $children->hash) {
234 printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
237 <li><form action="/writer">
238 <input type="hidden" name="cat" value="<:= $row->{id} :>" />
239 <input type="hidden" name="lang" value="<:= $row->{lang} :>" />
240 <input type="submit" value="Add" />