How to use the pug.compile function in pug

To help you get started, we’ve selected a few pug examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github codingphasedotcom / Dypsy / lib / tasks / pug.js View on Github external
const destPath = path.join(distPath, fileData.dir);
		console.log(fileData.base);
		// create destination directory
		fse.mkdirsSync(destPath);

		// read page file
		const data = fse.readFileSync(`${srcPath}/${file}`, 'utf-8');

		// render page
		let pageContent;
		let completePage;

		// generate page content according to file type
		switch (fileData.ext) {
			case '.pug':
				var fn = pug.compile(data, {
					filename: `${srcPath}/${fileData.name}`,
					pretty: true
				})({
					pageTitle: 'Joes'
				});
				completePage = fn;
				break;
			default:
				pageContent = pageData.body;
		}
		// save the html file
		fse.writeFileSync(`${destPath}/${fileData.name}.html`, completePage);
	});
github aerojs / aero / lib / App / loadTemplate.js View on Github external
module.exports = function*(file) {
	try {
		let source = yield fs.readFileAsync(file, 'utf8')

		return pug.compile(source, {
			filename: file
		})
	} catch(error) {
		if(error.code !== 'ENOENT' || error.path !== file) {
			this.log(error)

			// Return a function that renders the error
			let render = () => error.toString()
			render.isError = true

			return render
		}

		return null
	}
}
github algolia / talksearch / scripts / build / markdown.js View on Github external
const headings = this.getHeadings(htmlBody);
    const sidebar = _.clone(pageData.sidebar);
    _.each(sidebar, category => {
      _.each(category.pages, page => {
        const linkBasename = path.basename(page.url, '.html');
        if (linkBasename === currentUrl) {
          page.headings = headings; // eslint-disable-line no-param-reassign
        }
      });
    });

    // Init layout
    const layoutName = fileData.layout;
    const layoutFile = `./src/_layouts/${layoutName}.pug`;
    const layoutContent = await helper.readFile(layoutFile);
    const pugCompile = pug.compile(layoutContent, { filename: layoutFile });

    // Compile layout
    const compileData = {
      ...pageData,
      sidebar,
      current: {
        url: currentUrl,
        content: htmlBody,
        ...fileData,
      },
    };
    const htmlContent = pugCompile(compileData);

    // Save to disk
    await helper.writeFile(destination, htmlContent);
  },
github aMarCruz / rollup-plugin-pug / dist / rollup-plugin-pug.js View on Github external
if (!filter(id)) {
                return null;
            }
            const isStatic = matchStaticPattern(id);
            const pugOpts = clonePugOpts(config, id);
            let body;
            let map;
            let fn;
            if (isStatic) {
                /*
                  This template is executed now and, at runtime, will be loaded through
                  `import` so it will not have access to runtime variables or methods.
                  Instead, we use here the `local` variables and the compile-time options.
                */
                const staticOpts = Object.assign({}, config.locals, config, { filename: id });
                fn = pug.compile(code, pugOpts);
                body = fn(staticOpts);
                body = `export default ${JSON.stringify(body)};\n`;
            }
            else {
                /*
                  This template will generate a module with a function to be executed at
                  runtime. It will be user responsibility to pass the correct parameters
                  to the function, here we only take care of the `imports`, incluiding the
                  pug runtime.
                */
                const imports = [];
                if (config.sourceMap) {
                    pugOpts.compileDebug = map = true;
                }
                // move the imports from the template to the top of the output queue
                code = moveImports(code, imports);
github searKing / preview-vscode / src / manager / pugDocumentContentManager.ts View on Github external
private pugSrcSnippetWithNodeModules(pugContent: string): string {
        // compile
        var options = {
            pretty: true
        }
        var fn = pug.compile(pugContent, options);
        var html = fn();

        return html;
    }
    private pugSrcSnippet(editor: TextEditor): Promise {
github usebedrock / bedrock / core / templates / locals.js View on Github external
locals.render = function (id, language) {
    const componentFileLocation = path.join(paths.content.templates.components, id + '.pug');
    const pugMarkup = fs.readFileSync(componentFileLocation, 'utf8');

    if (!language || language === 'pug') {
      return pugMarkup;
    } else if (language === 'html') {
      const indentedPugMarkup = pugMarkup.split('\n').map(line => `    ${line}`).join('\n');
      const markupWithLayout = `extends /../core/templates/layouts/sample\n\nblock content\n${indentedPugMarkup}`;
      return pug.compile(markupWithLayout, {
        pretty: true,
        basedir: 'content',
        filename: componentFileLocation
      })(locals);
    }
  };
github SE7ENSKY / webpack-verstat / bin / utils-old.js View on Github external
content: (function () {
				const fn = compile(`${bemto}\n${extractedData.content}`);
				const initialLocals = { renderBlock: renderBlockEngine };
				const locals = merge({}, initialLocals, modifiedExtractedData, getGlobalData());
				return fn(locals);
			})()
		};
github SE7ENSKY / webpack-verstat / bin / core.js View on Github external
function compilePUG(filePath, fileExtname) {
	const content = customReadFileSync(filePath);
	if (!content.length) {
		return generateEmailTemplateData(filePath, fileExtname, null);
	}
	const fn = compile(content, { compileDebug: true });
	const locals = {};
	return generateEmailTemplateData(filePath, fileExtname, fn(locals));
}
github meteor-vue / vue-meteor / packages / vue-pug / vue-pug.js View on Github external
global.vue.lang.pug = Meteor.wrapAsync(function({ source, basePath, inputFile }, cb) {
  var fn = pug.compile(source, {
    filename: basePath,
    fileMode: true
  });

  var html = fn();

  cb(null, {
    template: html
  });
});
github SE7ENSKY / webpack-verstat / bin / utils-old.js View on Github external
function handleAdjacentTemplate(file, fileSystem, compiler, browserSync, event, mode) {
	const content = customReadFile(file);
	if (content.length) {
		const fn = compile(content);
		const locals = {};
		handleAdjacentHTML(
			file.replace(extname(file), '.html'),
			fn(locals),
			true,
			fileSystem,
			compiler,
			browserSync,
			event,
			mode
		);
	}
}