I tried the ExportPacket
approach described in Chris Degnen's answer, and while it's very simple, the need for FrontEndExecute
and the difficulties dealing with cells of text and section headings and the like make it less appealing. Here's a sketch of the approach I'll ultimately be using, though I haven't filled all the details in yet:
notebookToPackage[notebook_String, package_String] :=
With[{nbObj = Get@notebook},
If[
nbObj === $Failed,
$Failed,
Export[package, notebookToStrings@nbObj, {"Text", "Lines"}]]]
This just loads the notebook file and does some basic error handling and exports the result after notebookToStrings
converts the mess into strings. Using Get
to get the notebook is the easiest way to get it as an object with head Notebook
, and from there you can get at the Cell
structures.
notebookToStrings[nbObj_] :=
With[{cells =
Cases[nbObj,
Cell[body_, style : "Code" | "Text", ___] :> {body, style},
Infinity]},
Riffle[
Replace[cells,
$cellRules, {1}],
""]]
Since notebook objects are Mathematica structures like everything else, you can use the same old structural manipulations and rule replacements that you use everywhere else. Cases
proves once again that, with the Infinity
level specification, it's the most useful function in the Mathematica library, and the pattern matching means you can whitelist some cells while not having to decide what you want to do about input or output cells or subsections or whatever. Everything else is done using rules. Riffle
is just there to add white space to aesthetics.
Lastly, we have the rules, and some helper functions:
$cellRules = {
{body_, "Code"} :> boxesToStrings[body],
{body_String, "Text"} :> commentate[body]};
The contents of the "Code" cells are going to be boxes, and I turn the text in text cells into comments:
$scratchContext = "Scratch`Private`";
Attributes[stringulate] = {HoldAllComplete};
stringulate[expr_] :=
ToString[Unevaluated[expr], InputForm, CharacterEncoding -> "ASCII",
PageWidth -> 72]
boxesToStrings[boxes_] :=
Block[{$Context = $scratchContext},
stringulate @@
ToExpression[StripBoxes[boxes], StandardForm, HoldComplete]]
There's a little extra ceremony to keep this function from interning symbols into random contexts, and I had to introduce an extra function, stringulate
, to keep everything held until I make it into a string. The handling for text is simpler:
commentate[s_String] :=
ToString["(*\n" <> s <> "\n*)", OutputForm,
CharacterEncoding -> "ASCII", PageWidth -> Infinity]
Ultimately, I'll probably add some rules for handling "Title" cells, "Section"/"Subsection"/... cells, and the like. In particular, if you have a pair of comments like this:
(* ::Section:: *)
(* The section title *)
the code editor in Wolfram Workbench will fold the thing up like a notebook section when you double click on it. In the end, I'll probably also want to hold onto the whole Cell
expressions so that I can pick out input cells that are also initialization cells, because those should be dumped to the package as well.
Cases
, in the box form, and then convert to held expressions. I did play with the code a bit, so could post it if you think this could be an option for you. $\endgroup$ – Leonid Shifrin Feb 23 '12 at 18:43.nb
in the destination for the.m
package, then save the notebook as needed after -- just once -- setting theAutoGeneratedPackage
option for it? $\endgroup$ – murray Feb 23 '12 at 22:27