Here is an improved version of CleanNotebookForGit
with a fix for above-described Pattern
export bug. Another improvement is that it doesn't clean up options inside of BoxData
, where this can cause problems:
CleanNotebookForGit[inputFile_, outputFile_] :=
Module[{contents = Get[inputFile], newcontents,
uniqCellHeadName = "$$$MyUniqueCellHead$$$" <> ToString[RandomInteger[{10^8, 10^10}]],
uniqPatternHeadName =
"$$$MyUniquePatternHead$$$" <> ToString[RandomInteger[{10^8, 10^10}]]},
newcontents = contents /. {bd_BoxData :> bd,
HoldPattern[
CellLabel | CellChangeTimes | ExpressionUUID | WindowSize | WindowMargins -> _] :>
Sequence[]};
Export[outputFile,
StringReplace[
ExportString[
newcontents /. {Cell -> Symbol[uniqCellHeadName],
Pattern -> Symbol[uniqPatternHeadName]},
"Package", PageWidth -> Infinity, "Comments" -> None],
{uniqCellHeadName -> "\nCell", uniqPatternHeadName -> "Pattern"}], "Text"]];