Utente:Alex brollo/djvuEditor.js

Da Wikisource.

Nota: dopo aver pubblicato, potrebbe essere necessario pulire la cache del proprio browser per vedere i cambiamenti.

  • Firefox / Safari: tieni premuto il tasto delle maiuscole Shift e fai clic su Ricarica, oppure premi Ctrl-F5 o Ctrl-R (⌘-R su Mac)
  • Google Chrome: premi Ctrl-Shift-R (⌘-Shift-R su un Mac)
  • Internet Explorer / Edge: tieni premuto il tasto Ctrl e fai clic su Aggiorna, oppure premi Ctrl-F5
  • Opera: premi Ctrl-F5.
function edit(span) {recover();
var testo=$(span).text(); var testoWidth=$(span).width();
$(span).text("");
casella=$("<input>");
casella.attr("value",testo).attr("type","text").css("width",testoWidth+10);
$(span).append(casella).removeAttr("onclick");
}


// rimpiazzale parole modificate nel testo dsed
function preFix() {
	recover();
	r=/^( *\(word \d+ \d+ \d+ \d+ ".+"\))/gm;
	var preTesto=$("pre").text();
	var wordLines=preTesto.match(r);
	for (i=0;i<wordLines.length;i+=1) {
		var testoSpan=$("#page_text span").eq(i).text();
		wordLinesNew=wordLines[i].replace(/(.*").+(".+)/,"$1"+testoSpan+"$2");
		if (wordLinesNew!==wordLines[i]) preTesto=preTesto.replace(wordLines[i],wordLinesNew);
	}
	$("pre").text(preTesto);
}
function recover() {
	var testo=$("#page_text span input").val(); 
	var coord=$("#page_text span input").parent().attr("title");
	$("#page_text span input").parent().text(testo).attr("onmouseenter","edit(this)"); 
	$("#page_text span input").remove(); 
	var dsed=$("#codice pre").text();
	var r1=RegExp("("+coord+' ").+("\\))');
	$("#codice pre").text(dsed.replace(r1,"$1"+testo+"$2"));
	
}

function lista() {
	var testo=$("pre").text();
	var parola="";
	var r=/\(word +(\d+ \d+ \d+ \d+) "(.+)"\)+/;
	// analisi della struttura del file dsed
	// 1. Struttura canonica, tipo IA: words=True e lines=True
	// 2. Struttura da FineReader11, prima edizione: words=False e lines=True
	// 3. Struttura da FineReader11, edizioneXavier:words=True e lines=False
	// Altre strutture:words=False lines=False  NON GESTITA
	// non gestita nemmeno la struttura a dettaglio integrale char 
	
	var words=true;
	var lines=true;
	if (testo.indexOf("(word")===-1) words=false;
	if(testo.indexOf("(line")===-1) lines=false;
	
	// gestione struttura 1
	
	if (words && lines) {
		testo=testo.split("\n");
		var l=[];
		for (i=0;i<testo.length;i+=1) { 
			if (testo[i].indexOf("(line")!=-1) {l.push("<br/>");}
			if (testo[i].indexOf("(word")!=-1) {
				parola=testo[i].replace(r,'<span class="word" title="$1" onmouseenter="edit(this)">$2</span>');
				l.push(parola);
				// testo[i]=testo[i].replace('"'+parola+'"','"'+i+'"');
			}
		}
	}
	
	// gestione struttura 2 (words=false, lines=true)
	
	// g3stione struttura 3 (words=true, lines=false)
	$("#page_text").html(l.join(" "));
   return false;	
}

$(document).ready(function() {
if (wgPageName=="Utente:Alex_brollo/djvuEditor") {
   lista();
   }
});