Marzhill Musings

... Next>>

go-html-transform an html transformation and scraping library

Published On: 2013-02-26 17:05:00 is my html transformation library for go. I use it as an html templating language and scraping library. It's not your typical approach to html templating but it's an approach I've really come to enjoy. HTML templating can be grouped in roughly about 3 categories.

  1. Templating languages.
  2. HTML DSLs.
  3. Functional transforms.

go-html-transform is an example of that last one. The basic theory is that an html template is just data. No logic is in the template. All the logic is in the functions that operate on the template and any input data. Using the input data you can transfrom a template and then render the transformed AST back into html. This has a number of benefits.

  • Your template transforms are context aware.
  • Multipass templating is just another transform.
  • All your logic is expressed in real honest to goodness code not a limited templating language. In the case of go-html-transform your templating logic is actually typechecked by the go compiler.
  • It's impossible to generate bad html.
  • Your mocks are your templates.
  • You can use an html dsl in combination with this approach as well if the dsl outputs the same AST.

Example usage.

    1 package main
    3 import (
    4   "strings"
    5   "os"
    7   ""
    8   ""
    9 )
   11 func toSSL(url string) string {
   12   return strings.Replace("http:", "https:", 1)
   13 }
   15 func main() {
   16   f, err := os.Open("~/file.html")
   17   defer f.Close()
   18   if err != nil { return } // handle errors here.
   19   tree, err := transform.NewDocFromReader(f)
   20   if err != nil { return } // handle errors here.
   21   t := transform.NewTransformer(tree)
   22   t.ApplyAll(
   23     Trans(ReplaceChildren(h5.Text("foo"), "span"), // replace every span tags contents with foo
   24     // turn every link and img into an ssl link
   25     Trans(TransformAttrib("href", toSSL), "a"),
   26     Trans(TransformAttrib("src", toSSL), "img"),
   27   )
   29   t.Render(os.Stdout) // render html to stdout.
   30 }



Published On: 2005-08-26 01:03:28
Wykiwyg With the higher profile, dynamic javascript pages are getting, look to see a lot more folks working on this stuff. And this is a wonderful example of what you can do.


A look at an old favourite

Published On: 2005-04-29 02:45:06
css Zen Garden: The Beauty in CSS Design I took a look at an old favourite today. CSS Zen Garden still makes me awestruck. I am overcome by an urge to create something beautiful. Yet feel small when I compare my efforts to them. If you ever want to look at beauty on the web then just visit the above link. If you ever want to see what good designers can do with standards based tools then look at the above link. Maybe someday I will have a submission featured there. Who knows, stranger things have happened. CSS is as much a part of WebApp development as javascript, html, or xml are. It is what gives you the power to put a face on your app. It makes standardizing the interface to your app easier and more comprehensible with it's inheritance and cascading abilities. When you build you apps don't forget the visual design or the visual designer. Give him the tools to create beautiful things. Like here.

... Next>>