Skip to content

Bump lodash from 4.17.15 to 4.17.21 #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions .eslintrc.yml

This file was deleted.

3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ index.pug

# Vim
*.swp

# Compiled JS files
dist/
4 changes: 2 additions & 2 deletions .prettierrc.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
trailingComma = "es5"
trailingComma = "all"
tabWidth = 2
semi = false
semi = true
singleQuote = true
2 changes: 1 addition & 1 deletion src/cli.js → bin/html2pug
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const hasFlag = require('has-flag')
const getStdin = require('get-stdin')
const html2pug = require('./')
const html2pug = require('../dist')
const { version } = require('../package.json')

// help represents the usage guide
Expand Down
5,966 changes: 1,880 additions & 4,086 deletions package-lock.json

Large diffs are not rendered by default.

45 changes: 29 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
"name": "html2pug",
"version": "4.0.0",
"description": "Converts HTML to Pug",
"main": "src/index.js",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"bin": {
"html2pug": "src/cli.js"
"html2pug": "bin/html2pug"
},
"dependencies": {
"get-stdin": "^6.0.0",
Expand All @@ -13,24 +14,36 @@
"parse5": "^5.1.0"
},
"devDependencies": {
"ava": "^1.4.1",
"eslint": "^5.16.0",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-config-prettier": "^4.1.0",
"eslint-plugin-import": "^2.16.0",
"eslint-plugin-prettier": "^3.0.1",
"husky": "^1.3.1",
"lint-staged": "^8.1.5",
"prettier": "^1.16.4"
"@types/html-minifier": "^3.5.3",
"@types/parse5": "^5.0.2",
"ava": "^2.3.0",
"husky": "^3.0.5",
"lint-staged": "^9.2.5",
"prettier": "^1.16.4",
"ts-node": "^8.3.0",
"tslint": "^5.19.0",
"tslint-config-prettier": "^1.18.0",
"typescript": "^3.6.2"
},
"scripts": {
"test": "ava test.js",
"lint": "eslint .",
"lint:fix": "eslint . --fix"
"lint": "tslint --fix -p tsconfig.json",
"test": "ava --verbose",
"build": "tsc",
"prepare": "npm run build",
"prepublishOnly": "npm test && npm run lint"
},
"ava": {
"compileEnhancements": false,
"extensions": [
"ts"
],
"require": [
"ts-node/register"
]
},
"lint-staged": {
"*.js": [
"npm run lint:fix",
"*.ts": [
"npm run lint",
"git add"
]
},
Expand Down
43 changes: 43 additions & 0 deletions src/Attribute.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import test from 'ava';
import Attribute from './Attribute';

test('stringifies an ID', async t => {
const attr = new Attribute('id', 'foo');
t.is(attr.toString(), '#foo');
});

test('stringifies a single class selector', async t => {
const attr = new Attribute('class', 'foo');
t.is(attr.toString(), '.foo');
});

test('stringifies multiple class selectors', async t => {
const attr = new Attribute('class', 'foo bar baz qux');
t.is(attr.toString(), '.foo.bar.baz.qux');
});

test('stringifies a generic attribute', async t => {
const attr = new Attribute('type', 'number');
t.is(attr.toString(), "type='number'");
});

test('changes quote style', async t => {
const attr = new Attribute('quote-style', 'double', true);
t.is(attr.toString(), 'quote-style="double"');
});

test('escapes single quotes from values', async t => {
const attr = new Attribute(
'style',
"background-image: url('/path/to/nowhere')",
);
t.is(
attr.toString(),
"style='background-image: url(\\'/path/to/nowhere\\')'",
);
});

test('omits value if blank', async t => {
const attr = new Attribute('required');
t.is(attr.toString(), 'required');
});
53 changes: 53 additions & 0 deletions src/Attribute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Represents a single attribute on a [PugNode].
*/
class Attribute {
// Attribute name
public name: string;
// Attribute value
public value: string;
// Quote style for values
public doubleQuotes: boolean = false;

constructor(name: string, value: string, doubleQuotes?: boolean) {
this.name = name;
this.value = value;
if (doubleQuotes) {
this.doubleQuotes = doubleQuotes;
}
}

/**
* Returns a quote character based on quote style.
*/
private get quote() {
return this.doubleQuotes ? '"' : '\'';
}

/**
* Creates a string representation of the attribute.
* e.g. key="value"
*/
public toString(): string {
switch (this.name) {
case 'id':
return `#${this.value}`;

case 'class':
return `.${this.value.split(' ').join('.')}`;

default: {
// If value is blank, return just the name (shorthand)
if (!this.value) {
return this.name;
}
// Add escaped single quotes (\') to attribute values
// to allow for surrounding single quotes.
const safeValue: string = this.value.replace(/'/g, '\\\'');
return `${this.name}=${this.quote}${safeValue}${this.quote}`;
}
}
}
}

export default Attribute;
27 changes: 27 additions & 0 deletions src/Options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Defines all the options for html2pug.
*/
export interface Options {
caseSensitive: boolean;
collapseBooleanAttributes: boolean;
collapseWhitespace: boolean;
commas: boolean;
doubleQuotes: boolean;
fragment: boolean;
preserveLineBreaks: boolean;
removeEmptyAttributes: boolean;
tabs: boolean;
}

// Default options for html2pug.
export const defaultOptions = {
caseSensitive: true,
collapseBooleanAttributes: true,
collapseWhitespace: true,
commas: true,
doubleQuotes: false,
fragment: false,
preserveLineBreaks: true,
removeEmptyAttributes: true,
tabs: false,
};
45 changes: 45 additions & 0 deletions src/Parser.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import test from 'ava';
import Html2Pug from './index';

test('converts a single HTML element', async t => {
const pug = Html2Pug('<h1 class="title">Hello, world!</h1>', {
fragment: true,
});
t.is(pug, 'h1.title Hello, world!');
});

test('converts a nested HTML fragment', async t => {
const pug = Html2Pug(
'<ul id="fruits" class="list"><li class="item">Mango</li><li class="item">Apple</li></ul>',
{
fragment: true,
},
);
const expected = `ul#fruits.list
li.item Mango
li.item Apple`;
t.is(pug, expected);
});

test('removes whitespace between HTML elements', t => {
const pug = Html2Pug(
`<ul class="list">
<li>one</li>
<li>two</li>

<li>three</li>


<li>four</li>
</ul>`,
{ fragment: true },
);

const expected = `ul.list
li one
li two
li three
li four`;

t.is(pug, expected);
});
Loading