class PutText::Parser::Ruby

Constants

METHODS
PARAMS

Public Class Methods

new() click to toggle source
# File lib/puttext/parser/ruby.rb, line 41
def initialize
  @ruby_parser = ::Parser::CurrentRuby.new
end

Public Instance Methods

strings_from_source(source, filename: '(string)', first_line: 1) click to toggle source
# File lib/puttext/parser/ruby.rb, line 45
def strings_from_source(source, filename: '(string)', first_line: 1)
  buffer = ::Parser::Source::Buffer.new(filename, first_line)
  buffer.source = source

  @ruby_parser.reset
  ast = @ruby_parser.parse(buffer)

  if ast.is_a? ::Parser::AST::Node
    find_strings_in_ast(ast)
  else
    []
  end
end

Private Instance Methods

ast_node_location(ast_node) click to toggle source
# File lib/puttext/parser/ruby.rb, line 83
def ast_node_location(ast_node)
  filename = ast_node.location.expression.source_buffer.name
  line     = ast_node.location.line

  "#{filename}:#{line}"
end
find_strings_in_ast(ast_node) click to toggle source
# File lib/puttext/parser/ruby.rb, line 90
def find_strings_in_ast(ast_node)
  entries = []

  if ast_node.type == :send && METHODS[ast_node.children[1]]
    entries << po_entry_from_ast_node(
      ast_node,
      METHODS[ast_node.children[1]]
    )
  else
    entries += find_strings_in_each_ast(ast_node.children)
  end

  entries.compact
end
find_strings_in_each_ast(ast_nodes) click to toggle source
# File lib/puttext/parser/ruby.rb, line 105
def find_strings_in_each_ast(ast_nodes)
  entries = []

  ast_nodes.each do |node|
    next unless node.is_a? ::Parser::AST::Node
    entries += find_strings_in_ast(node)
  end

  entries
end
po_entry_from_ast_node(ast_node, type) click to toggle source
# File lib/puttext/parser/ruby.rb, line 70
def po_entry_from_ast_node(ast_node, type)
  entry_attrs = { references: [ast_node_location(ast_node)] }

  PARAMS[type].each_with_index do |name, index|
    next if name == :_ # skip parameters named _

    param = string_from_ast_node(ast_node.children[index + 2])
    entry_attrs[name] = param if param
  end

  PutText::POEntry.new(entry_attrs) if entry_attrs[:msgid]
end
string_from_ast_node(ast_node) click to toggle source
# File lib/puttext/parser/ruby.rb, line 61
def string_from_ast_node(ast_node)
  return if ast_node.nil?

  case ast_node.type
  when :str
    ast_node.children[0]
  end
end