(* Reading packages, scanning directories and locating pictures *) (* The following variables should be already defined: dir0, normally "d:\\temp\\html\\pictures\\archives" - HTML/pictures/archives-directory itest, itest!=0 for testing (less work) iprint, iprint!=0 to print results, iprint>1 for more detailed printing *) (**********) {minroll,maxroll}={1,999}; If[itest!=0,{minroll,maxroll}={1,12}]; allowedpic={"000.jpg"}; Do[allowedpic=Append[allowedpic,ToString[i]<>ToString[j]<>".jpg"],{i,0,3},{j,0,9}]; (**********) (* Eliminates flanking blanks or new-lines of a string. *) Module[{s,s0,s9}, flankblank[s_String]:= If[s=="",s, If[s0=StringTake[s,1];s0==" "||s0=="\n",flankblank[StringDrop[s,1]], If[s9=StringTake[s,-1];s9==" "||s9=="\n",flankblank[StringDrop[s,-1]],s]]] ]; (* Printing *) Module[{s}, p[s_String]:=Write[of,s] ]; Module[{s}, p1[s_String]:=Write[of1,s] ]; (* Shortening a title *) Module[{s,cleft,cright,b,ctot,wb,cb,wb1,cb1,cb1a,wb2,cb2,cb2a,wb3,version,ins}, shrink[s_String,cleft_Integer,cright_Integer,version_]:=( ins[0]:="<...>"; ins[1]:="...["<>ToString[wb3]<>" words]..."; b=ReadList[StringToStream[s],Word]; ctot=cleft+cright; wb=Length[b]; cb=Sum[StringLength[b[[n]]],{n,wb}]+wb-1; If[cb>ctot, wb1=cb1=0; Do[cb1a=cb1+StringLength[b[[n]]]+1; If[cb1a>cleft,Break[]]; wb1++; cb1=cb1a ,{n,wb}]; wb2=cb2=0; Do[cb2a=cb2+StringLength[b[[wb-n+1]]]+1; If[cb2a>cright,Break[]]; wb2++; cb2=cb2a ,{n,wb}]; wb3=wb-wb1-wb2; If[wb3>1, b=Flatten[{Take[b,wb1],ins[version],Take[b,-wb2]}] ] ]; b=StringReplace[ToString[b],{"{"->"","}"->"",", "->" "}] ) ]; (* Transforms rgb to hls *) Module[{r,g,b,rgbmax,rgbmin,h,l,s,rc,gc,bc}, rgbtohls[r_,g_,b_]:= ( (* Compute lightness *) rgbmax=Max[r,g,b]//N; rgbmin=Min[r,g,b]//N; l=(rgbmax+rgbmin)/2.0; (* Compute saturation *) If[rgbmax==rgbmin, s = 0.0, If[l<=0.5, s=(rgbmax-rgbmin)/(rgbmax+rgbmin), s=(rgbmax-rgbmin)/(2.0-rgbmax-rgbmin) ] ]; (* Compute the hue *) If[rgbmax==rgbmin, h=0.0, rc=(rgbmax-r)/(rgbmax-rgbmin); gc=(rgbmax-g)/(rgbmax-rgbmin); bc=(rgbmax-b)/(rgbmax-rgbmin); If[r==rgbmax, h=bc-gc, If[g==rgbmax, h=2.0+rc-bc, h=4.0+gc-rc ] ]; h=h/6.0; h=Mod[h,1.0]; ]; {h,l,s} )]; (********** Scanning **********) (* Reading year captions *) yearcap[string_String]:=""; filecapt=ToFileName[dir0,"captions.txt"]; data=ReadList[filecapt,Record,RecordSeparators -> {"#"}]; mrec=Length[data]; Do[rec=data[[nrec]]; pos=StringPosition[rec,{" ","\n",":"}]; n=pos[[1,1]]; head=StringTake[rec,n-1]; content=StringDrop[rec,n]//flankblank; yearcap[head]=StringReplace[content,{"\n"->" "}]; ,{nrec,mrec}]; (* Analyzing directories *) years={}; mmmpics=0; mmrolls=0; SetDirectory[dir0]; files=FileNames["*"]; mf=Length[files]; Do[year=files[[nf]]; SetDirectory[dir0]; If[FileType[year]=!=Directory,Continue[]]; numb=ToExpression[year]; If[!IntegerQ[numb],Continue[]]; If[numb<1950 || numb>2010,Continue[]]; years=Append[years,year]; (* Defining range of rolls for a particular year *) rolls[year]={}; mmpics[year]=0; SetDirectory[ToFileName[{dir0,year}]]; files1=FileNames["*"]; mf1=Length[files1]; Do[roll=files1[[nf1]]; SetDirectory[ToFileName[{dir0,year}]]; If[FileType[roll]=!=Directory,Continue[]]; numb=ToExpression[roll]; If[!IntegerQ[numb],Continue[]]; If[numbmaxroll,Continue[]]; rolls[year]=Append[rolls[year],roll]; (* Reading roll name *) filecapt=ToFileName[{dir0,year,roll},"captions.txt"]; data=ReadList[filecapt,Record,RecordSeparators -> {"#"}]; mrec=Length[data]; rolltitle[roll]=""; Do[rec=data[[nrec]]; pos=StringPosition[rec,{" ","\n",":"}]; n=pos[[1,1]]; head=StringTake[rec,n-1]; sep=StringTake[rec,{n,n}]; content=StringDrop[rec,n]//flankblank; If[head=="ROLL", If[rolltitle[roll]!="", Print["WARNING: several titles for the same roll encountered in the captions file "<>filecapt]]; rolltitle[roll]=content] ,{nrec,mrec}]; If[rolltitle[year,roll]=="",Print["WARNING: no title of the roll found in the captions file "<>filecapt]]; (* Defining range of pictures for a particular roll *) pics[roll]={}; SetDirectory[ToFileName[{dir0,year,roll,"jpeg"}]]; files2=ToLowerCase/@FileNames["*.jpg"]; mf2=Length[files2]; Do[pic=files2[[nf2]]; If[!MemberQ[allowedpic,pic],Continue[]]; pics[roll]=Append[pics[roll],pic]; (***********) (* Reading the title of the slide from the file captions.txt *) fn=StringDrop[pic,-4]; fn1=StringReplace[fn,"000"->"-1"]; nfn=ToExpression[fn1]; capt=gcapt=""; Do[rec=data[[nrec]]; pos=StringPosition[rec,{" ","\n",":"}]; n=pos[[1,1]]; head=StringTake[rec,n-1]; sep=StringTake[rec,{n,n}]; content=StringDrop[rec,n]//flankblank; If[sep=="\n"||sep==" ", If[head==fn, If[capt!="",Print["WARNING: several captions for the same slide "<>fn<>" encountered in the caption file "<>capt]]; capt=content;Continue[]]]; If[sep==":", head1=StringReplace[head,{"000"->"-1","-"->"+"}]; range=Flatten[Release[ToExpression["{"<>head1<>"}",InputForm,Hold]/.Plus->Range]]; If[MemberQ[range,nfn], gcapt=gcapt<>" -\n"<>content;Continue[]]]; ,{nrec,mrec}]; capt=flankblank[capt]; gcapt=flankblank[gcapt]; gcapt=StringReplace[gcapt,{" "->" ","""->""}]; While[StringMatchQ[gcapt,"*<*>*"], n1=StringPosition[gcapt,"<"][[1,1]]; n2=StringPosition[gcapt,">"][[1,1]]; gcapt=If[n1" ","'"->""}]]; (***********) ,{nf2,mf2}]; pics[roll]=Sort[pics[roll], Position[allowedpic,#1][[1,1]]&1, Print[Definition[mpics]]; Print[Definition[pics]]; Print[Definition[rolltitle]]; Print[Definition[description]]; ]; (* Enumeration of rolls and pictures *) iroll=ipic=0; Do[year=years[[nyear]]; rolls0=rolls[year]; mrolls0=mrolls[year]; Do[roll=rolls0[[nroll]]; toroll[++iroll]={year,roll}; fromroll[year,roll]=iroll; pics0=pics[roll]; mpics0=mpics[roll]; Do[pic=pics0[[npic]]; topic[++ipic]={year,roll,pic}; frompic[year,roll,pic]=ipic; ,{npic,mpics0}]; ,{nroll,mrolls0}]; ,{nyear,myears}];