Skip to content

Commit

Permalink
Remove 'use' prefix from options to match CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
izolate committed May 14, 2019
1 parent 0df8761 commit 434a18a
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 43 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [3.1.0] - 2019-05-14
## [4.0.0] - 2019-05-14
### Added
- Option `useDoubleQuotes` for attribute values
- Option `doubleQuotes` for attribute values
- Option `commas` to CLI
- Travis CI file

### Changed
- Refactored cli.js
- Option `useTabs` to `tabs` (**BREAKING CHANGE**)
- Option `useCommas` to `commas` (**BREAKING CHANGE**)
- Option `isFragment` to `fragment` (**BREAKING CHANGE**)

## [3.0.0] - 2019-04-07
### Changed
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ See `html2pug --help` for more information.
const html2pug = require('html2pug')

const html = '<header><h1 class="title">Hello World!</h1></header>'
const pug = html2pug(html, { useTabs: true })
const pug = html2pug(html, { tabs: true })
```

### Options

Name | Type | Default | Description
--- | --- | --- | ---
`useTabs` | `Boolean` | `false` | Use tabs instead of spaces
`useCommas` | `Boolean` | `true` | Use commas to separate node attributes
`useDoubleQuotes` | `Boolean` | `false` | Use double quotes instead of single quotes for attribute values
`isFragment` | `Boolean` | `false` | Wraps result in enclosing `<html>` and `<body>` tags if false
`tabs` | `Boolean` | `false` | Use tabs instead of spaces for indentation
`commas` | `Boolean` | `true` | Use commas to separate node attributes
`doubleQuotes` | `Boolean` | `false` | Use double quotes instead of single quotes for attribute values
`fragment` | `Boolean` | `false` | Wraps result in enclosing `<html>` and `<body>` tags if false
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "html2pug",
"version": "3.1.0",
"version": "4.0.0",
"description": "Converts HTML to Pug",
"main": "src/index.js",
"bin": {
Expand Down
8 changes: 4 additions & 4 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ if (hasFlag('v') || hasFlag('version')) {
}

const options = {
isFragment: hasFlag('f') || hasFlag('fragment'),
useTabs: hasFlag('t') || hasFlag('tabs'),
useCommas: hasFlag('c') || hasFlag('commas'),
useDoubleQuotes: hasFlag('d') || hasFlag('double-quotes'),
fragment: hasFlag('f') || hasFlag('fragment'),
tabs: hasFlag('t') || hasFlag('tabs'),
commas: hasFlag('c') || hasFlag('commas'),
doubleQuotes: hasFlag('d') || hasFlag('double-quotes'),
}

convert(options)
Expand Down
20 changes: 10 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ const Pugify = require('./parser')

const defaultOptions = {
// html2pug options
isFragment: false,
useTabs: false,
useCommas: true,
useDoubleQuotes: false,
fragment: false,
tabs: false,
commas: true,
doubleQuotes: false,

// html-minifier options
caseSensitive: true,
Expand All @@ -22,14 +22,14 @@ module.exports = (sourceHtml, options = {}) => {
const opts = { ...defaultOptions, ...options }
const html = minify(sourceHtml, opts)

const { isFragment, useTabs, useCommas, useDoubleQuotes } = opts
const { fragment, tabs, commas, doubleQuotes } = opts

// Parse HTML and convert to Pug
const documentRoot = isFragment ? parseFragment(html) : parse(html)
const pugify = new Pugify(documentRoot, {
useTabs,
useCommas,
useDoubleQuotes,
const doc = fragment ? parseFragment(html) : parse(html)
const pugify = new Pugify(doc, {
tabs,
commas,
doubleQuotes,
})
return pugify.parse()
}
8 changes: 4 additions & 4 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ class Parser {
this.pug = ''
this.root = root

const { useTabs, useCommas, useDoubleQuotes } = options
const { tabs, commas, doubleQuotes } = options

// Tabs or spaces
this.indentStyle = useTabs ? '\t' : ' '
this.indentStyle = tabs ? '\t' : ' '
// Comma separate attributes
this.separatorStyle = useCommas ? ', ' : ' '
this.separatorStyle = commas ? ', ' : ' '
// Single quotes or double
this.quoteStyle = useDoubleQuotes ? '"' : "'"
this.quoteStyle = doubleQuotes ? '"' : "'"
}

getIndent(level = 0) {
Expand Down
32 changes: 16 additions & 16 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ html(lang='en')
t.is(generated, pug)
})

test('result contains no outer html element when isFragment is truthy', t => {
const generated = html2pug('<h1>Hello World!</h1>', { isFragment: true })
test('result contains no outer html element when fragment is truthy', t => {
const generated = html2pug('<h1>Hello World!</h1>', { fragment: true })
t.falsy(generated.startsWith('html'))
})

Expand Down Expand Up @@ -82,15 +82,15 @@ test('creates multiline block when linebreaks are present', t => {
line
string`

const generated = html2pug(html, { isFragment: true })
const generated = html2pug(html, { fragment: true })
t.is(generated, pug)
})

test('uses div tag shorthand when id/class is present', t => {
const html = "<div id='foo' class='bar'>baz</div>"
const pug = '#foo.bar baz'

const generated = html2pug(html, { isFragment: true })
const generated = html2pug(html, { fragment: true })
t.is(generated, pug)
})

Expand All @@ -111,23 +111,23 @@ test('removes whitespace between HTML elements', t => {
li three
li four`

const generated = html2pug(html, { isFragment: true })
const generated = html2pug(html, { fragment: true })
t.is(generated, pug)
})

test('does not fail on unicode characters', t => {
const generated = html2pug('<h1 class="accents">â, é, ï, õ, ù</h1>', {
isFragment: true,
fragment: true,
})
const expected = 'h1.accents â, é, ï, õ, ù'

t.is(generated, expected)
})

test('uses tabs when useTabs is truthy', t => {
test('uses tabs when tabs is truthy', t => {
const generated = html2pug('<div><span>Tabs!</span></div>', {
isFragment: true,
useTabs: true,
fragment: true,
tabs: true,
})
const expected = 'div\n\tspan Tabs!'

Expand All @@ -136,7 +136,7 @@ test('uses tabs when useTabs is truthy', t => {

test('uses a comma to separate attributes', t => {
const generated = html2pug('<input type="text" name="foo" />', {
isFragment: true,
fragment: true,
})
const expected = "input(type='text', name='foo')"

Expand All @@ -145,8 +145,8 @@ test('uses a comma to separate attributes', t => {

test('uses a space to separate attributes', t => {
const generated = html2pug('<input type="text" name="foo" />', {
isFragment: true,
useCommas: false,
fragment: true,
commas: false,
})
const expected = "input(type='text' name='foo')"

Expand All @@ -155,8 +155,8 @@ test('uses a space to separate attributes', t => {

test('uses double quotes for attribute values', t => {
const generated = html2pug('<input type="text" name="foo" />', {
isFragment: true,
useDoubleQuotes: true,
fragment: true,
doubleQuotes: true,
})
const expected = 'input(type="text", name="foo")'

Expand All @@ -167,7 +167,7 @@ test('single quotes in attribute values are escaped', t => {
const generated = html2pug(
`<button aria-label="closin&apos;" onclick="window.alert('bye')">close</button>`,
{
isFragment: true,
fragment: true,
}
)
const expected = `button(aria-label='closin\\'', onclick='window.alert(\\'bye\\')') close`
Expand All @@ -178,7 +178,7 @@ test('single quotes in attribute values are escaped', t => {
test('collapses boolean attributes', t => {
const generated = html2pug(
`<input type="text" name="foo" disabled="disabled" readonly="readonly" />`,
{ isFragment: true }
{ fragment: true }
)
const expected = `input(type='text', name='foo', disabled, readonly)`

Expand Down

0 comments on commit 434a18a

Please sign in to comment.