Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 119 lines (115 sloc) 3.257 kb
2fc0df7 qweb patch
wis authored
1 #!/usr/bin/ruby
2 # vim:set noet fdm=syntax fdl=0 fdc=3 fdn=2:
3
4 require "qweb"
5
6 class QWebRHTML < QWeb
7 def render_rhtml(root)
8 @root=root
9 @t.each do |k,v|
10 @arg=0;
11 name=File.join(root,k)
12 FileUtils.mkdir_p(File.dirname(name))
13 fname="#{name}.rhtml"
14 if File.exists?(fname)
15 unless File.new(fname,"r").read =~ /qweb_vars/
16 puts "skip #{fname}"
17 next
18 end
19 end
20 f=File.new(fname,"w")
21 head=<<EOS.chomp!
22 <%
23 qweb_vars||={};
24 qweb_vars['qweb_vars']=qweb_vars;
25 %>
26 EOS
27 f.write(head+render_node(v,k))
28 f.close
29 # p "wrote #{fname}"
30 end
31 end
32 def render_att_att(e,an,av,v)
33 if an =~ Regexp.new("^#{@prefix}-attf-")
34 return " #{an[@prefix.length + 6..-1]}=\"<%=h \"#{av}\" %>\""
35 elsif an =~ Regexp.new("^#{@prefix}-att-")
36 return " #{an[@prefix.length + 5..-1]}=\"<%=h (#{av}).to_s %>\""
37 else
38 return " <%(#{av})[0]%>=\"<%=h (#{av})[1] %>\""
39 end
40 end
41 # Tags
42 def render_tag_raw(e,t_att,g_att,v)
43 if t_att["raw"]=="0"
44 return "<%= qweb_0 %>"
45 else
46 return "<%= #{t_att["raw"]} %>"
47 end
48 end
49 def render_tag_rawf(e,t_att,g_att,v)
50 return "<%= \"#{t_att["rawf"]}\" %>"
51 end
52 def render_tag_esc(e,t_att,g_att,v)
53 if t_att["esc"]=="0"
54 return "<%=h qweb_0 %>"
55 else
56 # TODO not escape &quot;
57 return "<%=h #{t_att["esc"]} %>"
58 end
59 end
60 def render_tag_escf(e,t_att,g_att,v)
61 # TODO not escape &quot;
62 return "<%=h \"#{t_att["escf"]}\" %>"
63 end
64 def render_tag_if(e,t_att,g_att,v)
65 return "<% if(#{t_att["if"]}) then %>#{render_element(e,g_att,v)}<% end %>"
66 end
67 def render_tag_foreach(e,t_att,g_att,v)
68 expr=t_att["foreach"]
69 n=t_att['as']
70 if not n
71 n=expr.gsub(/[^a-zA-Z0-9]/,'_')
72 end
73 inner=render_element(e,g_att,v)
74 pre="#{n}_all=#{expr};#{n}_size=-1;#{n}_size=#{n}_all.length if #{n}_all.respond_to? 'length';#{n}_index=0;"
75 inner0="qweb_vars['#{n}']=#{n}_value=#{n}; #{n}_first=#{n}_index==0; #{n}_even=#{n}_index%2; #{n}_odd=(#{n}_index+1)%2;"
76 inner1="#{n}_last=#{n}_index+1==#{n}_size; #{n}_parity=(#{n}_index%2==1 ? 'odd' : 'even');\n%>#{inner}<%\n#{n}_index+=1;"
77 code="<% \n#{pre}\n #{n}_all.each do |#{n}|\n #{inner0} \n #{inner1}\n end %>"
78 return code
79 end
80 def render_tag_call(e,t_att,g_att,v)
81 name=t_att["call"]
82 s=render_element(e,g_att,v)
83 if (s =~ /\<\%/) or (s =~ /\"/) or (s =~ /\\/) or (s =~ /\#/)
84 @arg+=1
85 arg="#{v}__#{@arg}"
86 f=File.new(File.join(@root,"#{arg}.rhtml"),"w")
87 f.write(s)
88 f.close
89 s="qweb_vars.merge('qweb_0' => render_file('#{arg}.rhtml',true,qweb_vars))"
90 else
91 s="qweb_vars.merge('qweb_0' => \"#{s}\")"
92 end
93 return "<%= render_file('#{name}.rhtml', true, #{s}) %>"
94 end
95 def render_tag_set(e,t_att,g_att,v)
96 name=t_att["set"]
97 if t_att["eval"]
98 return "<% #{name}=qweb_vars[\"#{name}\"]=#{t_att["eval"]} %>"
99 else
100 s=render_element(e,g_att,v)
101 if (s =~ /\<\%/) or (s =~ /\"/) or (s =~ /\\/) or (s =~ /\#/)
102 @arg+=1
103 arg="#{v}__#{@arg}"
104 f=File.new(File.join(@root,"#{arg}.rhtml"),"w")
105 f.write(s)
106 f.close
107 s="render_file('#{arg}.rhtml',true,qweb_vars)"
108 return "<% #{name}=qweb_vars[\"#{name}\"]=#{s} %>"
109 else
110 return "<% #{name}=qweb_vars[\"#{name}\"]=\"#{s}\" %>"
111 end
112 end
113 end
114 end
115
116 if __FILE__ == $0
117 q=QWebRHTML.new("qweb_template.xml").render_rhtml("../views")
118 end
Something went wrong with that request. Please try again.