משתמש:Freddy9/common.js
הערה: לאחר הפרסום, ייתכן שיהיה צורך לנקות את זיכרון המטמון (cache) של הדפדפן כדי להבחין בשינויים.
- פיירפוקס / ספארי: להחזיק את המקש Shift בעת לחיצה על טעינה מחדש (Reload) או ללחוץ על צירוף המקשים Ctrl-F5 או Ctrl-R (במחשב מק: ⌘-R).
- גוגל כרום: ללחוץ על צירוף המקשים Ctrl-Shift-R (במחשב מק: ⌘-Shift-R).
- אינטרנט אקספלורר / אדג': להחזיק את המקש Ctrl בעת לחיצה על רענן (Refresh) או ללחוץ על צירוף המקשים Ctrl-F5.
- אופרה: ללחוץ על Ctrl-F5.
$(function () {
var text = document.getElementById('editform').getElementsByTagName('textarea')[0].innerHTML;
//var text = "==שֵׁם עֵרֶךְ מְנֻקָּד== שלום סתם עוד טקסט";
// patterns - sorted by order
var patterns = [
{
regex: /^[=]{2}[^=]{1}.*[=]{2}/im,
compulsory: true,
description: 'כותרת ערך'
},
{
regex: /\{\{ניתוח דקדוקי\|/im,
compulsory: true,
description: 'ניתוח דקדוקי'
},
{
regex: /כתיב מלא\=/im,
compulsory: true,
description: 'כתיב מלא'
},
{
regex: /הגייה\=/im,
compulsory: true,
description: 'הגייה'
},
{
regex: /חלק דיבר\=/im,
compulsory: true,
description: 'חלק דיבר'
},
{
regex: /מין\=/im,
compulsory: true,
description: 'מין'
},
{
regex: /שורש\=/im,
compulsory: true,
description: 'שורש'
},
{
regex: /(\{\{שרש\d{0,1}[^\}]+\}\})/im,
compulsory: false,
description: 'מבנה שורש'
},
{
regex: /דרך תצורה\=/im,
compulsory: true,
description: 'דרך תצורה'
},
{
regex: /נטיות\=/im,
compulsory: true,
description: 'נטיות'
},
{
regex: /\}\}/im,
compulsory: true,
description: 'סוף ניתוח דקדוקי'
},
{
regex: /\#\s/im,
compulsory: true,
description: 'הגדרה מילונית'
},
{
regex: /\#\:\*\s/im,
compulsory: true,
description: 'משפט לדוגמה'
},
{
regex: /\=\=\=גיזרון\=\=\=/im,
compulsory: false,
description: 'גיזרון'
},
{
regex: /\=\=\=צירופים\=\=\=/im,
compulsory: false,
description: 'צירופים'
},
{
regex: /\=\=\=נגזרות\=\=\=/im,
compulsory: false,
description: 'נגזרות'
},
{
regex: /\=\=\=מילים נרדפות\=\=\=/im,
compulsory: false,
description: 'מילים נרדפות'
},
{
regex: /\=\=\=ניגודים\=\=\=/im,
compulsory: false,
description: 'ניגודים'
},
{
regex: /\=\=\=תרגום\=\=\=/im,
compulsory: false,
description: 'תרגום'
},
{
regex: /\=\=\=ראו גם\=\=\=/im,
compulsory: false,
description: 'ראו גם'
},
{
regex: /\=\=\=קישורים חיצוניים\=\=\=/im,
compulsory: false,
description: 'קישורים חיצוניים'
},
{
regex: /(\[\[קטגוריה\:.+\]\]\n)+/img,
compulsory: false,
description: 'קטגוריות'
},
];
function validateValue(text, patterns) {
var valid = true;
for (var i = 0; i < patterns.length; i++) {
// test
var result = patterns[i].regex.exec(text);
// if test failed, return the description of the pattern:
// if component is compulsory:
if (!result) {
if (patterns[i].compulsory) {
console.error('FAILED: ' + patterns[i].description);
mw.notify('the value is not valid. read in console for details.', {type: 'error', title:'NOT VALID', autoHide: false});
valid = false;
}
else {
console.log(patterns[i].description + ': failed and not compulsory');
}
}
// if pattern compulsory - sub the index of the end of the matched string from the whole string
else {
text = text.substring(result.index + result[0].length);
console.log(patterns[i].description + ': matched: "' + result[0] + '" result.index: ' + result.index + ', result[0].length: ' + result[0].length + ', interstring: ' + result.input.substring(0, result.index));
}
//console.log(text);
}
if (valid) mw.notify('the value is valid.');
return valid;
}
validateValue(text, patterns);
// TODO: add for translations: if last regex was found, then test; else - continue.
// this is just for NOUNS.
// TODO ask ori about shoresh in the end
// TODO: add iteration to values ( for <value> validateValue(value))
});