Class: Octopress::CodeHighlighter::OptionsParser

Inherits:
Object
  • Object
show all
Defined in:
lib/octopress-code-highlighter/options_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(markup) ⇒ OptionsParser

Returns a new instance of OptionsParser.



6
7
8
# File 'lib/octopress-code-highlighter/options_parser.rb', line 6

def initialize(markup)
  @input = markup.strip
end

Instance Attribute Details

#inputObject

Returns the value of attribute input.



4
5
6
# File 'lib/octopress-code-highlighter/options_parser.rb', line 4

def input
  @input
end

Instance Method Details

#boolize(str) ⇒ Object



120
121
122
123
124
125
# File 'lib/octopress-code-highlighter/options_parser.rb', line 120

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

#clean_markupObject



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/octopress-code-highlighter/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,'')
end

#endlineObject



93
94
95
96
97
98
99
100
101
# File 'lib/octopress-code-highlighter/options_parser.rb', line 93

def endline
  if range
    range.last
  else
    num = extract(/\s*end:\s*(\d+)/i)
    num = num.to_i unless num.nil?
    num
  end
end

#escapeObject



55
56
57
# File 'lib/octopress-code-highlighter/options_parser.rb', line 55

def escape
  boolize(extract(/\s*escape:\s*(\w+)/i))
end

#extract(regexp, indices_to_try = [1], default = nil) ⇒ Object



109
110
111
112
113
114
115
116
117
118
# File 'lib/octopress-code-highlighter/options_parser.rb', line 109

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

#langObject



39
40
41
# File 'lib/octopress-code-highlighter/options_parser.rb', line 39

def lang
  extract(/\s*lang:\s*(\S+)/i)
end

#linenosObject



51
52
53
# File 'lib/octopress-code-highlighter/options_parser.rb', line 51

def linenos
  boolize(extract(/\s*linenos:\s*(\w+)/i))
end


79
80
81
# File 'lib/octopress-code-highlighter/options_parser.rb', line 79

def link_text
  extract(/\s*link[-_]text:\s*(("(.+?)")|('(.+?)')|(\S+))/i, [3, 5, 6], 'link')
end

#marksObject

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


68
69
70
71
72
73
74
75
76
77
# File 'lib/octopress-code-highlighter/options_parser.rb', line 68

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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/octopress-code-highlighter/options_parser.rb', line 23

def parse_markup(defaults = {})
  options = {
    lang:      lang,
    url:       url,
    title:     title,
    linenos:   linenos,
    marks:     marks,
    link_text: link_text,
    start:     start,
    end:       endline,
    escape:    escape
  }
  options = options.delete_if { |k,v| v.nil? }
  defaults.merge(options)
end

#rangeObject



103
104
105
106
107
# File 'lib/octopress-code-highlighter/options_parser.rb', line 103

def range
  if input.match(/ *range:(\d+)-(\d+)/i)
    [$1.to_i, $2.to_i]
  end
end

#startObject



83
84
85
86
87
88
89
90
91
# File 'lib/octopress-code-highlighter/options_parser.rb', line 83

def start
  if range
    range.first
  else
    num = extract(/\s*start:\s*(\d+)/i)
    num = num.to_i unless num.nil?
    num
  end
end

#titleObject



47
48
49
# File 'lib/octopress-code-highlighter/options_parser.rb', line 47

def title
  extract(/\s*title:\s*(("(.+?)")|('(.+?)')|(\S+))/i, [3, 5, 6])
end

#urlObject



43
44
45
# File 'lib/octopress-code-highlighter/options_parser.rb', line 43

def url
  extract(/\s*url:\s*(("(.+?)")|('(.+?)')|(\S+))/i, [3, 5, 6])
end