Class: ClassMakerGyazz::Make

Inherits:
Object
  • Object
show all
Defined in:
lib/class_maker_gyazz.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ Make

Returns a new instance of Make.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/class_maker_gyazz.rb', line 9

def initialize text
  before_depth = current_depth = next_depth = 0
  rows = text.split("\n")
  html = rows.map.with_index do |m, i|
    prefix = "<li>"
    suffix = "</li>"
    current_depth = m.length - m.lstrip.length
    next_depth = rows[i + 1].length - rows[i + 1].lstrip.length unless rows.last == m
    suffix = "" if next_depth > current_depth # next row will start nest
    prefix = "<ul><li>" if before_depth < current_depth # nest start
    suffix = "#{suffix}#{'</li></ul>' * (current_depth - next_depth)}" if current_depth > next_depth # nest end
    before_depth = current_depth
    tag, end_tag = text_tag(m, current_depth, next_depth)
    "#{prefix}#{tag}#{check_text(m)}#{end_tag}#{suffix}"
  end
  @html = "<ul>#{html.join}</ul>"
end

Instance Attribute Details

#htmlObject

Returns the value of attribute html.



7
8
9
# File 'lib/class_maker_gyazz.rb', line 7

def html
  @html
end

Instance Method Details



51
52
53
54
55
56
# File 'lib/class_maker_gyazz.rb', line 51

def check_link text
  if text =~ /(http|https)\:\/\/\w.*/
    text = "#{text.gsub($&,"")}<a href='#{$&}'>#{$&}</a>"
  end
  text
end

#check_text(text) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/class_maker_gyazz.rb', line 39

def check_text text
  if is_img?(text)
    text = "<img class='img-thumbnail' src='#{clean_brackets(text)}'>"
  elsif text =~ /\[\[\[.*\]\]\]/
    text = "<strong>#{text}</strong>"
    text = check_link(clean_brackets(text))
  elsif text =~ /\[\[.*\]\]/
    text = check_link(clean_brackets(text))
  end
  text
end

#clean_brackets(text) ⇒ Object



62
63
64
# File 'lib/class_maker_gyazz.rb', line 62

def clean_brackets text
  text.gsub(/(\[\[\[|\]\]\])/, "").gsub(/(\[\[|\]\])/, "")
end

#is_img?(text) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/class_maker_gyazz.rb', line 58

def is_img? text
  text =~ /\[\[(http|https)\:\/\/\w.*(png|jpg|jpeg|gif)\]\]/
end

#text_tag(text, current_depth, next_depth) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/class_maker_gyazz.rb', line 27

def text_tag text, current_depth, next_depth
  if current_depth == 0
    tag = is_img?(text) ? "<h2>" : "<h2 class='page-header'>" 
    end_tag = "</h2>"
    return tag, end_tag
  elsif current_depth == 1
    tag = "<blockquote><p>"
    end_tag = "</p></blockquote>"
    return tag, end_tag
  end
end