Class: GetText::GtkBuilderUIDefinitionsParser

Inherits:
Object
  • Object
show all
Defined in:
lib/gettext/tools/parser/gtk_builder_ui_definitions.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ GtkBuilderUIDefinitionsParser

Returns a new instance of GtkBuilderUIDefinitionsParser.



56
57
58
59
# File 'lib/gettext/tools/parser/gtk_builder_ui_definitions.rb', line 56

def initialize(path, options={})
  @path = path
  @options = options
end

Class Method Details

.init(config) ⇒ Object

Sets some preferences to parse GtkBuilder UI definitions files.

  • config: a Hash of the config. It can takes some values below:
    • :extnames: An Array of target files extension. Default is [".ui", ".glade"].


35
36
37
38
39
# File 'lib/gettext/tools/parser/gtk_builder_ui_definitions.rb', line 35

def init(config)
  config.each do |k, v|
    @config[k] = v
  end
end

.parse(path, options = {}) ⇒ Object



50
51
52
53
# File 'lib/gettext/tools/parser/gtk_builder_ui_definitions.rb', line 50

def parse(path, options={})
  parser = new(path, options)
  parser.parse
end

.target?(file) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
# File 'lib/gettext/tools/parser/gtk_builder_ui_definitions.rb', line 41

def target?(file) # :nodoc:
  @config[:extnames].each do |extname|
    next unless File.extname(file) == extname
    next unless File.read(file).include?("<interface>")
    return true
  end
  false
end

Instance Method Details

#parseObject

:nodoc:



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
# File 'lib/gettext/tools/parser/gtk_builder_ui_definitions.rb', line 61

def parse # :nodoc:
  File.open(@path) do |file|
    po = []
    start_line_no = nil
    property = nil
    file.each_line do |line|
      case line
      when /<property/
        property = $POSTMATCH
        start_line_no = file.lineno
        if /<\/property>/ =~ property
          property << $PREMATCH
          add_po_entry(po, property, start_line_no)
          property = nil
        end
      when /<\/property>/
        property << $PREMATCH
        add_po_entry(po, property, start_line_no)
        property = nil
      else
        property << line if property
      end
    end
    po
  end
end