Class: HamlCoffee

Defined in: src/haml-coffee.coffee

Overview

The HamlCoffee class is the compiler that parses the source code and creates an syntax tree. In a second step the created tree can be rendered into either a JavaScript function or a CoffeeScript template.

Instance Method Summary

Constructor Details

- (void) constructor(options = {})

Construct the HAML Coffee compiler.

Parameters:

  • options (Object) the compiler options

Options Hash: (options):

  • escapeHtml (Boolean) escape the output when true
  • escapeAttributes (Boolean) escape the tag attributes when true
  • cleanValue (Boolean) clean CoffeeScript values before inserting
  • uglify (Boolean) don't indent generated HTML when true
  • format (String) the template format, either `xhtml`, `html4` or `html5`
  • preserveTags (String) a comma separated list of tags to preserve content whitespace
  • selfCloseTags (String) a comma separated list of self closing HTML tags
  • customHtmlEscape (String) the name of the function for HTML escaping
  • customCleanValue (String) the name of the function to clean code insertion values before output
  • customFindAndPreserve (String) the name of the function used to find and preserve whitespace
  • customPreserve (String) the name of the function used to preserve the whitespace

Instance Method Details

- (Boolean) indentChanged()

Test if the indention level has changed, either increased or decreased.

Returns:

  • (Boolean) true when indention changed

- (Boolean) isIndent()

Test if the indention levels has been increased.

Returns:

  • (Boolean) true when increased

- (void) updateTabSize()

Calculate the indention size

- (void) updateBlockLevel()

Update the current block level indention.

- (void) updateCodeBlockLevel(node)

Update the indention level for a code block.

Parameters:

  • node (Node) the node to update

- (void) updateParent()

Update the parent node. This depends on the indention if stays the same, goes one down or on up.

- (void) pushParent()

Indention level has been increased: Push the current parent node to the stack and make the current node the parent node.

- (void) popParent()

Indention level has been decreased: Make the grand parent the current parent.

- (Object) getNodeOptions(override = {})

Get the options for creating a node

Parameters:

  • override (Object) the options to override

Returns:

  • (Object) the node options

- (Node) nodeFactory(expression = '')

Get the matching node type for the given expression. This is also responsible for creating the nested tree structure, since there is an exception for creating the node tree: Within a filter expression, any empty line without indention is added as child to the previous filter expression.

Parameters:

  • expression (String) the HAML expression

Returns:

  • (Node) the parser node

- (void) parse(source = '')

Parse the given source and create the nested node structure. This parses the source code line be line, but looks ahead to find lines that should be merged into the current line. This is needed for splitting Haml attributes over several lines and also for the different types of filters.

Parsing does not create an output, it creates the syntax tree in the compiler. To get the template, use #render.

Parameters:

  • source (String) the HAML source code

- (void) evaluate(node)

Evaluate the parsed tree

Parameters:

  • node (Node) the node to evaluate

- (void) render(templateName, namespace = 'window.HAML')

Render the parsed source code as CoffeeScript template.

Parameters:

  • templateName (String) the name to register the template
  • namespace (String) the namespace to register the template

- (String) precompile()

Pre-compiles the parsed source and generates the function source code.

Returns:

  • (String) the template function source code

- (String) createCode()

Create the CoffeeScript code for the template.

This gets an array of all lines to be rendered in the correct sequence.

Returns:

  • (String) the CoffeeScript code

- (void) getBuffer(level)

Get the code buffer identifer

Parameters:

  • level (Number) the block indention level

- (Array<Object>) combineText(lines)

Optimize the lines to be rendered by combining subsequent text nodes that are on the same code line indention into a single line.

Parameters:

  • lines (Array) — the code lines

    Returns:

    • (Array) — the optimized lines

      - (String) convertBooleans(code)

      Adds a boolean convert logic that changes boolean attribute values depending on the output format.

      With the XHTML format, an attribute checked='true' will be converted to checked='checked' and checked='false' will be completely removed.

      With the HTML4 and HTML5 format, an attribute checked='true' will be converted to checked and checked='false' will be completely removed.

      Returns:

      • (String) the clean up whitespace code if necessary

      - (String) cleanupWhitespace(code)

      Adds whitespace cleanup function when needed by the template. The cleanup must be done AFTER the template has been rendered.

      The detection is based on hidden unicode characters that are placed as marker into the template:

      • \u0091 Cleanup surrounding whitespace to the left
      • \u0092 Cleanup surrounding whitespace to the right

      Parameters:

      • code (String) the template code

      Returns:

      • (String) the clean up whitespace code if necessary