(* Packages for organizing HTML files *)
Module[{file,file1,s,ms},
path[file_String]:=(
file1="\\"<>file<>"\\";
s=StringPosition[file1,"\\"];
ms=Length[s];
DeleteCases[Table[st={s[[ns,1]]+1,s[[ns+1,1]]-1};
StringTake[file1,st],{ns,ms-1}],""])
];
Module[{f,f0,fpath,f0path,mf,mf0,m,neq,n,n0,i,frel},
relpath[f_String,f0_String]:=(
fpath=path[f];
f0path=path[f0];
mf=Length[fpath];
mf0=Length[f0path];
m=Min[mf,mf0];
neq=0;
Do[If[fpath[[n]]=!=f0path[[n]],Break[]];
neq=n,{n,m}];
n=mf-neq;
n0=mf0-neq;
frel="";
Do[frel=frel<>"../",{i,n0}];
Do[frel=frel<>fpath[[mf-n+i]]<>"/",{i,n}];
If[f=!="" && frel=!="",If[StringTake[f,-1]=!="\\",frel=StringDrop[frel,-1]]];
frel
)];
Module[{f,d0,d1,file,fpath,mf,fdir,ifcopy},
(* Needs batchfile if ifcopy=False! *)
(* ifcopy = True, False, or None
- copy file by Mathematica, copy file by DOS, or copy directory only by Mathematica *)
copyfile[f_String,d0_String,d1_String,ifcopy_]:=(
If[FileType[d1]=!=Directory,
Print["The new directory "<>d1<> " does not exist!"] ];
file=ToFileName[d0,f];
If[FileType[file]=!=File && (ifcopy===True || ifcopy===False),
Print["File "<>file<> " does not exist!"] ];
fpath=path[f];
mf=Length[fpath];
SetDirectory[d1];
Do[fdir=fpath[[n]];
If[FileType[fdir]=!=Directory,
CreateDirectory[fdir]];
SetDirectory[fdir]
,{n,mf-1}];
If[ifcopy,CopyFile[file,fpath[[mf]]],
s=OpenAppend[batchfile,PageWidth->Infinity,FormatType->OutputForm];
Write[s,"copy "<>file<>" "<>ToFileName[d1,f]];
Close[s]
];
)];
Module[{ref0,ref,refprev,basedir,rootdir,basefiles,
none,p,ref1,fullbd,reftype,fullref,fref,relref,dirref,ref2,res,s,ifname,name},
checkref[ref0_String,refprev_String,basedir_String,rootdir_String,basefiles_List]:=(
none={None,"",""};
res=none;
ifname=False;
If[StringMatchQ[ref0,"*:*"],Goto[end]];
If[StringMatchQ[ref0,"* *"],Goto[end]];
If[StringMatchQ[ref0,"*\n*"],Goto[end]];
If[StringMatchQ[ref0,"*=*"],Goto[end]];
If[StringMatchQ[ref0,"*;*"],Goto[end]];
If[!StringMatchQ[refprev,"*="]
&& !StringMatchQ[refprev,"*("] && !StringMatchQ[refprev,"*,"],Goto[end]];
ref=ToLowerCase[ref0];
If[ifname=StringMatchQ[ref,"*#*"],
s=StringPosition[ref,"#",1][[1,1]];
name=StringDrop[ref,s];
ref=StringTake[ref,s-1];
(*
s=StringToStream[ref];
p=ReadList[s,Record,RecordSeparators ->{"#"},NullRecords->True];
Close[s];
name=Last[p];
ref=First[p]
*)
];
If[!StringMatchQ[ref,"*/*"] && !MemberQ[basefiles,ref],Goto[end]];
ref=StringReplace[ref,"/"->"\\"];
If[ref==="",Goto[end]];
If[ref==="",Goto[end]];
p=path[ref];
ref1=First[p];
If[ref1=!="." && ref1=!=".." && !MemberQ[basefiles,ref1],Goto[end]];
fullbd=SetDirectory[ToFileName[{rootdir,basedir}]];
reftype=FileType[ref];
If[reftype===Directory,
If[StringMatchQ[refprev,"* HREF=",IgnoreCase->True],
fullref=SetDirectory[ref];
Print["Found a reference to directory ",fullref];
fref=StringReplace[fullref,rootdir<>"\\"->""];
relref=relpath[fullref,fullbd];
res={Directory,fref,relref};Goto[end],
res=none;Goto[end]
]];
If[reftype===File, (* Start If *)
dirref=DirectoryName[ref];
ref2=Last[p];
fullref=ToFileName[If[dirref==="",fullbd,SetDirectory[dirref]],ref2];
fref=StringReplace[fullref,rootdir<>"\\"->""];
relref=relpath[fullref,fullbd];
res={File,fref,relref};Goto[end] ];
Label[end];
(* If[res[[1]]===Directory,res[[3]]=res[[3]]<>"/"];*)
If[ifname,res[[3]]=res[[3]]<>"#"<>name];
res
)];
mtypes=Length[types];
Module[{f,type,ext,mexts,ftype},
whattype[f_String]:=(
Do[type=types[[ntype,1]];
ext=types[[ntype,3]];
mexts=Length[ext];
Do[
If[StringMatchQ[f,"*."<>ext[[next]],IgnoreCase->True],
ftype=type;Goto[end] ];
,{next,mexts}];
,{ntype,mtypes}];
ftype="data";
Label[end];ftype
)];
Module[{refprev},
matchref[refprev_String]:=(
StringMatchQ[refprev,"*src=",IgnoreCase->True] ||
StringMatchQ[refprev,"* href=",IgnoreCase->True] ||
StringMatchQ[refprev,"* background=",IgnoreCase->True] ||
StringMatchQ[refprev,"*option value=",IgnoreCase->True]
)];