-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
40 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
); | ||
} | ||
|