This commit is contained in:
Billy Moon
2015-01-19 03:37:34 +00:00
commit 50526ac395
5 changed files with 120 additions and 0 deletions

3
bin/illiterate Executable file
View File

@@ -0,0 +1,3 @@
#!/usr/bin/env node
require('./illiterate.js');

11
bin/illiterate.js Normal file
View File

@@ -0,0 +1,11 @@
var out = [];
process.argv.slice(2).forEach(function(filename){
require('fs').readFileSync(filename).toString().split(/\r?\n/).forEach(function(line){
[' ', '\t'].forEach(function(delimiter){
if(line.indexOf(delimiter) === 0){
out.push(line.substring(delimiter.length));
}
});
});
});
console.log(out.join('\n'));

View File

@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>fileTypes</key>
<array>
<string>js.md</string>
</array>
<key>name</key>
<string>Literate Javascript</string>
<key>patterns</key>
<array>
<dict>
<key>match</key>
<string>^(?!([ ]{4}|\t)).*$</string>
<key>name</key>
<string>punctuation.definition.comment.litjs</string>
</dict>
<dict>
<key>begin</key>
<string>^([ ]{4}|\t)(?!$)</string>
<key>end</key>
<string>$</string>
<key>name</key>
<string>source.js.litjs</string>
<key>patterns</key>
<array>
<dict>
<key>include</key>
<string>source.js</string>
</dict>
</array>
</dict>
</dict>
<key>scopeName</key>
<string>source.js.litjs</string>
<key>uuid</key>
<string>df425cd1-34c4-49d2-ba09-f8445676ccd2</string>
</dict>
</plist>

5
package.json Normal file
View File

@@ -0,0 +1,5 @@
{
"name": "illiterate",
"version": "0.1.0",
"dependencies": {}
}

57
readme.js.md Normal file
View File

@@ -0,0 +1,57 @@
# Illiterate
This file is both the source and the documentation for the `illiterate` binary. Perhaps it will become the main executable one day too... who knows ,~)
## Usage
Extract code block from markdown: `illiterate file.lang.md`
This is useful to compile literate style code à la coffeescript, but for **any** language: `illiterate file.js.md > file.js`
After being processed by `illiterate`, this readme file contains only the indented javascript code blocks. The output can be seen in [bin/illiterate.js](./bin/illiterate.js).
## Build
There is no real build process to speak of. I tend to do something like...
> ./bin/illiterate readme.md > temp && rm bin/illiterate.js && mv temp bin/illiterate.js
(temp file is used to prevent issues with overwriting currently executing file)
## Source
This file is itself the source for a binary which is capable of compiling itself. Anything in this file which is indented is interpreted by markdown as code, and ends up in the compiled output.
### Initialize environment
Create a variable to store output as it is built up from input files
var out = [];
### Main loop
Loop through each input file (specified as command line arguments)
process.argv.slice(2).forEach(function(filename){
Read each file, and loop through each line
require('fs').readFileSync(filename).toString().split(/\r?\n/).forEach(function(line){
If the line starts with a tab or four spaces, strip it out and include the line in the output
[' ', '\t'].forEach(function(delimiter){
if(line.indexOf(delimiter) === 0){
out.push(line.substring(delimiter.length));
}
});
});
});
### Output
Finally, output all lines
console.log(out.join('\n'));