User:Guywan/Scripts/Headings.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// [[Category:Wikipedia scripts]]
// <nowiki>
$(function()
{
	if(mw.config.get("wgAction") != "view") return;
	
	const VERSION = "03.06.19";
	
	var level = "==";
	
	// Setup links.
	$("#p-cactions ul").append("<li id='ca-hm'><a href='#' title='Convert pseudo-headings to headings'>PH2H</a></li>");
	$("#ca-hm").on("click", function()
	{
		mw.notify("Starting conversion process.", "info");
		
		$.ajax(
		{
			url : mw.util.getUrl(mw.config.get("wgPageName")) + "?action=raw", dataType : "text"
		})
		.fail(function(result) { mw.notify("Failed to load: " + result, "error"); })
		.done(function(text)
		{
			// Split the page into lines of text.
			text = text.split("\n");
			
			// Run a regex (or few) over each line.
			var modified = false;
			for(var i = 0; i < text.length; i++)
			{
				if(text[i].match(/=====[^=]*=====/g))
				{
					level = "======";
				}
				else if(text[i].match(/====[^=]*====/g))
				{
					level = "=====";
				}
				else if(text[i].match(/===[^=]*===/g))
				{
					level = "====";
				}
				else if(text[i].match(/==[^=]*==/g))
				{
					level = "===";
				}
				else
				{
					// Don't touch definition lists ...
					if(text[i].match(/^;.*/g) && !text[i + 1].match(/^:.*/g))
					{
						text[i] = text[i].replace(/^;.*/g, hm_replace);
						
						modified = true;
					}
				}
			}
			
			// If nothing is modified, abort.
			if(!modified)
			{
				mw.notify("There are no pseudo-headings to convert!", "warn");
				return;
			}
			
			mw.notify("Completed. Showing preview.");
			
			// Put it back together!
			text = text.join("\n");
			
			// Setup the preview.
			new mw.Api().parse(text)
			.fail(function(result) { mw.notify("Failed to parse preview: " + result, "error"); })
			.done(function(data)
			{
				$("#mw-content-text").html(data).prepend("<div style='border-bottom:1px solid #a2a9b1;text-align:center'>"
				+ "<h3>This is a <strong>preview</strong> of the changes you will make.</h3><br/>"
				+ "<button id='hd-submit' class='mw-ui-button mw-ui-progressive'>Submit</button>"
				+ "<button id='hd-cancel' class='mw-ui-button mw-ui-quiet mw-ui-destructive'>Cancel</button></div>");
				
				$("#hd-submit").on("click", function()
				{
					// Post edited text.
					api.post(
					{
						"action" : "edit",
						"title" : mw.config.get("wgPageName"),
						"text" : text,
						"summary" : "Replacing pseudo-headings with headings using [[User:Guywan/Scripts/Headings.js|Headmaster]] V" + VERSION + ".",
						"minor" : true,
						"token" : mw.user.tokens.get("editToken")
					})
					.fail(function(result) { mw.notify("Failed to post: " + result, "error"); })
					.done(function()
					{
						mw.notify("Success! Refreshing.", "info");
						window.location.href = mw.util.getUrl(mw.config.get("wgPageName"));
					});
				});
				
				$("#hd-cancel").on("click", function()
				{
					window.location.href = mw.util.getUrl(mw.config.get("wgPageName"));
				});
			});
		});
	});
	
	function hm_replace(match)
	{
		// If there is a trailing ':', destroy it!
		if(match[match.length - 1] == ":")
		{
			return level + "  " + match.substring(1, match.length - 1) + "  " + level;
		}
		
		return level + " " + match.substr(1) + " " + level;
	}
});
// </nowiki>