You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Socket
Socket
Sign inDemoInstall

pug-parser

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pug-parser

The pug parser (takes an array of tokens and converts it to an abstract syntax tree)


Version published
Weekly downloads
1.5M
decreased by-3.41%
Maintainers
1
Install size
618 kB
Created
Weekly downloads
 

Package description

What is pug-parser?

The pug-parser npm package is a parser for the Pug templating language. It takes a Pug template and converts it into an Abstract Syntax Tree (AST) that can be further processed or transformed. This is useful for tasks such as template compilation, syntax analysis, and code generation.

What are pug-parser's main functionalities?

Parsing Pug Templates

This feature allows you to parse a Pug template into an Abstract Syntax Tree (AST). The code sample demonstrates how to use the pug-parser along with pug-lexer to convert a simple Pug template into its AST representation.

const pugParser = require('pug-parser');
const lex = require('pug-lexer');

const template = 'p Hello, World!';
const tokens = lex(template);
const ast = pugParser(tokens);
console.log(JSON.stringify(ast, null, 2));

Custom AST Transformations

This feature allows you to perform custom transformations on the AST generated from a Pug template. The code sample shows how to add a class attribute to all 'p' tags in the AST.

const pugParser = require('pug-parser');
const lex = require('pug-lexer');

const template = 'p Hello, World!';
const tokens = lex(template);
let ast = pugParser(tokens);

// Custom transformation: Add a class to all 'p' tags
function transformAST(node) {
  if (node.type === 'Tag' && node.name === 'p') {
    node.attrs.push({ name: 'class', val: 'greeting', mustEscape: true });
  }
  if (node.block) {
    node.block.nodes.forEach(transformAST);
  }
}
transformAST(ast);
console.log(JSON.stringify(ast, null, 2));

Other packages similar to pug-parser

Readme

Source

pug-parser

The pug parser (takes an array of tokens and converts it to an abstract syntax tree)

Build Status Dependency Status NPM version

Installation

npm install pug-parser

License

MIT

Keywords

FAQs

Package last updated on 23 Dec 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc