Module: Formatter
- Defined in:
- lib/livetext/formatter.rb
Constant Summary collapse
- Start =
/(?<start>(^| ))/- Stop =
/(?<stop> |$)/
Class Method Summary collapse
- .bracket(str, char, tag) ⇒ Object
- .double(str, char, tag) ⇒ Object
- .format(str) ⇒ Object
- .handle(str, char, tag) ⇒ Object
- .iterate(str, rx, tag) ⇒ Object
- .make_regex(sigil, cdata, stop = Stop) ⇒ Object
- .make_string(str, rx, tag) ⇒ Object
- .single(str, char, tag) ⇒ Object
Class Method Details
.bracket(str, char, tag) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/livetext/formatter.rb', line 35 def self.bracket(str, char, tag) cdata = /(?<cdata>[^\]]*)\]/ stop = /(?<stop> |$)/ sigil = Regexp.escape(char + "[") rx = make_regex(sigil, cdata, stop) str = iterate(str, rx, tag) str end |
.double(str, char, tag) ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/livetext/formatter.rb', line 18 def self.double(str, char, tag) cdata = /(?<cdata>[^ \.,]*?)/ stop = /(?<stop>[\.,]|$)/ sigil = Regexp.escape(char+char) rx = make_regex(sigil, cdata, stop) str = iterate(str, rx, tag) str end |
.format(str) ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/livetext/formatter.rb', line 59 def self.format(str) s2 = str.chomp s2 = handle(s2, "*", "b") s2 = handle(s2, "_", "i") s2 = handle(s2, "`", "tt") s2 = handle(s2, "~", "strike") s2 end |
.handle(str, char, tag) ⇒ Object
52 53 54 55 56 57 |
# File 'lib/livetext/formatter.rb', line 52 def self.handle(str, char, tag) s2 = double(str, char, tag) # in this order... s2 = single(s2, char, tag) s2 = bracket(s2, char, tag) s2 end |
.iterate(str, rx, tag) ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/livetext/formatter.rb', line 10 def self.iterate(str, rx, tag) loop do str, more = make_string(str, rx, tag) break unless more end str end |
.make_regex(sigil, cdata, stop = Stop) ⇒ Object
6 7 8 |
# File 'lib/livetext/formatter.rb', line 6 def self.make_regex(sigil, cdata, stop = Stop) rx = /#{Start}#{sigil}#{cdata}#{stop}/ end |
.make_string(str, rx, tag) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/livetext/formatter.rb', line 44 def self.make_string(str, rx, tag) md = rx.match(str) return [str, false] if md.nil? start, cdata, stop = md.values_at(:start, :cdata, :stop) str = str.sub(rx, start + "<#{tag}>" + cdata + "<\/#{tag}>" + stop) [str, true] end |
.single(str, char, tag) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/livetext/formatter.rb', line 27 def self.single(str, char, tag) cdata = /((?<cdata>[^$ \[\*][^ ]*))/ sigil = Regexp.escape(char) rx = make_regex(sigil, cdata) str = iterate(str, rx, tag) str end |