Class: Jekyll::GlossaryTooltip::OptionsParser

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-glossary_tooltip/options_parser.rb

Overview

Constant Summary collapse

ARGS_PATTERN =
%r{\s*(\w[-_\w]*):\s*(\w[^,\n\r]*)}
ARGS_ALLOWED =
%w[
  display
].freeze

Class Method Summary collapse

Class Method Details

.parse(raw_options) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/jekyll-glossary_tooltip/options_parser.rb', line 16

def parse(raw_options)
  options = {
    term_query: nil,
    display: nil
  }
  opt_segments = raw_options.strip.split(",")
  raise Errors::OptionsNoTermNameInTag unless opt_segments.length.positive?

  options[:term_query] = opt_segments[0]
  opt_segments.shift
  parse_segments(options, opt_segments)
  options
end

.parse_segments(options, opt_segments) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/jekyll-glossary_tooltip/options_parser.rb', line 30

def parse_segments(options, opt_segments)
  opt_segments.each do |opt_segment|
    raise Errors::OptionsBadTagArgumentFormat, options[:term_name] unless opt_segment =~ ARGS_PATTERN

    arg_name = Regexp.last_match(1)
    arg_value = Regexp.last_match(2)
    raise Errors::OptionsUnknownTagArgument, arg_name unless ARGS_ALLOWED.include?(arg_name)

    options[arg_name.to_sym] = arg_value
  end
end