Skip to content

Commit

Permalink
Binary to run govspeak from CLI
Browse files Browse the repository at this point in the history
Introduces a `govspeak` bin which can take input in three ways:

1. As an argument: `govspeak "render me"`
2. From a file: `govspeak --file render-me.md`
3. From stdin: `echo "render-me" | govspeak`

All output to stdout

Govspeak options can be passed in as JSON as a string through
`--options` or as a file through `--options-file options.json`
  • Loading branch information
kevindew committed Sep 22, 2016
1 parent 0af8489 commit 489cf98
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
8 changes: 8 additions & 0 deletions bin/govspeak
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env ruby

lib = File.expand_path("../../lib", __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

require "govspeak/cli"

Govspeak::CLI.new.run
4 changes: 4 additions & 0 deletions govspeak.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ Gem::Specification.new do |s|
library for use in the UK Government Single Domain project}

s.files = Dir[
'bin/*',
'lib/**/*',
'README.md',
'CHANGELOG.md',
'Gemfile',
'Rakefile'
]
s.test_files = Dir['test/**/*']
s.bindir = "bin"
s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
s.require_paths = ["lib"]

s.add_dependency 'kramdown', '~> 1.10.0'
Expand All @@ -32,6 +35,7 @@ library for use in the UK Government Single Domain project}
s.add_dependency 'actionview', '~> 4.1'
s.add_dependency 'i18n', '~> 0.7'
s.add_dependency 'money', '~> 6.7'
s.add_dependency 'commander', '~> 4.4'

s.add_development_dependency 'rake', '~> 0.9.0'
s.add_development_dependency 'gem_publisher', '~> 1.1.1'
Expand Down
2 changes: 2 additions & 0 deletions lib/govspeak.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'kramdown'
require 'active_support/core_ext/hash'
require 'govspeak/header_extractor'
require 'govspeak/structured_header_extractor'
require 'govspeak/html_validator'
Expand Down Expand Up @@ -29,6 +30,7 @@ def self.to_html(source, options = {})
end

def initialize(source, options = {})
options.deep_symbolize_keys!
@source = source ? source.dup : ""
@images = options.delete(:images) || []
@attachments = Array(options.delete(:attachments))
Expand Down
51 changes: 51 additions & 0 deletions lib/govspeak/cli.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require 'govspeak/version'
require 'govspeak'
require 'commander'

module Govspeak
class CLI
include Commander::Methods

def run
program(:name, 'Govspeak')
program(:version, Govspeak::VERSION)
program(:description, "A tool for rendering the GOV.UK dialect of markdown into HTML")
default_command(:render)
command(:render) do |command|
command.syntax = "govspeak render [options] <input>"
command.description = "Render Govspeak into HTML, can be sourced from stdin, as an argument or from a file"
command.option("--file FILENAME", String, "File to render")
command.option("--options JSON", String, "JSON to use as options")
command.option("--options-file FILENAME", String, "A file of JSON options")
command.action do |args, options|
input = get_input($stdin, args, options)
raise "Nothing to render. Use --help for assistance" unless input
puts Govspeak::Document.new(input, govspeak_options(options)).to_html
end
end
run!
end

private

def get_input(stdin, args, options)
return stdin.read unless stdin.tty?
return read_file(options.file) if options.file
args.empty? ? nil : args.join(" ")
end

def read_file(file_path)
path = Pathname.new(file_path).realpath
File.read(path)
end

def govspeak_options(command_options)
string = if command_options.options_file
read_file(command_options.options_file)
else
command_options.options
end
string ? JSON.load(string) : {}
end
end
end

0 comments on commit 489cf98

Please sign in to comment.