twineCSV 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 06aec5e520959cf309ed6116df17259cb8c00651
4
+ data.tar.gz: da2186e5c69935e4185a4602f7d379f54cc5db75
5
+ SHA512:
6
+ metadata.gz: 38071afdb7e035abde7637154d40711ddff5049eac12c941290df47ad0a30989aab8fd749472afc3d9de659d6d4182928de332538022eecf6c68a009b61c1979
7
+ data.tar.gz: 3e0735c9a7ef92c6d86aa8f36aecacb4d172b952af4066619ff091f928617f099406f7f8484dc3736533c0ec208c3d8519944d771ad50e0492993ed2f67fdce3
data/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # TwineCSV
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'twineCSV'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install twineCSV
18
+
19
+ ## Usage
20
+
21
+ This micro gem converts twine formatted txt files to csv and vice versa. The reason for this is that the localisation information is often provided by external services, which do not want to deal with restricted file formats but rather want to work with Excel or similar programs. Thus this gem was created to create csv files from your twine files. After you get your finalized csv files back you can convert them to a txt file, with the proper twine formatting. To get more information about twine in general visit their [github page](https://github.com/mobiata/twine).
22
+
23
+ ### Convert to csv
24
+
25
+ ```
26
+ twineCSV totwine localisation.txt converted.csv
27
+ ```
28
+
29
+ You have to proide at least the input file. When omitting the output file the filename is created based on the inputs filename.
30
+
31
+ ### Convert to twine file
32
+
33
+ ```
34
+ twineCSV tocsv converted.txt localisation_new.txt
35
+ ```
36
+
37
+ You have to proide at least the input file. When omitting the output file the filename is created based on the inputs filename.
38
+
39
+ ### Help
40
+
41
+ ```
42
+ twineCSV help
43
+ ```
44
+
45
+ ## Development
46
+
47
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
48
+
49
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
50
+
51
+ ## Contributing
52
+
53
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/twineCSV. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
54
+
55
+
56
+ ## License
57
+
58
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
59
+
data/bin/twineCSV ADDED
@@ -0,0 +1,50 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'commander/import'
4
+ require_relative '../lib/twineCSV'
5
+
6
+ program :name, 'twineCSV'
7
+ program :version, '1.0.0'
8
+ program :description, 'Converts your twine formatted localisation file to CSV and vice versa'
9
+
10
+ command :totwine do |c|
11
+ c.syntax = 'twineCSV totwine input [output]'
12
+ c.summary = 'Converts a csv file into a twine file'
13
+ c.description = 'Converts a csv file into a twine file. You have to provide at least the input file as first parameter. If the output file is omitted the output file will be named after the input file but with a proper extension.'
14
+ c.example 'Converts a csv file into a twine file', 'twineCSV totwine converted.csv localisation.txt'
15
+
16
+ c.action do |args, options|
17
+ abort("You have to provide at least a filename") if args[0].nil?
18
+
19
+ csv = args[0]
20
+ twine = args[1].nil? ? "#{args[0].split(".")[0]}.txt" : args[1].dup
21
+ twine << ".txt" unless twine.split(".")[-1] == 'txt'
22
+
23
+ puts "Converting input file #{csv}"
24
+
25
+ TwineCSV::to_twine csv, twine
26
+
27
+ puts "Done. Your output file is #{twine}"
28
+ end
29
+ end
30
+
31
+ command :tocsv do |c|
32
+ c.syntax = 'twineCSV tocsv input [output]'
33
+ c.summary = 'Converts a twine file into csv'
34
+ c.description = 'Converts a twine file into csv. You have to provide at least the input file as first parameter. If the output file is omitted the output file will be named after the input file but with a proper extension.'
35
+ c.example 'Convert a twine file into a csv file', 'twineCSV tocsv localisation.txt converted.csv'
36
+ c.action do |args, options|
37
+ abort("You have to provide at least a filename") if args[0].nil?
38
+
39
+ twine = args[0]
40
+ csv = args[1].nil? ? "#{args[0].split(".")[0]}.csv" : args[1].dup
41
+ csv << ".csv" unless csv.split(".")[-1] == 'csv'
42
+
43
+ puts "Converting input file #{twine}"
44
+
45
+ TwineCSV::to_csv twine, csv
46
+
47
+ puts "Done. Your output file is #{csv}"
48
+ end
49
+ end
50
+
@@ -0,0 +1,3 @@
1
+ module TwineCSV
2
+ VERSION = "1.0.2"
3
+ end
data/lib/twineCSV.rb ADDED
@@ -0,0 +1,75 @@
1
+ require "twineCSV/version"
2
+ require 'rubygems'
3
+
4
+ module TwineCSV
5
+
6
+ def self.to_csv input, output
7
+ abort("The input file must not be nil") if input.nil? or output.nil?
8
+ content = File.open(input, 'r').read
9
+ lines = content.each_line.to_a.map { |line|
10
+ result = line.gsub("\n", '').strip
11
+ result[-1] == ";" ? result[0..-2] : result
12
+ }
13
+ dictionary = {}
14
+ langs = []
15
+ current_key = ''
16
+ current_section = ''
17
+
18
+ lines.reject { |line| line.length == 0 }.each { |line|
19
+ if line.start_with? '[['
20
+ current_section = line[2..-3]
21
+ puts current_section
22
+ dictionary[current_section] = {}
23
+ elsif line.start_with? '['
24
+ current_key = line[1..-2]
25
+ dictionary[current_section][current_key] = {}
26
+ elsif current_key.length > 0
27
+ lang, value = line.split("=").map(&:strip)
28
+ langs << lang
29
+ dictionary[current_section][current_key][lang] = value || ''
30
+ end
31
+ }
32
+
33
+ File.open(output, 'wb+') { |f|
34
+
35
+ f << 'section;key;' << langs.uniq.join(';') << "\n"
36
+
37
+ dictionary.each { |k, v|
38
+ v.each { |k2, v2|
39
+ vlangs = langs.uniq.map(&:downcase).map { |lang| v2[lang] }
40
+ f << "#{k};#{k2};#{vlangs.join(";")}" << "\n"
41
+ }
42
+ }
43
+ }
44
+
45
+ end
46
+
47
+ def self.to_twine input, output
48
+ abort("The input file must not be nil") if input.nil? or output.nil?
49
+
50
+ content = File.open(input, 'r').read
51
+ lines = content.each_line.to_a.map { |line| line.gsub("\n", '').strip }
52
+ current_section = ''
53
+ result = []
54
+ langs = lines[0].split(';')[2..-1]
55
+
56
+ lines[1..-1].each { |line|
57
+ values = "#{line} ".split(";")
58
+ old_section = current_section
59
+ current_section = values.first
60
+
61
+ if current_section != old_section
62
+ result << "#{result.empty? ? '' : "\n"}[[#{current_section}]]"
63
+ result << " [#{values[1]}]" << values[2..-1].map.with_index { |value, i| " #{langs[i].downcase} = #{value.strip}" unless langs[i].nil? }
64
+ else
65
+ result << "\n [#{values[1]}]" << values[2..-1].map.with_index { |value, i| " #{langs[i].downcase} = #{value.strip}" unless langs[i].nil? }
66
+ end
67
+ }
68
+
69
+ File.open(output, 'wb+') { |f|
70
+ f << result.join("\n")
71
+ }
72
+
73
+ end
74
+
75
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'twineCSV'
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe TwineCSV do
4
+ it 'has a version number' do
5
+ expect(TwineCSV::VERSION).not_to be nil
6
+ end
7
+
8
+ it 'does something useful' do
9
+ expect(false).to eq(true)
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: twineCSV
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Stefan Neidig
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-01-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: commander
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.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: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: With twineCSV you can convert your localisation files to csv, so that
70
+ others can edit them via Excel. After exporting it back to csv you can convert it
71
+ to the twine format again.
72
+ email:
73
+ - s.neidig@appcom-interactive.de
74
+ executables:
75
+ - twineCSV
76
+ extensions: []
77
+ extra_rdoc_files: []
78
+ files:
79
+ - README.md
80
+ - bin/twineCSV
81
+ - lib/twineCSV.rb
82
+ - lib/twineCSV/version.rb
83
+ - spec/spec_helper.rb
84
+ - spec/twineCSV_spec.rb
85
+ homepage: http://www.appcom-interctive.de
86
+ licenses:
87
+ - MIT
88
+ metadata:
89
+ allowed_push_host: https://rubygems.org
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.5.1
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: twineCSV converts your twine localisation file to CSV and vice versa.
110
+ test_files:
111
+ - spec/spec_helper.rb
112
+ - spec/twineCSV_spec.rb