Class: Touch_Up

Inherits:
Object
  • Object
show all
Includes:
Twitter::Extractor
Defined in:
lib/touch_up.rb

Constant Summary collapse

NOTHING =
''.freeze
HREFS =
%r@(\*([^\*]+)\*\s+)?([^\.\s]+\.[^\.\s]+[^\s]+[^\.\s])@
MARK_DOWNS =
/(\&\#47\;|~~|\*)([^\1\<\>]+)(\1)/

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ Touch_Up

class self ===



16
17
18
# File 'lib/touch_up.rb', line 16

def initialize str
  @origin = str
end

Instance Method Details

#to_htmlObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/touch_up.rb', line 20

def to_html
  splits = Escape_Escape_Escape.html(@origin).gsub(HREFS) { |full, match|

    raw_text   = $2 ? Escape_Escape_Escape.decode_html($2) : nil
    raw_append = Escape_Escape_Escape.decode_html $3
    raw_link   = extract_urls(raw_append).first

    if !raw_link
      full
    else

      append   = Escape_Escape_Escape.html raw_append.sub(raw_link, NOTHING)
      text     = if raw_text
                   Escape_Escape_Escape.html(raw_text)
                 else
                   Escape_Escape_Escape.html(raw_link)
                 end

      raw_link = Escape_Escape_Escape.decode_html(raw_link)

      if !raw_link['://']
        raw_link = 'http://' + raw_link
      end

      begin
        link = Escape_Escape_Escape.href(raw_link)

        "<a href=\"#{link}\">#{text}</a>#{append}"

      rescue Escape_Escape_Escape::Invalid_HREF
        full
      end # begin

    end # if


  }.split(/(\<[^\>]+\>[^\<]+\<\/[^\>]+\>)/)

  # "i", "del", "strong" tags
  final = ""
  i = 0
  while i < splits.size
    final << (splits[i].gsub(MARK_DOWNS) { |full, match|
      case
      when $1 == $3 && $1 == '&#47;'
        "<i>#{$2}</i>"
      when $1 == '~~' && $3 == '~~'
        "<del>#{$2}</del>"
      when $1 == $3 && $1 == '*'
        "<strong>#{$2}</strong>"
      else
        full
      end
    })

    i += 1
    if splits[i]
      final << splits[i]
      i += 1
    end
  end

  final

end