PoParser 0.2.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a6d5eda39bf4fa5c541d80f5cdf99b2954417f13
4
- data.tar.gz: 8c9c0cfab8f937b70659d9a035f84f39123a9cc9
3
+ metadata.gz: 353e0d8587bb30e2c03053471cb8e81de7939893
4
+ data.tar.gz: db6dcd36fe50ad4d74ddd9c4de53a539e81fa73d
5
5
  SHA512:
6
- metadata.gz: b916c637294f7324bddeda83f23cefa0e200a920807a388eadddf63a1aa3d2f3c1cafaf37ba13c0e9f5565578ad65064d034a9ec571e79afcb4003f211eeedc5
7
- data.tar.gz: 6a27b643f92db3e4b4e1714e01c942298bbbdf7339a49a5e47ece71fe59c0cb1d4464240bdf0c2881be95eef4780ca7435a4239cd260fbdd88901358b131339c
6
+ metadata.gz: f4364ee022ee6f07667c1171446e803ac79873bee046cdb0647b053e8905f00fa4e8e7f19cd9e1bf58a3356581aa6605aa00b9da096a19bef574fb15ff8d7dbf
7
+ data.tar.gz: c254de7917c3d1270486248e59411c942c46a5e178a4d0d338fbe6c126db06ad3bdd3efb1a36fac014466189191a063f12b64e9a9057434b17c56dab28fbf751
data/.gitignore CHANGED
@@ -3,6 +3,8 @@
3
3
  .bundle
4
4
  .config
5
5
  .yardoc
6
+ .ruby-version
7
+ .gitignore
6
8
  Gemfile.lock
7
9
  InstalledFiles
8
10
  _yardoc
@@ -15,3 +17,4 @@ spec/reports
15
17
  test/tmp
16
18
  test/version_tmp
17
19
  tmp
20
+ tags
data/.travis.yml CHANGED
@@ -1,7 +1,7 @@
1
+ bundler_args: --without development
1
2
  language: ruby
2
3
  rvm:
3
4
  - 1.9.3
4
5
  - 2.0.0
5
6
  - 2.1.0
6
- - 2.1.0
7
7
  script: bundle exec rspec spec
data/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## Version 1.0.0
2
+
3
+ * add support for header entry
4
+ * `add_entry` renamed to `add` and returns `self`
5
+ * besides percentages, inspect of `po` now returns each entry count too
6
+ * `po.add` now raises error if you try to add an unknown label
7
+
1
8
  ## Version 0.2.1
2
9
 
3
10
  * Add search_in method.
data/Gemfile CHANGED
@@ -1,6 +1,16 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'coveralls', :require => false
3
+ group :test do
4
+ gem 'coveralls', :require => false
5
+ gem 'pry-byebug', :platforms => :ruby_20
6
+ gem 'rspec', [">= 2.14", "< 2.99"]
7
+ gem 'awesome_print'
8
+ end
9
+
10
+ group :development do
11
+ gem 'pry'
12
+ gem 'guard-rspec'
13
+ end
4
14
 
5
15
  # Specify your gem's dependencies in poparser.gemspec
6
16
  gemspec
data/README.md CHANGED
@@ -37,8 +37,8 @@ The `parse` method returns a `PO` object which contains all `Entries`:
37
37
  # get all entries
38
38
  po.entries # or .all alias
39
39
 
40
- # include cashed entries (started with "#~", these
41
- # entries are just kept by program for later use and are
40
+ # including cached entries (started with "#~", these
41
+ # entries are just kept by program for later use and are
42
42
  # not counted as active entries)
43
43
  po.entries(true)
44
44
 
@@ -68,14 +68,29 @@ new_entry = {
68
68
  msgstr: 'translated string'
69
69
  }
70
70
 
71
- po.add_entry(new_entry)
71
+ po.add(new_entry)
72
72
 
73
- # There's also an alias for add_entry
73
+ # There's also an alias for add `<<`
74
74
  po << new_entry
75
75
  ```
76
76
 
77
77
  You can pass an array of hashes to `new_entry` and it will be added to `PO` file.
78
78
 
79
+ Adding plural string is the same:
80
+
81
+ ```ruby
82
+ new_entry = {
83
+ translator_comment: 'comment',
84
+ refrence: 'refrence comment',
85
+ msgid_plural: 'untranslated',
86
+ 'msgstr[0]': 'translated string',
87
+ 'msgstr[1]': 'translated string'
88
+ }
89
+
90
+ ```
91
+
92
+ Note: currently `PoParser` won't warn you if you add a `msgstr[0]` without `msgid_plural`, any `msgid` at all or even if index numbers in `msgstr[0]` are not in any logical order.
93
+
79
94
  ### Entry
80
95
 
81
96
  Each entry can have following properties (for more information see [GNU PO file specification](https://www.gnu.org/software/gettext/manual/html_node/PO-Files.html)):
@@ -98,6 +113,8 @@ msgctxt
98
113
  The `PO` object contains many `Entry` objects. Number of methods are available to check state of the `Entry`:
99
114
 
100
115
  ```ruby
116
+ entry = po.entries[1]
117
+
101
118
  entry.untranslated? # or .incomplete? alias
102
119
  #=> false
103
120
  entry.translated? # or .complete? alias
@@ -113,7 +130,7 @@ You can get or edit each of property of the `Entry`:
113
130
  ```ruby
114
131
  entry.msgid.to_s
115
132
  #=> "This is an msgid that needs to get translated"
116
- entry.translate = "This entry is translated" # or msgstr= alias
133
+ entry.translate = "This entry is translated" # or `msgstr=` alias
117
134
  entry.msgstr.to_s
118
135
  #=> "This entry is translated"
119
136
  ```
@@ -167,10 +184,36 @@ po.path = 'example2.po'
167
184
  po.save_file
168
185
  ```
169
186
 
187
+ ## Header
188
+
189
+ The first entry of every PO file is reserved for header which represent license and general information about translators and the po file itself. For more information visit the [GNU website](https://www.gnu.org/software/gettext/manual/html_node/Header-Entry.html#Header-Entry).
190
+
191
+ Get header with `header`!:
192
+
193
+ ```ruby
194
+ po.header
195
+ ```
196
+
197
+ You can get and set following variables from `header`:
198
+
199
+ ```
200
+ pot_creation_date
201
+ po_revision_date
202
+ project_id
203
+ report_to
204
+ last_translator
205
+ team
206
+ language
207
+ charset
208
+ encoding
209
+ plural_forms
210
+ ```
211
+
170
212
  ##To-Do
171
213
 
172
214
  * Streaming support
173
- * Better error reporting
215
+ * Update header after changing/saving po
216
+ * add `before_save` and `after_save` callbacks
174
217
 
175
218
  ## Contributing
176
219
 
@@ -24,5 +24,9 @@ module PoParser
24
24
  def to_str
25
25
  @str
26
26
  end
27
+
28
+ def inspect
29
+ @str
30
+ end
27
31
  end
28
32
  end
@@ -16,4 +16,17 @@ module PoParser
16
16
  }
17
17
 
18
18
  LABELS = COMMENTS_LABELS.merge(ENTRIES_LABELS).keys
19
+
20
+ HEADER_LABELS = {
21
+ :pot_creation_date => "POT-Creation-Date",
22
+ :po_revision_date => "PO-Revision-Date",
23
+ :project_id => "Project-Id-Version",
24
+ :report_to => "Project-Id-Version",
25
+ :last_translator => "Last-Translator",
26
+ :team => "Language-Team",
27
+ :language => "Language",
28
+ :charset => "Content-Type",
29
+ :encoding => "Content-Transfer-Encoding",
30
+ :plural_forms => "Plural-Forms"
31
+ }
19
32
  end
@@ -9,6 +9,7 @@ module PoParser
9
9
 
10
10
  # Set passed arguments
11
11
  args.each do |name, value|
12
+ raise(ArgumentError, "Unknown label #{name}") if !valid_label? name
12
13
  set_instance_variable(name, value)
13
14
  end
14
15
 
@@ -21,14 +22,14 @@ module PoParser
21
22
 
22
23
  # If entry doesn't have any msgid, it's probably a cached entry that is
23
24
  # kept by the program for later use. These entries will usually start with: #~
24
- #
25
+ #
25
26
  # @return [Boolean]
26
27
  def cached?
27
28
  !@cached.nil?
28
29
  end
29
30
 
30
31
  # Checks if the entry is untraslated
31
- #
32
+ #
32
33
  # @return [Boolean]
33
34
  def untranslated?
34
35
  return false if cached? || fuzzy?
@@ -40,7 +41,7 @@ module PoParser
40
41
  alias_method :incomplete? , :untranslated?
41
42
 
42
43
  # Checks if the entry is translated
43
- #
44
+ #
44
45
  # @return [Boolean]
45
46
  def translated?
46
47
  return false if cached? || fuzzy?
@@ -49,14 +50,14 @@ module PoParser
49
50
  alias_method :complete? , :translated?
50
51
 
51
52
  # Checks if the entry is plural
52
- #
53
+ #
53
54
  # @return [Boolean]
54
55
  def plural?
55
56
  @msgid_plural != nil
56
57
  end
57
58
 
58
59
  # Checks if the entry is fuzzy
59
- #
60
+ #
60
61
  # @return [Boolean]
61
62
  def fuzzy?
62
63
  return false if cached?
@@ -113,6 +114,10 @@ module PoParser
113
114
  lines.join
114
115
  end
115
116
 
117
+ def inspect
118
+ to_s
119
+ end
120
+
116
121
  private
117
122
 
118
123
  def set_instance_variable(name, value)
@@ -156,5 +161,8 @@ module PoParser
156
161
  end
157
162
  end
158
163
 
164
+ def valid_label?(label)
165
+ !(label =~ /^msgstr\[[0-9]\]/).nil? || LABELS.include?(label)
166
+ end
159
167
  end
160
168
  end
@@ -0,0 +1,68 @@
1
+ module PoParser
2
+ class Header
3
+ attr_reader :entry, :original_configs
4
+ attr_accessor :comments, :pot_creation_date, :po_revision_date, :project_id,
5
+ :report_to, :last_translator, :team, :language, :charset,
6
+ :encoding, :plural_forms
7
+
8
+ def initialize(entry)
9
+ @entry = entry
10
+ @comments = entry.translator_comment.to_s
11
+ @original_configs = convert_msgstr_to_hash(entry.msgstr)
12
+
13
+ HEADER_LABELS.each do |k, v|
14
+ instance_variable_set "@#{k.to_s}".to_sym, @original_configs[v]
15
+ end
16
+ end
17
+
18
+ def configs
19
+ hash = {}
20
+ HEADER_LABELS.each do |k, v|
21
+ hash[v] = instance_variable_get "@#{k}".to_sym
22
+ end
23
+ @original_configs.merge(hash)
24
+ end
25
+
26
+ def to_h
27
+ @entry.to_h
28
+ end
29
+
30
+ def to_s
31
+ string = []
32
+ @comments.each do |comment|
33
+ string << "# #{comment}".strip
34
+ end
35
+ string << "msgid \"\"\nmsgstr \"\""
36
+ configs.each do |k, v|
37
+ string << "#{k}: #{v}\n".dump
38
+ end
39
+ string.join("\n")
40
+ end
41
+
42
+ private
43
+ def convert_msgstr_to_hash(msgstr)
44
+ options_array = msgstr.to_s.map do |options|
45
+ options.split(':', 2).each do |k|
46
+ k.strip!
47
+ k.chomp!
48
+ k.gsub!(/\\+n$/, '')
49
+ end
50
+ end
51
+ Hash[merge_to_previous_string(options_array)]
52
+ end
53
+
54
+ # Sometimes long lines are wrapped into new lines, this function
55
+ # join them back
56
+ #
57
+ # [['a', 'b'], ['c']] #=> [['a', 'bc']]
58
+ def merge_to_previous_string(array)
59
+ array.each_with_index do |key, index|
60
+ if key.length == 1
61
+ array[index -1][1] += key[0]
62
+ array.delete_at(index)
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+
@@ -33,6 +33,10 @@ module PoParser
33
33
  @str.is_a?(Array) ? @str.join : @str
34
34
  end
35
35
 
36
+ def inspect
37
+ @str
38
+ end
39
+
36
40
  private
37
41
  def remove_empty_line
38
42
  if @str.is_a? Array
data/lib/poparser/po.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  module PoParser
2
2
  # Po class keeps all entries of a Po file
3
- #
4
3
  class Po
5
4
  include Enumerable
5
+ attr_reader :header
6
6
  attr_accessor :path
7
7
 
8
8
  def initialize(args = {})
@@ -11,35 +11,34 @@ module PoParser
11
11
  end
12
12
 
13
13
  # add new entries to po file
14
- #
14
+ #
15
15
  # @example
16
- # entry = {
16
+ # entry = {
17
17
  # translator_comment: 'comment',
18
18
  # refrence: 'refrense comment',
19
19
  # flag: 'fuzzy',
20
- # msgstr: 'translatable string',
20
+ # msgid: 'translatable string',
21
21
  # msgstr: 'translation'
22
22
  # }
23
- # add_entry(entry)
24
- #
23
+ # add(entry)
24
+ #
25
25
  # @param entry [Hash, Array] a hash of entry contents or an array of hashes
26
- def add_entry(entry)
26
+ # @return [Po]
27
+ def add(entry)
27
28
  if entry.kind_of? Hash
28
- @entries << Entry.new(entry)
29
- @entries.last
29
+ import_hash(entry)
30
30
  elsif entry.kind_of? Array
31
- entry.each do |en|
32
- @entries << Entry.new(en)
33
- end
31
+ import_array(entry)
34
32
  else
35
33
  raise ArgumentError, 'Must be a hash or an array of hashes'
36
34
  end
35
+ self
37
36
  end
38
- alias_method :<<, :add_entry
37
+ alias_method :<<, :add
39
38
 
40
39
  # Returns an array of all entries in po file
41
- #
42
- # @param include_cached [Boolean] Whether include cached entries or not
40
+ #
41
+ # @param include_cached [Boolean] Whether include cached entries or not
43
42
  # @return [Array]
44
43
  def entries(include_cached=false)
45
44
  if include_cached
@@ -53,7 +52,7 @@ module PoParser
53
52
  alias_method :all, :entries
54
53
 
55
54
  # Finds all entries that are flaged as fuzzy
56
- #
55
+ #
57
56
  # @return [Array] an array of fuzzy entries
58
57
  def fuzzy
59
58
  find_all do |entry|
@@ -62,7 +61,7 @@ module PoParser
62
61
  end
63
62
 
64
63
  # Finds all entries that are untranslated
65
- #
64
+ #
66
65
  # @return [Array] an array of untranslated entries
67
66
  def untranslated
68
67
  find_all do |entry|
@@ -71,7 +70,7 @@ module PoParser
71
70
  end
72
71
 
73
72
  # Finds all entries that are translated
74
- #
73
+ #
75
74
  # @return [Array] an array of translated entries
76
75
  def translated
77
76
  find_all do |entry|
@@ -80,7 +79,7 @@ module PoParser
80
79
  end
81
80
 
82
81
  # Count of all entries without counting cached entries
83
- #
82
+ #
84
83
  # @return [String]
85
84
  def size
86
85
  entries.length
@@ -88,7 +87,7 @@ module PoParser
88
87
  alias_method :length, :size
89
88
 
90
89
  # Search for entries with provided string
91
- #
90
+ #
92
91
  # @param label [Symbol] One of the known LABELS
93
92
  # @param string [String] String to search for
94
93
  # @return [Array] Array of matched entries
@@ -104,7 +103,7 @@ module PoParser
104
103
  end
105
104
 
106
105
  # Shows statistics and status of the provided file in percentage.
107
- #
106
+ #
108
107
  # @return [Hash] a hash of translated, untranslated and fuzzy percentages
109
108
  def stats
110
109
  untranslated_size = untranslated.size
@@ -119,10 +118,11 @@ module PoParser
119
118
  end
120
119
 
121
120
  # Converts Po file to an hashes of entries
122
- #
121
+ #
123
122
  # @return [Array] array of hashes of entries
124
123
  def to_h
125
124
  array = []
125
+ array << @header.to_h if @header
126
126
  @entries.each do |entry|
127
127
  array << entry.to_h
128
128
  end
@@ -130,10 +130,13 @@ module PoParser
130
130
  end
131
131
 
132
132
  # Shows a String representation of the Po file
133
- #
133
+ #
134
134
  # @return [String]
135
135
  def to_s
136
136
  array = []
137
+ array << @header.to_s if @header
138
+ # add a blank line after header
139
+ array << ""
137
140
  @entries.each do |entry|
138
141
  array << entry.to_s
139
142
  end
@@ -155,16 +158,35 @@ module PoParser
155
158
  end
156
159
 
157
160
  def inspect
158
- "<#{self.class.name}, Translated: #{stats[:translated]}% Untranslated: #{stats[:untranslated]}% Fuzzy: #{stats[:fuzzy]}%>"
161
+ "<#{self.class.name}, Translated: #{translated.length}(#{stats[:translated]}%) Untranslated: #{untranslated.length}(#{stats[:untranslated]}%) Fuzzy: #{fuzzy.length}(#{stats[:fuzzy]}%)>"
159
162
  end
160
163
 
161
164
  private
162
165
  # calculates percentages based on total number of entries
163
- #
166
+ #
164
167
  # @param [Integer] number of entries
165
168
  # @return [Float] percentage of the provided entries
166
169
  def percentage(count)
167
170
  ((count.to_f / self.size) * 100).round(1)
168
171
  end
172
+
173
+ def import_hash(entry)
174
+ add_entry(entry)
175
+ end
176
+
177
+ def import_array(entry)
178
+ entry.each do |en|
179
+ add_entry(en)
180
+ end
181
+ end
182
+
183
+ def add_entry(entry)
184
+ if entry[:msgid] && entry[:msgid].length == 0
185
+ raise(RuntimeError, "Duplicate entry, header was already instantiated") if @header != nil
186
+ @header = Header.new(Entry.new(entry))
187
+ else
188
+ @entries << Entry.new(entry)
189
+ end
190
+ end
169
191
  end
170
192
  end
@@ -1,5 +1,3 @@
1
- require 'ap'
2
-
3
1
  module PoParser
4
2
  # Feed each block of PO file to Parser.
5
3
  class Tokenizer
@@ -1,3 +1,3 @@
1
1
  module PoParser
2
- VERSION = "0.2.2"
2
+ VERSION = "1.0.0"
3
3
  end
data/lib/poparser.rb CHANGED
@@ -8,6 +8,7 @@ require 'poparser/transformer'
8
8
  require 'poparser/tokenizer'
9
9
  require 'poparser/comment'
10
10
  require 'poparser/message'
11
+ require 'poparser/header'
11
12
  require 'poparser/entry'
12
13
  require 'poparser/po'
13
14
  require 'poparser/version'
data/poparser.gemspec CHANGED
@@ -22,10 +22,6 @@ Gem::Specification.new do |spec|
22
22
  spec.add_runtime_dependency "parslet", "~> 1.6"
23
23
 
24
24
  # Development deps
25
- spec.add_development_dependency "bundler", "~> 1.5"
25
+ spec.add_development_dependency "bundler", "~> 1.6"
26
26
  spec.add_development_dependency "rake"
27
- spec.add_development_dependency "rspec", [">= 2.14", "< 2.99"]
28
- spec.add_development_dependency "guard-rspec"
29
- spec.add_development_dependency "pry-debugger"
30
- spec.add_development_dependency "awesome_print"
31
27
  end
@@ -18,6 +18,12 @@ describe PoParser::Entry do
18
18
  end
19
19
  end
20
20
 
21
+ it 'should raise error if lable is unknown' do
22
+ expect {
23
+ PoParser::Entry.new({:msgstr => 'test', :blah_blah => 'error'})
24
+ }.to raise_error(ArgumentError, "Unknown label blah_blah")
25
+ end
26
+
21
27
  it 'should show a hash presentation of a entry' do
22
28
  @entry.msgid = 'string'
23
29
  @entry.msgstr = 'reshte'
@@ -0,0 +1,6 @@
1
+ # Arash Mousavi <mousavi.arash@gmail.com>, 2014.
2
+ #
3
+ msgid ""
4
+ msgstr ""
5
+ "Project-Id-Version: damned-lies master\n"
6
+ "Report-Msgid-Bugs-To: \n"
@@ -0,0 +1,13 @@
1
+ # Arash Mousavi <mousavi.arash@gmail.com>, 2014.
2
+ #
3
+ msgid ""
4
+ msgstr ""
5
+ "Project-Id-Version: damned-lies master\n"
6
+ "Report-Msgid-Bugs-To: \n"
7
+
8
+ # Arash Mousavi <mousavi.arash@gmail.com>, 2014.
9
+ #
10
+ msgid ""
11
+ msgstr ""
12
+ "Project-Id-Version: damned-lies master\n"
13
+ "Report-Msgid-Bugs-To: \n"
@@ -0,0 +1,44 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe PoParser::Header do
5
+ let(:entry) do
6
+ {translator_comment: ["Persian translation for gnome-shell-extensions.", "Copyright (C) 2011 Iranian Free Software Users Group (IFSUG.org) translation team.", "This file is distributed under the same license as the gnome-shell-extensions package.", "Arash Mousavi <mousavi.arash@gmail.com>, 2011, 2013, 2014.", ""],
7
+ msgstr: ["Project-Id-Version: gnome-shell-extensions gnome-3-0\\n", "Report-Msgid-Bugs-To: http://bugzilla.gnome.org/enter_bug.cgi?product=gnome-", "shell&keywords=I18N+L10N&component=extensions\\n", "POT-Creation-Date: 2014-08-28 07:40+0000\\n", "PO-Revision-Date: 2014-08-28 19:59+0430\\n", "Last-Translator: Arash Mousavi <mousavi.arash@gmail.com>\\n", "Language-Team: Persian <>\\n", "Language: fa_IR\\n", "MIME-Version: 1.0\\n", "Content-Type: text/plain; charset=UTF-8\\n", "Content-Transfer-Encoding: 8bit\\n", "X-Poedit-SourceCharset: utf-8\\n", "X-Generator: Gtranslator 2.91.6\\n", "Plural-Forms: nplurals=1; plural=0;\\n"]}
8
+ end
9
+
10
+ let(:comment) do
11
+ "Persian translation for gnome-shell-extensions.\nCopyright (C) 2011 Iranian Free Software Users Group (IFSUG.org) translation team.\nThis file is distributed under the same license as the gnome-shell-extensions package.\nArash Mousavi <mousavi.arash@gmail.com>, 2011, 2013, 2014.\n"
12
+ end
13
+
14
+ let(:labels) {
15
+ [
16
+ :pot_creation_date, :po_revision_date, :project_id,
17
+ :report_to, :last_translator, :team, :language, :charset,
18
+ :encoding, :plural_forms
19
+ ]
20
+ }
21
+
22
+ before {
23
+ @entry = PoParser::Entry.new(entry)
24
+ @header = PoParser::Header.new(@entry)
25
+ }
26
+
27
+ it 'should respond to labels' do
28
+ labels.each do |label|
29
+ @header.should respond_to label
30
+ end
31
+ end
32
+
33
+ it 'should convert configs to hash' do
34
+ expect(@header.original_configs).to eq(
35
+ {"Project-Id-Version"=>"gnome-shell-extensions gnome-3-0", "Report-Msgid-Bugs-To"=>"http://bugzilla.gnome.org/enter_bug.cgi?product=gnome-shell&keywords=I18N+L10N&component=extensions", "POT-Creation-Date"=>"2014-08-28 07:40+0000", "PO-Revision-Date"=>"2014-08-28 19:59+0430", "Last-Translator"=>"Arash Mousavi <mousavi.arash@gmail.com>", "Language-Team"=>"Persian <>", "Language"=>"fa_IR", "MIME-Version"=>"1.0", "Content-Type"=>"text/plain; charset=UTF-8", "Content-Transfer-Encoding"=>"8bit", "X-Poedit-SourceCharset"=>"utf-8", "X-Generator"=>"Gtranslator 2.91.6", "Plural-Forms"=>"nplurals=1; plural=0;"}
36
+ )
37
+ end
38
+
39
+ it 'always should show the updated hash of configs' do
40
+ @header.language = 'en_US'
41
+ expect(@header.configs['Language']).to eq('en_US')
42
+ end
43
+
44
+ end
@@ -2,7 +2,7 @@
2
2
  require 'spec_helper'
3
3
 
4
4
  describe PoParser::Po do
5
- let (:entry) do
5
+ let (:entry) do
6
6
  {
7
7
  translator_comment: 'comment',
8
8
  refrence: 'refrence comment',
@@ -10,19 +10,19 @@ describe PoParser::Po do
10
10
  msgstr: 'translated string'
11
11
  }
12
12
  end
13
-
13
+
14
14
  before(:each) do
15
15
  @po = PoParser::Po.new
16
16
  end
17
17
 
18
18
  it 'should be able to add an entry to Po' do
19
- # << is an alias for Po#add_entry
20
- expect(@po << entry).to be_a_kind_of PoParser::Entry
19
+ # << is an alias for Po#add
20
+ expect(@po << entry).to be_a_kind_of PoParser::Po
21
21
  end
22
22
 
23
23
  it 'should be able to add multiple entries' do
24
24
  entries = [entry, entry.dup]
25
- expect(@po << entries).to be_a_kind_of Array
25
+ expect(@po << entries).to be_a_kind_of PoParser::Po
26
26
  end
27
27
 
28
28
  it 'returns all fuzzy entries' do
@@ -61,7 +61,7 @@ describe PoParser::Po do
61
61
 
62
62
  it 'raises error if label is not valid' do
63
63
  expect{
64
- @po.search_in(:wrong, 'sth')
64
+ @po.search_in(:wrong, 'sth')
65
65
  }.to raise_error(ArgumentError, 'Unknown key: wrong')
66
66
  end
67
67
 
@@ -71,4 +71,30 @@ describe PoParser::Po do
71
71
  expect(result[0].msgid.str).to eq('Invalid action. Someone probably posted another action just before you.')
72
72
  end
73
73
  end
74
+
75
+ context 'Header' do
76
+ before do
77
+ path = Pathname.new('spec/poparser/fixtures/header.po').realpath
78
+ @po = PoParser::Tokenizer.new.extract_entries(path)
79
+ end
80
+
81
+ it 'should respond to header' do
82
+ expect(@po).to respond_to :header
83
+ end
84
+
85
+ it 'should recognize header' do
86
+ expect(@po.header).to be_a_kind_of PoParser::Header
87
+ end
88
+
89
+ it 'should have right content for header' do
90
+ expect(@po.header.comments).to eq(["Arash Mousavi <mousavi.arash@gmail.com>, 2014.", ""])
91
+ end
92
+
93
+ it 'throws error if there\'re two header string' do
94
+ path = Pathname.new('spec/poparser/fixtures/header_error.po').realpath
95
+ expect{
96
+ @po = PoParser::Tokenizer.new.extract_entries(path)
97
+ }.to raise_error(RuntimeError, "Duplicate entry, header was already instantiated")
98
+ end
99
+ end
74
100
  end
data/spec/spec_helper.rb CHANGED
@@ -4,9 +4,12 @@
4
4
  # loaded once.
5
5
  #
6
6
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+
7
8
  require 'poparser'
8
9
  require 'parslet/rig/rspec'
9
10
  require 'coveralls'
11
+ require 'awesome_print'
12
+
10
13
  Coveralls.wear!
11
14
 
12
15
  RSpec.configure do |config|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: PoParser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arash Mousavi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-15 00:00:00.000000000 Z
11
+ date: 2014-09-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parslet
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '1.5'
33
+ version: '1.6'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '1.5'
40
+ version: '1.6'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -52,68 +52,6 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
- - !ruby/object:Gem::Dependency
56
- name: rspec
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: '2.14'
62
- - - "<"
63
- - !ruby/object:Gem::Version
64
- version: '2.99'
65
- type: :development
66
- prerelease: false
67
- version_requirements: !ruby/object:Gem::Requirement
68
- requirements:
69
- - - ">="
70
- - !ruby/object:Gem::Version
71
- version: '2.14'
72
- - - "<"
73
- - !ruby/object:Gem::Version
74
- version: '2.99'
75
- - !ruby/object:Gem::Dependency
76
- name: guard-rspec
77
- requirement: !ruby/object:Gem::Requirement
78
- requirements:
79
- - - ">="
80
- - !ruby/object:Gem::Version
81
- version: '0'
82
- type: :development
83
- prerelease: false
84
- version_requirements: !ruby/object:Gem::Requirement
85
- requirements:
86
- - - ">="
87
- - !ruby/object:Gem::Version
88
- version: '0'
89
- - !ruby/object:Gem::Dependency
90
- name: pry-debugger
91
- requirement: !ruby/object:Gem::Requirement
92
- requirements:
93
- - - ">="
94
- - !ruby/object:Gem::Version
95
- version: '0'
96
- type: :development
97
- prerelease: false
98
- version_requirements: !ruby/object:Gem::Requirement
99
- requirements:
100
- - - ">="
101
- - !ruby/object:Gem::Version
102
- version: '0'
103
- - !ruby/object:Gem::Dependency
104
- name: awesome_print
105
- requirement: !ruby/object:Gem::Requirement
106
- requirements:
107
- - - ">="
108
- - !ruby/object:Gem::Version
109
- version: '0'
110
- type: :development
111
- prerelease: false
112
- version_requirements: !ruby/object:Gem::Requirement
113
- requirements:
114
- - - ">="
115
- - !ruby/object:Gem::Version
116
- version: '0'
117
55
  description: A PO file parser, editor and generator. PO files are translation files
118
56
  generated by GNU/Gettext tool.
119
57
  email:
@@ -135,6 +73,7 @@ files:
135
73
  - lib/poparser/comment.rb
136
74
  - lib/poparser/constants.rb
137
75
  - lib/poparser/entry.rb
76
+ - lib/poparser/header.rb
138
77
  - lib/poparser/message.rb
139
78
  - lib/poparser/parser.rb
140
79
  - lib/poparser/po.rb
@@ -144,10 +83,13 @@ files:
144
83
  - poparser.gemspec
145
84
  - spec/poparser/comment_spec.rb
146
85
  - spec/poparser/entry_spec.rb
86
+ - spec/poparser/fixtures/header.po
87
+ - spec/poparser/fixtures/header_error.po
147
88
  - spec/poparser/fixtures/multiline.po
148
89
  - spec/poparser/fixtures/plural.po
149
90
  - spec/poparser/fixtures/test.po
150
91
  - spec/poparser/fixtures/tokenizer.po
92
+ - spec/poparser/header_spec.rb
151
93
  - spec/poparser/message_spec.rb
152
94
  - spec/poparser/parser_spec.rb
153
95
  - spec/poparser/po_spec.rb
@@ -175,17 +117,20 @@ required_rubygems_version: !ruby/object:Gem::Requirement
175
117
  version: '0'
176
118
  requirements: []
177
119
  rubyforge_project:
178
- rubygems_version: 2.2.2
120
+ rubygems_version: 2.4.1
179
121
  signing_key:
180
122
  specification_version: 4
181
123
  summary: A PO file parser, editor and generator.
182
124
  test_files:
183
125
  - spec/poparser/comment_spec.rb
184
126
  - spec/poparser/entry_spec.rb
127
+ - spec/poparser/fixtures/header.po
128
+ - spec/poparser/fixtures/header_error.po
185
129
  - spec/poparser/fixtures/multiline.po
186
130
  - spec/poparser/fixtures/plural.po
187
131
  - spec/poparser/fixtures/test.po
188
132
  - spec/poparser/fixtures/tokenizer.po
133
+ - spec/poparser/header_spec.rb
189
134
  - spec/poparser/message_spec.rb
190
135
  - spec/poparser/parser_spec.rb
191
136
  - spec/poparser/po_spec.rb