#!/usr/bin/env ruby # frozen_string_literal: true require 'optparse' require 'ostruct' $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib') require 'commonmarker' root = File.expand_path('..', __dir__) $LOAD_PATH.unshift File.expand_path('lib', root) def parse_options options = OpenStruct.new extensions = CommonMarker.extensions parse_options = CommonMarker::Config::Parse render_options = CommonMarker::Config::Render options.active_extensions = [] options.active_parse_options = [:DEFAULT] options.active_render_options = [:DEFAULT] option_parser = OptionParser.new do |opts| opts.banner = 'Usage: commonmarker [--html-renderer] [--extension=EXTENSION]' opts.separator ' [--parse-option=OPTION]' opts.separator ' [--render-option=OPTION]' opts.separator ' [FILE..]' opts.separator '' opts.separator 'Convert one or more CommonMark files to HTML and write to standard output.' opts.separator 'If no FILE argument is provided, text will be read from STDIN.' opts.separator '' opts.on('--extension=EXTENSION', Array, 'Use EXTENSION for parsing and HTML output (unless --html-renderer is specified)') do |values| values.each do |value| if extensions.include?(value) options.active_extensions << value.to_sym else abort("extension '#{value}' not found") end end end opts.on('-h', '--help', 'Prints this help') do puts opts puts puts "Available extentions: #{extensions.join(', ')}" puts "Available parse options: #{parse_options.keys.join(', ')}" puts "Available render options: #{render_options.keys.join(', ')}" puts puts 'See the README for more information on these.' exit end opts.on('--html-renderer', 'Use the HtmlRenderer renderer rather than the native C renderer') do options.renderer = true end opts.on('--parse-option=OPTION', Array, 'OPTION passed during parsing') do |values| values.each do |value| if parse_options.key?(value.to_sym) options.active_parse_options << value.to_sym else abort("parse-option '#{value}' not found") end end end opts.on('--render-option=OPTION', Array, 'OPTION passed during rendering') do |values| values.each do |value| if render_options.key?(value.to_sym) options.active_render_options << value.to_sym else abort("render-option '#{value}' not found") end end end opts.on('-v', '--version', 'Version information') do puts "commonmarker #{CommonMarker::VERSION}" exit end end option_parser.parse! options end options = parse_options doc = CommonMarker.render_doc(ARGF.read, options.active_parse_options, options.active_extensions) if options.renderer renderer = CommonMarker::HtmlRenderer.new(extensions: options.active_extensions) STDOUT.write(renderer.render(doc)) else STDOUT.write(doc.to_html(options.active_render_options, options.active_extensions)) end