Class: Rsssf::PageConverter
- Inherits:
-
Object
- Object
- Rsssf::PageConverter
- Defined in:
- lib/rsssf/convert.rb
Constant Summary collapse
- HEADING2_RE =
todo/fix - use generic heading regex for all h2/h3/h4 etc.
exclude h1 - why? why not?
note - include leading and trailing spaces !!!
note - for content use non-greedy to allow
match of inside content too
%r{ \s* <H2> (?<title>.+?) </H2> \s* }imx
- HEADING4_RE =
%r{ \s* <H4> (?<title>.+?) </H4> \s* }imx
- A_NAME_RE =
note - for content use non-greedy to allow
match of inside content too
%r{<A [ ]+ NAME [ ]* = (?<name>[^>]+?) > (?<title>.+?) </A> }imx
- A_HREF_RE =
%r{<A \s+ HREF [ ]* = (?<href>[^>]+?) > (?<title>.+?) <\/A> }imx
- EMAIL_RE =
%r{ \s* \( [a-z][a-z0-9_]+ @[a-z]+(\.[a-z]+)+ \) }imx
Class Method Summary collapse
-
.convert(html, url:) ⇒ Object
convenience helper.
Instance Method Summary collapse
-
#convert(html, url:) ⇒ Object
add anchor: options or such lets you toggle adding anchors (§premier etc.) - why? why not?.
- #html_to_txt(html) ⇒ Object
-
#log(msg) ⇒ Object
more helpers.
- #patch_about(html) ⇒ Object
- #remove_emails(html) ⇒ Object
- #replace_a_href(html) ⇒ Object
- #replace_a_name(html) ⇒ Object
- #replace_h2(html) ⇒ Object
- #replace_h4(html) ⇒ Object
- #squish(str) ⇒ Object
Class Method Details
.convert(html, url:) ⇒ Object
convenience helper
6 7 8 9 |
# File 'lib/rsssf/convert.rb', line 6 def self.convert( html, url: ) @@converter ||= new ## use a "shared" built-in converter @@converter.convert( html, url: url ) end |
Instance Method Details
#convert(html, url:) ⇒ Object
add anchor: options or such
lets you toggle adding anchors (§premier etc.) - why? why not?
15 16 17 18 19 20 21 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 104 105 106 107 108 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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/rsssf/convert.rb', line 15 def convert( html, url: ) ### todo/fix: first check if html is all ascii-7bit e.g. ## includes only chars from 64 to 127!!! ## normalize newlines ## remove \r (form feed) used by Windows; just use \n (new line) html = html.gsub( "\r", '' ) ## check for html entities html = html.gsub( "ä", 'ä' ) html = html.gsub( "ö", 'ö' ) html = html.gsub( "ü", 'ü' ) html = html.gsub( "Ä", 'Ä' ) html = html.gsub( "Ö", 'Ö' ) html = html.gsub( "Ü", 'Ü' ) html = html.gsub( "ß", 'ß' ) ## typos / autofix - keep - why? why not? html = html.gsub( "&oulm;", 'ö' ) ## support typo in entity (ö) html = html.gsub( "¨", 'ü' ) ## support typo in entity (ü) - why? why not? html = html.gsub( "&slig;", "ß" ) ## support typo in entity (ß) html = html.gsub( "&aaacute;", "á" ) ## typo for á html = html.gsub( "É", 'É' ) html = html.gsub( "ø", 'ø' ) html = html.gsub( "ã", 'ã' ) html = html.gsub( "õ", 'õ' ) html = html.gsub( "ô", 'ô' ) entities = %w[ À À Á Á Â Â Ã Ã Ä Ä Å Å à à á á â â ã ã ä ä å å Æ Æ æ æ ß ß Ç Ç ç ç È È É É Ê Ê Ë Ë è è é é ê ê ë ë Ì Ì Í Í Î Î Ï Ï ì ì í í î î ï ï Ñ Ñ ñ ñ Ò Ò Ó Ó Ô Ô Õ Õ Ö Ö ò ò ó ó ô ô õ õ ö ö Ø Ø ø ø Ù Ù Ú Ú Û Û Ü Ü ù ù ú ú û û ü ü Ý Ý ý ý ÿ ÿ < < > > & & © © ® ® Š Š š š č č ć ć Ž Ž ’ ’ ] entities.each_slice(2) do |str, entity| html = html.gsub( entity, str ) end ############## ## check for more entities ## limit &---; to length 10 - why? why not? html = html.gsub( /&[^; ]{1,10};/) do |match| match = if match == 'ij' ## use like Van Dijk -> Van Dijk 'ij' else msg = "found unencoded html entity #{match}" puts "*** WARN - #{msg}" log( msg ) ## log too (see log.txt) match ## pass through as is (1:1) end match end ## todo/fix: add more entities ################################### ### smart quotes quick fixes ### convert all "smart" quote to (standard) single quotes ## D´Alessandro => D'Alessandro html = html.gsub( '´', "'" ) html = html.gsub( '’', "'" ) html = html.gsub( '‘', "'" ) html = html.gsub( '“', '"' ) html = html.gsub( '”', '"' ) ### convert fancy dashes/hyphens to plain dash/hyphen html = html.gsub( '–', '-' ) txt = html_to_txt( html ) header = <<EOS <!-- source: #{url} --> EOS header+txt ## return txt w/ header end |
#html_to_txt(html) ⇒ Object
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 |
# File 'lib/rsssf/convert.rb', line 374 def html_to_txt( html ) ### # todo: check if any tags (still) present?? ## cut off everything before body html = html.sub( /.+?<BODY>\s*/im, '' ) ## cut off everything after body (closing) html = html.sub( /<\/BODY>.*/im, '' ) html = patch_about( html ) ## remove cite html = html.gsub( /<CITE>([^<]+)<\/CITE>/im ) do |_| puts " remove cite >#{$1}<" "#{$1}" end html = html.gsub( /\s*<HR>\s*/im ) do |match| match = match.gsub( "\n", '$$' ) ## make newlines visible for debugging puts " replace horizontal rule (hr) - >#{match}<" "\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n" ## check what hr to use use - . - . - or =-=-=-= or somehting distinct? end ## replace break (br) ## note: do NOT use m/multiline for now - why? why not?? html = html.gsub( /<BR>\s*/i ) do |match| ## note: include (swallow) "extra" newline match = match.gsub( "\n", '$$' ) ## make newlines visible for debugging puts " replace break (br) - >#{match}<" "\n" end html = replace_a_href( html ) ## note a name="about" includes more a hrefs etc. # let it go first (before a href) html = replace_a_name( html ) ## replace paragrah (p) html = html.gsub( /\s*<P>\s*/im ) do |match| ## note: include (swallow) "extra" newline match = match.gsub( "\n", '$$' ) ## make newlines visible for debugging puts " replace paragraph (p) - >#{match}<" "\n\n" end html = html.gsub( /<\/P>/i, '' ) ## replace paragraph (p) closing w/ nothing for now ## remove i html = html.gsub( /<I>([^<]+)<\/I>/im ) do |_| puts " remove italic (i) >#{$1}<" "#{$1}" end html = replace_h2( html ) html = replace_h4( html ) ## remove b - note: might include anchors (thus, call after anchors) html = html.gsub( /<B>([^<]+)<\/B>/im ) do |_| puts " remove bold (b) >#{$1}<" "**#{$1}**" end ## replace preformatted (pre) html = html.gsub( /<PRE>|<\/PRE>/i ) do |_| puts " replace preformatted (pre)" '' # replace w/ nothing for now (keep surrounding newlines) end =begin puts puts puts "html:" puts html[0..2000] puts "-- snip --" puts html[-1000..-1] ## print last hundred chars =end html = remove_emails( html ) ## cleanup whitespaces ## todo/fix: convert newline in space first ## and than collapse spaces etc.!!! txt = String.new html.each_line do |line| line = line.gsub( "\t", ' ' ) # replace all tabs w/ two spaces for nwo line = line.rstrip # remove trailing whitespace (incl. newline/formfeed) txt << line txt << "\n" end txt end |
#log(msg) ⇒ Object
more helpers
482 483 484 485 486 487 488 489 |
# File 'lib/rsssf/convert.rb', line 482 def log( msg ) ## append msg to ./logs.txt ## use ./errors.txt - why? why not? File.open( './logs.txt', 'a:utf-8' ) do |f| f.write( msg ) f.write( "\n" ) end end |
#patch_about(html) ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/rsssf/convert.rb', line 219 def patch_about( html ) # <A name=about> # <H2>About this document</H2></A> # or # <A NAME="about"><H2>About this document</H2></A> # => change to (possible?) # <H2><A name=about>About this document</A></H2> html.sub( %r{<A [ ] name=(about|"about")> \s* <H2>About [ ] this [ ] document</H2></A> }ixm, "<H2><A name=about>About this document</A></H2>" ) end |
#remove_emails(html) ⇒ Object
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
# File 'lib/rsssf/convert.rb', line 346 def remove_emails( html ) ### remove converted ("blineded") mailto anchors ## note usually inside () e.g. ## (‹mailto›) ## plus slurp up all leading whitespace (incl. newline) - why? why not? html = html.gsub( /\s* \(‹mailto›\) /xm, '' ) ### ## remove "regular emails too e.g. ## ## Thanks to Marcelo Leme de Arruda (___@___.__.br), ## Ricardo FF Pontes (___@____.com), ## Santiago Reis (____@____.com.br), ## Marcos Lacerda Queiroz (___@____.com.br) ## etc. ## check for "free-standing e.g. on its own line" emails only for now html = html.gsub( EMAIL_RE ) do |match| puts "removing email >#{match}<" '' end html end |
#replace_a_href(html) ⇒ Object
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/rsssf/convert.rb', line 274 def replace_a_href( html ) ## remove anchors (a href) # note: heading 4 includes anchor (thus, let anchors go first) # note: <a \newline href is used for authors email - thus incl. support for newline as space html.gsub( A_HREF_RE ) do |match| ## note: use .+? non-greedy match m = Regexp.last_match href = m[:href].gsub( /["']/, '' ).strip ## remove ("" or '') title = m[:title].strip ## note: "save" caputure first; gets replaced by gsub (next regex call) ## e.g. ## ‹[email protected], see page mailto:[email protected]› ## ‹[email protected], see page mailto:[email protected]› ## ‹[email protected], see page mailto:[email protected]›) if href.start_with?( 'mailto:') puts " blank mailto - anchor (a) href >#{href}, >#{title}<" '‹mailto›' ## delete/remove email else puts " replace anchor (a) href >#{href}, >#{title}<" ## convert href to xref xref = if href.start_with?('#') ## in-page ref ", see §#{href[1..-1]}" elsif href.start_with?( /https?:/ ) ## external page ref ## skip - keep empty - why? why not? (or add url domain?) '' else ## hack - check for some custom excludes if title.start_with?( 'Rec.Sport.Soccer' ) ## skip - keep empty '' else ## strip (ending) .htm|html ", see page #{href.sub( /\.html?$/,'')}" end end "‹#{squish(title)}#{xref}›" end end end |
#replace_a_name(html) ⇒ Object
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
# File 'lib/rsssf/convert.rb', line 316 def replace_a_name( html ) ## ## remove (named) anchors html.gsub( A_NAME_RE ) do |match| ## note: use .+? non-greedy match m = Regexp.last_match name = m[:name].gsub( /["']/, '' ).strip ## remove ("" or '') title = m[:title].strip ## note: "save" caputure first; gets replaced by gsub (next regex call) match = match.gsub( "\n", '$$' ) ## make newlines visible for debugging puts " replace anchor (a) name >#{name}<, >#{title}< - >#{match}<" ## ## todo - report WARN if title incl. tags ## assumes text only for now - why? why not? ## add a name inside heading !!! ## do NOT add heading inside a name !!! "#{title} ‹§#{name}›" ## note - use two spaces min (between title & name) end end |
#replace_h2(html) ⇒ Object
196 197 198 199 200 201 202 |
# File 'lib/rsssf/convert.rb', line 196 def replace_h2( html ) html.gsub( HEADING2_RE ) do |_| m = Regexp.last_match puts " replace heading 2 (h2) >#{m[:title]}<" "\n\n## #{m[:title]}\n\n" ## note: make sure to always add two newlines end end |
#replace_h4(html) ⇒ Object
204 205 206 207 208 209 210 |
# File 'lib/rsssf/convert.rb', line 204 def replace_h4( html ) html.gsub( HEADING4_RE ) do |_| m = Regexp.last_match puts " replace heading 4 (h4) >#{m[:title]}<" "\n\n#### #{m[:title]}\n\n" ## note: make sure to always add two newlines end end |
#squish(str) ⇒ Object
213 214 215 216 |
# File 'lib/rsssf/convert.rb', line 213 def squish( str ) ## squish more than one white space to one space str.gsub( /[ \r\t\n]+/, ' ' ) end |