Skip to content

Instantly share code, notes, and snippets.

@deanebarker
Last active January 17, 2017 20:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deanebarker/4c2c214d0890592e4e5543fbf302504f to your computer and use it in GitHub Desktop.
Save deanebarker/4c2c214d0890592e4e5543fbf302504f to your computer and use it in GitHub Desktop.
An example of configuing Wyam to build from an HttpHandler.
using System;
using System.Configuration;
using System.Diagnostics;
using System.Web;
using Wyam.Core.Execution;
using Wyam.Core.Modules.Contents;
using Wyam.Core.Modules.Control;
using Wyam.Core.Modules.IO;
using Wyam.Core.Modules.Metadata;
using Wyam.Markdown;
using Wyam.Razor;
using Wyam.Yaml;
using WyamTrace = Wyam.Common.Tracing.Trace;
namespace Website
{
public class WyamBuilder : IHttpHandler
{
public bool IsReusable => true;
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
// This is an aliased class name; see the using directive above
WyamTrace.AddListener(new HttpResponseTraceListener(context.Response));
var engine = new Engine();
engine.Settings.CleanOutputPath = false;
engine.FileSystem.OutputPath = ConfigurationManager.AppSettings["wyam.outputPath"];
engine.FileSystem.InputPaths.Add(ConfigurationManager.AppSettings["wyam.inputPath"]);
/*
* Changes from the config method:
*
* Modules have to be formally instantiated with "new"
* -- OLD: ReadFiles("books/*.md"),
* -- NEW: new ReadFiles("books/*.md"),
*
* The Meta module actually takes a ContextConfig lambda, which has to be written out
* -- OLD: Meta("WritePath", $"books/{@doc.String("SourceFileBase")}/index.html")
* -- NEW: new Meta("WritePath", (doc, ctx) => $"books/{doc.String("SourceFileBase")}/index.html"),
*/
engine.Pipelines.Add("Books",
new ReadFiles("books/*.md"),
new FrontMatter(new Yaml()),
new Markdown(),
new ForEach(
new Meta("WritePath", (doc, ctx) => $"books/{doc.String("SourceFileBase")}/index.html"),
new Meta("Url", (doc, ctx) => $"/books/{doc.String("SourceFileBase")}/")
),
new Razor().WithLayout("/templates/book.cshtml"),
new WriteFiles()
);
engine.Pipelines.Add("BookIndex",
new ReadFiles("templates/books.cshtml"),
new Meta("WritePath", $"books/index.html"),
new Razor(),
new WriteFiles()
);
engine.Execute();
}
}
// I totally stole this from some question on StackOverflow. I had to add the indenting code, but otherwise it was a direct lift.
public class HttpResponseTraceListener : TraceListener
{
public HttpResponse Response { get; private set; }
public HttpResponseTraceListener(HttpResponse response)
{
Response = response;
}
public override void Write(string message)
{
WriteIndent();
if (!string.IsNullOrEmpty(message)
&& null != Response
&& null != Response.OutputStream
&& Response.OutputStream.CanWrite)
{
Response.Write(message);
}
}
protected override void WriteIndent()
{
if (NeedIndent)
{
string indent = new string(' ', IndentLevel * IndentSize);
Response.Write(indent);
}
}
public override void WriteLine(string message)
{
Write(message);
Response.Write(Environment.NewLine);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment