User:George Orwell III/parseIndex.js

From Wikisource
Jump to navigation Jump to search
Note: After saving, changes may not occur immediately. Click here to learn how to bypass your browser's cache.
  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Cmd-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (Cmd-Shift-R on a Mac)
  • Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Clear the cache in Tools → Preferences

For details and instructions about other browsers, see Wikipedia:Bypass your cache.

// <syntaxhighlight lang=javascript>
/* fills a data page used by Module_Pag */
function parseIndex(base) {
	if (base===undefined && wgPageName.indexOf("Module:Data/")===0) base=wgPageName.replace("Module:Data/","");
	var pdxpc = []; // pdxpc is a simple list in which the page is the index djvu
	var pd = ""; // pd is position djvu -:- should be the position in any source file
	var pc = ""; // pc is scanned page number

	// read html page index corresponding to base
	var html = $.ajax({
		url: "https:"+wgServer+"/w/index.php?action=render&title=Index:" + base,
		async: false
	}).responseText;

	// extraction of the list of links to pages
	var links = $("a[title^='Pag']",$("table",$(html)).eq(0));

	// extraction of pd and pc from the links and assign it to a list in which the index is pd and the value is pc
	for (var i = 0; i < links.length; i += 1) {
		pd = eval($(links[i]).attr("title").match(/\/(\d+) */)[1]);
		pc = $.trim($(links[i]).text().match(/^0*(.+)/)[1]);
		pdxpc[pd] = pc;

	}

	// the list is populated with empty string if gaps are detected
	for (var i = 1; i < pdxpc.length - 1; i += 1) {
		if (pdxpc[i] === undefined) pdxpc[i] = "";
	}
	// builds a text string that represents the Lua script
	// (see test in [[Module:Data/De re metallica (1912).djvu]])
	text = "local d2b = {}\nlocal b2d = {}\nlocal pages = {}\n";
	for (var i = 1; i < pdxpc.length; i += 1) {
		text += "d2b[" + i + "]=\"" + pdxpc[i] + "\"\n";
	}
	text += "for i,v in ipairs(d2b)\n    do\n      b2d[v]=i\n    end\n";


	// the text is written on the page (Module:Data/base) in edit
	// Important: disable the edit mode special
	// data reading chapters
	var wikitext = $.ajax({
		url: wgServer+"/w/index.php?action=raw&title=Index:" + base+ "&r=" + Math.random(),
		async: false
	}).responseText;
	switch(wgServer) {
		case "//en.wikisource.org":
			template="Content";
				break;
		case "//it.wikisource.org":
			template="Indice sommario";
				break;
		case "//la.wikisource.org":
			template="Liber index";
				break;
	}
	lcomm = generateList(wikitext, "<!--", "-->", 1);
	for (var i = 0; i < lcomm.length; i += 1) {
		wikitext = wikitext.replace(lcomm[i], "");
	}
	var s = generateList(wikitext, '"{{"'+template+'"|"', "}}", 1);
	for (var i = 0; i < s.length; i += 1) {
		s[i]=parseTemplate(template, s[i])[0];
		if (s[i].name != undefined && s[i].name ==undefined) s[i].name=s[i].name;
		if (s[i].title != undefined && s[i].title ==undefined) s[i].title=s[i].title;
	}

	for (var i=0;i<s.length-1;i+=1) {
		s[i].to=s[i+1].from-1;
	}
	s[s.length-1].to=pdxpc.length-1;
	text+="local cap={}\n";
	text+="local i=1\n";
	for (var i=0;i<s.length;i+=1) {
		text+='cap['+(i+1)+']={}\n';
		text+='cap['+(i+1)+'].name="'+s[i].name+'"\n';
		text+='cap['+(i+1)+'].title="'+s[i].title+'"\n';
		text+='cap['+(i+1)+'].from='+s[i].from+'\n';
		text+='cap['+(i+1)+'].to='+s[i].to+'\n';
		text+='cap['+(i+1)+'].delta="'+s[i].delta+'"\n';
	}

	text += "pages.d2b=d2b\npages.b2d=b2d\npages.cap=cap\nreturn pages";
	scriviBox(text);
}
// </syntaxhighlight>