OSDN Git Service

ab77beedecb616243b519687fa932c8f04b70158
[redminele/redminele.git] / ruby / lib / ruby / gems / 1.8 / gems / gettext-2.1.0 / lib / gettext / tools / parser / glade.rb
1 =begin
2   parser/glade.rb - parser for Glade-2
3
4   Copyright (C) 2004,2005  Masao Mutoh
5  
6   You may redistribute it and/or modify it under the same
7   license terms as Ruby or LGPL.
8 =end
9
10 require 'cgi'
11 require 'gettext'
12
13 module GetText
14   module GladeParser
15     extend GetText
16     extend self
17     
18     bindtextdomain("rgettext")
19
20     TARGET1 = /<property.*translatable="yes">(.*)/
21     TARGET2 = /(.*)<\/property>/
22
23     def parse(file, targets = []) # :nodoc: 
24       lines = IO.readlines(file)
25       parse_lines(file, lines, targets)
26     end
27
28     #from ary of lines.
29     def parse_lines(file, lines, targets) # :nodoc:
30       cnt = 0
31       target = false
32       line_no = 0
33       val = nil
34       
35       loop do 
36         line = lines.shift
37         break unless line
38         
39         cnt += 1
40         if TARGET1 =~ line
41           line_no = cnt
42           val = $1 + "\n"
43           target = true
44           if TARGET2 =~ $1
45             val = $1
46             add_target(val, file, line_no, targets)
47             val = nil
48             target = false
49           end
50         elsif target
51           if TARGET2 =~ line
52             val << $1
53             add_target(val, file, line_no, targets)
54             val = nil
55             target = false
56           else
57             val << line
58           end
59         end
60       end
61       targets
62     end
63
64     XML_RE = /<\?xml/ 
65     GLADE_RE = /glade-2.0.dtd/
66  
67     def target?(file) # :nodoc:
68       data = IO.readlines(file)
69       if XML_RE =~ data[0] and GLADE_RE =~ data[1]
70         true
71       else
72         if File.extname(file) == '.glade'
73           raise _("`%{file}' is not glade-2.0 format.") % {:file => file}
74         end
75         false
76       end
77     end
78
79     def add_target(val, file, line_no, targets) # :nodoc:
80       return unless val.size > 0
81       assoc_data = targets.assoc(val)
82       val = CGI.unescapeHTML(val)
83       if assoc_data 
84         targets[targets.index(assoc_data)] = assoc_data << "#{file}:#{line_no}"
85       else
86         targets << [val.gsub(/\n/, '\n'), "#{file}:#{line_no}"]
87       end
88       targets
89     end
90   end
91 end
92
93 if __FILE__ == $0
94   # ex) ruby glade.rb foo.glade  bar.glade
95   ARGV.each do |file|
96     p GetText::GladeParser.parse(file)
97   end
98 end