class GetText::GtkBuilderUIDefinitionsParser

Public Class Methods

init(config) click to toggle source

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”].

# 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
new(path, options={}) click to toggle source
# File lib/gettext/tools/parser/gtk_builder_ui_definitions.rb, line 56
def initialize(path, options={})
  @path = path
  @options = options
end
parse(path, options={}) click to toggle source
# File lib/gettext/tools/parser/gtk_builder_ui_definitions.rb, line 50
def parse(path, options={})
  parser = new(path, options)
  parser.parse
end

Private Instance Methods

add_po_entry(po, property, line_no) click to toggle source
# File lib/gettext/tools/parser/gtk_builder_ui_definitions.rb, line 89
def add_po_entry(po, property, line_no)
  raw_attributes, raw_data_and_close_tag = property.split(">", 2)
  raw_data, _close_tag = raw_data_and_close_tag.split("<", 2)
  return if raw_data.empty?

  attributes = parse_attributes(raw_attributes)
  return unless attributes["translatable"] == "yes"

  data = CGI.unescapeHTML(raw_data)
  context = attributes["context"]
  if context
    po_entry = POEntry.new(:msgctxt)
    po_entry.msgctxt = context
  else
    po_entry = POEntry.new(:normal)
  end
  po_entry.msgid = data
  po_entry.references << "#{@path}:#{line_no}"
  po << po_entry
end
parse_attributes(raw_attributes) click to toggle source
# File lib/gettext/tools/parser/gtk_builder_ui_definitions.rb, line 110
def parse_attributes(raw_attributes)
  scanner = StringScanner.new(raw_attributes)
  attributes = {}
  loop do
    scanner.scan(/\s*/m)
    break if scanner.eos?
    name = scanner.scan(/[^=]+/)
    break if name.nil?
    break unless scanner.scan(/=/)
    quote = scanner.scan(/["']/)
    break if quote.nil?
    value = scanner.scan(/[^#{Regexp.escape(quote)}]+/m)
    break if value.nil?
    break unless scanner.scan(/#{Regexp.escape(quote)}/)
    attributes[name] = CGI.unescapeHTML(value)
  end
  attributes
end