Skip to content
This repository was archived by the owner on Jan 16, 2024. It is now read-only.

Commit be696a9

Browse files
committed
Better normalization
1 parent 4ab6521 commit be696a9

File tree

1 file changed

+25
-30
lines changed

1 file changed

+25
-30
lines changed

proveit.js

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ var proveit = {
277277
// Second, look for all the citations and store them in an array for later
278278
var text = proveit.getTextbox().val(),
279279
citations = [],
280-
citationsRegExp = /<\s*ref\s+name\s*=\s*["|']?\s*([^"'\s]+)\s*["|']?\s*\/\s*>/gi, // Three possibilities: <ref name="foo" />, <ref name='foo' /> and <ref name=foo />
280+
citationsRegExp = /<\s*ref\s+name\s*=\s*["|']?\s*([^"'\s]+)\s*["|']?\s*\/\s*>/ig, // Three possibilities: <ref name="foo" />, <ref name='foo' /> and <ref name=foo />
281281
match,
282282
citation;
283283

@@ -287,20 +287,18 @@ var proveit = {
287287
}
288288

289289
// Third, look for all the raw and template references
290-
var matches = text.match( /<\s*ref[\s\S]*?<\s*\/\s*ref\s*>/gi );
290+
var matches = text.match( /<\s*ref[\s\S]*?<\s*\/\s*ref\s*>/ig );
291291

292292
if ( !matches ) {
293293
var noReferencesMessage = $( '<div>' ).attr( 'id', 'proveit-no-references-message' ).text( proveit.getMessage( 'no-references' ) );
294294
$( '#proveit-reference-list' ).append( noReferencesMessage );
295295
return false;
296296
}
297297

298-
var i, j, referenceString, reference, referenceItem;
298+
var i, j, reference, referenceItem;
299299
for ( i = 0; i < matches.length; i++ ) {
300300
// Turn all the matches into reference objects
301-
referenceString = matches[ i ];
302-
referenceString = referenceString.replace( /(\r\n|\n|\r)/gm, ' ' ); // Replace newlines by whitespaces
303-
reference = proveit.makeReference( referenceString );
301+
reference = proveit.makeReference( matches[ i ] );
304302

305303
// For each reference, check the citations array for citations to it
306304
for ( j = 0; j < citations.length; j++ ) {
@@ -327,38 +325,35 @@ var proveit = {
327325

328326
// First we need to determine what kind of reference we're dealing with
329327
// So we get all the template names and search for a match
330-
var registeredTemplatesArray = [],
331-
registeredTemplate;
328+
var registeredTemplate,
329+
registeredTemplatesArray = [];
332330
for ( registeredTemplate in proveit.templates ) {
333-
registeredTemplate = registeredTemplate.substr( registeredTemplate.indexOf( ':' ) + 1 ), // Remove the namespace
331+
registeredTemplate = registeredTemplate.substring( registeredTemplate.indexOf( ':' ) + 1 ); // Remove the namespace
334332
registeredTemplatesArray.push( registeredTemplate );
335333
}
336334
var registeredTemplatesDisjunction = registeredTemplatesArray.join( '|' ),
337-
regExp = new RegExp( '{{(' + registeredTemplatesDisjunction + ').*}}', 'i' ),
335+
regExp = new RegExp( '{{(' + registeredTemplatesDisjunction + ')([\\s\\S]*)}}', 'i' ),
338336
match = referenceString.match( regExp ),
339337
reference;
340338

341339
if ( match ) {
342340
reference = new proveit.TemplateReference({ 'string': referenceString });
343341

344-
// Extract the full template string
345-
var templateString = match[0];
346-
347342
// Extract the name of the template
348-
var template = match[1],
349-
template = template.charAt(0).toUpperCase() + template.slice(1); // Capitalize the first letter
343+
var template = match[1];
350344

351345
// Normalize it
352-
for ( registeredTemplate in proveit.templates ) {
353-
registeredTemplate
346+
for ( registeredTemplate in registeredTemplatesArray ) {
354347
if ( template.toLowerCase() === registeredTemplate.toLowerCase() ) {
355348
template = registeredTemplate;
356349
}
350+
console.log( registeredTemplatesArray, registeredTemplate, template );
357351
}
358352
reference.template = template;
359353

360354
// Next, extract the parameters
361-
var paramsString = templateString.substring( templateString.indexOf( '|' ) + 1, templateString.length - 2 ), // From after the first pipe to before the closing "}}"
355+
var paramsString = match[2],
356+
paramsString = paramsString.substring( paramsString.indexOf( '|' ) + 1 ), // Remove everything before the first pipe
362357
paramsArray = paramsString.split( '|' ),
363358
paramString, paramNameAndValue, paramName, paramValue;
364359

@@ -416,9 +411,9 @@ var proveit = {
416411
*
417412
* The citation class is the base class. It has the properties and methods common to all references.
418413
*
419-
* @param {object} argObj Data for constructing the object
414+
* @param {object} Data for constructing the object
420415
*/
421-
Citation: function ( argObj ) {
416+
Citation: function ( data ) {
422417

423418
/**
424419
* Name of the class
@@ -430,17 +425,17 @@ var proveit = {
430425
*
431426
* This is the value of the "name" parameter of the <ref> tag: <ref name="abc" />
432427
*/
433-
this.name = argObj.name;
428+
this.name = data.name;
434429

435430
/**
436431
* Location of this reference in the edit textbox
437432
*/
438-
this.index = argObj.index;
433+
this.index = data.index;
439434

440435
/**
441436
* Wikitext for this reference.
442437
*/
443-
this.string = argObj.string;
438+
this.string = data.string;
444439

445440
/**
446441
* Highlight the string in the textbox and scroll it to view
@@ -472,14 +467,14 @@ var proveit = {
472467
* Class for raw references: <ref>This is a raw reference, it uses no templates.</ref>
473468
*
474469
* @extends Citation
475-
* @param {object} argObj Data for constructing the object
470+
* @param {object} Data for constructing the object
476471
*/
477-
RawReference: function ( argObj ) {
472+
RawReference: function ( data ) {
478473

479474
/**
480475
* Extend the Citation class
481476
*/
482-
proveit.Citation.call( this, argObj );
477+
proveit.Citation.call( this, data );
483478

484479
/**
485480
* Name of the class
@@ -545,14 +540,14 @@ var proveit = {
545540
* Class for template references: <ref>{{Cite book |first=Charles |last=Darwin |title=The Origin of Species}}</ref>
546541
*
547542
* @extends RawReference
548-
* @param {object} argObj Data for constructing the object
543+
* @param {object} Data for constructing the object
549544
*/
550-
TemplateReference: function ( argObj ) {
545+
TemplateReference: function ( data ) {
551546

552547
/**
553548
* Extend the RawReference class
554549
*/
555-
proveit.RawReference.call( this, argObj );
550+
proveit.RawReference.call( this, data );
556551

557552
/**
558553
* Name of the class
@@ -564,7 +559,7 @@ var proveit = {
564559
/**
565560
* Name of the template used by this reference.
566561
*/
567-
this.template = argObj.template;
562+
this.template = data.template;
568563

569564
/**
570565
* Object mapping the parameter names of this reference to their values

0 commit comments

Comments
 (0)