diff --git a/Help.md b/Help.md index e5dccf9..b4508a9 100644 --- a/Help.md +++ b/Help.md @@ -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. @@ -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 " +"R " +"1 " +"* " +"1R " +"*R " +``` + +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 `" "`. diff --git a/lib/cli.js b/lib/cli.js index 5718bd3..a703c6e 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -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" ] }, @@ -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) { @@ -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)); } } } diff --git a/lib/msee.js b/lib/msee.js index 76c051c..edb057e 100644 --- a/lib/msee.js +++ b/lib/msee.js @@ -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; @@ -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); } diff --git a/lib/options.js b/lib/options.js new file mode 100644 index 0000000..2011572 --- /dev/null +++ b/lib/options.js @@ -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() + }) +}) diff --git a/package.json b/package.json index 5e92f7c..0de0690 100644 --- a/package.json +++ b/package.json @@ -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",