Comparing version 1.0.0 to 2.0.0
@@ -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 |
{ | ||
"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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
10943
11
225
0
70
0
0
10
+ Addedget-stdin@^5.0.1
+ Addedyargs@^8.0.2
+ Addedansi-regex@2.1.13.0.1(transitive)
+ Addedcamelcase@4.1.0(transitive)
+ Addedcliui@3.2.0(transitive)
+ Addedcode-point-at@1.1.0(transitive)
+ Addedcross-spawn@5.1.0(transitive)
+ Addeddecamelize@1.2.0(transitive)
+ Addederror-ex@1.3.2(transitive)
+ Addedexeca@0.7.0(transitive)
+ Addedfind-up@2.1.0(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-caller-file@1.0.3(transitive)
+ Addedget-stdin@5.0.1(transitive)
+ Addedget-stream@3.0.0(transitive)
+ Addedgraceful-fs@4.2.11(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedhosted-git-info@2.8.9(transitive)
+ Addedinvert-kv@1.0.0(transitive)
+ Addedis-arrayish@0.2.1(transitive)
+ Addedis-core-module@2.15.1(transitive)
+ Addedis-fullwidth-code-point@1.0.02.0.0(transitive)
+ Addedis-stream@1.1.0(transitive)
+ Addedisexe@2.0.0(transitive)
+ Addedlcid@1.0.0(transitive)
+ Addedload-json-file@2.0.0(transitive)
+ Addedlocate-path@2.0.0(transitive)
+ Addedlru-cache@4.1.5(transitive)
+ Addedmem@1.1.0(transitive)
+ Addedmimic-fn@1.2.0(transitive)
+ Addednormalize-package-data@2.5.0(transitive)
+ Addednpm-run-path@2.0.2(transitive)
+ Addednumber-is-nan@1.0.1(transitive)
+ Addedos-locale@2.1.0(transitive)
+ Addedp-finally@1.0.0(transitive)
+ Addedp-limit@1.3.0(transitive)
+ Addedp-locate@2.0.0(transitive)
+ Addedp-try@1.0.0(transitive)
+ Addedparse-json@2.2.0(transitive)
+ Addedpath-exists@3.0.0(transitive)
+ Addedpath-key@2.0.1(transitive)
+ Addedpath-parse@1.0.7(transitive)
+ Addedpath-type@2.0.0(transitive)
+ Addedpify@2.3.0(transitive)
+ Addedpseudomap@1.0.2(transitive)
+ Addedread-pkg@2.0.0(transitive)
+ Addedread-pkg-up@2.0.0(transitive)
+ Addedrequire-directory@2.1.1(transitive)
+ Addedrequire-main-filename@1.0.1(transitive)
+ Addedresolve@1.22.8(transitive)
+ Addedsemver@5.7.2(transitive)
+ Addedset-blocking@2.0.0(transitive)
+ Addedshebang-command@1.2.0(transitive)
+ Addedshebang-regex@1.0.0(transitive)
+ Addedsignal-exit@3.0.7(transitive)
+ Addedspdx-correct@3.2.0(transitive)
+ Addedspdx-exceptions@2.5.0(transitive)
+ Addedspdx-expression-parse@3.0.1(transitive)
+ Addedspdx-license-ids@3.0.20(transitive)
+ Addedstring-width@1.0.22.1.1(transitive)
+ Addedstrip-ansi@3.0.14.0.0(transitive)
+ Addedstrip-bom@3.0.0(transitive)
+ Addedstrip-eof@1.0.0(transitive)
+ Addedsupports-preserve-symlinks-flag@1.0.0(transitive)
+ Addedvalidate-npm-package-license@3.0.4(transitive)
+ Addedwhich@1.3.1(transitive)
+ Addedwhich-module@2.0.1(transitive)
+ Addedwrap-ansi@2.1.0(transitive)
+ Addedy18n@3.2.2(transitive)
+ Addedyallist@2.1.2(transitive)
+ Addedyargs@8.0.2(transitive)
+ Addedyargs-parser@7.0.0(transitive)
- Removedarrify@^1.0.1
- Removedminimist@^1.2.0
- Removedarrify@1.0.1(transitive)
- Removedminimist@1.2.8(transitive)