Class: PolyrexHeadings

Inherits:
Object
  • Object
show all
Defined in:
lib/polyrex-headings.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_s) ⇒ PolyrexHeadings

Returns a new instance of PolyrexHeadings.



14
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
# File 'lib/polyrex-headings.rb', line 14

def initialize(raw_s)
  
  buffer, type = RXFHelper.read(raw_s)
  
  if type == :unknown and buffer.lines.length <= 1 then
    raise PolyrexHeadingsException, 'File not found' 
  end    

  # add an empty space to the blank line where there is no 
  # raw record between headings
  
  buffer.gsub!(/(#+[^\n]+\n+)(?=\n#)/m,'\1 ')
  summary, *s = buffer.split(/(?=(?:^#|[\n]+\n-+))/,2)
  type = nil

  a = if raw_s =~ /----/ then       
  
    # this was the original code which used dashes to make an underline 
    # of a heading. Typically the heading is now prefixed with a hash (#)
    # which means this block is unlikely to be executed.

    s.join.lines.chunk{|x| x != "\n" }.map do |record, x| 

      if record then

        head = x.shift
        x.shift
        [head] + x.map{|y| '  ' + y}
      end
    end

  else
    
    a2 = summary.gsub(/\n{2,}/,"\n").lines

    if a2.last =~ /^--+/ then
      a2.pop
      summary = a2.join
      type = :rowx 
    end
      
    s.join.split(/^\n*#/)[1..-1].map do |x|  
      
      lines = x.lines
      raw_heading = lines.shift.rstrip        
      
      body = lines.join.strip

      if type == :rowx and body.length > 0

        lines2 = ["\n"] + 
            RowX.new(body, level: 0).to_lines(delimiter: ' # ') << "\n"
      else

        lines2 = lines
      end

      raw_indent = raw_heading.slice!(/#*/)
      n, heading = raw_indent.length, raw_heading.lstrip

      ([indent(n) + heading] + lines2.map{|x| indent(n+1) + x}).join        
    end

  end

  summary.sub!(/^(<\?)(ph|polyrex-headings)/,'\1polyrex')
  @to_s = string = summary + a.join
  
  px = Polyrex.new

  px.parse(string, delimiter: ' # ')
  
  @to_polyrex = @to_px = px

end

Instance Attribute Details

#to_polyrexObject (readonly)

Returns the value of attribute to_polyrex.



12
13
14
# File 'lib/polyrex-headings.rb', line 12

def to_polyrex
  @to_polyrex
end

#to_pxObject (readonly)

Returns the value of attribute to_px.



12
13
14
# File 'lib/polyrex-headings.rb', line 12

def to_px
  @to_px
end

#to_sObject (readonly)

Returns the value of attribute to_s.



12
13
14
# File 'lib/polyrex-headings.rb', line 12

def to_s
  @to_s
end