跳转到内容

User:Diskdance/MOSNUM dates.js

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
/**
 * 本工具用于修正日期格式,请在使用前阅读[[User:Vozhuo/Tool/MOSNUM_dates]]的文档。如工具出现问题,可以联系我。
 * 除主要选项外,侧栏还会出现一个正则表达式编辑器(Regex editor)
 */
$.when(
	mw.loader.using('ext.gadget.HanAssist'),
	$.ajax(
		'//tools-static.wmflabs.org/meta/scripts/pathoschild.templatescript.js',
		{ dataType: 'script', cache: true },
	),
	$.ajax(
		'//en.wikipedia.org/w/index.php?title=User:Ohconfucius/script/MOSNUM_utils.js&action=raw&ctype=text/javascript',
		{ dataType: 'script', cache: true },
	),
).then(function(require) {
	const { conv } = require('ext.gadget.HanAssist');

	/**
	 * Add custom extension methods to the TemplateScript editor.
	 * @param editor The TemplateScript editor to extend.
	 */
	function ohc_extend_editor(editor) {
		editor.ohc_regex = function(rg, sub, func) {
			var text = editor.get();
			text = ohc.dateutil.regex(text, rg, sub, func);
			editor.set(text);

			return editor;
		};
	}

	var summary = {
		en_date: conv({
			hans:
				'基于[[维基百科:格式手册/日期和数字|格式手册]]修正日期格式,由[[User:Vozhuo/Tool/MOSNUM dates|Javascript]]驱动',
			hant:
				'基於[[维基百科:格式手册/日期和数字|格式手冊]]修正日期格式,由[[User:Vozhuo/Tool/MOSNUM dates|Javascript]]驅動',
		}),
		zh_date: conv({
			hans: '参考资料中文日期格式[[User:Vozhuo/Tool/MOSNUM dates|转ISO]]',
			hant: '參考資料中文日期格式[[User:Vozhuo/Tool/MOSNUM dates|轉ISO]]',
		}),
		dmy_date: conv({
			hans: '参考资料ISO日月年格式[[User:Vozhuo/Tool/MOSNUM dates|修正]]',
			hant: '參考資料ISO日月年格式[[User:Vozhuo/Tool/MOSNUM dates|修正]]',
		}),
		mdy_date: conv({
			hans: '参考资料ISO月日年格式[[User:Vozhuo/Tool/MOSNUM dates|修正]]',
			hant: '參考資料ISO月日年格式[[User:Vozhuo/Tool/MOSNUM dates|修正]]',
		}),
		del_language: conv({
			hans: '[[User:Vozhuo/Tool/MOSNUM dates|删除]]所有语言参数',
			hant: '[[User:Vozhuo/Tool/MOSNUM dates|刪除]]所有語言參數',
		}),
	};

	function edit_summary(editor, summary) {
		editor
			.options({ minor: true })
			.appendEditSummary(summary)
			.clickDiff();
	}

	function fix_ISO_dates(editor) {
		// ISO style
		editor
			.ohc_regex(
				/(\|\s*(?:(?:(?:access|archive|)-?)date)\s*=\s*)@YYYY[.\/-]@MM[.\/-]@DD(?=[\s]*[|}])/gi,
				'$1@YYYY-@MM-@DD',
			);
	}

	function fix_mdy_dates(editor) {
		// US style
		editor
			.ohc_regex(
				/(\|\s*(?:(?:(?:access|archive|)-?)date)\s*=\s*)@MM[.\/-]@DD[.\/-]@YYYY(?=[\s]*[|}])/gi,
				'$1@YYYY-@MM-@DD',
				function(d) {
					return d.y > 12;
				},
			);
	}

	function fix_dmy_dates(editor) {
		// UK style
		editor
			.ohc_regex(
				/(\|\s*(?:(?:(?:access|archive|)-?)date)\s*=\s*)@DD[.\/-]@MM[.\/-]@YYYY(?=[\s]*[|}])/gi,
				'$1@YYYY-@MM-@DD',
				function(d) {
					return d.y === d.m || d.y > 12;
				},
			);
	}

	function en_dates(editor) {
		editor
			.ohc_regex(
				/(\|\s*(?:(?:(?:access|archive|)-?)date)\s*)=(\s*)@Day @Month,? @YYYY(?=[\s]*[|}])/gi,
				'$1=$2@YYYY-@MM-@DD',
			)
			.ohc_regex(
				/(\|\s*(?:(?:(?:access|archive|)-?)date)\s*)=(\s*)@Month @Day,? @YYYY(?=[\s]*[|}])/gi,
				'$1=$2@YYYY-@MM-@DD',
			)
			.ohc_regex(
				/(?:Retrieved |Accessed)[ ]*(?:on |)@Day @Month,? @YYYY(?=\s*[|}])/gi,
				'Retrieved @YYYY-@MM-@DD',
			)
			.ohc_regex(
				/(?:Retrieved |Accessed)[ ]*(?:on |)@Month @Day,? @YYYY(?=\s*[|}])/gi,
				'Retrieved @YYYY-@MM-@DD',
			)
			.replace(/\|\s*df\s*=\s*[\w-]*(?=[\s]*[|}])/gi, '')
			.replace(/{{\s*use (dmy|mdy) dates\s*\|[^}]*}}(\n)?/gi, '');
	}

	function zh_dates(editor) {
		editor
			.replace(
				/(\|\s*(?:access|archive|)-?date\s*=\s*\d+)年(\d+)月(\d+)日(?=[\s]*[|}])/gi,
				function(m, p1, p2, p3) {
					return p1 + '-' + (Array(2).join(0) + p2).slice(-2) + '-'
						+ (Array(2).join(0) + p3).slice(-2); // http://www.nowamagic.net/javascript/js_AddZeroFrontOfNumber.php
				},
			);
	}

	function delete_language(editor) {
		editor
			.replace(/(\|\s*language\s*=\s*)([\w-]*)(?=[\s]*[|}])/gi, '$1');
	}

	function en_dates_driver(editor) {
		ohc_extend_editor(editor);
		en_dates(editor);
		edit_summary(editor, summary.en_date);
	}

	function en_dates_no_diff_driver(editor) {
		ohc_extend_editor(editor);
		en_dates(editor);
		editor
			.options({ minor: true })
			.appendEditSummary(summary.en_date);
	}

	function zh_dates_driver(editor) {
		ohc_extend_editor(editor);
		zh_dates(editor);
		edit_summary(editor, summary.zh_date);
	}

	function dmy_dates_driver(editor) {
		ohc_extend_editor(editor);
		fix_ISO_dates(editor);
		fix_dmy_dates(editor);
		edit_summary(editor, summary.dmy_date);
	}

	function mdy_dates_driver(editor) {
		ohc_extend_editor(editor);
		fix_ISO_dates(editor);
		fix_mdy_dates(editor);
		edit_summary(editor, summary.mdy_date);
	}

	function del_language_driver(editor) {
		ohc_extend_editor(editor);
		delete_language(editor);
		edit_summary(editor, summary.del_language);
	}

	pathoschild.TemplateScript.add(
		[
			{
				name: '英文日期->ISO',
				tooltip: '英文日期格式修正至ISO格式',
				script: en_dates_driver,
			},
			{
				name: conv({
					hans: '英文日期->ISO (不显示更改)',
					hant: '英文日期->ISO (不顯示更改)',
				}),
				tooltip: '英文日期格式修正至ISO格式,但不显示所做的更改',
				script: en_dates_no_diff_driver,
			},
			{
				name: '中文日期->ISO',
				tooltip: '中文日期格式修正至ISO格式',
				script: zh_dates_driver,
			},
			{
				name: 'ISO日月年->ISO年月日',
				tooltip: 'ISO格式的日月年日期修正,包括ISO年月日标准格式修正',
				script: dmy_dates_driver,
			},
			{
				name: 'ISO月日年->ISO年月日',
				tooltip: 'ISO格式的月日年日期修正,包括ISO年月日标准格式修正',
				script: mdy_dates_driver,
			},
			{
				name: conv({ hans: '删除语言参数', hant: '刪除語言參數' }),
				tooltip: '删除参考资料语言标签',
				script: del_language_driver,
			},
		],
		// common options
		{
			forActions: 'edit',
			category: conv({
				hans: '参考资料日期修正',
				hant: '參考資料日期修正',
			}),
		},
	);
});