Module: MightyString::HTML
- Defined in:
- lib/mightystring/html.rb
Class Method Summary collapse
- .default_options ⇒ Object
- .html_math_exceptions(in_str = "") ⇒ Object
-
.strip_first_seq(mstr = "", mseq = "", cmpchar = ) ⇒ Object
strip sequence out ( master string, sequence to remove, any characters to swap inplace this for that ).
- .text(htmlstr, options = {}) ⇒ Object
Class Method Details
.default_options ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/mightystring/html.rb', line 61 def self. { :tag_markers => [["<",">"],["&",";"]], :mappings => { """=>"'","br"=>"\n","'" => "'", " " => " ", "™" => "(TM)", "©" => "(c)" }, :math_by_space => false # FIXME certain (long) A href tags slip through } end |
.html_math_exceptions(in_str = "") ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/mightystring/html.rb', line 36 def self.html_math_exceptions(in_str = "") if in_str["< "] or in_str["& "] return 1 # Execption found at beginning elsif in_str["&"] and in_str[";"] and (in_str[" "] or in_str.length > 7) # Shouldn't have spaces in html &code;s or be greater than 7 in length return 2 # Exception found for both else return 0 end end |
.strip_first_seq(mstr = "", mseq = "", cmpchar = ) ⇒ Object
strip sequence out ( master string, sequence to remove, any characters to swap inplace this for that )
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/mightystring/html.rb', line 48 def self.strip_first_seq( mstr = "", mseq = "", cmpchar = [:mappings] ) if not cmpchar.empty? and cmpchar.keys.any? {|mkey| mseq.downcase[mkey.downcase] } # keys exist and one of the keys match cmpchar.each_key { |mkey| if mseq.downcase[mkey.downcase] mstr = mstr[0,mstr.index(mseq)] + cmpchar[mkey] + mstr[(mstr.index(mseq)+mseq.length)..-1] end } elsif mstr.index(mseq) mstr = mstr[0,mstr.index(mseq)] + mstr[(mstr.index(mseq)+mseq.length)..-1] end return mstr end |
.text(htmlstr, options = {}) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/mightystring/html.rb', line 3 def self.text(htmlstr, = {}) [:mappings] = [:mappings]. merge(.delete(:mappings)) if .has_key?(:mappings) = .merge() [:tag_markers].each { |g| sh_endpoints = htmlstr.index_all(g[1]) if sh_endpoints.nil? break end sh_end = htmlstr.rindex(g[1]) sh_start = htmlstr.rindex(g[0]) while !!sh_end and !!sh_start do if sh_end > sh_start sh_seq = htmlstr[sh_start,sh_end - sh_start + 1] until sh_seq.count(g[1]) == 1 do # until we've selected only the inner block sh_end = htmlstr[0,sh_end-1].rindex(g[1]) sh_seq = htmlstr[sh_start,sh_end - sh_start + 1] end if not ([:math_by_space] and not html_math_exceptions(htmlstr[sh_start,sh_end - sh_start + 1]) == 0) htmlstr = strip_first_seq( htmlstr, htmlstr[sh_start,sh_end - sh_start + 1], [:mappings]) else sh_end = sh_end - 1 end else sh_start = sh_start - 1 end sh_end = htmlstr[0..sh_end].rindex(g[1]) sh_start = htmlstr[0..sh_start].rindex(g[0]) end } return htmlstr end |