User:Alex brollo/parseIndice.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.

/* fills a data page used by Module_Pag 

Test usage:
1. open in edit mode the page where you want to write the Lua data "container" code
2. load parseIndice.js into console
3. call parseIndice, "base" being pagename of Index (i.e. De re metallica (1912).djvu for Index:De re metallica (1912).djvu

It needs:
1. various scripts that can be loaded with importScript("User:Alex brollo/common.js");
2. a perfect pagelist tag
3. a perfect list of Template:Content (see Index:De re metallica (1912).djvu), or any other TOC template

(Hi George! :-) )

*/

function parseIndice(base) {

	var pdxpc = []; // a list with num (djvu page number) as positional key;
	var pd = ""; // num (djvu page number) mnemonic for Page Djvu
	var pc = ""; // pag (book page number or string) mnemonic, in Italian, Page paper (Carta)
	// if base is undefined script prompts for it
	if (base === undefined) base = prompt("Insert basepagename of Index page to manage:");
	// index page html is read
	var html = $.ajax({
		url: "http:" + wgServer + "/w/index.php?action=render&title=Index:" + base,
		async: false
	}).responseText;

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

	// pd and pc are extracted from links
	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;

	}

	// hooles in sequence are filled
	for (var i = 1; i < pdxpc.length - 1; i += 1) {
		if (pdxpc[i] === undefined) pdxpc[i] = "";
	}
	// here script Lua (data container) begins to be built

	testo = "local d2b = {}\nlocal b2d = {}\nlocal pagine = {}\n";
	// here first set of data il built
	for (var i = 1; i < pdxpc.length; i += 1) {
		testo += "d2b[" + i + "]=\"" + pdxpc[i] + "\"\n";
	}
	// here a Lua function to build the reverse lis is built
	testo += "for i,v in ipairs(d2b)\n    do\n      b2d[v]=i\n    end\n";

	// data about chapters are extracted from html
	var cap = $(".toc-data", $(html));
	var x = [];
	for (var i = 0; i < cap.length; i += 1) {
		x[i] = $(cap[i]).data().tocData;
		x[i].from = eval(x[i].from);
		// any x[i][i] now contains an object coming from data-toc-data  attribute (see Template:Content)
	}
	// a .to attribute is added (following .from -1)
	cap = x;
	for (var i = 0; i < cap.length - 1; i += 1) {

		cap[i].to = cap[i + 1].from - 1;
		// any cap[i] now contains too a to attribute (string)
	}
	// .to attribute for the last element is calculated 
	cap[cap.length - 1].to = pdxpc.length - 1;

	// Lua object is built
	testo += "local cap={}\n";
	testo += "local i=1\n";
	for (var i = 0; i < cap.length; i += 1) {
		testo += 'cap[' + (i + 1) + ']={}\n';
		testo += 'cap[' + (i + 1) + '].name="' + cap[i].name + '"\n';
		testo += 'cap[' + (i + 1) + '].title="' + cap[i].title + '"\n';
		testo += 'cap[' + (i + 1) + '].from=' + cap[i].from + '\n';
		testo += 'cap[' + (i + 1) + '].to=' + cap[i].to + '\n';
		testo += 'cap[' + (i + 1) + '].delta="' + cap[i].delta + '"\n';
	}

	// final row of Lua script
	testo += "pagine.d2b=d2b\npagine.b2d=b2d\npagine.cap=cap\nreturn pagine";
	// open text box is filled 
	scriviBox(testo);
}