class Querly::PP::CLI

Attributes

argv[R]
command[R]
load_paths[R]
requires[R]
stderr[R]
stdin[R]
stdout[R]

Public Class Methods

new(argv, stdin: STDIN, stdout: STDOUT, stderr: STDERR) click to toggle source
# File lib/querly/pp/cli.rb, line 15
def initialize(argv, stdin: STDIN, stdout: STDOUT, stderr: STDERR)
  @argv = argv
  @stdin = stdin
  @stdout = stdout
  @stderr = stderr

  @load_paths = []
  @requires = []

  OptionParser.new do |opts|
    opts.banner = "Usage: #{opts.program_name} pp-name [options]"
    opts.on("-I dir") {|path| load_paths << path }
    opts.on("-r lib") {|rq| requires << rq }
  end.permute!(argv)

  @command = argv.shift&.to_sym
end

Public Instance Methods

load_libs() click to toggle source
# File lib/querly/pp/cli.rb, line 33
def load_libs
  load_paths.each do |path|
    $LOAD_PATH << path
  end

  requires.each do |lib|
    require lib
  end

end
run() click to toggle source
# File lib/querly/pp/cli.rb, line 44
def run
  available_commands = [:haml, :erb]

  if available_commands.include?(command)
    send :"run_#{command}"
  else
    stderr.puts "Unknown command: #{command}"
    stderr.puts "  available commands: #{available_commands.join(", ")}"
    exit 1
  end
end
run_erb() click to toggle source
# File lib/querly/pp/cli.rb, line 74
def run_erb
  require 'better_html'
  require 'better_html/parser'
  load_libs
  source = stdin.read
  source_buffer = Parser::Source::Buffer.new('(erb)')
  source_buffer.source = source
  parser = BetterHtml::Parser.new(source_buffer, template_language: :html)

  new_source = source.gsub(/./, ' ')
  parser.ast.descendants(:erb).each do |erb_node|
    indicator_node, _, code_node, = *erb_node
    next if indicator_node&.loc&.source == '#'
    new_source[code_node.loc.range] = code_node.loc.source
    new_source[code_node.loc.range.end] = ';'
  end
  stdout.puts new_source
end
run_haml() click to toggle source
# File lib/querly/pp/cli.rb, line 56
def run_haml
  require "haml"
  load_libs
  source = stdin.read

  if Haml::VERSION >= '5.0.0'
    stdout.print Haml::Engine.new(source).precompiled
  else
    options = Haml::Options.new
    parser = Haml::Parser.new(source, options)
    parser.parse
    compiler = Haml::Compiler.new(options)
    compiler.compile(parser.root)

    stdout.print compiler.precompiled
  end
end