Class: Jekyll::KrokiTag::OptionParser
- Inherits:
-
Object
- Object
- Jekyll::KrokiTag::OptionParser
- Defined in:
- lib/jekyll/kroki_tag/option_parser.rb
Constant Summary collapse
- DEFINED_KWORDS =
%w[type format alt caption].map(&:to_sym).freeze
Instance Attribute Summary collapse
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#tree ⇒ Object
readonly
Returns the value of attribute tree.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(args) ⇒ OptionParser
constructor
A new instance of OptionParser.
- #node_attribute(ast_array, key: :type) ⇒ array
-
#node_type(ast_array) ⇒ Object
make sexp into type-type array of array.
-
#node_value(ast_array) ⇒ Object
make sexp into value-value array of array.
- #normalize(opts) ⇒ Hash
- #parse!(args) ⇒ Hash
- #valid_options?(opts) ⇒ true
Constructor Details
#initialize(args) ⇒ OptionParser
32 33 34 |
# File 'lib/jekyll/kroki_tag/option_parser.rb', line 32 def initialize(args) @opts = parse!(args) end |
Instance Attribute Details
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
35 36 37 |
# File 'lib/jekyll/kroki_tag/option_parser.rb', line 35 def opts @opts end |
#tree ⇒ Object (readonly)
Returns the value of attribute tree.
35 36 37 |
# File 'lib/jekyll/kroki_tag/option_parser.rb', line 35 def tree @tree end |
Class Method Details
.parse(args) ⇒ Hash
23 24 25 26 |
# File 'lib/jekyll/kroki_tag/option_parser.rb', line 23 def parse(args) parser = new(args) parser.opts end |
Instance Method Details
#node_attribute(ast_array, key: :type) ⇒ array
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/jekyll/kroki_tag/option_parser.rb', line 106 def node_attribute(ast_array, key: :type) type, *vals = ast_array case type when :hash vals.map { |e| node_attribute(e, key: key) } when :pair vals.map { |e| node_attribute(e, key: key) } when :sym, :str, :int, :float if key == :type type else vals.first end else raise ArgumentError.new("#{vals} is not in type Symbol, String, Integer and Float. Did you write invalid syntax ?") end end |
#node_type(ast_array) ⇒ Object
make sexp into type-type array of array
- [:sym, :str], [:sym, :sym], …
88 89 90 |
# File 'lib/jekyll/kroki_tag/option_parser.rb', line 88 def node_type(ast_array) node_attribute(ast_array, key: :type) end |
#node_value(ast_array) ⇒ Object
make sexp into value-value array of array
- [:type, “plantuml”], [:format, “png”], …
97 98 99 |
# File 'lib/jekyll/kroki_tag/option_parser.rb', line 97 def node_value(ast_array) node_attribute(ast_array, key: :value) end |
#normalize(opts) ⇒ Hash
58 59 60 61 62 63 64 65 |
# File 'lib/jekyll/kroki_tag/option_parser.rb', line 58 def normalize(opts) opts[:format] ||= "svg" [:type, :format].each { |k| opts[k].downcase! } opts end |
#parse!(args) ⇒ Hash
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/jekyll/kroki_tag/option_parser.rb', line 41 def parse!(args) @tree = Parser::CurrentRuby.parse("{ #{args} }") opts = Hash[*node_value(@tree.to_sexp_array).flatten] if (opts) raise MissingRequiredArgument.new("required arg `type` is missing") unless opts[:type] normalize(opts) end rescue Parser::SyntaxError => e raise SyntaxError.new(e) end |
#valid_options?(opts) ⇒ true
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/jekyll/kroki_tag/option_parser.rb', line 71 def (opts) opts.all? { |key, val| config = key.to_s.downcase.sub(/:$/, "").to_sym # standard:disable Lint/RedundantStringCoercion if DEFINED_KWORDS.include?(config) true else raise ArgumentError.new("`#{config}` is not allowed keyword") end } end |