(* ::Package:: *)

(************************************************************************)
(* This file was generated automatically by the Mathematica front end.  *)
(* It contains Initialization cells from a Notebook file, which         *)
(* typically will have the same name as this file except ending in      *)
(* ".nb" instead of ".m".                                               *)
(*                                                                      *)
(* This file is intended to be loaded into the Mathematica kernel using *)
(* the package loading commands Get or Needs.  Doing so is equivalent   *)
(* to using the Evaluate Initialization Cells menu command in the front *)
(* end.                                                                 *)
(*                                                                      *)
(* DO NOT EDIT THIS FILE.  This entire file is regenerated              *)
(* automatically each time the parent Notebook file is saved in the     *)
(* Mathematica front end.  Any changes you make to this file will be    *)
(* overwritten.                                                         *)
(************************************************************************)



(* ::Input::Initialization:: *)
SetDirectory["C:\\drorbn\\AcademicPensieve\\Projects/DPG"];
SetOptions[$FrontEndSession,PrintingStyleEnvironment->"Working"];


(* ::Input::Initialization:: *)
WinSort[names_List]:=names[[Ordering[Characters[names]]]];


(* ::Input::Initialization:: *)
ConditionalExport[fname_String, rest___] := Module[{temp, exists},
temp="ConditionalExportTemporary"<>"."<>FileExtension[fname];
exists=FileExistsQ[fname];
Export[temp, rest];
If[exists && FileByteCount[fname]===FileByteCount[temp],
DeleteFile[temp],
(* else *) (*Print["Exporting "<>fname<>"..."];*)
If[exists, DeleteFile[fname]];
RenameFile[temp, fname]
];
fname
];


(* ::Input::Initialization:: *)
TagProperties[_] := {};
Options[CellExport] = {
PageWidth -> 6, CellFilter -> Identity, ExportBaseFilename -> Automatic, ExportFormat -> ".pdf", ExportOptions -> {}, Split -> False };
CellExport[tag_String, opts___Rule] := CellExport[NotebookGet[EvaluationNotebook[]],tag, opts];
CellExport[nb_Notebook, tag_String] := CellExport[nb, tag, TagProperties[tag]];
CellExport[nb_Notebook, tag_String, opts___Rule] := Module[
{cells,p, cell,pwunit=108,
filename = ExportBaseFilename /.{opts}/.Options[CellExport]/. Automatic -> tag,
format=ExportFormat/.{opts}/.Options[CellExport],
filter=CellFilter/.{opts}/.Options[CellExport],
split=Split/.{opts}/.Options[CellExport],
pagewidth=PageWidth/.{opts}/.Options[CellExport],
exportoptions=ExportOptions/.{opts}/.Options[CellExport]
},
Print["exporting ",tag," with ", {opts},"/.",Options[CellExport],"..."];
cells={Table[nb[[Sequence@@p[[1;;-2]]]],
{p,Position[nb, CellTags->tag]}
] // filter}//Flatten;
If[split,
If[Length[cells]>=1,
If[Length[cells]==1,
cells=Append[First[cells], PageWidth ->  pwunit pagewidth],
cells=Cell[CellGroup[cells], PageWidth ->  pwunit pagewidth]
];
ConditionalExport[filename<>format,cells,ImageResolution -> 300,exportoptions]
],
k=0;
Table[
++k;
ConditionalExport[
filename<>"-"<>ToString[k]<>format,
Append[cell, PageWidth ->  pwunit pagewidth],ImageResolution -> 300,exportoptions],
{cell, cells}
]
]
];


(* ::Input::Initialization:: *)
MakeSnips[nb_,opts___Rule] := (
{
dt=DialogTags/.{opts}/.DialogTags->{},
force=Force/.{opts}/.Force->False
};
If[FileType["Snips/"<>nb]===None, CreateDirectory["Snips/"<>nb]];
If[force\[Or]FileType["Snips/"<>nb<>"/Verification.txt"]===None\[Or]AbsoluteTime@FileDate["Snips/"<>nb<>"/Verification.txt"]<AbsoluteTime@FileDate[nb<>".nb"],
If[Head[$FrontEnd]===FrontEndObject,
SetOptions[CellExport, ExportFormat -> ".pdf", Split -> True, PageWidth -> 7.12, ExportOptions -> {ImageSize->Scaled[0.5]}],
SetOptions[CellExport, ExportFormat -> ".pdf", Split -> True, PageWidth -> 7.12]
];
SourceNotebook =NotebookGet@NotebookOpen[Directory[]<>"/"<>nb<>".nb"];
tags=Cases[SourceNotebook, (CellTags->tag_) :> tag, Infinity ] // Union;
files=Reap[Flatten[Table[
Which[
tag=="817", { (* this is only left here as a demo for how to make an exception *)
Sow@CellExport[SourceNotebook, "817", CellFilter -> First, ExportBaseFilename -> "Snips/"<>nb<>"/817-In"],
Sow@CellExport[SourceNotebook, "817", CellFilter -> Rest, ExportBaseFilename -> "Snips/"<>nb<>"/817-Out", PageWidth -> 5.5*0.24/0.16]
},
MemberQ[dt, tag], {
Sow@CellExport[SourceNotebook, tag, CellFilter -> First, ExportBaseFilename -> "Snips/"<>nb<>"/"<>tag<>"-In"],
Sow@CellExport[SourceNotebook, tag, CellFilter -> Rest, ExportBaseFilename -> "Snips/"<>nb<>"/"<>tag<>"-Out"]
},
True, Sow@CellExport[SourceNotebook, tag, ExportBaseFilename -> "Snips/"<>nb<>"/"<>tag]
],
{tag, tags}]]];
PutAppend[DateString[], "Snips/"<>nb<>"/Verification.txt"];
files=Flatten[files];
(*DeleteFile /@ Complement[StringReplace["\\"\[Rule]"/"]/@FileNames["Snips/"<>nb<>"/*.pdf"], files];*)
WinSort@Union[files],
(*Else*)
WinSort@FileNames["Snips/"<>nb<>"/*.pdf"]
]
)
