-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
120 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# encoding: utf-8 | ||
|
||
|
||
# fix: move into TextUtils namespace/module!! | ||
|
||
class BlockReader | ||
|
||
include LogUtils::Logging | ||
|
||
def self.from_file( path ) | ||
## nb: assume/enfore utf-8 encoding (with or without BOM - byte order mark) | ||
## - see textutils/utils.rb | ||
text = File.read_utf8( path ) | ||
self.from_string( text ) | ||
end | ||
|
||
def self.from_string( text ) | ||
self.new( text ) | ||
end | ||
|
||
def initialize( text ) | ||
@text = text | ||
end | ||
|
||
def read | ||
## note returns an array of (line) strings e.g. | ||
## [ | ||
## "line1\nline2", ## -- block1 | ||
## "line1\nline2\nline3" ## -- block2 | ||
## ] | ||
|
||
blocks = [] | ||
buf = "" | ||
|
||
@text.each_line do |line| | ||
# comments allow: | ||
# 1) ##### (shell/ruby style) | ||
if line =~ /^\s*#/ | ||
# skip komments and do NOT copy to result (keep comments secret!) | ||
logger.debug 'skipping comment line' | ||
next | ||
end | ||
|
||
# if line =~ /^\s*$/ | ||
# # kommentar oder leerzeile überspringen | ||
# logger.debug 'skipping blank line' | ||
# next | ||
# end | ||
|
||
# pass 2) remove leading and trailing whitespace | ||
line = line.strip | ||
|
||
if line =~ /^-{3,}$/ ## three or more lines | ||
logger.debug 'block separator' | ||
blocks << buf.strip ## note: strip leading and trailing whitespace | ||
buf = "" | ||
else | ||
buf << "#{line}\n" | ||
end | ||
end # each lines | ||
|
||
blocks << buf.strip ## note: strip leading and trailing whitespace | ||
blocks | ||
end # method read | ||
|
||
end # class BlockReader | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#################################### | ||
# feedburner text pattern (regex) | ||
# | ||
# pattern (regex) | ||
# --- | ||
# test1 | ||
# --- | ||
# test2 | ||
# --- | ||
# etc. | ||
|
||
|
||
<img[^>]*? | ||
src=("|')(:?http:)?//feeds\.feedburner\.com/~r/[^>]+?\1 | ||
.*?> | ||
|
||
--- | ||
|
||
<img src="//feeds.feedburner.com/~r/Rubyflow/~4/1wUDnBztAJY" height="1" width="1" alt=""/> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
### | ||
# to run use | ||
# ruby -I ./lib -I ./test test/test_block_reader.rb | ||
# or better | ||
# rake test | ||
|
||
require 'helper' | ||
|
||
|
||
class TestBlockReader < MiniTest::Test | ||
|
||
def test_feedburner | ||
blocks = BlockReader.from_file( "#{TextUtils.root}/test/data/feedburner.txt" ).read | ||
|
||
## note: regex - use %q - do NOT escape \. or \1 etc. | ||
pattern = %q{<img[^>]*?src=("|')(:?http:)?//feeds\.feedburner\.com/~r/[^>]+?\1.*?>} | ||
|
||
test1 = %q{<img src="//feeds.feedburner.com/~r/Rubyflow/~4/1wUDnBztAJY" height="1" width="1" alt=""/>} | ||
|
||
assert_equal 2, blocks.size | ||
assert_equal pattern, blocks[0].gsub( /[\n ]/, '' ) ## note: need to remove newlines and spaces | ||
assert_equal test1, blocks[1] | ||
end | ||
|
||
end # class TestBlockReader | ||
|