Class: MTLite

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

Instance Method Summary collapse

Constructor Details

#initialize(raw_msg, debug: false) ⇒ MTLite

Returns a new instance of MTLite.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/mtlite.rb', line 10

def initialize(raw_msg, debug: false)

  @debug = debug
  
  # reveal the expanded URL from a shortened URL
  raw_msg.gsub!(/https?:\/\/[^ ]+/) do |url|      
    
    puts 'url: ' + url.inspect if @debug
    
    Embiggen::URI(url).expand.to_s
  end
  
  puts 'raw_msg: ' + raw_msg.inspect if @debug
  
  # make the smartlinks into Markdown links        
  @raw_msg = smartlink(raw_msg)
  
  puts '@raw_msg: '  + @raw_msg.inspect if @debug
  
end

Instance Method Details

#lengthObject



116
117
118
# File 'lib/mtlite.rb', line 116

def length
  @msg.gsub(/<\/?.[^>]*>/,'').length
end

#to_html(para: false, ignore_domainlabel: false) ⇒ Object



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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/mtlite.rb', line 31

def to_html(para: false, ignore_domainlabel: false)

  raw_msg = @raw_msg.clone    
  # if it looks like an MtLite list make it an MtLite list
  # e.g. "a todo list:\n* line 1\n* line 2" => 
  #                                       a todo list: [* line 1 * line 2]
  raw_msg.sub!(/^(?:[\*#][^\n]+\n?)+/,'[\0]') if raw_msg =~ /\n/
  raw_msg.gsub!(/\n/,' ')


  # add br tags to checklist items
  # todo list: [[x] line 1 [] line 2] => 
  #                         todo list: <br/>[x] line 1 <br/>[] line 2
  raw_msg.sub!(/\[\[.*\]/){|x| x[0..-2].gsub(/[^\n](\[[x\s]?\])/,'<br/>\1') }

  # convert square brackets to unicode check boxes
  # replaces a [] with a unicode checkbox, 
  #                         and [x] with a unicode checked checkbox
  raw_msg = raw_msg.gsub(/\[\s*\](?= )/,'&#9744;').gsub(/\[x\]/,'&#9745;')

  # convert fractions using their associated unicode character
  # i.e. 1/4, 1/2, 3/4 
  raw_msg = raw_msg.gsub('\b1/4\b','&#188;').gsub('\b1/2\b','&#189;')\
                  .gsub('\b3/4\b','&#190;')

  # add strikethru to completed items
  # e.g. -milk cow- becomes <del>milk cow</del>
  raw_msg.gsub!(/\s-[^-]+-?[^-]+-[\s\]]/) do |x|
    x.sub(/-([&\w]+.*\w+)-/,'<del>\1</del>')
  end
    
  unless ignore_domainlabel then
    
    # append a domain label after the URL
    raw_msg.gsub!(/(?:^\[|\s\[)[^\]]+\]\((https?:\/\/[^\s]+)/) do |x|
      s2 = x[/https?:\/\/([^\/]+)/,1].split(/\./)
      r = s2.length >= 3 ? s2[1..-1] :  s2
      "%s [%s]" % [x, r.join('.')]
    end
    
  end
  
  # generate html lists from mtlite 1-liner lists
  raw_msg.gsub!(/\[[\*#][^\]]+\]/) do |list|

    if not list[/\n/] then

      symbol = list[1]
      symbol.prepend '\\' if symbol == '*'
      tag = {'#' => 'ol', '\*' => 'ul'}[symbol]

      "<#{tag}>%s</#{tag}>" % list.strip[1..-2].split(/#{symbol}/)[1..-1].
        map{|x| "<li>%s</li>" % x.strip}.join
    else
      list
    end

  end

  msg = RDiscount.new(raw_msg).to_html
  msg.gsub!(/<\/?p[^>]*>/,'') unless para

  regex = %r([^ <]+)
  # generate anchor tags for URLs which don't have anchor tags
  msg.gsub!(/(?:^(https?:#{regex})|\s(https?:#{regex}))/,' <a href="\2">\2</a>')    
  
  msg.gsub!(/(?<=\>)https?:#{regex}/) do |x|
    
    # unescapeHTML will transform &amp; to &
    url = CGI.unescapeHTML x
    url2 = url.sub(/^https?:\/\//,'').sub(/^www\./,'')
    url3 = url2.length > 33 ? url2[0..33] + '...' : url2
    #" <a href='#{url}'>#{url3}</a>"
  end

  # add the target attribute to make all hyperlinks open in a new browser tab
  msg.gsub!(/<a /,'<a target="_blank" ')
  
  # undecorate h1 headings
  msg.gsub!(/<h1/,'<h1 style="font-size: 0.95em; font-weight: 600"')
      
  @msg = msg
  
end

#to_sObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/mtlite.rb', line 120

def to_s

  msg = @raw_msg.clone
  
  # remove markdown hyperlink markings
  msg.gsub!(/\[([^\]]+)\]\(([^\)]+)\)/, '\1 \2')

  # generate html lists from mtlite 1-liner lists
  msg.gsub!(/\[[\*#][^\]]+\]/) do |list|

    if not list[/\n/] then

      symbol = list[1]
      symbol.prepend '\\' if symbol == '*'

      list.strip[1..-2].split(/#{symbol}/)[1..-1]\
        .map{|x| "\n* %s" % x.strip}.join + "\n"
    else
      list
    end

  end

  @msg = msg.gsub('<br/>',"\n").gsub(/<\/?.[^>]*>/,'')
end