Class: Haml::I18n::Extractor

Inherits:
Object
  • Object
show all
Defined in:
lib/haml-i18n-extractor/version.rb,
lib/haml-i18n-extractor/flow/cli.rb,
lib/haml-i18n-extractor/flow/helpers.rb,
lib/haml-i18n-extractor/flow/prompter.rb,
lib/haml-i18n-extractor/flow/workflow.rb,
lib/haml-i18n-extractor/flow/user_action.rb,
lib/haml-i18n-extractor/extraction/extractor.rb,
lib/haml-i18n-extractor/extraction/yaml_tool.rb,
lib/haml-i18n-extractor/extraction/haml_parser.rb,
lib/haml-i18n-extractor/extraction/haml_reader.rb,
lib/haml-i18n-extractor/extraction/haml_writer.rb,
lib/haml-i18n-extractor/extraction/text_finder.rb,
lib/haml-i18n-extractor/extraction/tagging_tool.rb,
lib/haml-i18n-extractor/extraction/text_replacer.rb,
lib/haml-i18n-extractor/extraction/exception_finder.rb

Defined Under Namespace

Modules: Helpers Classes: AbortFile, CLI, ExceptionFinder, HamlParser, HamlReader, HamlWriter, InvalidSyntax, NotADirectory, NotDefinedLineType, Prompter, TaggingTool, TextFinder, TextReplacer, UserAction, Workflow, YamlTool

Constant Summary collapse

VERSION =
"0.4.2"
LINE_TYPES_ALL =
[:plain, :script, :silent_script, :haml_comment, :tag, :comment, :doctype, :filter, :root]
LINE_TYPES_ADD_EVAL =
[:plain, :tag]
DEFAULT_LINE_LOCALE_HASH =
{ :modified_line => nil,:keyname => nil,:replaced_text => nil, :path => nil }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(haml_path, opts = {}) ⇒ Extractor

Returns a new instance of Extractor.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/haml-i18n-extractor/extraction/extractor.rb', line 28

def initialize(haml_path, opts = {})
  @options = opts
  @type = @options[:type]
  @interactive = @options[:interactive]
  @prompter = Haml::I18n::Extractor::Prompter.new
  @haml_reader = Haml::I18n::Extractor::HamlReader.new(haml_path)
  validate_haml(@haml_reader.body)
  @haml_writer = Haml::I18n::Extractor::HamlWriter.new(haml_path, {:type => @type})
  @yaml_tool = Haml::I18n::Extractor::YamlTool.new(@options[:i18n_scope], @options[:yaml_file])
  @tagging_tool ||= Haml::I18n::Extractor::TaggingTool.new
  # hold all the processed lines
  @body = []
  # holds a line_no => {info_about_line_replacemnts_or_not}
  @locale_hash = {}

  self.class.extractors << self
end

Instance Attribute Details

#current_lineObject (readonly)

Returns the value of attribute current_line.



24
25
26
# File 'lib/haml-i18n-extractor/extraction/extractor.rb', line 24

def current_line
  @current_line
end

#haml_readerObject (readonly)

Returns the value of attribute haml_reader.



22
23
24
# File 'lib/haml-i18n-extractor/extraction/extractor.rb', line 22

def haml_reader
  @haml_reader
end

#haml_writerObject (readonly)

Returns the value of attribute haml_writer.



22
23
24
# File 'lib/haml-i18n-extractor/extraction/extractor.rb', line 22

def haml_writer
  @haml_writer
end

#locale_hashObject (readonly)

Returns the value of attribute locale_hash.



23
24
25
# File 'lib/haml-i18n-extractor/extraction/extractor.rb', line 23

def locale_hash
  @locale_hash
end

#typeObject (readonly)

Returns the value of attribute type.



23
24
25
# File 'lib/haml-i18n-extractor/extraction/extractor.rb', line 23

def type
  @type
end

#yaml_toolObject (readonly)

Returns the value of attribute yaml_tool.



23
24
25
# File 'lib/haml-i18n-extractor/extraction/extractor.rb', line 23

def yaml_tool
  @yaml_tool
end

Class Method Details

.debug?Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/haml-i18n-extractor/extraction/extractor.rb', line 5

def self.debug?
  ENV['DEBUG']
end

.extractorsObject

helpful for debugging



10
11
12
# File 'lib/haml-i18n-extractor/extraction/extractor.rb', line 10

def self.extractors
  @extractors ||= []
end

Instance Method Details

#assign_new_bodyObject



53
54
55
# File 'lib/haml-i18n-extractor/extraction/extractor.rb', line 53

def assign_new_body
  @haml_writer.body = new_body
end

#assign_replacementsObject



61
62
63
64
# File 'lib/haml-i18n-extractor/extraction/extractor.rb', line 61

def assign_replacements
  assign_new_body
  assign_yaml
end

#assign_yamlObject



57
58
59
# File 'lib/haml-i18n-extractor/extraction/extractor.rb', line 57

def assign_yaml
  @yaml_tool.locale_hash = @locale_hash
end

#interactive?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/haml-i18n-extractor/extraction/extractor.rb', line 113

def interactive?
  !!@interactive
end

#new_bodyObject



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/haml-i18n-extractor/extraction/extractor.rb', line 66

def new_body
  begin
    @current_line = 1
    @haml_reader.lines.each do |orig_line|
      process_line(orig_line, @current_line)
      @current_line += 1
    end
  rescue AbortFile
    @prompter.moving_to_next_file
    add_rest_of_file_to_body(@current_line)
  end
  @body.join("\n") + "\n"
end

#process_line(orig_line, line_no) ⇒ Object

this is the bulk of it: where we end up setting body info and locale_hash. not write, just set that info in memory in correspoding locations. refactor more?



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/haml-i18n-extractor/extraction/extractor.rb', line 84

def process_line(orig_line, line_no)
  orig_line.chomp!
  orig_line, whitespace = handle_line_whitespace(orig_line)
  line_type, line_match = handle_line_finding(orig_line, line_no)
  should_be_replaced, text_to_replace, line_locale_hash = gather_replacement_info(orig_line, line_match, line_type, line_no)

  user_action = Haml::I18n::Extractor::UserAction.new('y') # default if no prompting: just do it.
  if should_be_replaced
    if interactive?
      user_action = @prompter.ask_user(orig_line,text_to_replace)
    end
  end

  if user_action.tag?
    @tagging_tool.write(line_locale_hash[:path], line_no)
    add_to_body("#{whitespace}#{orig_line}")
  elsif user_action.next?
    raise AbortFile, "stopping to process the rest of the file"
 elsif user_action.replace_line?
    append_to_locale_hash(line_no, line_locale_hash)
    add_to_body("#{whitespace}#{text_to_replace}")
  elsif user_action.no_replace?
    append_to_locale_hash(line_no, DEFAULT_LINE_LOCALE_HASH)
    add_to_body("#{whitespace}#{orig_line}")
  end

  return should_be_replaced
end

#runObject



46
47
48
49
50
51
# File 'lib/haml-i18n-extractor/extraction/extractor.rb', line 46

def run
  assign_replacements
  validate_haml(@haml_writer.body)
  @yaml_tool.write_file
  @haml_writer.write_file
end