Pensieve header: A program for exporting tagged cells, continued pensieve://2013-03/.

BeginPackage["CellExport`"];

CellExport; TagProperties; CellFilter; ExportFormat; ExportOptions; Split;

Begin["Private`"];

HackedExport[fname_String, rest___] := Module[{out},
If[FileExtension[fname]=!="png",
Export[fname, rest],
Export["HackedExportTemporary.gif", rest];
out=Export[fname, Import["HackedExportTemporary.gif"]];
DeleteFile["HackedExportTemporary.gif"];
out
]
];

TagProperties[_] := {};
Options[CellExport] = {
PageWidth -> 6, CellFilter -> Identity, 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, OptionsPattern[]] := Module[
{cells, cell},
Print["exporting ",tag,"..."];
cells=OptionValue[CellFilter][Cases[
nb, c_Cell /; FreeQ[List@@c, Cell] && !FreeQ[c, CellTags->tag],
Infinity
]];
If[!OptionValue[Split],
If[Length[cells]≥1,
If[Length[cells]==1,
cells=Append[First[cells], PageWidth -> 72OptionValue[PageWidth]],
cells=Cell[CellGroup[cells], PageWidth -> 72OptionValue[PageWidth]]
];
HackedExport[
tag<>OptionValue[ExportFormat],cells,
ImageResolution -> 300,
OptionValue[ExportOptions]
]
],
k=0;
Table[
++k;
HackedExport[
tag<>"-"<>ToString[k]<>OptionValue[ExportFormat],
Append[cell, PageWidth -> 72OptionValue[PageWidth]],
ImageResolution -> 300,
OptionValue[ExportOptions]
],
{cell, cells}
]
]
];

End[]; EndPackage[];