File: amritadoc.rb

package info (click to toggle)
ruby-amrita 1.0.2-10
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 1,940 kB
  • sloc: ruby: 9,159; xml: 978; makefile: 78
file content (77 lines) | stat: -rw-r--r-- 1,623 bytes parent folder | download | duplicates (4)
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
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
require 'amrita/amx'

class AmritaDocumentTemplate < Amx::Template
  class Section
    include Amrita
    include Amrita::ExpandByMember
    attr_reader :title, :sections, :paragraphs
    
    def initialize(x)
      @title = convert(x.attributes['title'] || "")
      @sections = x.elements.to_a('section').collect do |sec|
        Section.new(sec)
      end

      @paragraphs = x.elements.find_all do |x|
        x.name != "section"
      end
    end
  end

  attr_reader :doc_root

  def befor_expand
    @doc_root = doc.elements['document']
    @refs = {}
    @doc_root.elements.to_a('head/links/ref').each do |ref|
      @refs[ref.attributes['id']] = ref.attributes['url']
    end
  end

  def header_title
    doc_root.elements['head/title']
  end

  def sections
    doc_root.elements.to_a('body/section').collect do |sec|
      Section.new(sec)
    end
  end

  def paragraphs
    doc_root.elements.find_all do |x|
      x.name != "section"
    end
  end

  def rexml2amrita(xml)
    case xml
    when REXML::Element
      case xml.name
      when "list"
        xml.name = "ul"
        super
      when "item"
        xml.name = "li"
        super
      when "code"
        xml.name = "tt"
        pre { super }
      when "link"
        id = xml.attributes['id']
        if id
          url = @refs[id]
          raise %Q[link "#{id}" not found] unless url
          e(:a, :href=>url) { convert(xml.get_text.to_s) }
        else
          url = xml.attributes['url']
          e(:a, :href=>url) { convert(xml.get_text.to_s) }
        end
      else
        super
      end
    else
      super
    end
  end
end