0.0.2 • Published 7 years ago

brakkoli-pmb v0.0.2

Weekly downloads
2
License
ISC
Repository
github
Last release
7 years ago

brakkoli-pmb

An SGML (XML, HTML) template engine that uses arrays for hierarchy.

Early Access Warning

This module is in the first stages of development. Expect major differences between docs and implementation.

Usage

from test/usage.js:

var brakk = require('brakkoli-pmb')(), spec,
  recipes = { basicPage: require('brakkoli-html5-pmb/basic_page') };

recipes.createAndSharePage = function (topic, answers) {
  var pg = recipes.basicPage({ indent: 2,
    title: 'Create & share ->your<- ' + topic,
    });
  pg.head.push({ link_css: 'quiz.css' });
  pg.body.push([ 'h1', pg.head.title ],
    [ 'form method="get" action="x-nope://"',
      [ 'ul class="quiz"'].concat(answers.map(recipes.checkboxAnswer)),
      [ 'input type="submit">' ],
      ]);
  return pg;
};

recipes.checkboxAnswer = function (ans) {
  return [ [ 'li', { indentTags: 0,  }, [ 'label',
      [ 'input type="checkbox" name="ingr[]">',
        { attr: { value: ans } } ],
      '\n  ', ans ] ], '\n' ];
};

spec = recipes.createAndSharePage('sandwich', [ 'bacon',
  'lettuce', 'tomato', 'cheese', 'onion',
  'hot & spicy sauce', 'mustard', '"Ben\'s beans"' ]);

tu.expectEqual(brakk(spec),   expectedHTML);
tu.expectEqual(spec,          expectedTree);

API

var brakkoliFactory = require('brakkoli-pmb'),
  brakk = brakkoliFactory(),    // <-- using default config
  customBrakk = brakkoliFactory({ tplSlotRx: /\{{2}([\w\-]*)\}{2}/g });

brakk expects one argument, spec, which should be an array. If multiple argument are given, spec is constructed internally as an array that contains the empty string, followed by all arguments.

brakk maintains a dictionary object fxcfg to keep track of special effects settings. The factory function accepts a single, optional argument fxdefaults with your favorite defaults for fxcfg.

spec's elements are processed, in original order, depending on their type:

  • undefined and null values are ignored.
  • Any non-Array object: Its keys are processed, in default sort order, as effect commands (see below).
  • Tag head: The first element that's neither of the above.
  • Tag content: All elements that are neither of the above.

All tag content is converted as described below and concatenated to construct the initial result string.

Tag head special effects

Currently, the only supported type of tag head is a string. If the tag head is not empty, the template effects (see below) are applied. If the tag head still is not empty, an opening tag is constructed from it, and is prepended before the result string. If the tag head ends with a character other than >, a closing tag is constructed and appended to the result string.

Tag content special effects

Arrays are converted using brakk, and special effect processing is skipped since it should have happened already, as part of the conversion.

All other values are String()ified and then special effects may be applied:

  • First, template effects.
  • If fxcfg.rawXml !== false, XML special characters are replaced like xmldefuse does in default mode.

Template effects

(to be documented)

Effect commands

  • store Object upd: Object.assign(fxcfg.tplVars, upd);

Known issues

  • needs more/better tests and docs

 

License

ISC