Class: Feedtxt::JsonParser
- Inherits:
-
Object
- Object
- Feedtxt::JsonParser
- Includes:
- LogUtils::Logging
- Defined in:
- lib/feedtxt/parser/json.rb
Constant Summary collapse
- FEED_BEGIN =
note:
regex excape pipe: | to \| \\ needs to get escaped twice e.g. (\\ becomes \) e.g. |{ or |{{{
"^[ ]*\\|{+[ ]*$"
- FEED_END =
e.g. }| or }}}|
"^[ ]*}+\\|[ ]*$"
- FEED_NEXT =
e.g.}/{ or }}}/{{{
todo/check: also allow }///{ or } /// { why,why not?
"^[ ]*}+/{+[ ]*$"
- FEED_META =
e.g. }—{ or }}}—{{{ or }-{
todo/check: also allow }.{ with dot why? why not? also allow } - { or } ---- { why? why not?
"^[ ]*}+-+{+[ ]*$"
Class Method Summary collapse
-
.parse(text, opts = {}) ⇒ Object
convenience class/factory method.
Instance Method Summary collapse
-
#initialize(text) ⇒ JsonParser
constructor
Note: lets keep/use same API as RSS::Parser for now.
- #parse ⇒ Object
Constructor Details
#initialize(text) ⇒ JsonParser
Note: lets keep/use same API as RSS::Parser for now
17 18 19 |
# File 'lib/feedtxt/parser/json.rb', line 17 def initialize( text ) @text = text end |
Class Method Details
.parse(text, opts = {}) ⇒ Object
convenience class/factory method
12 13 14 |
# File 'lib/feedtxt/parser/json.rb', line 12 def self.parse( text, opts={} ) self.new( text ).parse end |
Instance Method Details
#parse ⇒ Object
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 |
# File 'lib/feedtxt/parser/json.rb', line 42 def parse ## find start marker e.g. |>>> ## use regex - allow three or more >>>>>> or <<<<<< ## allow spaces before and after s = StringScanner.new( @text ) prolog = s.scan_until( /(?=#{FEED_BEGIN})/ ) ## pp prolog feed_begin = s.scan( /#{FEED_BEGIN}/ ) if feed_begin.empty? ## use blank? why? why not?? ## nothing found return empty array for now; return nil - why? why not? puts "warn !!! no begin marker found e.g. |>>>" return [] end buf = s.scan_until( /(?=#{FEED_END})/ ) buf = buf.strip # remove leading and trailing whitespace feed_end = s.scan( /#{FEED_END}/ ) if feed_end.empty? ## use blank? why? why not?? ## nothing found return empty array for now; return nil - why? why not? puts "warn !!! no end marker found e.g. <<<|" return [] end #### ## pass 1: split blocks by }/{ ### note: allows }}}/{{{ blocks = buf.split( /#{FEED_NEXT}/ ) ## pp blocks ## 1st block is feed meta data block1st = blocks.shift ## get/remove 1st block from blocks block1st = block1st.strip # remove leading and trailing whitespaces = ::JSON.parse( "{ #{block1st} }" ) feed_items = [] blocks.each do |block| ### note: do NOT use split e.g.--- is used by markdown ## only search for first --- to split (all others get ignored) ## todo: make three dashes --- (3) not hard-coded (allow more) s2 = StringScanner.new( block ) = s2.scan_until( /(?=#{FEED_META})/ ) = .strip # remove leading and trailing whitespace = ::JSON.parse( "{ #{} }" ) ## convert to hash with yaml = s2.scan( /#{FEED_META}/ ) item_content = s2.rest item_content = item_content.strip # remove leading and trailing whitespace feed_items << [, item_content] end [ , feed_items ] end |