1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00
haml--haml/lib/hamlit/cli.rb
2015-10-24 20:32:26 +09:00

62 lines
1.3 KiB
Ruby

require 'hamlit'
require 'thor'
module Hamlit
class CLI < Thor
class_option :style, type: :string, aliases: ['-t'], default: 'pretty'
desc 'render HAML', 'Render haml template'
def render(file)
code = generate_code(file)
puts eval(code)
end
desc 'compile HAML', 'Show compile result'
def compile(file)
print_code generate_code(file)
end
desc 'parse HAML', 'Show parse result'
def parse(file)
pp generate_ast(file)
end
private
def generate_code(file)
template = File.read(file)
Hamlit::Engine.new(engine_options).call(template)
end
def generate_ast(file)
template = File.read(file)
Hamlit::Parser.new.call(template)
end
def engine_options
{ pretty: options['style'] == 'pretty' }
end
# Flexible default_task, compatible with haml's CLI
def method_missing(*args)
return super(*args) if args.length > 1
render(args.first.to_s)
end
def print_code(code)
require 'pry'
puts Pry.Code(code).highlighted
rescue LoadError
puts code
end
# Enable colored pretty printing only for development environment.
def pp(arg)
require 'pry'
Pry::ColorPrinter.pp(arg)
rescue LoadError
require 'pp'
super(arg)
end
end
end