Class: Haml::I18n::Extractor
- Inherits:
-
Object
- Object
- Haml::I18n::Extractor
- Defined in:
- lib/haml-i18n-extractor/helpers.rb,
lib/haml-i18n-extractor/version.rb,
lib/haml-i18n-extractor/prompter.rb,
lib/haml-i18n-extractor/workflow.rb,
lib/haml-i18n-extractor/extractor.rb,
lib/haml-i18n-extractor/yaml_tool.rb,
lib/haml-i18n-extractor/haml_reader.rb,
lib/haml-i18n-extractor/haml_writer.rb,
lib/haml-i18n-extractor/text_finder.rb,
lib/haml-i18n-extractor/user_action.rb,
lib/haml-i18n-extractor/tagging_tool.rb,
lib/haml-i18n-extractor/text_replacer.rb,
lib/haml-i18n-extractor/cli.rb
Defined Under Namespace
Modules: Helpers Classes: AbortFile, CLI, HamlReader, HamlWriter, InvalidSyntax, NotADirectory, NotDefinedLineType, Prompter, TaggingTool, TextFinder, TextReplacer, UserAction, Workflow, YamlTool
Constant Summary collapse
- VERSION =
"0.2.1"- LINE_TYPES_ALL =
[:text, :not_text, :loud, :silent, :element]
- LINE_TYPES_ADD_EVAL =
[:text, :element]
- DEFAULT_LINE_LOCALE_HASH =
{ :modified_line => nil,:keyname => nil,:replaced_text => nil, :path => nil }
Instance Attribute Summary collapse
-
#haml_reader ⇒ Object
readonly
Returns the value of attribute haml_reader.
-
#haml_writer ⇒ Object
readonly
Returns the value of attribute haml_writer.
-
#locale_hash ⇒ Object
readonly
Returns the value of attribute locale_hash.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#yaml_tool ⇒ Object
readonly
Returns the value of attribute yaml_tool.
Class Method Summary collapse
Instance Method Summary collapse
- #assign_new_body ⇒ Object
- #assign_replacements ⇒ Object
- #assign_yaml ⇒ Object
-
#initialize(haml_path, opts = {}) ⇒ Extractor
constructor
A new instance of Extractor.
- #interactive? ⇒ Boolean
- #new_body ⇒ Object
-
#process_line(orig_line, line_no) ⇒ Object
this is the bulk of it: where we end up setting body info and locale_hash.
- #run ⇒ Object
Constructor Details
#initialize(haml_path, opts = {}) ⇒ Extractor
Returns a new instance of Extractor.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/haml-i18n-extractor/extractor.rb', line 22 def initialize(haml_path, opts = {}) = opts @type = [:type] @interactive = [: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 @yaml_tool.yaml_file = ([:yaml_file] || :en).to_sym @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 = {} end |
Instance Attribute Details
#haml_reader ⇒ Object (readonly)
Returns the value of attribute haml_reader.
17 18 19 |
# File 'lib/haml-i18n-extractor/extractor.rb', line 17 def haml_reader @haml_reader end |
#haml_writer ⇒ Object (readonly)
Returns the value of attribute haml_writer.
17 18 19 |
# File 'lib/haml-i18n-extractor/extractor.rb', line 17 def haml_writer @haml_writer end |
#locale_hash ⇒ Object (readonly)
Returns the value of attribute locale_hash.
18 19 20 |
# File 'lib/haml-i18n-extractor/extractor.rb', line 18 def locale_hash @locale_hash end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
18 19 20 |
# File 'lib/haml-i18n-extractor/extractor.rb', line 18 def type @type end |
#yaml_tool ⇒ Object (readonly)
Returns the value of attribute yaml_tool.
18 19 20 |
# File 'lib/haml-i18n-extractor/extractor.rb', line 18 def yaml_tool @yaml_tool end |
Class Method Details
.debug? ⇒ Boolean
5 6 7 |
# File 'lib/haml-i18n-extractor/extractor.rb', line 5 def self.debug? ENV['DEBUG'] end |
Instance Method Details
#assign_new_body ⇒ Object
46 47 48 |
# File 'lib/haml-i18n-extractor/extractor.rb', line 46 def assign_new_body @haml_writer.body = new_body end |
#assign_replacements ⇒ Object
54 55 56 57 |
# File 'lib/haml-i18n-extractor/extractor.rb', line 54 def assign_replacements assign_new_body assign_yaml end |
#assign_yaml ⇒ Object
50 51 52 |
# File 'lib/haml-i18n-extractor/extractor.rb', line 50 def assign_yaml @yaml_tool.locale_hash = @locale_hash end |
#interactive? ⇒ Boolean
105 106 107 |
# File 'lib/haml-i18n-extractor/extractor.rb', line 105 def interactive? !!@interactive end |
#new_body ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/haml-i18n-extractor/extractor.rb', line 59 def new_body begin @haml_reader.lines.each_with_index do |orig_line, line_no| @current_line_no = line_no process_line(orig_line,line_no) end rescue AbortFile @prompter.moving_to_next_file add_rest_of_file_to_body(@current_line_no) end @body.join("\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?
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/haml-i18n-extractor/extractor.rb', line 76 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) 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 |
#run ⇒ Object
39 40 41 42 43 44 |
# File 'lib/haml-i18n-extractor/extractor.rb', line 39 def run assign_replacements validate_haml(@haml_writer.body) @haml_writer.write_file @yaml_tool.write_file end |