Class: MTLite
- Inherits:
-
Object
- Object
- MTLite
- Defined in:
- lib/mtlite.rb
Instance Method Summary collapse
-
#initialize(raw_msg) ⇒ MTLite
constructor
A new instance of MTLite.
- #length ⇒ Object
- #to_html(para: false, ignore_domainlabel: false) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(raw_msg) ⇒ MTLite
Returns a new instance of MTLite.
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/mtlite.rb', line 10 def initialize(raw_msg) # reveal the expanded URL from a shortened URL raw_msg.gsub!(/https?:\/\/[\w\-_\.\/?+&]+/) do |url| Embiggen::URI(url)..to_s end # make the smartlinks into Markdown links @raw_msg = smartlink(raw_msg) end |
Instance Method Details
#length ⇒ Object
105 106 107 |
# File 'lib/mtlite.rb', line 105 def length @msg.gsub(/<\/?.[^>]*>/,'').length end |
#to_html(para: false, ignore_domainlabel: false) ⇒ Object
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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/mtlite.rb', line 22 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*\](?= )/,'☐').gsub(/\[x\]/,'☑') # convert fractions using their associated unicode character # i.e. 1/4, 1/2, 3/4 raw_msg = raw_msg.gsub('\b1/4\b','¼').gsub('\b1/2\b','½')\ .gsub('\b3/4\b','¾') # 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([\w\-/?=.#\(\)]+) # 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})|\s(https?:#{regex}))/) do |x| url = $2 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_s ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/mtlite.rb', line 109 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 |