Socket
Socket
Sign inDemoInstall

html2pug

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

html2pug - npm Package Compare versions

Comparing version 1.0.0 to 2.0.0

test.js

8

CHANGELOG.md

@@ -7,2 +7,10 @@ # Changelog

## [2.0.0] - 2017-08-12
### Changed
- CLI invocation syntax
### Added
- Support for tab indentation
- Tests
## [1.0.0] - 2017-07-09

@@ -9,0 +17,0 @@ ### Changed

11

package.json
{
"name": "html2pug",
"version": "1.0.0",
"version": "2.0.0",
"description": "Converts HTML to Pug",

@@ -10,8 +10,9 @@ "main": "src/index.js",

"dependencies": {
"arrify": "^1.0.1",
"get-stdin": "^5.0.1",
"html-minifier": "^3.5.2",
"minimist": "^1.2.0",
"parse5": "^2.1.5"
"parse5": "^2.1.5",
"yargs": "^8.0.2"
},
"devDependencies": {
"ava": "^0.21.0",
"eslint": "^4.1.1",

@@ -28,3 +29,3 @@ "eslint-config-standard": "^10.2.1",

"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"test": "ava",
"standard": "prettier-standard 'src/**/*.js'",

@@ -31,0 +32,0 @@ "lint": "eslint src",

# html2pug
Converts HTML to Pug (formerly Jade)
Converts **HTML** to **Pug** templating language (_formerly Jade_).
Requires Node.js version `7.6` or higher.
Turns this :unamused:
```html
<!doctype html>
<html lang="en">
<head>
<title>Hello World!</title>
</head>
<body>
<div id="content">
<h1 class="title">Hello World!</h1>
</header>
</body>
</html>
```
Into this :tada:
```pug
!doctype html
html(lang='en')
head
title Hello World!
body
#content
h1.title Hello World!
```
## Install
Get it on npm:
Get it on [npm](https://www.npmjs.com/package/html2pug):

@@ -14,4 +41,30 @@ ```bash

### CLI
Accept input from a file and write to stdout:
```bash
html2pug -f /path/to/file.html
html2pug < example.html
```
Or write to a file:
```bash
html2pug < example.html > example.pug
```
See `html2pug --help` for more information.
### Programmatically
```js
const html2pug = require('html2pug')
const html = '<header><h1 class="title">Hello World!</h1></header>'
const pug = html2pug(html, { tabs: true })
```
### Options
Name | Type | Default | Description
--- | --- | --- | ---
tabs | Boolean | `false` | Use tabs instead of spaces
fragment | Boolean | `false` | Wrap in enclosing `<html>` and `<body>` tags

@@ -5,24 +5,54 @@ #!/usr/bin/env node

const argv = require('minimist')(process.argv.slice(2))
const html2pug = require('./index')
const fs = require('fs')
const arrify = require('arrify')
const { version } = require('../package.json')
const getStdin = require('get-stdin')
const html2pug = require('./')
const argv = require('yargs').argv
if (argv.f) {
const files = arrify(argv.f)
const options = {
fragment: argv.hasOwnProperty('fragment')
/**
* Create a help page
*/
const help = [
'\n Usage: html2pug [options] < [file]\n',
' Options:\n',
` -f, --fragment Don't wrap output in <html>/<body> tags`,
` -t, --tabs Use tabs instead of spaces`,
` -h, --help Show this page`,
` -v, --version Show version\n`,
' Examples:\n',
' # Accept input from file and write to stdout',
' $ html2pug < example.html\n',
' # Or write to a file',
' $ html2pug < example.html > example.pug \n'
].join('\n')
/**
* Convert HTML from stdin to Pug
*/
async function main ({ fragment, needsHelp, showVersion, tabs }) {
const stdin = await getStdin()
if (showVersion) {
return console.log(version)
}
files.forEach(file =>
fs.readFile(file, (err, buf) => {
if (err) {
throw new Error(err)
} else {
html2pug(buf.toString(), options)
.then(pug => console.log(pug))
.catch(err => console.log(err.stack))
}
})
)
if (needsHelp || !stdin) {
return console.log(help)
}
try {
const pug = await html2pug(stdin, { tabs, fragment })
console.log(pug)
} catch (e) {
throw e
}
}
/**
* Get the CLI options and run program
*/
main({
fragment: !!(argv.fragment || argv.f),
needsHelp: !!(argv.help || argv.h),
showVersion: !!(argv.version || argv.v),
tabs: !!(argv.tabs || argv.t)
})
'use strict'
const minify = require('html-minifier').minify
const parse5 = require('parse5')
const { minify } = require('html-minifier')
const { parse, parseFragment } = require('parse5')
const Parser = require('./parser')
module.exports = async (sourceHtml, opts = {}) => {
module.exports = (
sourceHtml,
{
tabs = false,
fragment = false,
caseSensitive = true,
removeEmptyAttributes = true,
collapseWhitespace = true,
collapseBooleanAttributes = true,
collapseInlineTagWhitespace = true
} = {}
) => {
// Minify source HTML
const html = minify(sourceHtml, {
removeEmptyAttributes: true,
collapseWhitespace: true,
collapseBooleanAttributes: true,
collapseInlineTagWhitespace: true,
caseSensitive: true
removeEmptyAttributes,
collapseWhitespace,
collapseBooleanAttributes,
collapseInlineTagWhitespace,
caseSensitive
})
// Server-side
const document = opts.fragment
? parse5.parseFragment(html)
: parse5.parse(html)
// Parse minified HTML
const parser = new Parser({
root: fragment ? parseFragment(html) : parse(html),
tabs
})
const parser = new Parser(document)
return parser.parse()
}

@@ -9,7 +9,12 @@ const {

class Parser {
constructor (root) {
constructor ({ root, tabs = false }) {
this.root = root
this.tabs = tabs
this.pug = ''
}
get indent () {
return this.tabs ? '\t' : ' '
}
parse () {

@@ -66,3 +71,3 @@ const walk = this.walk(this.root.childNodes, 0)

parseNode (node, level) {
const indent = ' '.repeat(level)
const indent = this.indent.repeat(level)

@@ -69,0 +74,0 @@ if (isDocumentTypeNode(node)) {

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc