1# pug-code-gen 2 3Default code-generator for pug. It generates HTML via a JavaScript template function. 4 5<!-- [](https://travis-ci.org/pugjs/pug-code-gen) --> 6[](https://david-dm.org/pugjs/pug?path=packages/pug-code-gen) 7[](https://www.npmjs.org/package/pug-code-gen) 8 9## Installation 10 11 npm install pug-code-gen 12 13## Usage 14 15```js 16var generateCode = require('pug-code-gen'); 17``` 18 19### `generateCode(ast, options)` 20 21Generate a JavaScript function string for the given AST. 22 23`ast` is a fully expanded AST for Pug, with all inclusion, extends, and filters resolved. 24 25`options` may contain the following properties that have the same meaning as the options with the same names in `pug`: 26 27 - pretty (boolean): default is `false` 28 - compileDebug (boolean): default is `true` 29 - doctype (string): default is `undefined` 30 - inlineRuntimeFunctions (boolean): default is `false` 31 - globals (array of strings): default is `[]` 32 - self (boolean): default is `false` 33 34In addition to above, `pug-code-gen` has the following unique options: 35 36 - includeSources (object): map of filename to source string; used if `compileDebug` is `true`; default is `undefined` 37 - templateName (string): the name of the generated function; default is `'template'` 38 39```js 40var lex = require('pug-lexer'); 41var parse = require('pug-parser'); 42var wrap = require('pug-runtime/wrap'); 43var generateCode = require('pug-code-gen'); 44 45var funcStr = generateCode(parse(lex('p Hello world!')), { 46 compileDebug: false, 47 pretty: true, 48 inlineRuntimeFunctions: false, 49 templateName: 'helloWorld' 50}); 51//=> 'function helloWorld(locals) { ... }' 52 53var func = wrap(funcStr, 'helloWorld'); 54func(); 55//=> '\n<p>Hello world!</p>' 56``` 57 58### `new generateCode.CodeGenerator(ast, options)` 59 60The constructor for the internal class of the code generator. You shouldn't need to use this for most purposes. 61 62## License 63 64 MIT 65