Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/cli options #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion Help.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
msee
===

`msee` is a command-line tool to read markdown file.
`msee` is a command-line tool to read markdown files.

And it's a library help your command-line software to output readable markdown content.

Expand Down Expand Up @@ -29,3 +29,26 @@ msee.parse(str, {
// parse markdown file
msee.parseFile('~/doc/readme.md');
```

## Options
${options}

### Format: Padding
Padding can be specified in the following format:

```
"L <left padding string>"
"R <right padding string>"
"1 <first line left padding string>"
"* <regular line left padding string>"
"1R <first line right padding string>"
"*R <regular line right padding string>"
```

With a `\n` to separate multiple statements. Example:

```
"1L - \nR $\n*L "
```

Here every line will have `$` as a right padding. The first line will start with `" - "` and follow-up lines will start with `" "`.
104 changes: 92 additions & 12 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,70 @@ var msee = require('./msee');
var nopt = require('nopt');
var path = require('path');
var tty = require('tty');
var options = require('./options');
var fs = require('fs');

function showHelp() {
var helpFile = path.resolve(__dirname, '../Help.md');
var helpText = msee.parseFile(helpFile);

console.log(helpText);
function optionsHelp() {
return '| option | info | default |\n' +
'|--------|------|---------|\n' +
Object.keys(options).map(function (option) {
var optionInfo = options[option]
var def = optionInfo.default
if (typeof def === 'object') {
def = Object.keys(def).map(function (defName) {
var name;
var value = def[defName];
if (defName === 'first') {
if (typeof value === 'object') {
var result = '';
if (value.left) {
result += '1L ' + value.left;
}
if (value.regular) {
if (value.left) {
result += '\\n';
}
result += '1R ' + value.regular;
}
return result
}
name = '1 ' + value;
} else if (defName === 'left') {
name = 'L ' + value;
} else if (defName === 'regular') {
name = 'R ' + value;
} else {
name = '?';
}
return name;
}).join('\\n');
}
if (typeof def === 'string') {
def = '"' + def.split('\n').join('\\n') + '"'
}
return '| **--' + optionInfo.cli + '** | ' + optionInfo.description + (optionInfo.padding ? '_(format: padding)_' : '') + ' | `' + def + '` |';
}).join('\n');
}

function applyPadding(target, rightLeft, value) {
if (rightLeft === 'R') {
target.right = value;
} else if (rightLeft === 'L') {
target.left = value;
} else {
return value;
}
return target
}

exports.main = function() {
var opts = nopt(
{
"help": Boolean
},
var keys = {
"help": Boolean
};
Object.keys(options).forEach(function (option) {
keys[option] = typeof option.default === 'string' ? String : Number;
});
var opts = nopt(keys,
{
"h": [ "--help" ]
},
Expand All @@ -26,11 +77,37 @@ exports.main = function() {
showHelp();
}
else {
var runtimeOptions = Object.keys(options).reduce(function (runtimeOptions, option) {
var optionInfo = options[option]
var value = opts[optionInfo.cli]
if (value) {
if (optionInfo.padding) {
var lines = String(value).split(/\n|\\n/ig)
lines.forEach(function (line) {
var part = /^(((1|\*)(R|L)?)|(R|F))\s+(.*)/.exec(line)
if (part) {
if (typeof value === 'string') {
value = {}
}
if (part[3] === '1') {
value.first = applyPadding(value.first || {}, part[4], part[6])
} else if (part[3] === '*') {
value.regular = applyPadding(value.regular || {}, part[4], part[6])
} else {
value = applyPadding(value, part[5], part[6])
}
}
})
}
runtimeOptions[option] = value
}
return runtimeOptions
}, {})
var files = opts.argv.remain;
if (files.length > 0) {
try {
var file = path.resolve(process.cwd(), files[0]);
var text = msee.parseFile(file);
var text = msee.parseFile(file, runtimeOptions);
process.stdout.write(text);
}
catch (e) {
Expand All @@ -43,12 +120,15 @@ exports.main = function() {
text += chunk;
});
process.stdin.on('end', function() {
var out = msee.parse(text);
var out = msee.parse(text, runtimeOptions);
process.stdout.write(out);
});
}
else {
showHelp();
var helpFile = path.resolve(__dirname, '../Help.md');
var text = fs.readFileSync(helpFile).toString();
text = text.replace('${options}', optionsHelp());
process.stdout.write(msee.parse(text, runtimeOptions));
}
}
}
49 changes: 8 additions & 41 deletions lib/msee.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,18 @@ var chalk = require('chalk');
var wcstring = require('wcstring');
var os = require('os');

var cliOptions = require('./options');
var defaultOptions = {
collapseNewlines: true,
space: '',
hrStart: '',
hrChar: '-',
hrEnd: '',
headingStart: '\n',
headingEnd: '\n\n',
headingIndentChar: '#',
headingIndent: function (token) {
return Array(token.depth + 1).join(this.headingIndentChar)
},
codeStart: '\n',
codeEnd: '\n\n',
codePad: ' ',
codeTheme: os.platform() === 'win32' ? require('./syntaxColor_win') : require('./syntaxColor'),
blockquoteStart: '\n',
blockquoteEnd: '\n\n',
blockquoteColor: 'blockquote',
blockquotePad: ' > ',
blockquotePadColor: 'syntax',
listStart: '\n',
listEnd: '\n',
listItemStart: '',
listItemEnd: '\n',
listItemColor: 'ul',
listItemPad: {first: ' * ', regular: ' '},
listItemPadColor: 'syntax',
paragraphStart: '',
paragraphEnd: '\n\n',
width: process.stdout.columns || 80,
maxWidth: -1,
tableStart: '\n',
tableSeparator: ' ',
tableEnd: '\n\n'
maxWidth: -1
};
Object.keys(cliOptions).forEach(function (option) {
defaultOptions[option] = cliOptions[option].default;
});


var tokens;
var inline;
Expand Down Expand Up @@ -350,15 +326,6 @@ exports.parse = function(text, options) {

exports.parseFile = function(file, options) {
var filePath = path.resolve(__dirname, file);
var ret = '';

try {
var text = fs.readFileSync(filePath).toString();
ret = exports.parse(text, options);
}
catch (e) {
throw e;
}

return ret;
var text = fs.readFileSync(filePath).toString();
return exports.parse(text, options);
}
127 changes: 127 additions & 0 deletions lib/options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
module.exports = {
collapseNewlines: {
description: 'Reduce new lines to never have more than one empty line.',
default: true
},
space: {
description: 'Replace space characters.',
default: ''
},
hrStart: {
description: 'Start of an horizontal line.',
default: ''
},
hrChar: {
description: 'Character to be repeated for the horizontal line.',
default: '-'
},
hrEnd: {
description: 'Character at the end of the horizontal line.',
default: ''
},
headingStart: {
description: 'Character at the start of a header.',
default: '\n'
},
headingEnd: {
description: 'Character at the end of a header.',
default: '\n\n'
},
headingIndentChar: {
description: 'Character repeated for the depth of the indentation.',
default: '#'
},
codeStart: {
description: 'Start of a code block.',
default: '\n'
},
codeEnd: {
description: 'End of a code block.',
default: '\n\n'
},
codePad: {
padding: true,
description: 'Padding for code blocks.',
default: ' '
},
blockquoteStart: {
description: 'Start of a quote blocks.',
default: '\n'
},
blockquoteEnd: {
description: 'End of a quote block.',
default: '\n\n',
},
blockquoteColor: {
description: 'Color of a blockQuote',
default: 'blockquote'
},
blockquotePad: {
padding: true,
description: 'Padding for blockquote blocks.',
default: ' > '
},
blockquotePadColor: {
description: 'Color for the padding of a block.',
default: 'syntax'
},
listStart: {
description: 'Start of list block.',
default: '\n'
},
listEnd: {
description: 'End of list block.',
default: '\n'
},
listItemStart: {
description: 'Start of list-item block.',
default: ''
},
listItemEnd: {
description: 'End of list-item block.',
default: '\n'
},
listItemColor: {
description: 'Color of list items',
default: 'ul'
},
listItemPad: {
padding: true,
description: 'Padding for list items. ',
default: {first: ' * ', regular: ' '}
},
listItemPadColor: {
description: 'Color of list-item padding.',
default: 'syntax'
},
paragraphStart: {
description: 'Start of paragraph.',
default: ''
},
paragraphEnd: {
description: 'End of paragraph',
default: '\n\n'
},
width: {
description: 'Width of the output block. Falls back to terminal width if possible.',
default: process.stdout.columns || 80,
},
tableStart: {
description: 'Start of a table block.',
default: '\n',
},
tableSeparator: {
description: 'Separator between columns.',
default: ' ',
},
tableEnd: {
description: 'End of a table block.',
default: '\n\n'
}
}

Object.keys(module.exports).forEach(function (option) {
module.exports[option].cli = option.replace(/[A-Z]/g, function (found) {
return '-' + found.toLowerCase()
})
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "msee",
"version": "0.3.1",
"version": "0.3.2",
"description": "Msee is a command-line tool to read Markdown file in your terminal, and it's a library help your command-line software to output readable markdown content.",
"main": "./lib/msee.js",
"bin": "./bin/msee",
Expand Down