Skip to content
This repository has been archived by the owner on Apr 30, 2019. It is now read-only.

Commit

Permalink
Using htmllint package now
Browse files Browse the repository at this point in the history
  • Loading branch information
akyoto committed Sep 21, 2016
1 parent 35dc1e2 commit 393933e
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 47 deletions.
102 changes: 57 additions & 45 deletions lib/App/checkRoute.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
const requester = Promise.promisifyAll(require('request'))
const zlib = Promise.promisifyAll(require('zlib'))
const html5Lint = Promise.promisify(require('html5-lint'))

const enablePositiveMessages = false
const htmllint = Promise.promisify(require('htmllint'))

const htmlOptions = {
'doctype-html5': true,
'id-no-dup': true,
'attr-quote-style': false,
'indent-style': false,
'line-end-style': false,
'id-class-style': 'dash',
'tag-name-lowercase': false,
'tag-name-match': false,
'tag-bans': ['b']
}

let checkHTML = code => htmllint(code, htmlOptions)

let checkHTMLResponse = response => {
if(response.headers['content-encoding'] === 'gzip') {
return zlib.gunzipAsync(response.body)
.then(unzippedCode => unzippedCode.toString())
.then(checkHTML)
} else {
return checkHTML(response.body)
}
}

module.exports = function*(method, route) {
// The purpose of this function is only to write stuff to the console.
Expand Down Expand Up @@ -66,56 +88,46 @@ module.exports = function*(method, route) {

let pageLog = logMsg => console.log(this.chalk.blue('/' + route + ' '.repeat(Math.max(this.columns - 4 - this.chalk.stripColor(logMsg).length - route.length, 0))), logMsg)

// HTML5 check
let checkHTML5 = code => html5Lint(code).then(results => results.messages)
let checkHTML5Response = response => {
if(response.headers['content-encoding'] === 'gzip') {
return zlib.gunzipAsync(response.body)
.then(unzippedCode => unzippedCode.toString())
.then(checkHTML5)
} else {
return checkHTML5(response.body)
// HTML5 validator
if(method === 'GET' && contentType && contentType.includes('text/html')) {
let displayHTMLErrors = errors => {
if(errors.length === 0)
return;

errors.forEach(error => {
// There seems to be a bug with these rules: Ignore them.
if(error.rule === 'tag-name-match' || error.rule === 'indent-style')
return;

let keys = Object.keys(error.data)
let prefix = this.chalk.blue('/' + route) + ' ' + this.chalk.yellow(error.rule)

if(keys.length === 0) {
console.log(prefix)
} else if(keys.length === 1) {
console.log(prefix + ' ' + error.data[keys[0]])
} else {
console.log(prefix)
keys.forEach(key => console.log(' ' + this.chalk.dim(key) + ': ' + error.data[key]))
}
})
}
}

// HTML5 validator
if(!this.html5ValidatorDisabled && method === 'GET' && contentType && contentType.includes('text/html')) {
// Do not yield this because we don't want to wait for this slow response
checkHTML5Response(response).then(messages => {
if(messages.length === 0) {
if(enablePositiveMessages)
pageLog(this.chalk.green('HTML'))
return
}

messages.forEach(msg => console.log(this.chalk.blue('/' + route) + ' ' + this.chalk.yellow(msg.message)))
}).catch(error => {
if(this.html5ValidatorDisabled)
return

console.error(this.chalk.red('https://html5.validator.nu/ seems to be offline'))
this.html5ValidatorDisabled = true
})
let check = () => checkHTMLResponse(response).then(displayHTMLErrors).catch(console.error)

// If we're doing a reload of all pages because htmlLinterTasks is defined,
// delay the linter checks until we saved the page response times.
// Otherwise check it instantly.
if(this.htmlLinterTasks)
this.htmlLinterTasks.push(check)
else
check()
}

// JSON validator
if(response.body.toString().startsWith('{')) {
try {
JSON.parse(response.body)

if(enablePositiveMessages) {
let validMsg = 'JSON'

// Green if the JSON file is valid and content type fits
// Yellow if the JSON file is valid and content type is not correct
let color = this.chalk.green
if(!contentType || contentType.indexOf('application/json') === -1) {
color = this.chalk.yellow
validMsg = 'Content type not set to application/json'
}

pageLog(color(validMsg))
}
} catch(error) {
pageLog(this.chalk.red(error.toString()))
}
Expand Down
8 changes: 8 additions & 0 deletions lib/App/checkRoutes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module.exports = function*() {
this.htmlLinterTasks = []

let methods = {}

for(let tuple of this.pages) {
Expand All @@ -19,4 +21,10 @@ module.exports = function*() {
yield check()
}
}

for(let task of this.htmlLinterTasks) {
yield task()
}

delete this.htmlLinterTasks
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aero",
"version": "1.6.17",
"version": "1.7.0",
"description": "The fastest web framework on the node platform.",
"repository": "aerojs/aero",
"homepage": "https://github.com/aerojs/aero",
Expand All @@ -13,7 +13,7 @@
"bluebird": "3.x",
"chalk": "1.x",
"etag": "1.x",
"html5-lint": "0.x",
"htmllint": "0.x",
"image-size": "0.x",
"jade": "1.x",
"node-watch": "0.x",
Expand Down

0 comments on commit 393933e

Please sign in to comment.