File: renderer.rb

package info (click to toggle)
ruby-xpath 2.0.0-1~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 180 kB
  • sloc: ruby: 993; makefile: 2
file content (174 lines) | stat: -rw-r--r-- 3,973 bytes parent folder | download | duplicates (3)
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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
module XPath
  class Renderer
    def self.render(node, type)
      new(type).render(node)
    end

    def initialize(type)
      @type = type
    end

    def render(node)
      arguments = node.arguments.map { |argument| convert_argument(argument) }
      send(node.expression, *arguments)
    end

    def convert_argument(argument)
      case argument
        when Expression, Union then render(argument)
        when Array then argument.map { |element| convert_argument(element) }
        when String then string_literal(argument)
        when Literal then argument.value
        else argument.to_s
      end
    end

    def string_literal(string)
      if string.include?("'")
        string = string.split("'", -1).map do |substr|
          "'#{substr}'"
        end.join(%q{,"'",})
        "concat(#{string})"
      else
        "'#{string}'"
      end
    end

    def this_node
      '.'
    end

    def descendant(parent, element_names)
      if element_names.length == 1
        "#{parent}//#{element_names.first}"
      elsif element_names.length > 1
        "#{parent}//*[#{element_names.map { |e| "self::#{e}" }.join(" | ")}]"
      else
        "#{parent}//*"
      end
    end

    def child(parent, element_names)
      if element_names.length == 1
        "#{parent}/#{element_names.first}"
      elsif element_names.length > 1
        "#{parent}/*[#{element_names.map { |e| "self::#{e}" }.join(" | ")}]"
      else
        "#{parent}/*"
      end
    end

    def axis(parent, name, tag_name)
      "#{parent}/#{name}::#{tag_name}"
    end

    def node_name(current)
      "name(#{current})"
    end

    def where(on, condition)
      "#{on}[#{condition}]"
    end

    def attribute(current, name)
      "#{current}/@#{name}"
    end

    def equality(one, two)
      "#{one} = #{two}"
    end

    def is(one, two)
      if @type == :exact
        equality(one, two)
      else
        contains(one, two)
      end
    end

    def variable(name)
      "%{#{name}}"
    end

    def text(current)
      "#{current}/text()"
    end

    def normalized_space(current)
      "normalize-space(#{current})"
    end

    def literal(node)
      node
    end

    def css(current, selector)
      paths = Nokogiri::CSS.xpath_for(selector).map do |xpath_selector|
        "#{current}#{xpath_selector}"
      end
      union(paths)
    end

    def union(*expressions)
      expressions.join(' | ')
    end

    def anywhere(element_names)
      if element_names.length == 1
        "//#{element_names.first}"
      elsif element_names.length > 1
        "//*[#{element_names.map { |e| "self::#{e}" }.join(" | ")}]"
      else
        "//*"
      end
    end

    def contains(current, value)
      "contains(#{current}, #{value})"
    end

    def starts_with(current, value)
      "starts-with(#{current}, #{value})"
    end

    def and(one, two)
      "(#{one} and #{two})"
    end

    def or(one, two)
      "(#{one} or #{two})"
    end

    def one_of(current, values)
      values.map { |value| "#{current} = #{value}" }.join(' or ')
    end

    def next_sibling(current, element_names)
      if element_names.length == 1
        "#{current}/following-sibling::*[1]/self::#{element_names.first}"
      elsif element_names.length > 1
        "#{current}/following-sibling::*[1]/self::*[#{element_names.map { |e| "self::#{e}" }.join(" | ")}]"
      else
        "#{current}/following-sibling::*[1]/self::*"
      end
    end

    def previous_sibling(current, element_names)
      if element_names.length == 1
        "#{current}/preceding-sibling::*[1]/self::#{element_names.first}"
      elsif element_names.length > 1
        "#{current}/preceding-sibling::*[1]/self::*[#{element_names.map { |e| "self::#{e}" }.join(" | ")}]"
      else
        "#{current}/preceding-sibling::*[1]/self::*"
      end
    end

    def inverse(current)
      "not(#{current})"
    end

    def string_function(current)
      "string(#{current})"
    end
  end
end