Created
June 30, 2013 05:50
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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