Class: PoToJson

Inherits:
Object
  • Object
show all
Defined in:
lib/po_to_json/version.rb,
lib/po_to_json.rb

Overview

Copyright © 2012-2015 Dropmysite.com <dropmyemail.com> Copyright © 2015 Webhippie <www.webhippie.de>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Defined Under Namespace

Classes: Version

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(files, glue = "|") ⇒ PoToJson

Returns a new instance of PoToJson.



49
50
51
52
# File 'lib/po_to_json.rb', line 49

def initialize(files, glue = "|")
  @files = files
  @glue = glue
end

Instance Attribute Details

#buffer=(value) ⇒ Object

Sets the attribute buffer

Parameters:

  • value

    the value to set the attribute buffer to.



45
46
47
# File 'lib/po_to_json.rb', line 45

def buffer=(value)
  @buffer = value
end

#errors=(value) ⇒ Object

Sets the attribute errors

Parameters:

  • value

    the value to set the attribute errors to.



43
44
45
# File 'lib/po_to_json.rb', line 43

def errors=(value)
  @errors = value
end

#filesObject

Returns the value of attribute files.



40
41
42
# File 'lib/po_to_json.rb', line 40

def files
  @files
end

#glueObject

Returns the value of attribute glue.



41
42
43
# File 'lib/po_to_json.rb', line 41

def glue
  @glue
end

#lastkey=(value) ⇒ Object

Sets the attribute lastkey

Parameters:

  • value

    the value to set the attribute lastkey to.



46
47
48
# File 'lib/po_to_json.rb', line 46

def lastkey=(value)
  @lastkey = value
end

#optionsObject

Returns the value of attribute options.



42
43
44
# File 'lib/po_to_json.rb', line 42

def options
  @options
end

#trans=(value) ⇒ Object

Sets the attribute trans

Parameters:

  • value

    the value to set the attribute trans to.



47
48
49
# File 'lib/po_to_json.rb', line 47

def trans=(value)
  @trans = value
end

#values=(value) ⇒ Object

Sets the attribute values

Parameters:

  • value

    the value to set the attribute values to.



44
45
46
# File 'lib/po_to_json.rb', line 44

def values=(value)
  @values = value
end

Instance Method Details

#flush_bufferObject



89
90
91
92
93
94
95
96
# File 'lib/po_to_json.rb', line 89

def flush_buffer
  return unless buffer[:msgid]

  build_trans
  assign_trans

  reset_buffer
end

#generate_for_jed(language, overwrite = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/po_to_json.rb', line 54

def generate_for_jed(language, overwrite = {})
  @options = parse_options(overwrite.merge(language: language))
  @parsed ||= inject_meta(parse_document)

  generated = build_json_for(build_jed_for(@parsed))

  [
    "var #{@options[:variable]} = #{@options[:variable]} || {};",
    "#{@options[:variable]}['#{@options[:language]}'] = #{generated};"
  ].join(" ")
end

#generate_for_json(language, overwrite = {}) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/po_to_json.rb', line 66

def generate_for_json(language, overwrite = {})
  @options = parse_options(overwrite.merge(language: language))
  @parsed ||= inject_meta(parse_document)

  generated = build_json_for(build_json_for(@parsed))

  fail "Not implemented yet, current value is #{generated}!"
end

#parse_documentObject



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/po_to_json.rb', line 75

def parse_document
  reset_buffer
  reset_result

  File.foreach(files) do |line|
    matches_values_for(line.chomp)
  end

  flush_buffer
  parse_header

  values
end

#parse_headerObject



98
99
100
101
102
103
104
105
106
107
# File 'lib/po_to_json.rb', line 98

def parse_header
  return if reject_header

  values[""][1].split("\\n").each do |line|
    next if line.empty?
    build_header_for(line)
  end

  values[""] = headers
end

#reject_headerObject



109
110
111
112
113
114
115
116
# File 'lib/po_to_json.rb', line 109

def reject_header
  if values[""].nil? || values[""][1].nil?
    values[""] = {}
    true
  else
    false
  end
end