Class: Haml::I18n::Extractor

Inherits:
Object
  • Object
show all
Defined in:
lib/haml-i18n-extractor/helpers.rb,
lib/haml-i18n-extractor/version.rb,
lib/haml-i18n-extractor/flow/cli.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/haml_parser.rb,
lib/haml-i18n-extractor/extraction/haml_reader.rb,
lib/haml-i18n-extractor/extraction/haml_writer.rb,
lib/haml-i18n-extractor/extraction/yaml_writer.rb,
lib/haml-i18n-extractor/extraction/tagging_writer.rb,
lib/haml-i18n-extractor/extraction/finder/text_finder.rb,
lib/haml-i18n-extractor/extraction/replacer/text_replacer.rb,
lib/haml-i18n-extractor/extraction/finder/exception_finder.rb,
lib/haml-i18n-extractor/extraction/replacer/replacer_result.rb,
lib/haml-i18n-extractor/extraction/replacer/interpolation_helper.rb

Defined Under Namespace

Modules: Helpers Classes: AbortFile, CLI, ExceptionFinder, HamlParser, HamlReader, HamlWriter, InterpolationHelper, InvalidSyntax, NotADirectory, NotDefinedLineType, Prompter, ReplacerResult, TaggingWriter, TextFinder, TextReplacer, UserAction, Workflow, YamlWriter

Constant Summary collapse

VERSION =
"0.5.9"
LINE_TYPES_ALL =
[:plain, :script, :silent_script, :haml_comment, :tag, :comment, :doctype, :filter, :root]
LINE_TYPES_ADD_EVAL =
[:plain, :tag, :script]

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.



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

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_writer = Haml::I18n::Extractor::YamlWriter.new(@options[:i18n_scope], @options[:yaml_file])
  @tagging_writer ||= Haml::I18n::Extractor::TaggingWriter.new
  # hold all the processed lines
  @body = []
  # holds a line_no => {info_about_line_replacemnts_or_not}
  @info_for_yaml = {}

  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

#info_for_yamlObject (readonly)

Returns the value of attribute info_for_yaml.



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

def info_for_yaml
  @info_for_yaml
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_writerObject (readonly)

Returns the value of attribute yaml_writer.



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

def yaml_writer
  @yaml_writer
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_EXTRACTOR']
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



51
52
53
# File 'lib/haml-i18n-extractor/extraction/extractor.rb', line 51

def assign_new_body
  @haml_writer.body = new_body
end

#assign_replacementsObject



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

def assign_replacements
  assign_new_body
  assign_yaml
end

#assign_yamlObject



55
56
57
# File 'lib/haml-i18n-extractor/extraction/extractor.rb', line 55

def assign_yaml
  @yaml_writer.info_for_yaml = @info_for_yaml
end

#interactive?Boolean

Returns:

  • (Boolean)


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

def interactive?
  !!@interactive
end

#new_bodyObject



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

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 info_for_yaml. not write, just set that info in memory in correspoding locations. refactor more?



82
83
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
112
# File 'lib/haml-i18n-extractor/extraction/extractor.rb', line 82

def process_line(orig_line, line_no)
  orig_line.chomp!
  orig_line, whitespace = handle_line_whitespace(orig_line)
  finder_result = finding_result(orig_line, line_no)
  replacer_result = replacement_result(orig_line, finder_result.match, finder_result.type, line_no)
  should_be_replaced = replacer_result.should_be_replaced
  text_to_replace = replacer_result.modified_line

  user_action = should_be_replaced ? user_action_yes : user_action_no

  if should_be_replaced
    if interactive?
      user_action = @prompter.ask_user(orig_line,text_to_replace)
    end
  end

  if user_action.tag?
    @tagging_writer.write(@haml_reader.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?
    add_to_yaml_info(line_no, replacer_result.info)
    add_to_body("#{whitespace}#{text_to_replace}")
  elsif user_action.no_replace?
    add_to_yaml_info(line_no, Haml::I18n::Extractor::ReplacerResult.new(nil,nil,nil,false,nil).info)
    add_to_body("#{whitespace}#{orig_line}")
  end

  return should_be_replaced
end

#runObject



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

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