bham

package
v0.0.0-...-428fbb4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 8, 2013 License: BSD-2-Clause, BSD-2-Clause Imports: 10 Imported by: 0

README

bham - Blocky Hypertext Abstraction Markup

bham is a similar language to something like haml, but it implements a subset of what haml does in order to keep the language neat and tidy. I think of it as the haml syntax mixed with the command language of Go's builtin template library, not that suprising since this library compiles into ParseTrees for the builtin template libraries. It can interoperate with existing go templates, and benefits from the html/template's escaping functionality.


Documentation

Working Markup Examples

For a web page with the title set to 'Example Page' and an h1 tag with the content 'Whatever' you would do the following.

<!DOCTYPE html>
%html
  %head
    %title Example Page
  %body
    %h1 Whatever

If I wanted to display a default title if there wasn't a PageTitle attibute in the Render Arguments when Rendering the template, and otherwise to use the PageTitle argument, then you would do the following. I'm not sure that the {{ }} bit works yet...

<!DOCTYPE html>
%html
  %head
    = if .PageTitle
      %title
        = .PageTitle
    = else
      %title No Title Set

And the big one, that shows off pretty much all the features...

<!DOCTYPE html>
%html(ng-app)
  %head
    = $current := .Current.Page.Name
    = javascript_include_tag "jquery" "angular"
    %title Web Introduction: {{ $current }}
    = if .ExtraJsFiles
      = javascript_include_tag .ExtraJsFiles
    = stylesheet_link_tag "ui-bootstrap"
    = with $current := .Current.Variables
      = template "Layouts/CurrentJs.html" $current
  %body
    %div(class="header {{ .HeaderType }}")
    .hello Welcome to the web {{ .User.Name }}
      You are in section {{ $current }}.

    .row-fluid
      .span3
        = template "Layouts/Navigation.html" .
      .span9
        = yield .
    .row-fluid
      .span10.offset1
        = range $index, $sponsor := .Sponsors
          .sponsor-mini(data-bg-image="sponsor-{{ $sponsor.Img }}")
            = link_to $sponsor.Name $sponsor.Url "class='name'"

Documentation

Overview

bham or the "blocky hypertext abstraction markup" is an attempt to take what is good about languages like haml, jade, slim, etc. and port it to Go, but not blindly. It will take into account the capabilities of Go's template libraries to parse directly into the internal template structures that the stdlib template libraries use to provide both speed and interoperability with standard Go templates.

Plain Text

Most web templates are mostly html content, or just plain text content, as opposed to executable code. If bham doesn't think a line can be executed, it will just output the raw version to html. If bham thinks your line could be executable, but is going to error it should return you an error during the parse step.

%first
  %second
    %frist
      <a href="#">Showdown</a>

would be turned into the html content:

<first>
  <second>
    <frist>
      <a href="#">Showdown</a>
    </frist>
  </second>
</first>

You should notice that placing a % before a word will turn it into a tag, while at the same time the a tag was sent though as well. For those of us wondering about automatic escaping, putting plain html into a bham template will not escape any characters.

Todo: Escaping

To have a line display as written, whether that line would be executed or is html code just add a \ as the first non-whitespace character, and that line will be html escaped before it is inserted into the template.

Attributes

The normal attribute syntax is the html style attributes, in the form of

%script(type="text/javascript" src="/js/{{.script}}.js")

TODO: To set attributes on a tag by a map, put the map in single { }, multiple maps map be put in the { } and the attribute will be put together. If attributes are present in multiple maps, but they aren't of the key class or id, the first map with that key will be the value appearing.

%script(type="text/javascript){ .script }

Classes and ID's

Period and pound signs are used in the same manner as haml. Use them to quickly add classes or an id to tags. Multiple class names are each added to the class attribute, while multiple id parts will be joined with underscores in the compiled form.

%span#tesla(id="tower")
  %img.watt.ohm(src="img1.png")

would be compiled into

<span id="tesla_tower">
  <img class="watt ohm" src="img1.png"></img>
</span>

If you do not specify the tag, it will be assumed to be a div. The following two lines are identical after compilation.

%div.classy#restaurant
.classy#restaurant

TODO: Empty tags. Tags that have no content, and are on the autoclose list of tags (see bham.AutocloseTags) will either have no closing tag, or will end with />

TODO: Whitespace non-removal. By default, bham likes to remove whitespace from your templates. It still needs some tweaking. But maybe there needs to be some operation to keep whitespace?

TODO Reference Structs

There should be some interface definition to allow developers to use the an instance of a struct that implements an interface to use that instance to template out some attributes, content, etc.

Doctypes

Doctypes are placed near the beginning of files and start with three exclamation marks. There is a list of default Doctypes in the Doctypes variable. You can add or modify your own doctypes. Here's an example:

!!! Strict

would be compiled into

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

Todo Comments

Adding a forward slash should turn the more indented lines into an html comment. Adding [] should make it a conditional comment for IE. Adding a -# should make it a silent comment that doesn't show up in the compiled form.

Filters

Sometimes you need to embed a bit of javascript or css into your html template, in this case you can use a filter to automatically turn it into a different format. For example

:javascript
  $(".name").html("Hello World");

would compile into

<script type="text/javascipt">
  $(".name").html("Hello World");
</script>

Currently you cannot embed values into lines that are embedded into filters, this will be fixed.

Template code

Pretty much the same as the standard go templates, but I'm not good at complex number parsing.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Strict determines whether only tabs will be considered
	// as indentation operators (Strict == true) or whether
	// two spaces can be counted as an indentation operator
	// (Strict == false), this is included for haml
	// semi-comapibility
	Strict bool

	// To add multiple id declarations, the outputter puts them together
	// with a join string, by default this is an underscore
	IdJoin = "_"

	// Like the template library, you need to be able to set code delimeters
	LeftDelim  = "{{"
	RightDelim = "}}"
)
View Source
var Doctypes = map[string]string{
	"":             `<!DOCTYPE html>`,
	"Transitional": `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">`,
	"Strict":       `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">`,
	"Frameset":     `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">`,
	"5":            `<!DOCTYPE html>`,
	"1.1":          `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">`,
	"Basic":        `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">`,
	"Mobile":       `<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd">`,
	"RDFa":         `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">`,
}
View Source
var Filters = []FilterHandler{
	FilterHandler{
		Trigger: ":javascript",
		Open:    `<script type="text/javascript">`,
		Close:   "</script>",
		Handler: Transformer(func(s string) string { return s }),
	},
	FilterHandler{
		Trigger: ":css",
		Open:    `<style>`,
		Close:   "</style>",
		Handler: Transformer(func(s string) string { return s }),
	},
}

Functions

func Parse

func Parse(name, text string) (map[string]*parse.Tree, error)

parse will return a parse tree containing a single

Types

type BhamParser

type BhamParser string

func (BhamParser) ApplicableExtensions

func (bp BhamParser) ApplicableExtensions() []string

func (BhamParser) ParseFile

func (bp BhamParser) ParseFile(name, content string) (map[string]*parse.Tree, error)

func (BhamParser) RequiredHtmlFuncs

func (bp BhamParser) RequiredHtmlFuncs() htmlTemplate.FuncMap

func (BhamParser) RequiredTextFuncs

func (bp BhamParser) RequiredTextFuncs() textTemplate.FuncMap

type FilterHandler

type FilterHandler struct {
	Trigger     string
	Open, Close string
	Handler     Transformer
}

type Transformer

type Transformer func(string) string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL