This now exists as a GitBook too: https://b3m2a1.gitbooks.io/mathematica-tutorial/content/
I wrote a little script to turn a website generated with the SiteBuilder
package into a GitBook ready website:
gitBookCopyContent[srcDir_, bookDir_] :=
With[{
main = FileNameJoin@{srcDir, "content"}
},
Export[
FileNameJoin@{bookDir, "README.md"},
StringSplit[
Import[FileNameJoin@{main, "pages", "About.md"}, "Text"],
"\n\n",
2
][[2]],
"Text",
CharacterEncoding -> "UTF8"
];
Map[
If[! MatchQ[FileNameTake[#], "pages" | "posts" | ".git"],
With[{f = FileNameJoin@{bookDir, StringTrim[#, main]}},
Which[DirectoryQ@#,
If[FileExistsQ@f,
DeleteDirectory[f, DeleteContents -> True]
];
CopyDirectory[#, f],
MatchQ[FileExtension[#], "png" | "jpeg"],
CopyFile[#, f, OverwriteTarget -> True]
]
]
] &,
FileNames["*", main]
]
];
gitBookExportMD[outdir_, bookDir_, content_] :=
KeyValueMap[
Function[
With[{f =
FileNameJoin@Flatten@
{
bookDir,
FileNameSplit@
StringTrim[#, outdir]
}
},
If[! DirectoryQ@DirectoryName@f,
CreateDirectory[DirectoryName@f,
CreateIntermediateDirectories -> True]
];
Export[
f,
StringJoin[
StringReplace[#2,
{
"{{" -> "{ {",
"}}" -> "} }",
"{filename}" ->
URLBuild@
ConstantArray["..",
FileNameDepth[StringTrim[#, outdir]] - 2
]
}
] /.
StringExpression[a___] :> a
],
"Text",
CharacterEncoding -> "UTF8"
]
]
],
content
];
gitBookMakeSummary[outdir_, bookDir_, metas_] :=
Module[{data, cells, nb},
data =
GroupBy[First -> Last] /@
GroupBy[#[[1, 1]] & -> (#[[1, 2]] -> #[[2]] &)]@
KeyValueMap[
URLParse[#2["Path"], "Path"][[;; 2]] ->
<|
"Path" ->
URLBuild[FileNameSplit@StringTrim[#, outdir]],
"Title" -> #2["Title"]
|> &,
metas
];
cells =
KeyValueMap[
Cell[
CellGroupData[Flatten@{
Cell[#, "Section"],
KeyValueMap[
Cell[
CellGroupData[
Flatten@{
Cell[#, "Subsection"],
Map[
Cell[
TextData[
ButtonBox[#Title,
BaseStyle -> "Hyperlink",
ButtonData ->
{
FrontEnd`FileName[
Evaluate@URLParse[#Path, "Path"]],
None
}
]
],
"Item"
] &,
#2
]
}
]
] &,
#2
]
}
]
] &,
data
];
CheckAbort[
nb =
CreateDocument[
Prepend[
cells,
Cell[
TextData[
ButtonBox[
"Introduction",
BaseStyle -> "Hyperlink",
ButtonData ->
{
"README.md",
None
}
]
],
"Item"
]
],
Visible -> False,
StyleDefinitions ->
FrontEnd`FileName[{"BTools"}, "NotebookToMarkdown.nb"],
NotebookFileName ->
FileNameJoin@{bookDir, "SUMMARY.nb"}
];
NotebookMarkdownSave[nb];
NotebookClose[nb];
DeleteFile[FileNameJoin@{bookDir, "SUMMARY.nb"}];,
NotebookClose[nb]
];
];
gitBookBuild[srcDir_, bookDir_] :=
Module[
{outdir, content},
gitBookCopyContent[srcDir, bookDir];
outdir =
FileNameJoin@{srcDir, "content", "posts"};
content =
AssociationMap[
With[{split = StringSplit[Import[#, "Text"], "\n\n", 2]},
<|
"Meta" ->
Association@
Map[
Rule @@ StringTrim@StringSplit[#, ":", 2] &,
StringSplit[split[[1]], "\n"]
],
"Body" ->
split[[2]]
|>
] &,
FileNames["*.md", outdir, \[Infinity]]
];
gitBookExportMD[outdir, bookDir, content[[All, "Body"]] ];
gitBookMakeSummary[outdir, bookDir,
SortBy[
content[[All, "Meta"]],
ToExpression[StringSplit[#ID, "."]] &
]
];
bookDir
];
This doesn't mirror over the theme, but it gets the job done
I use it like this:
gitBookBuild[
FileNameJoin@{$WebSiteDirectory, "tutorial"},
FileNameJoin@{$UserDocumentsDirectory, "GitHub",
"mathematica-tutorial"}
];
and then commit the mirrored repo on GitHub that GitBook builds from