4 title => 'words cheat sheet admin',
13 font-family: monospace;
17 display: inline-block;
18 box-sizing: border-box;
22 vertical-align: text-top;
34 document.addEventListener('DOMContentLoaded', () => {
35 var wpinput = document.getElementById('wptitle');
36 var wpbutton = wpinput.parentNode.appendChild(document.createElement('button'));
37 wpbutton.type = 'button';
38 wpbutton.append('Copy');
39 wpbutton.onclick = () => {
40 let wptitle = wpinput.value || document.getElementById('form').value;
41 let wppage = 'https://en.wikipedia.org/w/api.php?action=parse&format=json&origin=*&prop=text&page='+wptitle;
42 fetch(wppage).then(res => res.json()).then(json => {
43 if (json.error) throw `error returned: ${json.error.info}`;
44 wpinput.value = json.parse.title;
45 let imginput = document.getElementById('source');
46 if (imginput.value) return;
47 let wpimages = json.parse.text['*'].match(/<img\s[^>]+/g);
48 imginput.value = wpimages[0].match(/\ssrc="([^"]+)"/)[1]
49 .replace(/^(?=\/\/)/, 'https:')
50 .replace(/\/thumb(\/.+)\/[^\/]+$/, '$1');
51 }).catch(error => alert(error));
59 use List::Util qw( pairs pairkeys );
63 'DBI:Pg:dbname=sheet;host=localhost', 'sheetadmin', 'fairuse',
64 ) or die "database not configured\n";
66 DBIx::Simple->new(@dbinfo[0..2], {
70 } or Abort('Database error', 501, $@);
73 form => 'Translation',
74 wptitle => 'Wikipedia',
78 source => 'Image URL',
79 thumb => 'Convert options',
81 my ($find) = map {{id => $_}} $fields{id} || $Request || ();
84 if (exists $get{copy}) {
85 $row = {%fields{ qw(lang cat) }};
87 elsif ($ENV{REQUEST_METHOD} eq 'POST') {
88 $row = {%post{ pairkeys @wordcols }};
89 $_ = length ? $_ : undef for values %{$row};
91 my %res = (returning => '*');
92 my $query = $find ? $db->update(word => $row, $find, \%res) :
93 $db->insert(word => $row, \%res);
95 } or Alert("Entry could not be saved", $@);
97 my $imgpath = "data/word/org/$row->{id}.jpg";
98 if (my $download = $row->{source} and !-e $imgpath) {
99 require LWP::UserAgent;
100 my $ua = LWP::UserAgent->new;
102 my $status = $ua->mirror($download, $imgpath);
103 $status->is_success or Alert([
104 "Source image not found",
105 "Download from <q>$download</q> failed: ".$status->status_line,
109 my $thumbpath = "data/word/eng/$row->{form}.jpg";
111 my @cmds = @{ $row->{thumb} // [] };
112 unshift @cmds, -gravity => @cmds ? 'northwest' : 'center';
113 unshift @cmds, 'convert';
114 push @cmds, -resize => '300x200^', -extent => '300x200';
115 push @cmds, '-strip', -quality => '60%', -interlace => 'plane';
116 push @cmds, $imgpath, $thumbpath;
117 my $status = system @cmds;
118 $status == 0 or Alert([
119 "Thumbnail image not generated",
120 "Failed to convert source image, error code ".($status >> 8),
125 $row = $db->select(word => '*', $find)->hash
126 or Abort("Word not found", 404);
129 my $title = $row->{id} ? "entry <small>#$row->{id}</small>" : 'new entry';
131 <h1>Words <:= $title :></h1>
133 <form action="?" method="post">
134 <input id="id" name="id" value="<:= $row->{id} // '' :>" type="hidden" />
138 for my $col (pairs @wordcols) {
139 my $val = $row->{$col->key} // '';
140 $val = '{'.join(',', map {s/,/\\,/gr} @{$val}).'}' if ref $val eq 'ARRAY';
141 printf '<dt><label for="%s">%s</label></dt>'
142 . '<dd><input id="%1$s" name="%1$s" value="%s" />',
143 $col->key, $col->value, Entity($val);
144 -e and printf ' <img src="/%s" alt="%s" />', $_, $row->{form} for
145 $col->key eq 'source' ? "data/word/org/$row->{id}.jpg" :
146 $col->key eq 'thumb' ? "data/word/eng/$row->{form}.jpg" :
153 <input type="submit" value="Save" />
154 <input type="submit" value="New" formaction="/writer?copy=cat" />