From c771218eda4e31673d35a3353981f7f28f7d6e20 Mon Sep 17 00:00:00 2001 From: Nathan Houle Date: Sat, 20 Sep 2014 17:43:40 -0700 Subject: [PATCH] Initial commit --- .gitignore | 2 + CHANGELOG.md | 3 ++ LICENSE.md | 21 ++++++++++ Makefile | 14 +++++++ README.md | 54 +++++++++++++++++++++++++ lib/index.js | 54 +++++++++++++++++++++++++ package.json | 30 ++++++++++++++ test/fixtures/basic/expected/index.html | 8 ++++ test/fixtures/basic/src/index.txt | 3 ++ test/index.js | 22 ++++++++++ 10 files changed, 211 insertions(+) create mode 100644 .gitignore create mode 100644 CHANGELOG.md create mode 100644 LICENSE.md create mode 100644 Makefile create mode 100644 README.md create mode 100644 lib/index.js create mode 100644 package.json create mode 100644 test/fixtures/basic/expected/index.html create mode 100644 test/fixtures/basic/src/index.txt create mode 100644 test/index.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0533388 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +test/fixtures/**/build diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..9cf92c3 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,3 @@ +## 1.0.0 + +- :+1: diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..5fee62d --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2014 Nathan Houle + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5aa8289 --- /dev/null +++ b/Makefile @@ -0,0 +1,14 @@ +NPM = $(shell which npm) +MOCHA = ./node_modules/.bin/mocha + +MOCHA_OPTS = \ + --check-leaks \ + --reporter spec + +node_modules: package.json + $(NPM) install + +test: node_modules + $(MOCHA) $(MOCHA_OPTS) test/index.js + +.PHONY: test diff --git a/README.md b/README.md new file mode 100644 index 0000000..564a30e --- /dev/null +++ b/README.md @@ -0,0 +1,54 @@ +# metalsmith-asciidoc + +A Metalsmith plugin to convert asciidoc files. + + +## Installation + +```sh +$ npm install metalsmith-asciidoc +``` + + +## CLI Usage + +Install via `npm`, then add `metalsmith-asciidoc` key to your `metalsmith.json` plugins section: + +```json +{ + "plugins": { + "metalsmith-asciidoc": {} + } +} +``` + + +## API Usage + +Install via `npm` and require `metalsmith-asciidoc`, then pass to Metalsmith using the `use` method: + +```js +var asciidoc = require('metalsmith-asciidoc'); + +metalsmith.use(asciidoc()); +``` + +## Development + +Clone the repo and install dependencies: + +```sh +git clone https://github.com/ndhoule/metalsmith-asciidoc.git +npm install +``` + +Running tests: + +```sh +make test +``` + + +## License + +Code copyright 2014 [Nathan Houle](mailto:nathan+github@nathanhoule.com). Released under the [MIT license](LICENSE.md). diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..763b43e --- /dev/null +++ b/lib/index.js @@ -0,0 +1,54 @@ +'use strict'; + +var path = require('path'); +var asciidoc = ( require('asciidoctor.js')() ).Asciidoctor(true); + +var rAsciidoc = /\.(txt|adoc|asciidoc)$/; + +/** + * A function that + * + * @param {string} filename The filename to test. + * @return {boolean} Returns `true` if the file has a recognized Asciidoc extension, otherwise + * `false`. + */ +var isAsciidoc = function(filename) { + return rAsciidoc.test(filename); +}; + +/** + * Metalsmith plugin that converts Asciidoc documents to HTML files. + * + * @param {Object} [options] A list of options to pass to the Asciidoctor compiler. + * TODO: Currently unused, to be implemented in the future. + * @return {Function} A plugin function, to be consumed by Metalsmith. + */ +module.exports = function plugin(options) { + options = options || {}; + + return function(files, metalsmith, done) { + setImmediate(done); + + Object.keys(files).forEach(function(filename) { + if (!isAsciidoc(filename)) { + return; + } + + var data = files[filename]; + var dir = path.dirname(filename); + // The name of the file, without its path or file extension. + var baseFilename = path.basename(filename, path.extname(filename)); + // The name of the output file. + var outpath = (dir !== '.' ? path.join(dir, baseFilename) : baseFilename) +'.html'; + + // Convert the input Asciidoc to HTML and replace the file's contents + data.contents = new Buffer(asciidoc.$convert(data.contents.toString())); + + // Remove the old filename from the output list + delete files[filename]; + + // Add the new file to the output list + files[outpath] = data; + }); + }; +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..98987ff --- /dev/null +++ b/package.json @@ -0,0 +1,30 @@ +{ + "name": "metalsmith-asciidoc", + "version": "1.0.0", + "description": "A Metalsmith plugin to convert asciidoc files.", + "author": "Nathan Houle (http://nathanhoule.com)", + "license": "MIT", + "repository": { + "type": "git", + "url": "https://github.com/ndhoule/metalsmith-asciidoc.git" + }, + "bugs": "https://github.com/ndhoule/metalsmith-asciidoc/issues", + "main": "lib/index.js", + "scripts": { + "test": "make test" + }, + "keywords": [ + "metalsmith", + "metalsmith-plugin", + "plugin" + ], + "dependencies": { + "asciidoctor.js": "^1.5.0" + }, + "devDependencies": { + "assert": "^1.1.2", + "assert-dir-equal": "^1.0.1", + "metalsmith": "^0.11.0", + "mocha": "^1.21.4" + } +} diff --git a/test/fixtures/basic/expected/index.html b/test/fixtures/basic/expected/index.html new file mode 100644 index 0000000..fb8ec9c --- /dev/null +++ b/test/fixtures/basic/expected/index.html @@ -0,0 +1,8 @@ +
+

Asciidoc Roolz

+
+
+

OMG it’s WAY better than Markdown, or any of those other markup languages.

+
+
+
\ No newline at end of file diff --git a/test/fixtures/basic/src/index.txt b/test/fixtures/basic/src/index.txt new file mode 100644 index 0000000..636dcf9 --- /dev/null +++ b/test/fixtures/basic/src/index.txt @@ -0,0 +1,3 @@ +== Asciidoc Roolz + +OMG it's _WAY_ better than *Markdown*, or any of those other markup languages. diff --git a/test/index.js b/test/index.js new file mode 100644 index 0000000..1444a89 --- /dev/null +++ b/test/index.js @@ -0,0 +1,22 @@ +'use strict'; + +var assert = require('assert'); +var equal = require('assert-dir-equal'); +var Metalsmith = require('metalsmith'); +var asciidoc = require('..'); + +describe('metalsmith-asciidoc', function() { + it('should convert asciidoc files', function(done) { + Metalsmith('test/fixtures/basic') + .use(asciidoc()) + .build(function(err) { + if (err) { + return done(err); + } + + equal('test/fixtures/basic/expected', 'test/fixtures/basic/build'); + + done(); + }); + }); +}); \ No newline at end of file