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

Commit

Permalink
feat(parser): add asciidoc parser
Browse files Browse the repository at this point in the history
asciidoc parser to convert ascii doc files to html
  • Loading branch information
thetutlage committed Sep 18, 2016
1 parent 3897655 commit 7d2b7f0
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"author": "adonisjs",
"license": "MIT",
"dependencies": {
"asciidoctor.js": "^1.5.5-2",
"cat-log": "^1.0.0",
"fs-extra": "^0.30.0",
"gray-matter": "^2.0.2",
Expand Down
40 changes: 40 additions & 0 deletions src/AsciiDoc/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict'

/*
* docketjs
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

const asciidoctor = require('asciidoctor.js')()
const matter = require('gray-matter')
const opal = asciidoctor.Opal
const processor = asciidoctor.Asciidoctor(true)

class AsciiDoc {

constructor (attributes) {
attributes = attributes || ['icon=font', 'skip-front-matter', 'sectlinks', 'sectanchors', 'toc=macro']
this.options = opal.hash({doctype: 'article', attributes})
}

/**
* Converts an ascii doc to HTML.
*
* @param {String} content
*
* @return {Object}
*/
convert (content) {
const doc = processor.$load(content, this.options)
const frontMatter = doc.$attr('front-matter')
const meta = typeof (frontMatter) === 'string' ? matter(`---\n${frontMatter}\n---`) : {}
return {meta: meta.data, html: doc.$content()}
}

}

module.exports = AsciiDoc
40 changes: 40 additions & 0 deletions test/ascii.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict'

/*
* docketjs
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

const chai = require('chai')
const expect = chai.expect
const path = require('path')
const fs = require('fs')
const cheerio = require('cheerio')
const AsciiDoc = require('../src/AsciiDoc')

describe('AsciiDoc', function () {
it('should be able to parse ascii doc string to html', function () {
const asciidoc = new AsciiDoc()
const asciiFile = path.join(__dirname, './docs/abc.adoc')
const doc = asciidoc.convert(fs.readFileSync(asciiFile, 'utf-8'))
expect(doc).to.have.property('html')
expect(doc).to.have.property('meta')
expect(doc.meta.permalink).to.equal('hello-world')
expect(doc.meta.title).to.equal('Hello World')
expect(doc.html).to.be.a('string')
})

it('should be generate toc for the document', function () {
const asciidoc = new AsciiDoc()
const asciiFile = path.join(__dirname, './docs/abc.adoc')
const doc = asciidoc.convert(fs.readFileSync(asciiFile, 'utf-8'))
expect(doc).to.have.property('html')
expect(doc).to.have.property('meta')
const $ = cheerio.load(doc.html)
expect($('#toctitle')).to.have.length(1)
})
})
15 changes: 15 additions & 0 deletions test/docs/abc.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
title: Hello World
permalink: hello-world
---

toc::[]

== Hello World
This is a sample abc file.

=== A Nested Heading
This has some related content

== A top Level One Again
I am the king
4 changes: 2 additions & 2 deletions test/readers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require('co-mocha')
describe('Readers', function () {
context('Fs Reader', function () {
it('should return an array of all markdown files from a given directory', function * () {
const fsReader = new FSReader(path.join(__dirname, './docs'))
const fsReader = new FSReader(path.join(__dirname, './docs'), ['.md'])
const docs = yield fsReader.getDocs()
expect(docs).to.be.an('array')
expect(docs.length).to.equal(3)
Expand All @@ -30,7 +30,7 @@ describe('Readers', function () {
})

it('should be able to define doc extensions for the files to be returned', function * () {
const fsReader = new FSReader(path.join(__dirname, './docs'), ['.adoc'])
const fsReader = new FSReader(path.join(__dirname, './docs'), ['.aciidoc'])
const docs = yield fsReader.getDocs()
expect(docs).to.be.an('array')
expect(docs.length).to.equal(0)
Expand Down

0 comments on commit 7d2b7f0

Please sign in to comment.