Browse Source

wikipedia: new twig filter 'wikipediaAbstract'

master
parent
commit
80e547294e
  1. 1
      doc/TwigJS.md
  2. 44
      src/wikipedia.js

1
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 }}

44
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 += ' <a target="_blank" href="' + result.languages[result.language] + '">' + lang('more') + '</a>'
}
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 '<div class="wikipedia" data-id="' + value + '" data-done="true">' + result + '</div>'
}
return '<div class="wikipedia" data-id="' + value + '"><a href="https://wikidata.org/wiki/' + value + '">' + value + '</a></div>'
})
Loading…
Cancel
Save