mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Cite
synced 2024-12-01 01:56:20 +00:00
Clean up and comment the Cite extension a bit.
This commit is contained in:
parent
0fed6f9c79
commit
8e8362b8f0
|
@ -1,16 +1,20 @@
|
||||||
/**
|
/**
|
||||||
* The ref / references tags don't do any fancy HTML, so we can actually
|
* Simple token transform version of the Cite extension.
|
||||||
* implement this in terms of parse tree manipulations, skipping the need
|
|
||||||
* for renderer-specific plugins as well.
|
|
||||||
*
|
*
|
||||||
* Pretty neat huh!
|
* @class
|
||||||
|
* @constructor
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function Cite () {
|
function Cite () {
|
||||||
this.refGroups = {};
|
this.refGroups = {};
|
||||||
this.refTokens = [];
|
this.refTokens = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register with dispatcher.
|
||||||
|
*
|
||||||
|
* @method
|
||||||
|
* @param {Object} TokenTransformDispatcher to register to
|
||||||
|
*/
|
||||||
Cite.prototype.register = function ( dispatcher ) {
|
Cite.prototype.register = function ( dispatcher ) {
|
||||||
// Register for ref and references tag tokens
|
// Register for ref and references tag tokens
|
||||||
var self = this;
|
var self = this;
|
||||||
|
@ -27,9 +31,17 @@ Cite.prototype.register = function ( dispatcher ) {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// Convert list of key-value pairs to object, with first entry for a key
|
/**
|
||||||
// winning.
|
* Convert list of key-value pairs to object, with first entry for a
|
||||||
// XXX: Move to general util module
|
* key.
|
||||||
|
*
|
||||||
|
* XXX: Move to general utils
|
||||||
|
*
|
||||||
|
* @static
|
||||||
|
* @method
|
||||||
|
* @param {Array} List of [key, value] pairs
|
||||||
|
* @returns {Object} Object with key/values set, first entry wins.
|
||||||
|
*/
|
||||||
Cite.prototype.attribsToObject = function ( attribs ) {
|
Cite.prototype.attribsToObject = function ( attribs ) {
|
||||||
if ( attribs === undefined ) {
|
if ( attribs === undefined ) {
|
||||||
return {};
|
return {};
|
||||||
|
@ -44,7 +56,13 @@ Cite.prototype.attribsToObject = function ( attribs ) {
|
||||||
return obj;
|
return obj;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle ref tag tokens.
|
||||||
|
*
|
||||||
|
* @method
|
||||||
|
* @param {Object} TokenContext
|
||||||
|
* @returns {Object} TokenContext
|
||||||
|
*/
|
||||||
Cite.prototype.onRef = function ( tokenCTX ) {
|
Cite.prototype.onRef = function ( tokenCTX ) {
|
||||||
|
|
||||||
var refGroups = this.refGroups;
|
var refGroups = this.refGroups;
|
||||||
|
@ -131,19 +149,21 @@ Cite.prototype.onRef = function ( tokenCTX ) {
|
||||||
{
|
{
|
||||||
type: 'TAG',
|
type: 'TAG',
|
||||||
name: 'span',
|
name: 'span',
|
||||||
attribs: [['id', linkback],
|
attribs: [
|
||||||
['class', 'reference'],
|
['id', linkback],
|
||||||
// ignore element when serializing back to wikitext
|
['class', 'reference'],
|
||||||
['data-nosource', '']]
|
// ignore element when serializing back to wikitext
|
||||||
|
['data-nosource', '']
|
||||||
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: 'TAG',
|
type: 'TAG',
|
||||||
name: 'a',
|
name: 'a',
|
||||||
attribs:
|
attribs: [
|
||||||
[['data-type', 'hashlink'],
|
['data-type', 'hashlink'],
|
||||||
['href', '#' + ref.target]
|
['href', '#' + ref.target]
|
||||||
// XXX: Add round-trip info here?
|
// XXX: Add round-trip info here?
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: 'TEXT',
|
type: 'TEXT',
|
||||||
|
@ -161,6 +181,13 @@ Cite.prototype.onRef = function ( tokenCTX ) {
|
||||||
return tokenCTX;
|
return tokenCTX;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle references tag tokens.
|
||||||
|
*
|
||||||
|
* @method
|
||||||
|
* @param {Object} TokenContext
|
||||||
|
* @returns {Object} TokenContext
|
||||||
|
*/
|
||||||
Cite.prototype.onReferences = function ( tokenCTX ) {
|
Cite.prototype.onReferences = function ( tokenCTX ) {
|
||||||
|
|
||||||
var refGroups = this.refGroups;
|
var refGroups = this.refGroups;
|
||||||
|
@ -174,36 +201,41 @@ Cite.prototype.onReferences = function ( tokenCTX ) {
|
||||||
attribs: [['id', ref.target]]
|
attribs: [['id', ref.target]]
|
||||||
}];
|
}];
|
||||||
if (ref.linkbacks.length == 1) {
|
if (ref.linkbacks.length == 1) {
|
||||||
out = out.concat([{
|
out = out.concat([
|
||||||
type: 'TAG',
|
{
|
||||||
name: 'a',
|
type: 'TAG',
|
||||||
attribs:
|
name: 'a',
|
||||||
[['data-type', 'hashlink'],
|
attribs: [
|
||||||
['href', '#' + ref.linkbacks[0]]
|
['data-type', 'hashlink'],
|
||||||
]
|
['href', '#' + ref.linkbacks[0]]
|
||||||
},
|
]
|
||||||
{type: 'TEXT', value: arrow},
|
},
|
||||||
{type: 'ENDTAG', name: 'a'}
|
{type: 'TEXT', value: arrow},
|
||||||
],
|
{type: 'ENDTAG', name: 'a'}
|
||||||
ref.tokens // The original content tokens
|
],
|
||||||
|
ref.tokens // The original content tokens
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
out.content.push({type: 'TEXT', value: arrow});
|
out.content.push({type: 'TEXT', value: arrow});
|
||||||
$.each(ref.linkbacks, function(i, linkback) {
|
$.each(ref.linkbacks, function(i, linkback) {
|
||||||
out = out.concat([{
|
out = out.concat([
|
||||||
type: 'TAG',
|
{
|
||||||
name: 'a',
|
type: 'TAG',
|
||||||
attribs:
|
name: 'a',
|
||||||
[['data-type', 'hashlink'],
|
attribs: [
|
||||||
['href', '#' + ref.linkbacks[0]]
|
['data-type', 'hashlink'],
|
||||||
]
|
['href', '#' + ref.linkbacks[0]]
|
||||||
},
|
]
|
||||||
// XXX: make formatNum available!
|
},
|
||||||
//{type: 'TEXT', value: env.formatNum( ref.groupIndex + '.' + i)},
|
// XXX: make formatNum available!
|
||||||
{type: 'TEXT', value: ref.groupIndex + '.' + i},
|
//{
|
||||||
{type: 'ENDTAG', name: 'a'}
|
// type: 'TEXT',
|
||||||
],
|
// value: env.formatNum( ref.groupIndex + '.' + i)
|
||||||
ref.tokens // The original content tokens
|
//},
|
||||||
|
{type: 'TEXT', value: ref.groupIndex + '.' + i},
|
||||||
|
{type: 'ENDTAG', name: 'a'}
|
||||||
|
],
|
||||||
|
ref.tokens // The original content tokens
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -220,22 +252,35 @@ Cite.prototype.onReferences = function ( tokenCTX ) {
|
||||||
if (options.group in refGroups) {
|
if (options.group in refGroups) {
|
||||||
var group = refGroups[options.group];
|
var group = refGroups[options.group];
|
||||||
var listItems = $.map(group.refs, renderLine);
|
var listItems = $.map(group.refs, renderLine);
|
||||||
tokenCTX.token = [{
|
tokenCTX.token = [
|
||||||
type: 'TAG',
|
{
|
||||||
name: 'ol',
|
type: 'TAG',
|
||||||
attribs: [['class', 'references']]
|
name: 'ol',
|
||||||
}].concat(listItems, {type: 'ENDTAG', name: 'ol'});
|
attribs: [
|
||||||
|
['class', 'references']
|
||||||
|
]
|
||||||
|
}
|
||||||
|
].concat( listItems, { type: 'ENDTAG', name: 'ol' } );
|
||||||
} else {
|
} else {
|
||||||
tokenCTX.token = {
|
tokenCTX.token = {
|
||||||
type: 'SELFCLOSINGTAG',
|
type: 'SELFCLOSINGTAG',
|
||||||
name: 'placeholder',
|
name: 'placeholder',
|
||||||
attribs: [['data-origNode', JSON.stringify(token)]]
|
attribs: [
|
||||||
|
['data-origNode', JSON.stringify(token)]
|
||||||
|
]
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return tokenCTX;
|
return tokenCTX;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle end token.
|
||||||
|
*
|
||||||
|
* @method
|
||||||
|
* @param {Object} TokenContext
|
||||||
|
* @returns {Object} TokenContext
|
||||||
|
*/
|
||||||
Cite.prototype.onEnd = function ( tokenCTX ) {
|
Cite.prototype.onEnd = function ( tokenCTX ) {
|
||||||
// XXX: Emit error messages if references tag was missing!
|
// XXX: Emit error messages if references tag was missing!
|
||||||
// Clean up
|
// Clean up
|
||||||
|
|
Loading…
Reference in a new issue