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;
49 document.addEventListener('DOMContentLoaded', () => {
50 var wpinput = document.getElementById('wptitle');
51 var wpbutton = wpinput.parentNode.appendChild(document.createElement('button'));
52 wpbutton.type = 'button';
53 wpbutton.append('Copy');
54 wpbutton.onclick = () => {
55 let wptitle = wpinput.value || document.getElementById('form').value;
56 let wppage = 'https://en.wikipedia.org/w/api.php?action=parse&format=json&origin=*&prop=text&page='+wptitle;
57 fetch(wppage).then(res => res.json()).then(json => {
58 if (json.error) throw `error returned: ${json.error.info}`;
59 wpinput.value = json.parse.title;
60 let imginput = document.getElementById('source');
61 if (imginput.value) return;
62 let wpimages = json.parse.text['*'].match(/<img\s[^>]+>/g);
63 let wpselect = wpinput.parentNode.appendChild(document.createElement('ul'));
64 wpselect.className = 'popup';
65 wpimages.forEach(img => {
66 let selectitem = wpselect.appendChild(document.createElement('li'));
67 selectitem.insertAdjacentHTML('beforeend', img);
68 selectitem.onclick = e => {
69 let imgsrc = e.target.src
70 .replace(/^(?=\/\/)/, 'https:')
71 .replace(/\/thumb(\/.+)\/[^\/]+$/, '$1');
72 imginput.value = imgsrc;
77 }).catch(error => alert(error));
85 use List::Util qw( pairs pairkeys );
89 'DBI:Pg:dbname=sheet;host=localhost', 'sheetadmin', 'fairuse',
90 ) or die "database not configured\n";
92 DBIx::Simple->new(@dbinfo[0..2], {
96 } or Abort('Database error', 501, $@);
99 form => 'Translation',
100 wptitle => 'Wikipedia',
104 source => 'Image URL',
105 thumb => 'Convert options',
107 my ($find) = map {{id => $_}} $fields{id} || $Request || ();
110 if (exists $get{copy}) {
111 $row = {%fields{ qw(lang cat) }};
113 elsif ($ENV{REQUEST_METHOD} eq 'POST') {
114 $row = {%post{ pairkeys @wordcols }};
115 $_ = length ? $_ : undef for values %{$row};
117 my %res = (returning => '*');
118 my $query = $find ? $db->update(word => $row, $find, \%res) :
119 $db->insert(word => $row, \%res);
121 } or Alert("Entry could not be saved", $@);
123 my $imgpath = "data/word/org/$row->{id}.jpg";
124 if (my $download = $row->{source} and !-e $imgpath) {
125 require LWP::UserAgent;
126 my $ua = LWP::UserAgent->new;
128 my $status = $ua->mirror($download, $imgpath);
129 $status->is_success or Alert([
130 "Source image not found",
131 "Download from <q>$download</q> failed: ".$status->status_line,
135 my $thumbpath = "data/word/eng/$row->{form}.jpg";
137 my @cmds = @{ $row->{thumb} // [] };
140 -delete => '1--1', -background => 'white',
141 -gravity => @cmds ? 'northwest' : 'center',
143 -resize => '300x200^', -extent => '300x200',
144 '-strip', -quality => '60%', -interlace => 'plane',
145 $imgpath => $thumbpath
147 my $status = system @cmds;
148 $status == 0 or Alert([
149 "Thumbnail image not generated",
150 "Failed to convert source image, error code ".($status >> 8),
155 $row = $db->select(word => '*', $find)->hash
156 or Abort("Word not found", 404);
159 my $title = $row->{id} ? "entry <small>#$row->{id}</small>" : 'new entry';
161 <h1>Words <:= $title :></h1>
163 <form action="?" method="post">
164 <input id="id" name="id" value="<:= $row->{id} // '' :>" type="hidden" />
168 for my $col (pairs @wordcols) {
169 my $val = $row->{$col->key} // '';
170 $val = '{'.join(',', map {s/,/\\,/gr} @{$val}).'}' if ref $val eq 'ARRAY';
171 printf '<dt><label for="%s">%s</label></dt>'
172 . '<dd><input id="%1$s" name="%1$s" value="%s" />',
173 $col->key, $col->value, Entity($val);
174 -e and printf ' <img src="/%s" alt="%s" />', $_, $row->{form} for
175 $col->key eq 'source' ? "data/word/org/$row->{id}.jpg" :
176 $col->key eq 'thumb' ? "data/word/eng/$row->{form}.jpg" :
183 <input type="submit" value="Save" />
184 <input type="submit" value="New" formaction="/writer?copy=cat" />