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
This commit is contained in:
Gabriel Wicke 2012-07-23 17:49:54 -07:00
parent 305b5981fe
commit fe97271394
2 changed files with 23 additions and 21 deletions

View file

@ -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
)

View file

@ -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!