Skip to content

Commit

Permalink
Tweaks to the Wikipedia script
Browse files Browse the repository at this point in the history
  • Loading branch information
juhana committed Sep 6, 2019
1 parent 59686be commit e948244
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 139 deletions.
12 changes: 12 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ See also the changelog of the main JavaScript library
(https://github.com/vorple/vorple/CHANGES).


Release 3.2
===========================================

Vorple
------

- Modified the Wikipedia script in The Sum of Human Knowledge example to make
use of Wikipedia's new extracts feature, which returns a clean summary of the
article (inform7 #14)



Release 3.1
===========================================

Expand Down
2 changes: 0 additions & 2 deletions examples/The Sum of Human Knowledge.inf
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ Array myTopic buffer 1000;
if (isVorpleSupported()){
VorplePlaceBlockLevelElement("dictionary-entry", "");
VorpleExecuteJavaScriptCommand( BuildCommand("return wikipedia_query('", VorpleEscape(myTopic), "')"));
! run paragraph on ?
VorpleBlockUserInterface();
} else {
print "You find the correct volume and learn about ";
print (PrintStringOrArray) myTopic;
Expand Down
80 changes: 0 additions & 80 deletions resources/encyclopedia.js

This file was deleted.

85 changes: 28 additions & 57 deletions resources/javascript/encyclopedia.js
Original file line number Diff line number Diff line change
@@ -1,80 +1,51 @@
/**
* This is a custom JavaScript file used by the example The Sum of Human Knowledge in the core Vorple extension.
*/

function wikipedia_query( topic ) {
// prevent the player from continuing before the data has been fetched
vorple.layout.block();
vorple.prompt.hide();

$.getJSON(
'http://en.wikipedia.org/w/api.php?callback=?',
'http://en.wikipedia.org/w/api.php?exintro&explaintext&callback=?',
{
action: 'query',
titles: topic,
format: 'json',
prop: 'revisions',
rvprop: 'content',
rvparse: '1',
redirects: '1'
prop: 'extracts',
redirects: '1',
titles: topic
},
function( data ) {
var $dictEntry = $( '.dictionary-entry:last' );
// the element where the results will be placed, created by Inform
$dictEntry = $( '.dictionary-entry:last' );

try {
// get the page info (we don't know its key so it can't be referenced directly)
for( var id in data.query.pages ) {
var $article = $( '<div>'+data.query.pages[ id ].revisions[ 0 ]['*']+'</div>' );

// get the first paragraph
var $para = $article.find( 'p' ).filter( function() { return $( this ).text().trim().length > 0; } ).first();

// try to detect if it's a disambiguation page; if so,
// show the entire page.
if( $para.text().indexOf( 'may refer to' ) !== -1 ) {
$article.find( 'table' ).remove();
$para = $article;
}
const pageId = Object.keys( data.query.pages )[ 0 ];
const extract = data.query.pages[ pageId ].extract;

$para.find( 'a' ).each( function() {
var $this = $( this );
var href = $this.attr( 'href' );

// remove citation links
if( /\[\d*\]/.test( $this.text() ) ) {
$this.remove();
}
// unlink references to the same page and Wikipedia info pages
else if( href.indexOf( '#' ) === 0 || /^\/wiki\/(.*)\:/.test( href ) ) {
$this.replaceWith( '<span>'+$this.html()+'</span>' );
}
// internal Wikipedia links trigger a new search inside the story
else if( href.indexOf( '/wiki/' ) === 0 ) {
$this.on( 'click', function( e ) {
vorple.prompt.queueCommand( 'look up ' + decodeURI( href.substr( 6 ).replace( /\_/g, ' ' ) ) );
e.preventDefault();
});
}
// external links open in new window
else {
$this.attr( 'target', '_blank' );
}
});

// remove edit links
$para.find( '.editsection' ).remove();
if( !extract ) {
// nothing was found, throw an error so that the "you find nothing" text can be printed
throw new Error();
}

// remove references and other Wiki markup
$para.find( 'sup' ).remove();
// turn line breaks into paragraph breaks by duplicating all line breaks (\n)
const paragraphs = extract.replace( /\n+/g, '\n\n' );

$( '.dictionary-entry:last' ).append( $para );
break;
}
// show the result on the page
$dictEntry.append( '<span>' + paragraphs + '\n</span>' );
}
catch( e ) {
// An error is to be expected when there are no search results
$dictEntry.html( 'You find nothing about that topic.' );
// an error is to be expected when there are no search results
$dictEntry.html( '<span>You find nothing about that topic.\n</span>' );
}

vorple.layout.scrollToEnd();
// scroll to show the text
vorple.layout.scrollTo( $dictEntry );

// let the player continue playing
vorple.layout.unblock();
vorple.parser.unhide();
vorple.prompt.unhide();
}
);
}

0 comments on commit e948244

Please sign in to comment.