User:Ohconfucius/dates.js

维基百科,自由的百科全书

注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。

// *********************************************************************************************

//This is a test (non-production) script, and may have untested errors. Please exercise due care should you decide to use it.

// *********************************************************************************************


// Feedback and constructive criticism are welcome

/**
 * Regex menu framework
 * by [[m:user:Pathoschild]] <http://meta.wikimedia.org/wiki/User:Pathoschild/Scripts/Regex_menu_framework>
 * - adds a sidebar menu of user-defined scripts.
 */
mw.loader.load('https://meta.wikimedia.org/w/index.php?title=User:Pathoschild/Scripts/Regex_menu_framework.js&action=raw&ctype=text/javascript');

function ohc_delink_ISO()
{
    regex(/\[\[([12]\d{3}年)\]\]/gi, '$1');
    regex(/\[\[([1]?\d月[123]?\d日)\]\]/gi, '$1');

    regex(/([^\w\d\/-])((?:1\d|20)\d{2})-(1\d)-([1-2]\d|30|31)(?=[^\w\d\/-])/gi, '$1$2年$3月$4日');
    regex(/([^\w\d\/-])((?:1\d|20)\d{2})-(1\d)-0?(\d)(?=[^\w\d\/-])/gi, '$1$2年$3月$4日');
    regex(/([^\w\d\/-])((?:1\d|20)\d{2})-0?(\d)-([1-2]\d|30|31)(?=[^\w\d\/-])/gi, '$1$2年$3月$4日');
    regex(/([^\w\d\/-])((?:1\d|20)\d{2})-0?(\d)-0?(\d)(?=[^\w\d\/-])/gi, '$1$2年$3月$4日');
}


/** ------------------------------------------------------------------------ **/
/// DATE PROTECTION

var linkmap=[];
function ohc_protect_dates()
{
	// protects dates within links, quotes, etc
	// the sensitive part is stored and replaced with a unique identifier,
	// which is later replaced with the stored part.

	var protect_function = function(s, begin, replace, end) {
		linkmap.push(replace);
		return begin + "⍌"+(linkmap.length-1)+"⍍" + end;
	}

	regex(/((?:Category|Image|File):)([^|\]]*)([\|\]])/gi, protect_function);
	regex(/(\{(?:See ?also|Main))(\|[^\}]*)(\})/gi, protect_function);
	regex(/(\[(?:http:|https:|ftp:))([^\s\]]*)([\s\]])/gi, protect_function);
	regex(/(<blockquote>)(.*?)(<\/blockquote>)/gi, protect_function);
	regex(/((?:file|image\d?|image location\d?|img|pic|title|quote|journal|url|work|doi)\s*=)([^\|\}]*)([\|\}])/gi, protect_function);
	regex(/((?:file|image\d?|image location\d?|img|pic|title|quote|journal|url|work|doi)\s*=)([^\|\}]*)([\|\}])/gi, protect_function);
	regex(/(\{[^\{]{0,6}(?:quot[^\|]{1,7}\s?|sic)\|)([^\}]+)(\})/gi, protect_function);
	regex(/([\s\(]")([^"\n]*)(")/gi, protect_function);
	regex(/([\s\(]“)([^”\n]*)(”)/gi, protect_function);

}

function ohc_unprotect_dates()
{
		//unprotect all dates
	regex(/⍌([0-9]+)⍍/g, function(x, n) {
		var res = linkmap[n];
		res = res.replace(/⍌([0-9]+)⍍/g, function(x, n) {
			var res = linkmap[n];
			res = res.replace(/⍌([0-9]+)⍍/g, function(x, n) {
				var res = linkmap[n];
				res = res.replace(/⍌([0-9]+)⍍/g, function(x, n) {
					return linkmap[n];
				});
				return res;
			});
			return res;
		});
		return res;
	});
}

/** ------------------------------------------------------------------------ **/
/// EDIT SUMMARIES 

function ohc_MOSNUM_edit_summary()
{
	//Add a tag to the summary box
	setoptions(minor='true'); 		
	setreason('统一格式(年月日)', 'append');
	doaction('diff');
}

/** ------------------------------------------------------------------------ **/
/// CUSTOMIZATION POINTS
function ohc_customize_all_to_dmy() {}
function ohc_customize_all_to_mdy() {}
function ohc_customize_body_to_dmy() {}
function ohc_customize_body_to_mdy() {}
function ohc_customize_ISO_to_dmy() {}
function ohc_customize_ISO_to_mdy() {}

/** ------------------------------------------------------------------------ **/
/// DRIVER FUNCTIONS

function ohc_ISO_to_ymd_driver()
{
	ohc_protect_dates();
	ohc_delink_ISO();
	ohc_customize_ISO_to_dmy();
	ohc_unprotect_dates();

  ohc_MOSNUM_edit_summary();
}

$(function () {
	if(document.forms.editform) {
	mw.util.addPortletLink('p-tb', 'javascript:ohc_ISO_to_ymd_driver()', 'ISO 年月日', 't-ISOymd', 'ISO 年月日', '', '');
	}
});