cirandas.net

ref: master

app/models/doc_topic.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class DocTopic < DocItem
  def self.loadfile(file)
    if !File.exist?(file)
      raise DocItem::NotFound
    end
    lines = File.readlines(file) 
    title_line = _find_title(lines)
    File.basename(file) =~ /(.*)\.([^\.]+)\.xhtml$/
    id = $1
    language = $2
    new(:id => id, :title => title(title_line), :text => lines.join, :language => language, :order => order(title_line))
  end

  def self._find_title(lines)
    lines.find {|line| line =~ /^(<h1.*>.*<\/h1>)/ }
    $1
  end

  def self.title(line)
    line =~ /<h1.*>(.*)<\/h1>/
    $1
  end

  def self.order(line)
    if line =~ /<h1 class="order-(.*)">.*<\/h1>/
      $1.to_i
    else
      0
    end
  end

end