Class: PxIndex

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

Instance Method Summary collapse

Constructor Details

#initialize(raw_s = nil, debug: false, allsorted: false, indexsorted: false) ⇒ PxIndex

Returns a new instance of PxIndex.



13
14
15
16
17
18
19
20
# File 'lib/pxindex.rb', line 13

def initialize(raw_s=nil, debug: false, allsorted: false, indexsorted: false)

  @allsorted, @indexsorted = allsorted, indexsorted
  @debug = debug    
  
  read raw_s if raw_s

end

Instance Method Details

#build_htmlObject



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
# File 'lib/pxindex.rb', line 71

def build_html()
  
  @px.each_recursive do |x, parent|
    
    if x.is_a? Entry then
      
      trail = parent.attributes[:trail]
      s = x.title.gsub(/ +/,'-')
      x.attributes[:trail] = trail.nil? ? s : trail + '/' + s
      
    end
    
  end

  doc  = Nokogiri::XML(@px.to_xml)
  xsl  = Nokogiri::XSLT(xslt())

  html_doc = Rexle.new(xsl.transform(doc).to_s)
      
  html_doc.root.css('.atopic').each do |e|      
    
    puts 'e: ' + e.parent.parent.xml.inspect if @debug
    
    href = e.attributes[:href]
    if href.empty? or href[0] == '!' then
      
      if block_given? then
        
        yield(e)
        
      else
        
        e.attributes[:href] = '#' + e.attributes[:trail].split('/')\
            .last.downcase
        
      end
    end
  end
  
  html_doc.xml(pretty: true, declaration: false)
  
end

#import(s) ⇒ Object

Returns a PxIndexBuilder object which can build from am index or phrases



24
25
26
27
28
# File 'lib/pxindex.rb', line 24

def import(s)
  
  read(PxIndexBuilder.new(s, debug: @debug).to_s)
  
end

#parentObject



30
31
32
# File 'lib/pxindex.rb', line 30

def parent()
  @rs.first
end

#q?(s) ⇒ Boolean Also known as: query

Returns:

  • (Boolean)


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
# File 'lib/pxindex.rb', line 34

def q?(s)
  
  return @a.last if s == @s
  puts '@s : ' + @s.inspect if @debug
  puts 's: ' + s.inspect if @debug
  
  # @s is used to store the previous string input to compare it with 
  # the new string input
  
  if (s.length - @s.length).abs >= 1 or s[0..-2] != @s then
    
    @s = s      

    @rs = @px.records.flat_map(&:records)
    
    s2 = ''
    
    @a = s.chars.map do |x|

      s2 += x
      found = search_records(s2, @rs)
      
      break if not found
      found
      
    end

    return @a ? @a.last  : nil
    
  end

  return []

end

#to_pxObject



114
115
116
# File 'lib/pxindex.rb', line 114

def to_px()
  @px
end

#to_sObject



118
119
120
# File 'lib/pxindex.rb', line 118

def to_s()
  @raw_px
end