ผู้ใช้:Patsagorn Y./common.js

จาก วิกิซอร์ซ

หมายเหตุ: หลังเผยแพร่ คุณอาจต้องล้างแคชเว็บเบราว์เซอร์ของคุณเพื่อดูการเปลี่ยนแปลง

  • ไฟร์ฟอกซ์ / ซาฟารี: กด Shift ค้างขณะคลิก Reload หรือกด Ctrl-F5 หรือ Ctrl-R (⌘-R บนแมค)
  • กูเกิล โครม: กด Ctrl-Shift-R (⌘-Shift-R บนแมค)
  • อินเทอร์เน็ตเอกซ์พลอเรอร์ และ Edge: กด Ctrl ค้างขณะคลิก Refresh หรือกด Ctrl-F5
  • โอเปร่า: กด Ctrl-F5
// <nowiki>
mw.loader.load("//wikisource.org/w/index.php?title=MediaWiki:GoogleOCR.js&action=raw&ctype=text/javascript"); // Google OCR
// mw.loader.load("//en.wikisource.org/w/index.php?title=MediaWiki:Gadget-ocr.js&action=raw&ctype=text/javascript"); // OCR
mw.loader.load("//en.wikisource.org/w/index.php?title=MediaWiki:Gadget-Preload_Page_Images.js&action=raw&ctype=text/javascript");
/**
 * TemplateScript adds configurable templates and scripts to the sidebar, and adds an example regex editor.
 * @see https://meta.wikimedia.org/wiki/TemplateScript
 * @update-token [[File:Pathoschild/templatescript.js]]
 */

const temmodule = {
    thaiNum:     "๐๑๒๓๔๕๖๗๘๙".split(''),
    thaiNumWord: "ศูนย์ หนึ่ง สอง สาม สี่ ห้า หก เจ็ด แปด เก้า".split(" ")
};
 
function numToThaiNum (num) {
        return !!temmodule.thaiNum[num] ? temmodule.thaiNum[num] : num;
}
 
function dotToThaiNum (dot) {
        let regex = /\.(.+)\./i;
        if (!regex.test(dot)) return;
        let match = dot.match(regex);
        let dotToNum = temmodule.thaiNumWord.indexOf(match[1]) == -1 ? '‘' + match[1] + '’' : temmodule.thaiNumWord.indexOf(match[1]);
        return numToThaiNum(dotToNum);
}

function onTemplate (content) {
    content = content.replace(/ฃ(.+)ฃ/, '{{$1|')
    .replace(/ฃ/ig,'|')
    .replace(/ฅ$/, '}}');
    return content;
}

$.ajax('//tools-static.wmflabs.org/meta/scripts/pathoschild.templatescript.js', {
    dataType: 'script',
    cache: true
}).then(function() {
    pathoschild.TemplateScript.add({
        category: 'เพิ่มความสะดวกสะบาย',
        name: 'ขยายโค้ดย่อ',
        title: 'ขยาย short hand ที่ใช้',
        accessKey: 'x',
        forActions: 'edit',
        script: function(editor) {
            var escaped = editor.escape(/(\{\{[^}]+\}\}|\\\,|\\\.)/g);
            editor
			// วรรคหน้า-หลังเลขไทย
			.replace(/([^ฃ])([๐-๙]+)([^ฃ])/g,'$1 $2 $3')
				 
			// วรรคหน้า-หลัง 'ๆ'
			.replace(/([^ฃ\ ])ๆ(\S)/ig, '$1 ๆ $2')
				
			// แปลงเลขไทย '.หนึ่ง.' -> '๑'
			.replace(/\.([\u0E00-\u0E7Fa-zA-Z]+)\./ig, dotToThaiNum)
				
			// วรรคหน้า-หลัง '‘’'
			.replace(/(\S)\‘(.+)\’(\S)/ig, '$1 ‘$2’ $3')
			.replace(/(\s)\‘(.+)\’(\S)/ig, '$1‘$2’ $3')
			.replace(/(\S)\‘(.+)\’(\s)/ig, '$1 ‘$2’$3')
				
			// จัดการแม่แบบ
			.replace(/ฃ([^ฅ]+)ฅ/g, onTemplate)
            .unescape(escaped)
            .clickPreview();
        }
    });
});

// </nowiki>