Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

grunt-literate

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

grunt-literate - npm Package Compare versions

Comparing version 0.1.3 to 0.1.4

bin/ljs.js

18

footer.md

@@ -1,17 +0,1 @@

## Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style.
Add unit tests for any new or changed functionality.
Lint and test your code using [Grunt](http://gruntjs.com/).
Make a pull request, but don't commit `README.md`!
## Release History
- 0.1.3 Directives
- 0.1.2 Newline improvements
- Newline at the end of comment
- Only one newline at the end of generated file
- 0.1.1 Fix issue with unindenting
- 0.1.0 Initial release
## Related work

@@ -22,1 +6,3 @@

however *literate* is markup-language-agnostic.
## LICENSE

@@ -19,2 +19,3 @@ /*

"lib/*.js",
"bin/*.js",
"tasks/*.js",

@@ -21,0 +22,0 @@ ],

@@ -0,1 +1,6 @@

/**
### Lex.js
This is for demo of `/// include` directive.
*/
"use strict";

@@ -2,0 +7,0 @@

"use strict";
/**
#### lib/literate.js
### Library
This is the heart of this task. Check the file, if you're interested
You can also use *grunt-literate* as a normal library:
*NOTE* this is demo of `include` directive
```
var documentation = require("grunt-literate")("hello.js", { code: true });
```
*/

@@ -15,2 +17,3 @@

var assert = require("assert");
var glob = require("glob");

@@ -32,14 +35,35 @@ var whitespaceEndRe = /^\s*$/;

function stripShebang(contents) {
var m = contents.match(/^#!\/[^\n]*\n/);
return m ? contents.substr(m[0].length) : contents;
}
function fileDirective(filename, value, regexp, callback) {
var m = value.match(regexp);
if (m) {
var directivePattern = m[1];
var globPattern = path.join(path.dirname(filename), directivePattern);
var files = glob.sync(globPattern);
if (files.length === 0) {
throw new Error(directivePattern + " doesn't match any files");
}
files.forEach(callback);
return true;
} else {
return false;
}
}
function getTokens(filename) {
var contents = fs.readFileSync(filename).toString();
contents = stripShebang(contents);
var tokens = lex(contents);
var m, value, includename;
var resTokens = [];
tokens.forEach(function (token) {
var r;
if (token.type === "Comment" && token.value.type === "Line" && token.value.value[0] === "/") {
value = token.value.value.substr(1);
m = value.match(/^\s*plain\s+(.*?)\s*$/);
if (m) {
includename = path.join(path.dirname(filename), m[1]);
var value = token.value.value.substr(1);
r = fileDirective(filename, value, /^\s*plain\s+(.*?)\s*$/, function (includename) {
resTokens.push({

@@ -49,11 +73,9 @@ type: "Plain",

});
return;
}
});
if (r) { return; }
m = value.match(/^\s*include\s+(.*?)\s*$/);
if (m) {
includename = path.join(path.dirname(filename), m[1]);
r = fileDirective(filename, value, /^\s*include\s+(.*?)\s*$/, function (includename) {
resTokens = resTokens.concat(getTokens(includename));
return;
}
});
if (r) { return; }

@@ -60,0 +82,0 @@ assert(false, "unknown directive: " + value);

{
"name": "grunt-literate",
"description": "Generate docs from your source",
"version": "0.1.3",
"version": "0.1.4",
"homepage": "https://github.com/phadej/grunt-literate",

@@ -21,6 +21,9 @@ "author": {

"type": "BSD3",
"url": "https://github.com/phadej/grunt-literate/blob/master/LICENSE-BSD3"
"url": "https://github.com/phadej/grunt-literate/blob/master/LICENSE"
}
],
"main": "Gruntfile.js",
"main": "lib/literate.js",
"bin": {
"ljs": "./bin/ljs.js"
},
"engines": {

@@ -33,3 +36,3 @@ "node": ">= 0.8.0"

"devDependencies": {
"grunt-contrib-jshint": "~0.6.0",
"grunt-contrib-jshint": "~0.7.1",
"grunt": "~0.4.1"

@@ -42,7 +45,12 @@ },

"gruntplugin",
"literate"
"literate",
"weave",
"programming",
"ljs"
],
"dependencies": {
"esprima": "~1.0.4"
"esprima": "~1.0.4",
"optimist": "~0.6.0",
"glob": "~3.2.6"
}
}

@@ -6,2 +6,4 @@ # grunt-literate

[![Code Climate](https://codeclimate.com/github/phadej/grunt-literate.png)](https://codeclimate.com/github/phadej/grunt-literate)
[![NPM version](https://badge.fury.io/js/grunt-literate.png)](http://badge.fury.io/js/grunt-literate)
[![Dependency Status](https://gemnasium.com/phadej/grunt-literate.png)](https://gemnasium.com/phadej/grunt-literate)

@@ -55,9 +57,25 @@ ## Getting Started

#### lib/literate.js
### Lex.js
This is the heart of this task. Check the file, if you're interested
This is for demo of `/// include` directive.
*NOTE* this is demo of `include` directive
### Library
You can also use *grunt-literate* as a normal library:
```
var documentation = require("grunt-literate")("hello.js", { code: true });
```
### ljs
If `grunt-literate` is installed globally,
you can use `ljs` command line tool to process your literate javascript files
```sh
$ ljs -c -o foo.md foo.js
$ ljs --help
```
## Contributing

@@ -72,2 +90,6 @@

- 0.1.4 Usage as executable and library
- run `ljs`
- or `require("grunt-literate")(filename)`
- also you may use glob patterns in directives
- 0.1.3 Directives

@@ -79,3 +101,2 @@ - 0.1.2 Newline improvements

- 0.1.0 Initial release
## Related work

@@ -86,1 +107,34 @@

however *literate* is markup-language-agnostic.
## LICENSE
Copyright Oleg Grenrus 2013
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of Oleg Grenrus nor the names of other
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

@@ -18,2 +18,4 @@

[![Code Climate](https://codeclimate.com/github/phadej/grunt-literate.png)](https://codeclimate.com/github/phadej/grunt-literate)
[![NPM version](https://badge.fury.io/js/grunt-literate.png)](http://badge.fury.io/js/grunt-literate)
[![Dependency Status](https://gemnasium.com/phadej/grunt-literate.png)](https://gemnasium.com/phadej/grunt-literate)

@@ -67,12 +69,98 @@ ## Getting Started

### Lex.js
This is for demo of `/// include` directive.
```js
"use strict";
var esprima = require("esprima");
function lex(contents) {
var syntax = esprima.parse(contents, {
tokens: true,
loc: true,
range: true,
comment: true
});
var tokens = [];
var currRange = 0;
function addWhitespace(from, to) {
var ws;
var comments = syntax.comments.filter(function (comment) {
return comment.range[0] >= from && comment.range[1] <= to;
});
comments.forEach(function (comment) {
if (comment.range[0] !== from) {
ws = contents.substr(from, comment.range[0] - from);
tokens.push({
type: "Whitespace",
value: ws,
range: [
from,
comment.range[0]
]
});
}
tokens.push({
type: "Comment",
value: comment,
range: comment.range,
});
from = comment.range[1];
});
if (from !== to) {
ws = contents.substr(from, to - from);
tokens.push({
type: "Whitespace",
value: ws,
range: [
from,
to
]
});
}
}
// Go thru all tokens esprima returns
syntax.tokens.forEach(function (token) {
// Some comments and whitespace skipped
if (token.range[0] !== currRange) {
addWhitespace(currRange, token.range[0]);
}
tokens.push(token);
currRange = token.range[1];
});
// trailing whitespace
if (contents.length !== currRange) {
addWhitespace(currRange, contents.length);
}
return tokens;
}
module.exports = lex;
"use strict";
```
#### lib/literate.js
### Library
This is the heart of this task. Check the file, if you're interested
You can also use *grunt-literate* as a normal library:
*NOTE* this is demo of `include` directive
```
var documentation = require("grunt-literate")("hello.js", { code: true });
```

@@ -85,2 +173,3 @@

var assert = require("assert");
var glob = require("glob");

@@ -102,14 +191,35 @@ var whitespaceEndRe = /^\s*$/;

function stripShebang(contents) {
var m = contents.match(/^#!\/[^\n]*\n/);
return m ? contents.substr(m[0].length) : contents;
}
function fileDirective(filename, value, regexp, callback) {
var m = value.match(regexp);
if (m) {
var directivePattern = m[1];
var globPattern = path.join(path.dirname(filename), directivePattern);
var files = glob.sync(globPattern);
if (files.length === 0) {
throw new Error(directivePattern + " doesn't match any files");
}
files.forEach(callback);
return true;
} else {
return false;
}
}
function getTokens(filename) {
var contents = fs.readFileSync(filename).toString();
contents = stripShebang(contents);
var tokens = lex(contents);
var m, value, includename;
var resTokens = [];
tokens.forEach(function (token) {
var r;
if (token.type === "Comment" && token.value.type === "Line" && token.value.value[0] === "/") {
value = token.value.value.substr(1);
m = value.match(/^\s*plain\s+(.*?)\s*$/);
if (m) {
includename = path.join(path.dirname(filename), m[1]);
var value = token.value.value.substr(1);
r = fileDirective(filename, value, /^\s*plain\s+(.*?)\s*$/, function (includename) {
resTokens.push({

@@ -119,11 +229,9 @@ type: "Plain",

});
return;
}
});
if (r) { return; }
m = value.match(/^\s*include\s+(.*?)\s*$/);
if (m) {
includename = path.join(path.dirname(filename), m[1]);
r = fileDirective(filename, value, /^\s*include\s+(.*?)\s*$/, function (includename) {
resTokens = resTokens.concat(getTokens(includename));
return;
}
});
if (r) { return; }

@@ -226,3 +334,87 @@ assert(false, "unknown directive: " + value);

### ljs
If `grunt-literate` is installed globally,
you can use `ljs` command line tool to process your literate javascript files
```sh
$ ljs -c -o foo.md foo.js
$ ljs --help
```
```js
"use strict";
var optimist = require("optimist");
var fs = require("fs");
var literate = require("../lib/literate.js");
optimist.usage("ljs [options] file.js");
optimist.boolean("h").options("h", {
alias: "help",
describe: "Show brief help information",
});
optimist.boolean("v").options("v", {
alias: "version",
describe: "Display version information and exit.",
});
optimist.options("o", {
alias: "output",
describe: "Output file.",
});
optimist.boolean("c").options("c", {
alias: "code",
describe: "Include code in output file.",
default: true,
});
function cli(argv) {
var options = optimist.parse(argv);
if (options.help) {
console.log(optimist.help());
return 0;
}
if (options.version) {
var pkg = JSON.parse(fs.readFileSync(__dirname + "/../package.json"));
console.log("jsgrep, part of jsstana version " + pkg.version);
return 0;
}
if (options._.length !== 1) {
console.error("Error: input file is required");
console.log(optimist.help());
return 0;
}
// Literate
var filename = options._[0];
var litContents;
try {
litContents = literate(filename, { code: options.code });
} catch (e) {
console.error("Error: while literating -- " + e.message);
return 1;
}
// Output
if (options.o) {
fs.writeFileSync(options.o, litContents);
} else {
console.log(litContents);
}
}
var ret = cli(process.argv.slice(2));
process.exit(ret);
```
## Contributing

@@ -237,2 +429,6 @@

- 0.1.4 Usage as executable and library
- run `ljs`
- or `require("grunt-literate")(filename)`
- also you may use glob patterns in directives
- 0.1.3 Directives

@@ -244,3 +440,2 @@ - 0.1.2 Newline improvements

- 0.1.0 Initial release
## Related work

@@ -252,2 +447,35 @@

## LICENSE
Copyright Oleg Grenrus 2013
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of Oleg Grenrus nor the names of other
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
```js

@@ -254,0 +482,0 @@ "use strict";

@@ -17,2 +17,4 @@ // vim: set sts=2 sw=2 ts=2 et:

[![Code Climate](https://codeclimate.com/github/phadej/grunt-literate.png)](https://codeclimate.com/github/phadej/grunt-literate)
[![NPM version](https://badge.fury.io/js/grunt-literate.png)](http://badge.fury.io/js/grunt-literate)
[![Dependency Status](https://gemnasium.com/phadej/grunt-literate.png)](https://gemnasium.com/phadej/grunt-literate)

@@ -66,4 +68,8 @@ ## Getting Started

*/
/// include ../lib/literate.js
/// include ../lib/*
/// include ../bin/ljs.js
/// plain ../CONTRIBUTING.md
/// plain ../CHANGELOG.md
/// plain ../footer.md
/// plain ../LICENSE

@@ -70,0 +76,0 @@ "use strict";

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc