Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ndhoule committed Sep 21, 2014
0 parents commit c771218
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
test/fixtures/**/build
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 1.0.0

- :+1:
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -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.
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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:[email protected]). Released under the [MIT license](LICENSE.md).
54 changes: 54 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -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;
});
};
};
30 changes: 30 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "metalsmith-asciidoc",
"version": "1.0.0",
"description": "A Metalsmith plugin to convert asciidoc files.",
"author": "Nathan Houle <[email protected]> (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"
}
}
8 changes: 8 additions & 0 deletions test/fixtures/basic/expected/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div class="sect1">
<h2 id="_asciidoc_roolz">Asciidoc Roolz</h2>
<div class="sectionbody">
<div class="paragraph">
<p>OMG it&#8217;s <em>WAY</em> better than <strong>Markdown</strong>, or any of those other markup languages.</p>
</div>
</div>
</div>
3 changes: 3 additions & 0 deletions test/fixtures/basic/src/index.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
== Asciidoc Roolz

OMG it's _WAY_ better than *Markdown*, or any of those other markup languages.
22 changes: 22 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
});

0 comments on commit c771218

Please sign in to comment.