User:Prosody/pagelinker.js

//fixme: magic period at end of roman numerals //todo: storage a la TOCindex //     multi-volume support

importScript('User:Prosody/textinputs_standalone.js');

var romandigits = { "i":1, "v":5, "x":10, "l":50, "c":100, "d":500, "m":1000 };

//transform valid roman numeral into decimal numeral function parseRoman (roman) { roman = roman.toLowerCase; var value = 0; value += romandigits[roman.substr(0,1)]; for (var i = 1; i < roman.length; i++) { if (romandigits[roman.substr(i-1,1)] < romandigits[roman.substr(i,1)]) { value += romandigits[roman.substr(i,1)] - romandigits[roman.substr(i-1,1)] - romandigits[roman.substr(i-1,1)]; } else { value += romandigits[roman.substr(i,1)]; }	}	return value; }

//produces a TOCified number function pageLink (pageNum, chaptersArray, pageOffset, anchor) { try { var realPageNum = getRealPageNum(pageNum, chaptersArray, pageOffset); var chapterNum = lookupChapterNum(pageNum, chaptersArray); return ""; } catch (e) { return " " + pageNum; } }

//replaces all numbers with TOCified numbers function linkPages (text) { var pageDict = new Object; var basere = /[^//]+///; $.get('/w/index.php?title=Index:' + basere.exec(wgTitle) + '&action=render', function(data) {		pageRe = /\.+?\<\/a\>/g;		pageNums= data.match(pageRe);		for (var i = 0; i < pageNums.length; i++) {			realNum = pageNums[i].match(/title=\".+?(\d+)\"/)[1];			realNum = parseInt(realNum.match(/\d+$/)[0]);			canNum = pageNums[i].match(/\(.+)\<\/a\>/)[1];			canNum = canNum.replace(/\&nbsp\;/, "");			canNum = canNum.replace(/&#\d+;/, "");			canNum = canNum.replace(/\/, "");			pageDict[canNum]=realNum;		}}, "html"); result = text.replace(/(\d+)\-?\d*/g, function(fullmatch, a) { return pageLink(parseInt(a), chaptersArray, pageOffset, fullmatch)}); if (lastRomanPage(chaptersArray) > 0) { result = result.replace(/\b([iIvVxXlLcCdDmM]+)\.?(\-[iIvVxXlLcCdDmM]+\.?)?\b/g, function(fullmatch, a) { return pageLink(a, chaptersArray, pageOffset, fullmatch)}); }	result = result.replace(/!!(\d+)!!/g, function(fullmatch, a) { return chaptersArray[a][0]; }); return result; }

function pageLinkDriver { try { var editbox=document.getElementById('wpTextbox1'); var editsummary=document.getElementById('wpSummary'); var selection = rangyTextInputs.getSelection(editbox); if (selection.length!=0) { rangyTextInputs.replaceSelectedText(editbox, linkPages(selection.text)); } else { editbox.value = linkPages(editbox.value); }		editsummary.value = editsummary.value + "Automatically linking pages with User:Prosody/pagelinker.js."; if (/you have to manually link\.\}\}/.test(editbox.value)) { throw ("There were some errors matching page numbers to chapters. Please manually edit."); }	} catch (e) { alert(e); } }

//haphazardly adapted from http://en.wikipedia.org/wiki/User:NerdyScienceDude/Scripts/extratoolbarbuttons.js and UsabilityInitiative/EditToolbar/EditToolbar.js if ( typeof $j != 'undefined' && typeof $.fn.wikiEditor != 'undefined' ) { $(document).ready( function {		$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', { 'section': 'main', 'group': 'insert', 'tools': { 'linkpage': { label: 'Link page numbers', type: 'button', icon: '//upload.wikimedia.org/wikipedia/commons/f/f3/Button_link_pages.png', action: { type: 'callback', execute: pageLinkDriver }				}			}		});	}); } else if (typeof $j != 'undefined') { $(document).ready(function {		var image = document.createElement( 'img' );		image.width = 23;		image.height = 22;		image.className = 'mw-toolbar-editbutton';		image.id = 'mw-toolbar-linkpages';		image.src = '//upload.wikimedia.org/wikipedia/commons/f/f3/Button_link_pages.png';		image.border = 0;		image.alt = 'Link together pages.';		image.title = 'Link together pages.';		image.style.cursor = 'pointer';		image.onclick = pageLinkDriver;		document.getElementById( 'toolbar' ).appendChild(image);	}); }