Class: Middleman::Vegas::OptionsParser
- Inherits:
-
Object
- Object
- Middleman::Vegas::OptionsParser
- Defined in:
- lib/middleman-vegas/options_parser.rb
Instance Attribute Summary collapse
-
#input ⇒ Object
Returns the value of attribute input.
Instance Method Summary collapse
- #boolize(str) ⇒ Object
- #classnames ⇒ Object
- #clean_markup ⇒ Object
- #endline ⇒ Object
- #escape ⇒ Object
- #extract(regexp, indices_to_try = [1], default = nil) ⇒ Object
-
#initialize(markup) ⇒ OptionsParser
constructor
A new instance of OptionsParser.
- #lang ⇒ Object
- #linenos ⇒ Object
- #link_text ⇒ Object
-
#marks ⇒ Object
Public: Matches pattern for line marks and returns array of line numbers to mark.
- #parse_markup(defaults = {}) ⇒ Object
- #range ⇒ Object
- #start ⇒ Object
- #startinline ⇒ Object
- #title ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(markup) ⇒ OptionsParser
Returns a new instance of OptionsParser.
6 7 8 |
# File 'lib/middleman-vegas/options_parser.rb', line 6 def initialize(markup) @input = markup.strip end |
Instance Attribute Details
#input ⇒ Object
Returns the value of attribute input.
4 5 6 |
# File 'lib/middleman-vegas/options_parser.rb', line 4 def input @input end |
Instance Method Details
#boolize(str) ⇒ Object
132 133 134 135 136 137 |
# File 'lib/middleman-vegas/options_parser.rb', line 132 def boolize(str) return nil if str.nil? return true if str == true || str =~ (/(true|t|yes|y|1)$/i) return false if str == false || str =~ (/(false|f|no|n|0)$/i) || str.strip.size > 1 return str end |
#classnames ⇒ Object
51 52 53 |
# File 'lib/middleman-vegas/options_parser.rb', line 51 def classnames extract(/\s*class:\s*(("(.+?)")|('(.+?)')|(\S+))/i, [3, 5, 6]) end |
#clean_markup ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/middleman-vegas/options_parser.rb', line 10 def clean_markup input.sub(/\s*lang:\s*\S+/i,'') .sub(/\s*title:\s*(("(.+?)")|('(.+?)')|(\S+))/i,'') .sub(/\s*url:\s*(\S+)/i,'') .sub(/\s*link_text:\s*(("(.+?)")|('(.+?)')|(\S+))/i,'') .sub(/\s*mark:\s*\d\S*/i,'') .sub(/\s*linenos:\s*\w+/i,'') .sub(/\s*start:\s*\d+/i,'') .sub(/\s*end:\s*\d+/i,'') .sub(/\s*range:\s*\d+-\d+/i,'') .sub(/\s*escape:\s*\w+/i,'') .sub(/\s*startinline:\s*\w+/i,'') .sub(/\s*class:\s*(("(.+?)")|('(.+?)')|(\S+))/i,'') end |
#endline ⇒ Object
105 106 107 108 109 110 111 112 113 |
# File 'lib/middleman-vegas/options_parser.rb', line 105 def endline if range range.last else num = extract(/\s*end:\s*(\d+)/i) num = num.to_i unless num.nil? num end end |
#escape ⇒ Object
67 68 69 |
# File 'lib/middleman-vegas/options_parser.rb', line 67 def escape boolize(extract(/\s*escape:\s*(\w+)/i)) end |
#extract(regexp, indices_to_try = [1], default = nil) ⇒ Object
121 122 123 124 125 126 127 128 129 130 |
# File 'lib/middleman-vegas/options_parser.rb', line 121 def extract(regexp, indices_to_try = [1], default = nil) thing = input.match(regexp) if thing.nil? default else indices_to_try.each do |index| return thing[index] if thing[index] end end end |
#lang ⇒ Object
43 44 45 |
# File 'lib/middleman-vegas/options_parser.rb', line 43 def lang extract(/\s*lang:\s*(\S+)/i) end |
#linenos ⇒ Object
63 64 65 |
# File 'lib/middleman-vegas/options_parser.rb', line 63 def linenos boolize(extract(/\s*linenos:\s*(\w+)/i)) end |
#link_text ⇒ Object
91 92 93 |
# File 'lib/middleman-vegas/options_parser.rb', line 91 def link_text extract(/\s*link[-_]text:\s*(("(.+?)")|('(.+?)')|(\S+))/i, [3, 5, 6], 'link') end |
#marks ⇒ Object
Public: Matches pattern for line marks and returns array of line
numbers to mark
Example input
Input: "mark:1,5-10,2"
Output: [1,2,5,6,7,8,9,10]
Returns an array of integers corresponding to the lines which are
indicated as marked
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/middleman-vegas/options_parser.rb', line 80 def marks marks = [] if input =~ / *mark:(\d\S*)/i marks = $1.gsub /(\d+)-(\d+)/ do ($1.to_i..$2.to_i).to_a.join(',') end marks = marks.split(',').collect {|s| s.to_i}.sort end marks end |
#parse_markup(defaults = {}) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/middleman-vegas/options_parser.rb', line 25 def parse_markup(defaults = {}) = { lang: lang, url: url, title: title, linenos: linenos, marks: marks, link_text: link_text, start: start, end: endline, escape: escape, startinline: startinline, class: classnames } = .delete_if { |k,v| v.nil? } defaults.merge() end |
#range ⇒ Object
115 116 117 118 119 |
# File 'lib/middleman-vegas/options_parser.rb', line 115 def range if input.match(/ *range:(\d+)-(\d+)/i) [$1.to_i, $2.to_i] end end |
#start ⇒ Object
95 96 97 98 99 100 101 102 103 |
# File 'lib/middleman-vegas/options_parser.rb', line 95 def start if range range.first else num = extract(/\s*start:\s*(\d+)/i) num = num.to_i unless num.nil? num end end |
#startinline ⇒ Object
47 48 49 |
# File 'lib/middleman-vegas/options_parser.rb', line 47 def startinline boolize(extract(/\s*startinline:\s*(\w+)/i)) end |
#title ⇒ Object
59 60 61 |
# File 'lib/middleman-vegas/options_parser.rb', line 59 def title extract(/\s*title:\s*(("(.+?)")|('(.+?)')|(\S+))/i, [3, 5, 6]) end |
#url ⇒ Object
55 56 57 |
# File 'lib/middleman-vegas/options_parser.rb', line 55 def url extract(/\s*url:\s*(("(.+?)")|('(.+?)')|(\S+))/i, [3, 5, 6]) end |