Skip to content

Instantly share code, notes, and snippets.

@keshavsaharia
Created June 30, 2013 05:50
Show Gist options
  • Save keshavsaharia/5894031 to your computer and use it in GitHub Desktop.
Save keshavsaharia/5894031 to your computer and use it in GitHub Desktop.
Use Mathematica to generate static webpages by filling HTML comments with strings generated from Mathematica expressions.
Templatize[source_, replacement_] := If[StringQ[replacement],
StringReplace[source, {"<!--$[]-->" -> ToString[replacement]}, 1],
If[ListQ[replacement] && Length[replacement] > 0,
Templatize[Templatize[source, First[replacement]],
Rest[replacement]],
If[Head[replacement] === Rule,
If[ListQ[Last[replacement]],
Templatize[ source, (First[replacement] -> #) & /@ Last[replacement]],
StringReplace[ source, {"<!--$[" <> ToString[First[replacement]] <> "]-->" ->ToString[Last[replacement]]}, 1]], source]]]
TemplatizeAll[source_, replacement_] := If[StringQ[replacement],
StringReplace[source, {"<!--$[]-->" -> ToString[replacement]}],
If[ListQ[replacement] && Length[replacement] > 0,
TemplatizeAll[TemplatizeAll[source, First[replacement]], Rest[replacement]],
If[Head[replacement] === Rule, If[ListQ[Last[replacement]],
TemplatizeAll[ source, (First[replacement] -> #) & /@ Last[replacement]],
StringReplace[ source, {"<!--$[" <> ToString[First[replacement]] <> "]-->" ->
ToString[Last[replacement]]}]], source
]]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment