From fe972713940a9d116e5e2958f44d1ecae16b3d7b Mon Sep 17 00:00:00 2001 From: Gabriel Wicke Date: Mon, 23 Jul 2012 17:49:54 -0700 Subject: [PATCH] Use various RDFa types for links * the used RDFa types for links are now identical to those listed in http://www.mediawiki.org/wiki/Parsoid/RDFa_vocabulary, and are supported for serialization * Editors are responsible for adjusting the type when converting between link types. Adding a caption to an mw:UrlLink for example should convert it into an mw:ExtLink. Update: rebased on top of trace patches Change-Id: Ie1b882e2b3fbad08be94769e1167dccd8dfea65d --- modules/parser/ext.core.LinkHandler.js | 17 ++++++------ .../parser/mediawiki.WikitextSerializer.js | 27 ++++++++++--------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/modules/parser/ext.core.LinkHandler.js b/modules/parser/ext.core.LinkHandler.js index 242f3d553d..6b5211b7e6 100644 --- a/modules/parser/ext.core.LinkHandler.js +++ b/modules/parser/ext.core.LinkHandler.js @@ -47,8 +47,7 @@ WikiLinkHandler.prototype.onWikiLink = function ( token, frame, cb ) { var normalizedHref = title.makeLink(), obj = new TagTk( 'a', [ - new KV( 'href', normalizedHref ), - new KV('rel', 'mw:WikiLink') + new KV( 'href', normalizedHref ) ], token.dataAttribs ), content = token.attribs.slice(2); @@ -66,10 +65,11 @@ WikiLinkHandler.prototype.onWikiLink = function ( token, frame, cb ) { out.push( '|' ); } } + obj.attribs.push( new KV('rel', 'mw:WikiLink') ); content = out; } else { content = [ Util.decodeURI(href) ]; - obj.dataAttribs.gc = 1; + obj.attribs.push( new KV('rel', 'mw:SimpleWikiLink') ); } var tail = Util.lookupKV( token.attribs, 'tail' ).v; @@ -371,9 +371,8 @@ ExternalLinkHandler.prototype.onUrlLink = function ( token, frame, cb ) { new TagTk( 'a', [ new KV( 'href', href ), - new KV('rel', 'mw:ExtLink') - ], - { stx: 'urllink' } ), + new KV('rel', 'mw:UrlLink') + ] ), href, new EndTagTk( 'a' ) ] @@ -386,7 +385,8 @@ ExternalLinkHandler.prototype.onUrlLink = function ( token, frame, cb ) { ExternalLinkHandler.prototype.onExtLink = function ( token, manager, cb ) { var env = this.manager.env, href = Util.sanitizeURI(env.tokensToString( Util.lookupKV( token.attribs, 'href' ).v )), - content= Util.lookupKV( token.attribs, 'content' ).v; + content= Util.lookupKV( token.attribs, 'content' ).v, + rdfaType = 'mw:ExtLink'; //console.warn('extlink href: ' + href ); //console.warn( 'content: ' + JSON.stringify( content, null, 2 ) ); // validate the href @@ -394,6 +394,7 @@ ExternalLinkHandler.prototype.onExtLink = function ( token, manager, cb ) { if ( ! content.length ) { content = ['[' + this.linkCount + ']']; this.linkCount++; + rdfaType = 'mw:NumberedExtLink'; } if ( content.length === 1 && content[0].constructor === String && @@ -417,7 +418,7 @@ ExternalLinkHandler.prototype.onExtLink = function ( token, manager, cb ) { new TagTk ( 'a', [ new KV('href', href), - new KV('rel', 'mw:ExtLink') + new KV('rel', rdfaType) ], token.dataAttribs ) diff --git a/modules/parser/mediawiki.WikitextSerializer.js b/modules/parser/mediawiki.WikitextSerializer.js index 8a214ad62a..4ebb081b0b 100644 --- a/modules/parser/mediawiki.WikitextSerializer.js +++ b/modules/parser/mediawiki.WikitextSerializer.js @@ -526,7 +526,7 @@ WSP._linkHandler = function( state, tokens ) { var attribDict = env.KVtoHash( token.attribs ); if ( attribDict.rel && attribDict.href !== undefined ) { var tokenData = token.dataAttribs; - if ( attribDict.rel === 'mw:WikiLink' ) { + if ( attribDict.rel === 'mw:WikiLink' || attribDict.rel === 'mw:SimpleWikiLink' ) { var base = env.wgScriptPath; var href = attribDict.href; var prefix = href.substr(0, base.length); @@ -535,7 +535,11 @@ WSP._linkHandler = function( state, tokens ) { var tail = tokenData.tail; if ( tail && tail.length ) { - target = tokenData.gc ? tokenData.sHref : target.replace( /_/g, ' ' ); + if ( attribDict.rel === 'mw:SimpleWikiLink' ) { + target = tokenData.sHref; + } else { + target = target.replace( /_/g, ' ' ); + } } else { tail = ''; var origLinkTgt = tokenData.sHref; @@ -555,7 +559,7 @@ WSP._linkHandler = function( state, tokens ) { // FIXME: Properly handle something like [[{{Foo}}]]s target = env.tokensToString( target ); - if ( tokenData.gc ) { + if ( attribDict.rel === 'mw:SimpleWikiLink' ) { return '[[' + target + ']]' + tail; } else { var content = state.serializer.serializeTokens( tokens ).join(''); @@ -565,16 +569,13 @@ WSP._linkHandler = function( state, tokens ) { return '[[' + target + '|' + content + ']]' + tail; } } else if ( attribDict.rel === 'mw:ExtLink' ) { - // TODO: use data-{gen,sem,special} instead! - if ( tokenData.stx === 'urllink' ) { - return attribDict.href; - } else if ( tokenData.gc ) { - return '[' + attribDict.href + ']'; - } else { - return '[' + attribDict.href + ' ' + - state.serializer.serializeTokens( tokens ).join('') + - ']'; - } + return '[' + attribDict.href + ' ' + + state.serializer.serializeTokens( tokens ).join('') + + ']'; + } else if ( attribDict.rel === 'mw:UrlLink' ) { + return attribDict.href; + } else if ( attribDict.rel === 'mw:NumberedExtLink' ) { + return '[' + attribDict.href + ']'; } else if ( attribDict.rel === 'mw:Image' ) { // simple source-based round-tripping for now.. // TODO: properly implement!