├── .gitignore ├── README.adoc ├── LICENSE ├── package.json └── bin └── asciidoctorjs /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | -------------------------------------------------------------------------------- /README.adoc: -------------------------------------------------------------------------------- 1 | = Asciidoctor.js CLI 2 | 3 | The Asciidoctor.js command line interface. 4 | 5 | Install this globally and you'll have access to the `asciidoctorjs` command anywhere on your system. 6 | 7 | npm install -g asciidoctor-cli 8 | 9 | Type `asciidoctorjs --help` for more information. 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Asciidoctor 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "asciidoctor-cli", 3 | "version": "1.5.2-alpha.2", 4 | "description": "The Asciidoctor.js command line interface", 5 | "main": "asciidoctorjs.js", 6 | "files": [ 7 | "bin" 8 | ], 9 | "bin": { 10 | "asciidoctorjs": "bin/asciidoctorjs" 11 | }, 12 | "preferGlobal": true, 13 | "scripts": { 14 | "test": "echo \"Error: no test specified\" && exit 1" 15 | }, 16 | "repository": { 17 | "type": "git", 18 | "url": "https://github.com/asciidoctor/asciidoctor-cli.js.git" 19 | }, 20 | "keywords": [ 21 | "asciidoc", 22 | "asciidoctor", 23 | "asciidoctor-cli", 24 | "opal", 25 | "javascript", 26 | "library" 27 | ], 28 | "authors": [ 29 | "Dan Allen (https://github.com/mojavelinux)", 30 | "Guillaume Grossetie (https://github.com/mogztter)", 31 | "Anthonny Quérouil (https://github.com/anthonny)" 32 | ], 33 | "license": { 34 | "type": "MIT", 35 | "url": "https://github.com/asciidoctor/asciidoctor-cli.js/blob/master/LICENSE" 36 | }, 37 | "bugs": { 38 | "url": "https://github.com/asciidoctor/asciidoctor-cli.js/issues" 39 | }, 40 | "homepage": "https://github.com/asciidoctor/asciidoctor-cli.js", 41 | "dependencies": { 42 | "cli": "^0.6.5", 43 | "mkdirp": "^0.5.0", 44 | "asciidoctor.js": "1.5.2" 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /bin/asciidoctorjs: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | 'use strict'; 4 | 5 | process.title = 'asciidoctorjs'; 6 | 7 | var cli = require('cli').enable('status'); 8 | var fs = require('fs'); 9 | var path = require('path'); 10 | var mkdirp = require('mkdirp'); 11 | var asciidoctor = require('asciidoctor.js')(); 12 | var opal = asciidoctor.Opal; 13 | var processor = asciidoctor.Asciidoctor(true); 14 | 15 | var convert_options = function (cliOptions) { 16 | var backend = cliOptions['backend']; 17 | var doctype = cliOptions['doctype']; 18 | var safeMode = cliOptions['safe-mode']; 19 | var noHeaderFooter = cliOptions['no-header-footer'] 20 | var sectionNumbers = cliOptions['section-numbers']; 21 | var baseDir = cliOptions['base-dir']; 22 | var destinationDir = cliOptions['destination-dir']; 23 | var quiet = cliOptions['quiet']; 24 | var verbose = cliOptions['verbose']; 25 | var timings = cliOptions['timings']; 26 | var trace = cliOptions['trace']; 27 | var hardBreaks = cliOptions['hard-breaks']; 28 | cli.debug('backend ' + backend); 29 | cli.debug('doctype ' + doctype); 30 | cli.debug('header_footer ' + !noHeaderFooter); 31 | cli.debug('section-numbers ' + sectionNumbers); 32 | cli.debug('quiet ' + quiet); 33 | cli.debug('verbose ' + verbose); 34 | cli.debug('timings ' + timings); 35 | cli.debug('trace ' + trace); 36 | cli.debug('baseDir ' + baseDir); 37 | cli.debug('destinationDir ' + destinationDir); 38 | cli.debug('hardBreaks ' + hardBreaks); 39 | var verboseMode = quiet ? 0 : verbose ? 2 : 1; 40 | var attributes; 41 | if (noHeaderFooter) { 42 | attributes = 'showtitle'; 43 | } 44 | else { 45 | // Asciidoctor.js currently cannot read or resolve the default stylesheet 46 | attributes = 'linkcss'; 47 | } 48 | if (sectionNumbers === true) { 49 | attributes = attributes.concat(' ').concat('sectnums'); 50 | } 51 | if (hardBreaks === true) { 52 | attributes = attributes.concat(' ').concat('hardbreaks'); 53 | } 54 | cli.debug('verboseMode ' + verboseMode); 55 | cli.debug('attributes ' + attributes); 56 | var options = { 57 | backend: backend, 58 | doctype: doctype, 59 | safe: safeMode, 60 | header_footer: !noHeaderFooter, 61 | verbose: verboseMode, 62 | // FIXME 22/12/2014, exception is thrown at runtime 63 | //timings: timings, 64 | trace: trace 65 | }; 66 | if (baseDir != null) { 67 | options.base_dir = baseDir 68 | }/* 69 | if (destinationDir != null) { 70 | options.to_dir = destinationDir; 71 |  }*/ 72 | options.attributes = attributes; 73 | cli.debug('options ' + JSON.stringify(options)); 74 | return opal.hash(options); 75 | } 76 | 77 | var convert_file = function (file, options) { 78 | cli.debug('convert file ' + file + ' with options ' + options); 79 | var data = fs.readFileSync(file, 'utf8'); 80 | return processor.$convert(data, options); 81 | }; 82 | 83 | var output_result = function (data, file, cliOptions) { 84 | var backend = cliOptions['backend']; 85 | var outFile = cliOptions['out-file']; 86 | var destinationDir = cliOptions['destination-dir']; 87 | cli.debug('out-file ' + outFile); 88 | if (outFile == '') { 89 | console.log(data); 90 | } else { 91 | if (destinationDir != null) { 92 | var outDir = destinationDir; 93 | mkdirp.sync(outDir); 94 | } else { 95 | var outDir = path.dirname(file); 96 | } 97 | if (outFile == null) { 98 | var extname = path.extname(file); 99 | if (backend == 'docbook45' || backend == 'docbook5') { 100 | var outputExtname = '.xml'; 101 | } else { 102 | var outputExtname = '.html'; 103 | } 104 | var outputFile = outDir + path.sep + path.basename(file, extname) + outputExtname; 105 | } else { 106 | var outputFile = outDir + path.sep + outFile; 107 | } 108 | cli.debug('write result in ' + outputFile); 109 | fs.writeFileSync(outputFile, data); 110 | cli.ok(file + ' converted to ' + backend + ' in ' + outputFile); 111 | } 112 | } 113 | 114 | cli.parse({ 115 | 'backend': ['b', 'set output format backend (default: html5)', 'string', 'html5'], 116 | 'doctype': ['d', 'document type to use when converting document: [article, book, manpage, inline] (default: article)', 'string', 'article'], 117 | 'out-file': ['o', 'output file (default: based on path of input file); use \'\' to output to STDOUT', 'file'], 118 | 'safe-mode': ['S', 'set safe mode level explicitly: [unsafe, safe, server, secure] (default: unsafe)), disables potentially dangerous macros in source files, such as include::[]', 'string', 'unsafe'], 119 | 'no-header-footer': ['s', 'suppress output of header and footer (default: false)', 'boolean', false], 120 | 'section-numbers': ['n', 'auto-number section titles in the HTML backend; disabled by default', 'boolean', 'false'], 121 | 'base-dir': ['B', 'base directory containing the document and resources (default: directory of source file)', 'path'], 122 | 'destination-dir': ['D', 'destination output directory (default: directory of source file)', 'path'], 123 | 'quiet': ['q', 'suppress warnings (default: false)', 'boolean', 'false'], 124 | 'trace': [false, 'include backtrace information on errors (default: false)', 'boolean', 'false'], 125 | 'verbose': ['v', 'enable verbose mode (default: false)', 'boolean', 'false'], 126 | 'timings': ['t', 'enable timings mode (default: false)', 'boolean', 'false'], 127 | 'hard-breaks': ['h', 'inject the asciidoctor \'hardbreaks\' attribute, used for generating hard returns (default: false)', 'boolean', 'false'] 128 | }); 129 | 130 | cli.main(function (cliArgs, cliOptions) { 131 | var options = convert_options(cliOptions); 132 | cliArgs.forEach(function (file) { 133 | var data = convert_file(file, options); 134 | output_result(data, file, cliOptions); 135 | }); 136 | }); 137 | --------------------------------------------------------------------------------