Module: MightyString::HTML
- Defined in:
- lib/mightystring/strip_html.rb
Class Method Summary collapse
-
.html_math_exceptions(in_str = "") ⇒ Object
End define generic rules ***.
-
.html_to_text_codes ⇒ Object
Define some generic rules here *** —- COOL note: you can insert ASCII color escape code rules here…
- .license ⇒ Object
- .math_by_space ⇒ Object
-
.strip_first_seq(mstr = "", mseq = "", cmpchar = self.html_to_text_codes) ⇒ Object
strip sequence out ( master string, sequence to remove, any characters to swap inplace this for that ).
-
.strip_html(htmlstr = "", xarg = [["<",">"],["&",";"]]) ⇒ Object
Pick tags/blocks of string to remove (ex: “&”, “;” like in “"” can become “” or “‘” if rules set)).
- .testCase ⇒ Object
Class Method Details
.html_math_exceptions(in_str = "") ⇒ Object
End define generic rules ***
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/mightystring/strip_html.rb', line 69 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 |
.html_to_text_codes ⇒ Object
Define some generic rules here *** —- COOL note: you can insert ASCII color escape code rules here… like for href then blue and for /a then plain
59 60 61 |
# File 'lib/mightystring/strip_html.rb', line 59 def self.html_to_text_codes {"""=>"'","br"=>"\n","'" => "'", "td" => " | ", " " => " ", "™" => "(TM)", "©" => "(c)"} # replace html segment and insert plan text equivalent end |
.license ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/mightystring/strip_html.rb', line 134 def self.license license = "Mighty_String::Strip_HTML is licensed under 'The MIT License (MIT)' Copyright (c) 2012 Daniel P. Clark & 6ft Dan(TM) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE." puts puts license puts end |
.math_by_space ⇒ Object
63 64 65 |
# File 'lib/mightystring/strip_html.rb', line 63 def self.math_by_space false # TODO FIXME exceptions get past a href 12-12-12 end |
.strip_first_seq(mstr = "", mseq = "", cmpchar = self.html_to_text_codes) ⇒ Object
strip sequence out ( master string, sequence to remove, any characters to swap inplace this for that )
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/mightystring/strip_html.rb', line 81 def self.strip_first_seq( mstr = "", mseq = "", cmpchar = self.html_to_text_codes ) if not cmpchar.empty? and cmpchar.keys.any? {|mkey| mseq.match_pci(mkey) } # keys exist and one of the keys match cmpchar.each_key { |mkey| if mseq.match_pci(mkey) 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 |
.strip_html(htmlstr = "", xarg = [["<",">"],["&",";"]]) ⇒ Object
Pick tags/blocks of string to remove (ex: “&”, “;” like in “"” can become “” or “‘” if rules set))
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/mightystring/strip_html.rb', line 95 def self.strip_html( htmlstr = "", xarg = [["<",">"],["&",";"]] ) # xarg start, end xarg.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]) 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 |
.testCase ⇒ Object
125 126 127 128 129 130 131 132 |
# File 'lib/mightystring/strip_html.rb', line 125 def self.testCase pagesample = "<html><body>This code primarily removes (less than)tags(greater than) and (amperstand)code(semicolon).<br>This default behavior can be modified to fit your needs.<br>4>3 doesn't pair up, so it's visible.<br>As well as this with a space 4 > 3.<br>The opposite is 3 < 4. Can you see me?<br>These following punctions don't get removed because they are out of matching order. ';and&'.<br>< This is visible because of the first space before the less than symbol. ><br>&This shows because it's longer then characters in length and has a space in it.;<br><br><table><tr><td>My Box Table</td></tr></table> <!-- <div>Old HTML commented out. This is a unique case.<br>The code finds the innermost blocks and removes them outwards. So something like '< !-- < tag >' or '< /tag > -- >' won't raise an error.<br>(I added the spaces so you can still see the ouput print.)</div> --><br><br><h1>Ruby is quite nice!</h1><br><a href='_blank'>http://www.6ftdan.com</a></body></html>" puts pagesample puts puts " * - * - * - Before test is above. - * - * - after striphtml follows - * - * - *" puts puts strip_html(pagesample) end |