Class: Journaltxt::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/journaltxt/parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ Parser

Returns a new instance of Parser.



11
12
13
# File 'lib/journaltxt/parser.rb', line 11

def initialize( text )
  @text = text
end

Class Method Details

.parse(text) ⇒ Object

convenience helper



7
8
9
# File 'lib/journaltxt/parser.rb', line 7

def self.parse( text )   ## convenience helper
  self.new( text ).parse
end

Instance Method Details

#parseObject



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
# File 'lib/journaltxt/parser.rb', line 15

def parse

  puts ":: Text :::::::::::::::::::::"
  puts @text

  ## allow trailing spaces for now
  ##  - allow leadin spaces too - why? why not?

  ## remove leading 1st separator --- first
  text = @text.sub( /^---[ ]*$\n?/, '' )
  b = text.split( /^---[ ]*$\n?/ )

  items = b.each_slice(2).to_a


  ## process meta data blocks convert to hash via yaml

  ##
  ## todo: check for required entries
  ##
  ##   first entry needs/requires:
  ##      year/month/day
  ##   all others requireÖ
  ##      day

  last_page_date = nil

  items = items.each_with_index.map do |item,i|
    page_meta   = YAML.load( item[0] )  ## convert to hash (from yaml text)
    pp page_meta
    page_content = item[1]

    ## remove all (short-cut) date entries
    year  = page_meta.delete( 'year' )
    month = page_meta.delete( 'month' )
    day   = page_meta.delete( 'day' )

    puts "  year: >#{year}< : #{year.class.name}, month: >#{month}< : #{month.class.name}, day: >#{day}< : #{day.class.name}"

    ## convert all date entries to ints
    ##   lets us handle day => Sun 23 etc.

    ##  note: assume year is always a number
    if year.nil?
       if last_page_date
         year = last_page_date.year
       else
         ### fix/todo: throw format/parse exception!!
         puts "** year entry required / expected for first entry / meta data block"
         exit 1
       end
    end


    if day && day.is_a?(String)
       puts "  trying to convert day to int..."
       nums_day = day.scan( /[0-9]+/ )   ## returns array e.g. ['12']
       day = nums_day[0].to_i
       puts "  day:   >#{day}< : #{day.class.name}"
    end

    if day.nil?   ### fix/add - check if day >0 and< 31 why? why not??
      ### fix/todo: throw format/parse exception!!
      puts "** day entry required in meta data block"
      exit 1
    end


    if month && month.is_a?(String)
      puts "  trying to convert month to int..."
      ## for now let stdlib handle conversion
      ##   supports abbreviated names (e.g. Jan) and full names (e.g. January)

      ## todo/fix: check what happens if montn is invalid/unknow
      ##    returns nil? throws exception?
      date_month = Date.parse( "#{year}/#{month}/#{day}" )
      month = date_month.month
      puts "  month: >#{month}< : #{month.class.name}"
    end

    if month.nil?
      if last_page_date
        month = last_page_date.month
      else
        ### fix/todo: throw format/parse exception!!
        puts "** month entry required for first entry / meta data block"
        exit 1
      end
    end


    page_date = Date.new( year, month, day )
    last_page_date = page_date

    ## todo:check if date exists? possible?
    ##     issue warning or something - will get replaced - why? why not?
    page_meta['date']  = page_date

    ## pp YAML.dump( h )

    [page_meta, page_content]
  end
  items
end