Content deleted Content added
Copy button |
YMD date button; removing <ref name="..." |
||
(One intermediate revision by the same user not shown) | |||
Line 1:
// v.
window.webRef = window.webRef || {}; // object used to communicate with webRefSetup
window.webRef.getRef = (function () {
Line 31:
// these words are used when spliting names in the case of multipe article authors
var andWords = ['and']; // LOCALIZE by adding the word(s) for "and" in the local lang
// Use %D% or %DD%, %M% or %MM% or %MMM%, %YY% or %YYYY%.
// %M% -> Feb is 2; %MM% -> Feb is 02; %MMM% -> Feb is monthNumToName[1]
var dateFormatMDY = '%MMM% %D%, %YYYY%';
var dateFormatDMY = '%D% %MMM% %YYYY%';
var dateFormatYMD = '%YYYY%-%MM%-%DD%';
var dateFormatDefault = dateFormatDMY;
var dateFormatRetrieved = ''; // Leave empty to use dateFormatDefault
var templateParams = { // LOCALIZE
Line 38 ⟶ 46:
work: 'work',
date: 'date',
accessDate: '
url: 'url',
publisher: 'publisher',
Line 46 ⟶ 54:
last: 'last',
first: 'first',
coauthors: 'coauthors', // not
authorWikiArticle: 'authorlink',
barV: '| ', // bar and spaces around it (but no newlines) for Vertical format
barH: ' |', // ... for Horizontal format
eqV: ' = ', // equal sign and spaces around it (but no newline) for Vertical format
eqH: '=', // ... for Horizontal format
urlEqH: '= ' // a space before the URL helps with line wrapping
};
var tp = templateParams;
var barV = tp.barV, barH = tp.barH, eqV = tp.eqV, eqH = tp.eqH;
var msgs = webRef.idToText = webRef.msgs = webRef.msgs || webRef.idToText || {};
Line 64 ⟶ 75:
setupButton: 'Site setup',
hideButton: 'Close',
copyButton: 'Copy',
compactCopyButton: 'Compact & Copy',
compactSelectButton : 'Compact & Select',
copyFailed: ' Copying failed!',
reloadButton: 'Reload',
formatNamesPromptButton: 'Authors',
formatNamesPrompt: 'Enter the author names (separated by commas if more than one author) to be formatted and inserted into the template:',
dmy: 'DMY',
mdy: 'MDY',
ymd: 'YMD',
refName: 'RefName',
singleMultiLineButton: 'Horiz./Vertical',
couldntParse: 'Sorry, I couldn\'t parse that.',
Line 144 ⟶ 158:
var siteLang; // used for date formatting if equals en-US and month is represented as a number
var monthWarningNeeded; // set in dateFormatter if not clear if date is in U.S. format or not
var siteDateIsUS = false;
var refDocData = window.webRef.refDocData = {
Line 316 ⟶ 331:
var monthStr = '';
var month;
format = format || dateFormatDefault;
for (var m in monthNameToNum) {
Line 384 ⟶ 400:
function formatDateParams(format, useUSFormatIfUnclear) {
var trimCollapse = aux.collapseWhitespace;
var strt = '\\|\\s*';
var end = '\\s*=\\s*([^|}]
var oldVal = codeTextArea.value;
var val = formatDateParam(oldVal, format, tp.date);
if (tp.accessDate && dateFormatRetrieved == '')
val = formatDateParam(val, format, tp.accessDate);
if (val != oldVal) {
codeTextArea.value = val;
Line 399 ⟶ 413:
}
function formatDateParam(templStr, format, paramName
var dateParamRe = new RegExp(strt + paramName + end);
var dateInTemplate = trimCollapse((templStr.match(dateParamRe) || ['', ''])[1]);
if (dateInTemplate)
monthWarningNeeded = false;
if (
var
if (pos < 0) pos = templStr.indexOf('|');
if (pos < 0) pos = 0;
templStr = templStr.replace(dateParamRe, '');
templStr = templStr.slice(0, pos) + '\n'
if (monthWarningNeeded)
addWarning(msgs.monthWarning, msgs.monthWarningTitle);
}
else
alert(msgs.programName + ':\n' + msgs.couldntParse + '\n' +
}
return templStr;
Line 431 ⟶ 441:
function formatNamesPrompt() {
var val = codeTextArea.value;
var names = '';
var authorParamNames = [
for (var n = 1; n <= 9; n++) {
authorParamNames.push(
authorParamNames.push(
}
var strt = '\\|\\s*';
var end = '\\s*=\\s*([^|}]+)';
names = (val.match(strt +
for (var n = 0; n <= 9; n++)
names += (val.match(strt +
.replace(/ /g, '_') + ','; if (
names += (val.match(strt +
names = names.replace(/\s+/g, ' ').replace(/\s?,\s?/g, ',')
.replace(/,,+/g, ',').replace(/^,|,$/g, '').replace(/,/g, ', ');
Line 454 ⟶ 463:
var namesNewParams = authorParams(nameFormatter(userAuthors));
if (namesNewParams) {
var
if (
for (var n = 0; n < authorParamNames.length; n++)
val = val.replace(new RegExp(strt + authorParamNames[n] + end), '');
val = val.slice(0, pos) + namesNewParams + val.slice(pos);
codeTextArea.value = val;
Line 489 ⟶ 497:
function authorParams(
var str = '';
var nl = '\n
if (!authorsArr && siteObj.authorName) {
if (siteObj.authorWikiArticle)
str += nl +
}
else if ((
if (additionalNotAnAuthor) { // to remove e.g. "CNN" from "James Smith, CNN"
var i = authorsArr.length;
while (i--) {
if (authorsArr[i][0] === additionalNotAnAuthor && !authorsArr[i][1])
}
}
if ( authorsArr.length == 1 && !authorsArr[0][1] ) // only 1 author without a first name
str = nl +
else { // use the 'last' and 'first' params
for (var i =
var num = (i > 0 || authorsArr.length > 1 ? String(i + 1) : ''); // omit number if only 1 author
if (
if (
}
}
}
return str;
Line 547 ⟶ 535:
var val = aux.trimStr(codeTextArea.value);
var nowMulti = (val.indexOf('\n') > -1) && !toMulti;
if ( toMulti === false || (!toMulti && nowMulti) ) {
val = val.replace(/\n/g, ' ')
.replace(/\s*\|\s*([^|= ]+)\s*=\s*/g, barH + '$1' + eqH)
// a space after |url= helps with line wrapping :
} else {
val = val.replace(/\s*\|\s*([^|= ]+)\s*=\s*/g, '\n' + barV + '$1' + eqV ).replace(/\s*}}/, '\n}}')
.replace( // move the "last" and "first" parameters on one line
new RegExp('(\\|\\s*' +
'$1 $2'
);
Line 589 ⟶ 579:
var buttons = [
{
id: copyButtonSupported ? '
onclick: function () {
var
val = val.replace(new RegExp('\\s*\\|\\s*' + tp.quote + '\\s*=\\s*(\\||})'), '$1') // rm empty quote
.replace(new RegExp('\\s*\\|\\s*' + tp.lang + '\\s*=\\s*(\\||})'), '$1') // rm empty lang
.replace(/\s+}}\s*$/, '}}'); // remove spaces before and after final }}
codeTextArea.value = val;
singleMultiLine(false);
codeTextArea.focus();
codeTextArea.select();
if (copyButtonSupported) {
var copyFailed = !refFrame.doc.queryCommandEnabled('copy');
Line 608 ⟶ 603:
},
{
id: '
onclick: function () { formatDateParams(dateFormatDMY); }
},
{
id: 'mdy',
onclick: function () { formatDateParams(dateFormatMDY); }
},
{
id: 'ymd',
onclick: function () { formatDateParams(dateFormatYMD); }
},
{
id: 'refName',
onclick: function () {
var val, oldVal = codeTextArea.value;
val = oldVal.replace(/^<ref name="[^"]*"/, '<ref');
if (val == oldVal)
val = val.replace(/^<ref/, '<ref name=""');
codeTextArea.value = val;
}
},
Line 653 ⟶ 660:
br();
var btn;
for (var i = 0; i < buttons.length; i++) {
if (!btn.id)
continue;
docFrag.appendChild(dom.newEl('button', {
text: msgs[btn.id],
onclick: btn.onclick
}));
dom.text(' ', docFrag);
}
dom.text('| ', docFrag);
docFrag.appendChild(dom.newEl('a', {
Line 811 ⟶ 821:
function dateToString(day, month, year, format) { // month is 0..11
day = +day; month = +month; year = +year;
return
.replace(/%DD%/, (day > 9 ? '' : '0') + day)
.replace(/%D%/, day)
Line 818 ⟶ 828:
.replace(/%M%/, (month + 1))
.replace(/%YYYY%/, year)
.replace(/%YY%/, year % 100
}
Line 828 ⟶ 838:
today.getMonth(),
today.getFullYear(),
(
);
}
Line 844 ⟶ 854:
}
return siteName;
}
Line 910 ⟶ 915:
function go() {
var title, date, lang, langParam, quote, selection, work, publisher;
Line 929 ⟶ 933:
title = cleanParam(searchFor('title'));
monthWarningNeeded = false;
date = searchFor('date');
Line 945 ⟶ 950:
}
if (!lang || lang != langToIgnore) // add the param only if lang is not langToIgnore
langParam =
quote =
selection = getSelectedText();
if (selection) {
Line 961 ⟶ 966:
publisher = siteObj.publisher || '';
codeTextArea.value = '<ref
+
+ '\n
+ (!lang || lang == langToIgnore ? '' : '\n
+ authorParams(null, work)
+ '\n
+ '\n
+ '\n
+ '\n
+ (publisher ?
+ langParam
+ quote
|