*	COPAC-CLEAN.SNO
*	Edits library cards from COPAC removing BibTeX errors
*	version 0.7 29.08.2015
*	guido.milanese@unicatt.it
*	requires xclip and zenity
*  called from copac-clean.sh
*
*	---> Set tabstop=3 to read/edit this file
*
*	LIBRARY FILES
-include "bq.sno"				;* shell operations
-include "itrim.inc"			;*	initial trim
-include "mami.inc"			;* converts uppercase to lowercase
-include "newline.inc"		;* sets newline according to OS
-include "nopairs.inc"		;* nopairs ('  ' = ' ')
-include "repl.inc"			;* string-by-string replacement
*************************
*	INITIALISATIONS
*************************
*	Copacbib is the BibTeX item produced by COPAC, now empty
	Copacbib	=		
	nl			= newline()				;* sets newline according to OS
	ErrMsg0	=	"\t\tNo selection!\n"
+					"Check again the Copac page and your selection\n\n"
+					"\t\t\tExiting"
	ErrMsg1	=	"\t\tNo valid BibTeX file!\n"
+					"Check again the Copac page and your selection\n\n"
+					"\t\t\tExiting"
*	Pattern: hyphenation. Admits 2 forms, old (wrong) and new
	P_hyphen = (("language = {") | ("langage = {"))
+	(("English" | "French" | "German" | "Italian" | "Latin") . Language) '}'
*	Pattern: entrytype
	P_entrytype = '@' (arb . V_entrytype) '{'
*	Pattern: author. Accepts space/no space
	P_author = "author" . Label_author  
+	(span(' ') | '') '='  (span(' ') | '') '{' (arb . Name_author) "}," 	
*	Pattern: note, as above 
	P_note = ((nl | ' ') "note" . Label_note)
+	(span(' ') | '') '=' (span(' ') | '') arb "}," 	
*	Pattern: title, as above 
	P_title = "title"  (span(' ') | '') '=' (span(' ') | '') arb "}," 	
*	Pattern: label
	P_label = '@'  break('{') len(1) (arb . V_label) "," 	
*************************
*	INTERNAL FUNCTIONS
*************************
	
**************************************
*-- Function READANDCHECK
*-- Reads from clipbpoard, checks if it is all right
**************************************
*-- requires itrim.inc
            define("readandcheck()Par1,Par2")   :(readandcheck_end)
readandcheck
readandcheck_bg
*	first check zenity:
*	if zenity does not work exits
	Zchk = bq('zenity  --version')
	(leq(Zchk)) (Terminal = "Zenity is not installed")	:s(end)
*
*	now read/check the COPAC item
*	Copacbib is the BibTeX item produced by COPAC
*	reads from the clipboard, if fails exits
	Copacbib = bq("xclip -o")						;*:f(freturn)
	leq(Copacbib)	:f(readancheck_goon)
	(bq('zenity --error --no-wrap --text="' ErrMsg0 '"')) :(end)
*	checks if it is a valid BibTeX item:
*	if Copacbib does not begin with a '@' fails and exits
*	First removes any blank at the beginning
readancheck_goon
	Copacbib = itrim(Copacbib)
	Copacbib ? pos(0) '@'	:s(readandcheck_rt)
	bq('zenity --error --no-wrap --text="' ErrMsg1 '"')	:(end)
readandcheck_rt   readandcheck   =  Copacbib             :(return)
readandcheck_end
**************************************
*-- Function CPCTR
*-- Transformation of COPAC data
**************************************
            define("cpctr(Copacdata)Par1,Par2")   :(cpctr_end)
cpctr
cpctr_bg
* common errors in the AUTHOR field
	Copacdata ? P_author
	Name_author2 = Name_author
authors1	Name_author2 ? ", and"	= " and" :s(authors1)
authors2	Name_author2 ? ". and"	= " and" :s(authors2) ;* 28.12.2013
	Copacdata ? Name_author = Name_author2
*
* if entrytype is proceedings changes "author" to "editor"
* entrytype?
			Copacdata ? P_entrytype
*	Copacdata ? P_author
	(
+	(V_entrytype ? "proceedings")
+	(Copacdata ? Label_author = "editor")
+	)
*
*	adds always a SHORTTITLE field.
*	The SHORTTITLE is to be later edited manually, according to the
*	user's preferences.
*	Adds a BOOKTITLE field only if entrytype is PROCEEDINGS
*	Useless for BIBLATEX but necessary for standard BIBTEX
*  title
	Copacdata ? P_title . V_title
	Shorttitle = " short" V_title	
	Booktitle = " book" V_title	;* leave it here!
	Copacdata ? V_title = V_title Shorttitle
	((V_entrytype ? "proceedings") (Copacdata ? V_title = V_title Booktitle))
*
*	Hyphenation. Works only if ONE language is declared
	Copacdata ? P_hyphen . V_hyphen
	V_hyphen2 = V_hyphen
	V_hyphen2 ? "langage" = "hyphenation"	;* try also the wrong form, no harm
*	V_hyphen2 ? "language" = "hyphenation"	;* corrected by COPAC, at last!
	V_hyphen2 ? "language" = "langid"		;* changed 0.8 28.08.2015
	Language2 = mami(Language)
	Language2 ? "english" = "british"		;* optional
	Language2 ? "latin" = "italian"
	V_hyphen2 ? Language = Language2
	Copacdata ? V_hyphen = V_hyphen2
*
* Location. Changes to 'll' (optional)
	Copacdata ? "location " = "ll "
*
*	Changes NOTE fields to ANNOTE: the NOTE field is normally printed,
*	while the NOTE field produced by Copac is normally an annotation
*  loops because there are frequently more than 1 NOTE fields
cpctrlpn Copacdata ? P_note . V_note	:f(cpctrlpn_nd)
	V_note2 = V_note
	V_note2 ? "note" = "annote"
	Copacdata ? V_note = V_note2	:(cpctrlpn)
cpctrlpn_nd
*
* Punctuation cleanup
punct1	Copacdata ? " : }"	= "}"		:s(punct1)
punct2	Copacdata ? (any(",.:; ") '}') . W 	= "}"	:s(punct2)
punct3	Copacdata ? "title=" 	= "title = "		:s(punct3)
			Copacdata = nopairs(Copacdata)
newlines Copacdata = repl(Copacdata,"},", "}," nl)
	Copacdata = repl(Copacdata, ", author", ',' nl "author")
	Copacdata = repl(Copacdata, ", editor", ',' nl "editor")
	Copacdata = repl(Copacdata," :",':')
	Copacdata ? P_label
	Output = Copacdata
	OkMsg1	=	"BibTeX entry "  V_label " transformed.\n"
+	"Now paste the clipboard contents to your BibTeX archive\n\n"
+	"\t\t\t Exiting"
	bq('zenity --info --no-wrap --title="Done!" --text="' OkMsg1 '"')
cpctr_rt    cpctr = Copacdata	:s(return)f(freturn)
cpctr_end
******************************
*	MAIN PROGRAMME
******************************
* Reads from clipboard and checks data
	Copacbib = readandcheck()	:f(end)
* Transforms data
	(
+		~(Copacbib = Cpctr(Copacbib))	
+		(Terminal = "Something wrong!")
+	)
end
*
******************************
*	HISTORY
******************************
10.09.2013	version 0.1
15.09.2013	version 0.2 -- first usable
30.10.2013	version 0.3 -- some fields corrected; comments edited
27.12.2013	version 0.4 -- changes author to editor in proceedings
28.12.2013	version 0.5 -- general editing of functions
05.01.2014	version 0.6 -- improved check