Class: Querly::PP::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/querly/pp/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv, stdin: STDIN, stdout: STDOUT, stderr: STDERR) ⇒ CLI

Returns a new instance of CLI.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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

Instance Attribute Details

#argvObject (readonly)

Returns the value of attribute argv.



6
7
8
# File 'lib/querly/pp/cli.rb', line 6

def argv
  @argv
end

#commandObject (readonly)

Returns the value of attribute command.



7
8
9
# File 'lib/querly/pp/cli.rb', line 7

def command
  @command
end

#load_pathsObject (readonly)

Returns the value of attribute load_paths.



8
9
10
# File 'lib/querly/pp/cli.rb', line 8

def load_paths
  @load_paths
end

#requiresObject (readonly)

Returns the value of attribute requires.



9
10
11
# File 'lib/querly/pp/cli.rb', line 9

def requires
  @requires
end

#stderrObject (readonly)

Returns the value of attribute stderr.



12
13
14
# File 'lib/querly/pp/cli.rb', line 12

def stderr
  @stderr
end

#stdinObject (readonly)

Returns the value of attribute stdin.



11
12
13
# File 'lib/querly/pp/cli.rb', line 11

def stdin
  @stdin
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



13
14
15
# File 'lib/querly/pp/cli.rb', line 13

def stdout
  @stdout
end

Instance Method Details

#load_libsObject



33
34
35
36
37
38
39
40
41
42
# 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

#runObject



44
45
46
47
48
49
50
51
52
53
54
# 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_erbObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 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|
    _, _, code_node, = *erb_node
    new_source[code_node.loc.range] = code_node.loc.source
    new_source[code_node.loc.range.end] = ';'
  end
  stdout.puts new_source
end

#run_hamlObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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