diff --git a/doc/TwigJS.md b/doc/TwigJS.md index 5ed56f2a..6e21ed3d 100644 --- a/doc/TwigJS.md +++ b/doc/TwigJS.md @@ -76,6 +76,7 @@ Extra filters: * filter `md5`: calculate md5 hash of a string. * filter `enumerate`: enumerate the given list, e.g. "foo, bar, and bla". Input either an array (`[ "foo", "bar", "bla" ]|enumerate`) or a string with `;` as separator (`"foo;bar;bla"|enumerate`). * filter `debug`: print the value (and further arguments) to the javascript console (via `console.log()`) +* filter `wikipediaAbstract`: shows the abstract of a Wikipedia article in the selected data language (or, if not available, the language which was used in input, resp. 'en' for Wikidata input). Input is either 'language:article' (e.g. 'en:Douglas Adams') or a wikidata id (e.g. 'Q42'). Notes: * Variables will automatically be HTML escaped, if not the filter raw is used, e.g.: {{ tags.name|raw }} diff --git a/src/wikipedia.js b/src/wikipedia.js index 9cabb9e7..d99cf851 100644 --- a/src/wikipedia.js +++ b/src/wikipedia.js @@ -1,3 +1,6 @@ +const async = require('async') +const OverpassLayer = require('overpass-layer') + var wikidata = require('./wikidata') const displayBlock = require('./displayBlock') @@ -116,11 +119,43 @@ function getAbstract (value, callback) { text += ' ' + lang('more') + '' } + getAbstractCache[value] = text + callback(err, text) } ) } +function updateDomWikipedia (dom, callback) { + const wikipediaQueries = dom.querySelectorAll('.wikipedia') + async.each( + wikipediaQueries, + (div, done) => { + if (div.hasAttribute('data-done')) { + return done() + } + + getAbstract(div.getAttribute('data-id'), + (err, result) => { + if (result) { + div.innerHTML = result + div.setAttribute('data-done', 'true') + } + done() + } + ) + }, + () => { + callback() + } + ) +} + +register_hook('show-popup', function (data, category, dom, callback) { + updateDomWikipedia(dom, () => updateDomWikipedia(dom, () => {})) + callback() +}) + register_hook('show-details', function (data, category, dom, callback) { var ob = data.object var found = 0 @@ -381,3 +416,12 @@ function getImages (tagValue, callback) { module.exports = { getImages: getImages } + +OverpassLayer.twig.extendFilter('wikipediaAbstract', function (value, param) { + if (value in getAbstractCache) { + let result = getAbstractCache[value] + return '
' + result + '
' + } + + return '
' + value + '
' +})