diff --git a/README.md b/README.md index aeb6f1c..b6e590f 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,8 @@ [![Greenkeeper badge](https://badges.greenkeeper.io/andrejewski/himalaya.svg)](https://greenkeeper.io/) [Try online 🚀](http://andrejewski.github.io/himalaya) +| +[Read the specification 📖](https://github.com/andrejewski/himalaya/blob/master/text/ast-spec-v1.md) ## Usage @@ -17,9 +19,10 @@ npm install himalaya ``` ```js -var himalaya = require('himalaya') -var html = require('fs').readFileSync('/webpage.html', {encoding: 'utf8'}) -var json = himalaya.parse(html) +import fs from 'fs' +import {parse} from 'himalaya' +const html = fs.readFileSync('/webpage.html', {encoding: 'utf8'}) +const json = parse(html) console.log('👉', json) ``` @@ -27,91 +30,23 @@ console.log('👉', json) Download [himalaya.js](https://github.com/andrejewski/himalaya/blob/master/docs/dist/himalaya.js) and put it in a `" - const xml = "" - const jsonHTML = parse(html) - const jsonXML = parse(xml) - - t.is(toHTML(jsonHTML), html) - t.is(toHTML(jsonHTML, { - doctype: 'xml' - }), xml) - - t.is(toHTML(jsonXML), html) - t.is(toHTML(jsonXML, { - doctype: 'xml' - }), xml) -}) - -test('toPug() should handle plain text', t => { - const html = 'This is text.' - const jade = '| This is text.' - t.is(toPug(parse(html)), jade) -}) - -test('toPug() should handle multi-line plain text', t => { - const html = 'This is multiline text.\nLook newlines.' - const jade = '| This is multiline text.\n| Look newlines.' - t.is(toPug(parse(html)), jade) -}) - -test('toPug() should handle inline comments', t => { - const html = '' - const jade = '// Comment ' - t.is(toPug(parse(html)), jade) -}) - -test('toPug() should handle multi-line comments', t => { - const html = [ - '' - ].join('\n') - const jade = [ - '//', - ' This is a multiline comment.', - ' Look newlines.' - ].join('\n') - t.is(toPug(parse(html)), jade) -}) - -test('toPug() should write short-hand tag ids', t => { - const html = "
" - const jade = 'article#story' - t.is(toPug(parse(html)), jade) -}) - -test('toPug() should write short-hand tag classes', t => { - const html = "
" - const jade = 'article.story.story--main' - t.is(toPug(parse(html)), jade) -}) - -test('toPug() should ignore `div` if an id or class(es) are provided', t => { - const htmlId = "
" - const jadeId = '#block' - t.is(toPug(parse(htmlId)), jadeId) - - const htmlClass = "
" - const jadeClass = '.block' - t.is(toPug(parse(htmlClass)), jadeClass) - - const htmlClasses = "
" - const jadeClasses = '.block.block--jumbo' - t.is(toPug(parse(htmlClasses)), jadeClasses) -}) - -test('toPug() should write attributes', t => { - const html = "" - const jade = "canvas(width='500', height='400')" - t.is(toPug(parse(html)), jade) -}) - -test('toPug() should write data-* attributes', t => { - const html = "
" - const jade = "div(data-one='5', data-two='five')" - t.is(toPug(parse(html)), jade) -}) - -test('toPug() should do basic escaping if a value contains either single or double quotes', t => { - const html = '
' - const jade = 'div(data-val="cake is \'good\'")' - t.is(toPug(parse(html)), jade) -}) - -test('toPug() should write the style attribute', t => { - const html = "Word" - const jade = "b(style='font-weight: bold; font-style: italics') Word" - t.is(toPug(parse(html)), jade) -}) - -test('toPug() should appropriate place tag inner text', t => { - const htmlInline = '

Hello

' - const jadeInline = 'h1 Hello' - t.is(toPug(parse(htmlInline)), jadeInline) - - const htmlMultiline = '

Hello\nWorld

' - const jadeMultiline = 'h1.\n Hello\n World' - t.is(toPug(parse(htmlMultiline)), jadeMultiline) -}) - -test('toPug() should use tabs for indentation if configured', t => { - const html = '

Hello\nWorld

' - const jade = 'h1.\n\t\tHello\n\t\tWorld' - const jadeOptions = {indentation: '\t\t'} - t.is(toPug(parse(html), jadeOptions), jade) -}) - -test('toPug() should work for script and style tags', t => { - const htmlScript = "" - const jadeScript = "script(type='text/javascript').\n console.log('yes');\n console.log('no');" - t.is(toPug(parse(htmlScript)), jadeScript) - - const htmlStyle = '' - const jadeStyle = 'style.\n h1 {color: #fff;}\n .text {font-size: 12px;}' - t.is(toPug(parse(htmlStyle)), jadeStyle) -}) - -test('toPug() should handle receiving a single root node', t => { - const html = '

Words are words.

' - const jade = [ - 'div', - ' p Words are words.' - ].join('\n') - t.is(toPug(parse(html)[0]), jade) -}) - -test('toPug() should work for void tags', t => { - const html = '' - const jade = "img(src='cake.png')" - t.is(toPug(parse(html)), jade) -}) - -test('toPug() should prioritize using a configured doctype', t => { - const html = '' - const jade = 'doctype foobar' - const jadeOptions = {doctype: 'foobar', indentation: ' '} - t.is(toPug(parse(html), jadeOptions), jade) -}) - -test('toPug() should produce proper shorthand doctypes', t => { - const cases = [ - [ - '', - 'doctype transitional' - ], - [ - '', - 'doctype frameset' - ], - [ - '', - 'doctype strict' - ], - [ - '', - 'doctype basic' - ], - [ - '', - 'doctype 1.1' - ], - [ - '', - 'doctype mobile' - ], - ['', 'doctype html'] - ] - - cases.forEach(([html, jade]) => { - t.is(toPug(parse(html)), jade) - }) -}) diff --git a/test/translate/v1.js b/test/translate/v1.js deleted file mode 100644 index 5a1be6f..0000000 --- a/test/translate/v1.js +++ /dev/null @@ -1,61 +0,0 @@ -import test from 'ava' -import himalaya from '../../lib' -import format from '../../lib/formats/v1' -import translate from '../../lib/translate/v1' -const {toHTML} = translate - -const parseDefaults = Object.assign( - {}, - himalaya.parseDefaults, - {format} -) - -const parse = html => himalaya.parse(html, parseDefaults) - -test('toHTML() should handle simple conversions', t => { - const str1 = '

Text

' - t.is(toHTML(parse(str1)), str1) - - const str2 = 'Text' - t.is(toHTML(parse(str2)), str2) - - const str3 = '' - t.is(toHTML(parse(str3)), str3) -}) - -test('toHTML() should work for void elements', t => { - const meta = "" - t.is(toHTML(parse(meta)), meta) - - const link = "" - t.is(toHTML(parse(link)), link) -}) - -test('toHTML() should build the class attribute properly', t => { - const elem = "
" - t.is(toHTML(parse(elem)), elem) -}) - -test('toHTML() should build data-* attributes properly', t => { - const elem = "
" - t.is(toHTML(parse(elem)), elem) -}) - -test('toHTML() should build the style attribute properly', t => { - const elem = "
" - t.is(toHTML(parse(elem)), elem) -}) - -test('toHTML() should do basic escaping if a value contains either single or double quotes', t => { - const html = '
' - t.is(toHTML(parse(html)), html) -}) - -test('toHTML() should preserve whitespace', t => { - const html = [ - ' ', - '

Document

', - ' ' - ].join('\n') - t.is(toHTML(parse(html)), html) -}) diff --git a/text/translation.md b/text/translation.md deleted file mode 100644 index 4a367d7..0000000 --- a/text/translation.md +++ /dev/null @@ -1,55 +0,0 @@ -# Himalaya Translations - -This document describes the functions included with the Himalaya parser for transforming the [abstract syntax tree](https://github.com/andrejewski/himalaya/tree/master/docs/ast-spec.md) into different HTML-based languages. - -## Version 1 Translations - -```js -import himalaya from 'himalaya' -import {toHTML} from 'himalaya/lib/translate/v1' - -const source = "" -const json = himalaya.parse(source) - -const html = toHTML(json) -// => "" -``` - -*Note: Version 1 does not include the `toPug` transform.* - -## Version 0 Translations - -The language translations included are for HTML and [Pug](https://pugjs.org/api/getting-started.html). - -```js -import himalaya from 'himalaya' -import {toHTML, toPug} from 'himalaya/lib/translate/v0' - -const source = "" -const json = himalaya.parse(source) - -const html = toHTML(json) -// => "" - -const pug = toPug(json) -// => "img(src='bar.png' async)" - -const xml = toHTML(json, {doctype: 'xml'}) -// => "" - -const pugXml = toPug(json, {doctype: 'xml'}) -// => "img(src='bar.png', async='async')" -``` - -### toHTML(ast, [options]) HTML -The HTML translator takes a given **ast** and an optional **options** object containing: - -- `doctype` which if set to `"xml"` will render boolean attributes and void tags in XML format - -### toPug(ast, [options]) Pug -The Pug translator takes a given **ast** and an optional **options** object containing: - -- `doctype` which if set to `"xml"` will render boolean attributes in XML format -- `indentation` defaults to two spaces - -The Pug translator is not a one-to-one translation with HTML. HTML transformed into Pug and then into HTML will lose some of the original's whitespace. diff --git a/translate.js b/translate.js deleted file mode 100644 index 37c24fa..0000000 --- a/translate.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = require('./lib/translate/v0').default