word/edit: story column to store text descriptions
authorMischa POSLAWSKY <perl@shiar.org>
Fri, 2 Jul 2021 16:54:43 +0000 (18:54 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Mon, 5 Jul 2021 01:54:37 +0000 (03:54 +0200)
Copy Wikipedia intros as basic summary, for additional details/flavour
after guessing in future quizzes?

editor.css
tools/word.pg.sql
writer.js
writer.plp

index fd926012f925d9ab3fbe1b71bbbefe27078d46c1..077de812a71ca0c78a9304262ab8ca64c3e5e6aa 100644 (file)
@@ -24,11 +24,11 @@ form > ul > li > label + * {
 }
 
 .multiinput,
-input,select {
+input, textarea, select {
        box-sizing: border-box;
        flex-grow: 1;
 }
-input:not([type=submit]) {
+input:not([type=submit]), textarea {
        padding: .4rem;
        font-family: monospace;
 }
index fb1e5d11a586d388a5641a7d2edf091eb1091192..ec552c968b4a0add4f328d6e676deb434eccad60 100644 (file)
@@ -20,6 +20,7 @@ CREATE TABLE word (
        source     text                 CHECK (source ~ '^https?://'),
        thumb      text[],
        wptitle    text,
+       story      text,
        creator    integer              REFERENCES login (id),
        created    timestamptz          DEFAULT now(),
        updated    timestamptz,
@@ -37,6 +38,7 @@ COMMENT ON COLUMN word.cover      IS 'highlight if selected';
 COMMENT ON COLUMN word.source     IS 'URI of downloaded image';
 COMMENT ON COLUMN word.thumb      IS 'ImageMagick convert options to create thumbnail from source image';
 COMMENT ON COLUMN word.wptitle    IS 'reference Wikipedia article';
+COMMENT ON COLUMN word.story      IS 'paragraph defining or describing the entity, wikipedia intro';
 COMMENT ON COLUMN word.updated    IS 'last significant change';
 COMMENT ON COLUMN word.creator    IS 'user responsible for initial submit';
 
@@ -59,6 +61,7 @@ CREATE OR REPLACE VIEW _word_ref AS
                coalesce(r.source,  w.source ) source,
                coalesce(r.thumb,   w.thumb  ) thumb,
                coalesce(r.wptitle, w.wptitle) wptitle,
+               coalesce(r.story,   w.story  ) story,
                r.creator, r.created, r.updated,
                CASE WHEN r.source IS NULL THEN w.id ELSE r.id END id -- image id
        FROM word r
index ebace02f376569c53a99f00a0ce6f342b5a51b4c..a958732274c5e43ecff87c2fe8c95ff16428a54a 100644 (file)
--- a/writer.js
+++ b/writer.js
@@ -36,10 +36,21 @@ document.addEventListener('DOMContentLoaded', () => {
                                        });
                                });
 
+                               // copy first paragraph to story
+                               let wptext = json.parse.text['*'];
+                               let storyinput = document.getElementById('story');
+                               if (storyinput && wptext) {
+                                       storyinput.innerHTML = wptext
+                                               .replace(/<h2.*/s, '') // prefix
+                                               .replace(/<table.*?<\/table>/sg, '') // ignore infobox
+                                               .match(/<p>(.*?)<\/p>/s)[0] // first paragraph
+                                               .replace(/<[^>]*>/g, '') // strip html tags
+                               }
+
                                // list images in article html
                                let imginput = document.getElementById('source');
                                if (!imginput || imginput.value) return;
-                               let wpimages = json.parse.text['*'].match(/<img\s[^>]+>/g);
+                               let wpimages = wptext.match(/<img\s[^>]+>/g);
                                let wpselect = wpinput.parentNode.appendChild(document.createElement('ul'));
                                wpselect.className = 'popup';
                                wpimages.forEach(img => {
index 84dc4a8a2b66795ca574173d6e9d96fd6dd6b7d3..39a373bdc1f9dd7207d02370980753662522f012 100644 (file)
@@ -99,6 +99,7 @@ my %wordcol = (
        wptitle => {-label => 'Wikipedia'},
        source  => {-label => 'Image'},
        thumb   => {-label => 'Convert options', -multiple => 1},
+       story   => {-label => 'Story', type => 'textarea'},
 );
 
 if (my $search = $fields{q}) {
@@ -272,6 +273,16 @@ package Shiar_Sheet::FormRow {
                                '</select>',
                        );
                }
+               elsif ($attr->{type} eq 'textarea') {
+                       return (
+                               (map {
+                                       sprintf('<label for="%s">%s</label>', $col, $_)
+                               } $attr->{-label} // ()),
+                               sprintf('<textarea id="%s" name="%1$s"%s>%s</textarea>',
+                                       $col, $html, EscapeHTML($val)
+                               ),
+                       );
+               }
                elsif ($attr->{type} eq 'checkbox') {
                        $html .= ' checked' if $val;
                        return sprintf(