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.



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

def initialize(raw_s)

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

  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

    s.join.split(/^\n*#/)[1..-1].map do |x|  
      

      lines = x.lines

      raw_heading = lines.shift.rstrip        

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

      if a2.last =~ /^--+/ then

        a2.pop
        summary = a2.join

        lines = ["\n"] + RowX.new(lines.join, level: 0).to_lines(delimiter: ' # ')
        
      end

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

      ([indent(n) + heading] + lines.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 = px

end

Instance Attribute Details

#to_polyrexObject (readonly)

Returns the value of attribute to_polyrex.



9
10
11
# File 'lib/polyrex-headings.rb', line 9

def to_polyrex
  @to_polyrex
end

#to_sObject (readonly)

Returns the value of attribute to_s.



9
10
11
# File 'lib/polyrex-headings.rb', line 9

def to_s
  @to_s
end