RejectedSoftware Forums

Sign up

Compile diet templates offline?

Is there a way to compile diet templates that contain no actual dynamic
code to html? I'm writing some static html pages, and finding I miss the
terse diet format. Maybe there's an external tool that does this?

Would be cool to have a runtime diet parser that does this and just
errors when finding dynamic data.

-Steve

Re: Compile diet templates offline?

On Thu, 18 May 2017 15:33:42 -0400, Steven Schveighoffer wrote:

Is there a way to compile diet templates that contain no actual dynamic
code to html? I'm writing some static html pages, and finding I miss the
terse diet format. Maybe there's an external tool that does this?

Would be cool to have a runtime diet parser that does this and just
errors when finding dynamic data.

-Steve

I did this ugly workaround, which is only tested with the diet template below:

  1. Create a Dub project with diet-ng as a dependency.
  2. Create a new file called src/html.d with this content: https://raw.githubusercontent.com/rejectedsoftware/diet-ng/master/source/diet/html.d
  3. Do the following change:
--- a/source/html.d
+++ b/source/html.d
@@ -1,6 +1,6 @@
 /** HTML output generator implementation.
 */
-module diet.html;
+module html;

 import diet.defs;
 import diet.dom;
@@ -187,8 +187,7 @@ string getHTMLMixin(in Document doc, string range_name = dietOutputRangeName, HT
        CTX ctx;
        ctx.pretty = style == HTMLOutputStyle.pretty;
        ctx.rangeName = range_name;
-       string ret = "import diet.internal.html : htmlEscape, htmlAttribEscape;\n";
-       ret ~= "import std.format : formattedWrite;\n";
+       string ret;
        foreach (i, n; doc.nodes)
                ret ~= ctx.getHTMLMixin(n, false);
        ret ~= ctx.flushRawText();
@@ -545,7 +544,6 @@ private struct CTX {
        {
                string ret;
                if (!this.inRawText) {
-                       ret = this.rangeName ~ ".put(\"";
                        this.inRawText = true;
                }
                ret ~= outputPendingNewline();
@@ -558,7 +556,7 @@ private struct CTX {
        {
                if (this.inRawText) {
                        this.inRawText = false;
-                       return "\");\n";
+                       return "";
                }
                return null;
        }
  1. Add the following code to src/app.d:
import std.stdio;

import diet.parser;
import diet.dom;
import html;

void main()
{
    auto diet = "
doctype html
html
    head
        title D statement test
    body
        this is the body
";
    auto doc = parseDiet(diet);
    auto content = doc.getHTMLMixin();

	writeln(content);
}

To me it seems it would be fairly simple to make a few changes to diet-ng to have this working out of the box.

/Jacob Carlborg

Re: Compile diet templates offline?

On Fri, 19 May 2017 07:23:11 GMT, Jacob Carlborg wrote:

To me it seems it would be fairly simple to make a few changes to diet-ng to have this working out of the box.

Yes, should be a matter of providing a slightly adjusted CTX as a template argument to the private getHTMLMixin.

There is #16 open to keep track of this.

Would be nice to choose some useful output format for this even for dynamic content. It could be something theoretically suitable for being converted back to Diet (even if not perfectly lossless).