jgdavey-movable_erb 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,3 @@
1
+ v0.2.0 Complete rework from ground up. Old API is deprecated.
2
+
3
+ v0.1.3 First working version on Github
@@ -1,31 +1,5 @@
1
- h1. Movable Erb
2
- by Joshua Davey
3
- http://github.com/jgdavey/movable_erb
4
-
5
- h2. DESCRIPTION:
6
-
7
- A simple CSV to MTImport conversion utility
8
-
9
- h2. FEATURES/PROBLEMS:
10
-
11
- * Commandline access
12
-
13
- h2. SYNOPSIS:
14
-
15
- @movable_erb [options] yourfile.csv@
16
-
17
- h2. REQUIREMENTS:
18
-
19
- * fastercsv gem
20
-
21
- h2. INSTALL:
22
-
23
- @sudo gem install movable_erb@
24
- (PENDING release)
25
-
26
- h2. LICENSE:
27
-
28
- (The MIT License)
1
+ The MIT License
2
+ ---------------
29
3
 
30
4
  Copyright (c) 2009
31
5
 
data/Manifest ADDED
@@ -0,0 +1,20 @@
1
+ CHANGELOG
2
+ LICENSE
3
+ Manifest
4
+ README.md
5
+ Rakefile
6
+ bin/movable_erb
7
+ cucumber.yml
8
+ features/csv.feature
9
+ features/step_definitions/csv_steps.rb
10
+ features/step_definitions/tmp.csv
11
+ features/support/env.rb
12
+ lib/movable_erb.rb
13
+ lib/templates/mtimport.erb
14
+ spec/csv_spec.rb
15
+ spec/fixtures/advanced.csv
16
+ spec/fixtures/example.csv
17
+ spec/fixtures/template.erb
18
+ spec/spec.opts
19
+ spec/spec_helper.rb
20
+ tasks/rspec.rake
data/README.md ADDED
@@ -0,0 +1,57 @@
1
+ Movable Erb
2
+ ===========
3
+
4
+ by Joshua Davey
5
+ http://github.com/jgdavey/movable_erb
6
+
7
+ Description
8
+ -----------
9
+
10
+ A simple CSV to MTImport conversion utility.
11
+ Useful for mapping contacts data into individual entries in Movable Type.
12
+
13
+ MTImport format is simple enough that most blogging platforms can import it, so this utility could ease the pain of moving some older data structure in CSV format into a blog platform.
14
+
15
+ Requirements
16
+ ------------
17
+
18
+ * fastercsv gem
19
+
20
+ Installation
21
+ ------------
22
+
23
+ [sudo] gem install jgdavey-movable_erb
24
+
25
+
26
+ Usage
27
+ -----
28
+
29
+ After installing the gem, just run
30
+
31
+ movable_erb [options] yourfile.csv
32
+
33
+
34
+
35
+ The MIT License
36
+ ---------------
37
+
38
+ Copyright (c) 2009
39
+
40
+ Permission is hereby granted, free of charge, to any person obtaining
41
+ a copy of this software and associated documentation files (the
42
+ 'Software'), to deal in the Software without restriction, including
43
+ without limitation the rights to use, copy, modify, merge, publish,
44
+ distribute, sublicense, and/or sell copies of the Software, and to
45
+ permit persons to whom the Software is furnished to do so, subject to
46
+ the following conditions:
47
+
48
+ The above copyright notice and this permission notice shall be
49
+ included in all copies or substantial portions of the Software.
50
+
51
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
52
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
53
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
54
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
55
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
56
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
57
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile CHANGED
@@ -1,40 +1,17 @@
1
- # Look in the tasks/setup.rb file for the various options that can be
2
- # configured in this Rakefile. The .rake files in the tasks directory
3
- # are where the options are used.
4
-
5
- begin
6
- require 'bones'
7
- Bones.setup
8
- rescue LoadError
9
- begin
10
- load 'tasks/setup.rb'
11
- rescue LoadError
12
- raise RuntimeError, '### please install the "bones" gem ###'
13
- end
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('movable_erb') do |p|
6
+ p.description = "A General-purpose CSV to ERB template formatter"
7
+ p.url = "http://github.com/jgdavey/movable_erb"
8
+ p.author = "Joshua Davey"
9
+ p.email = "josh@joshuadavey.com"
10
+ p.ignore_pattern = ["tmp/*", "script/*", "coverage/*"]
11
+ p.development_dependencies = ['rspec']
12
+ p.executable_pattern = ["bin/*"]
13
+ p.runtime_dependencies = ["fastercsv", "trollop"]
14
14
  end
15
15
 
16
- ensure_in_path 'lib'
17
- require 'movable_erb'
18
-
19
- task :default => 'spec:run'
20
-
21
- PROJ.name = 'movable_erb'
22
- PROJ.authors = 'Joshua Davey'
23
- PROJ.email = 'josh@joshuadavey.com'
24
- PROJ.summary = 'A simple CSV to MTImport conversion utility'
25
- PROJ.description = 'Usage: movable_erb [options]'
26
- PROJ.url = 'http://github.com/jgdavey/movable_erb/'
27
- PROJ.version = MovableErb::VERSION
28
- PROJ.rubyforge.name = 'movable_erb'
29
-
30
- PROJ.gem.dependencies = ['fastercsv']
31
-
32
- PROJ.readme_file = 'README.textile'
33
- PROJ.exclude << %w(\.git ^pkg tmp$)
34
-
35
- PROJ.notes.exclude = %w(^README\.txt$ ^data/)
36
- PROJ.rdoc.exclude = %w(^README\.txt$ ^data/)
37
-
38
- PROJ.spec.opts << '--color'
16
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
39
17
 
40
- # EOF
data/bin/movable_erb CHANGED
@@ -1,66 +1,37 @@
1
1
  #!/usr/bin/env ruby
2
- require 'optparse'
3
- require 'ostruct'
4
2
  require 'pp'
3
+ require 'rubygems'
4
+ require 'trollop'
5
5
 
6
- require File.expand_path(
7
- File.join(File.dirname(__FILE__), %w[.. lib movable_erb]))
6
+ require File.expand_path(File.join(File.dirname(__FILE__), %w[.. lib movable_erb]))
8
7
 
9
- class MovableErbCLI
10
- def self.parse(args)
11
- # The options specified on the command line will be collected in *options*.
12
- # We set default values here.
13
- options = OpenStruct.new
14
- options.template = nil
15
- options.file = ''
8
+ DEFAULT_TEMPLATE = File.expand_path(File.join(File.dirname(__FILE__), %w[.. lib templates mtimport.erb]))
16
9
 
17
- opts = OptionParser.new do |opts|
18
- opts.banner = "Usage: movable_erb [options]"
19
-
20
- opts.separator ""
21
- opts.separator "It is often useful to pipe the output from STDOUT to a file"
22
- opts.separator "e.g. # movable_erb -f yourfile.csv > import.txt"
23
-
24
- opts.separator ""
25
- opts.separator "Specific options:"
10
+ opts = Trollop::options do
26
11
 
27
- opts.on("-f", "--file FILE", "CSV file to parse") do |f|
28
- options.file = f
29
- end
30
-
31
- opts.on("-t", "--template TEMPLATE", "Template to use (default is MTImport)") do |t|
32
- options.template = t
33
- end
12
+ version "MovableErb v#{MovableErb::VERSION} (c) 2009 Joshua Davey"
13
+ banner <<-EOS
14
+ Usage:
34
15
 
35
- opts.separator ""
36
- opts.separator "Common options:"
16
+ movable_erb [options] <filenames.csv>
37
17
 
38
- # No argument, shows at tail. This will print an options summary.
39
- # Try it and see!
40
- opts.on_tail("-h", "--help", "Show this message") do
41
- puts opts
42
- exit
43
- end
18
+ Options:
44
19
 
45
- # Another typical switch to print the version.
46
- opts.on_tail("--version", "Show version") do
47
- puts MovableErb::VERSION
48
- exit
49
- end
50
- end
20
+ EOS
51
21
 
52
- opts.parse!(args)
53
- if options.file == ''
54
- opts
55
- else
56
- mtnew = MovableErb::MTImport.new(:csv => {:file => options.file,:template => options.template })
57
- mtnew.setup_column_nums
58
- mtnew.render_with_template
59
- end
60
- end # parse()
61
- end
22
+ # Options
23
+ opt :separator, "Separator between records", :default => "", :short => '-s'
24
+ opt :template, "path to ERB template", :default => DEFAULT_TEMPLATE, :short => '-t'
25
+
26
+ # Show the help screen when no file given
27
+ educate if ARGV.empty?
62
28
 
63
- result = MovableErbCLI.parse(ARGV)
64
- puts result
29
+ # Only continue if the CSV file is found
30
+ Trollop::die "That file could not be found" unless File.exist?(ARGV.first)
31
+ end
65
32
 
66
- # EOF
33
+ # Paths for CSV files are left in ARGV
34
+ ARGV.each do |csv|
35
+ opts.merge!(:csv => csv)
36
+ puts MovableErb.new(opts).convert
37
+ end
data/cucumber.yml ADDED
@@ -0,0 +1 @@
1
+ default: --tags ~wip features
@@ -0,0 +1,40 @@
1
+ Feature: CSV Conversion
2
+ In order to convert my list of contacts to blog format
3
+ As a Command-line user
4
+ I want to use commandline utility to convert CSV files to MTImport files
5
+
6
+ Scenario: A short CSV of contacts
7
+ Given I am on the commandline
8
+ When I invoke the utility with the following CSV file:
9
+ """
10
+ Title,Body,Body
11
+ John,773-123-1234,john@example.com
12
+ Abigail,,abby@example.com
13
+ Bernard,903-294-3921,
14
+ """
15
+ Then I should see the following output:
16
+ """
17
+ TITLE: John
18
+ -----
19
+ BODY:
20
+
21
+ 773-123-1234
22
+ john@example.com
23
+
24
+ --------
25
+ TITLE: Abigail
26
+ -----
27
+ BODY:
28
+
29
+ abby@example.com
30
+
31
+ --------
32
+ TITLE: Bernard
33
+ -----
34
+ BODY:
35
+
36
+ 903-294-3921
37
+
38
+ --------
39
+
40
+ """
@@ -0,0 +1,14 @@
1
+ Given /^I am on the commandline$/ do
2
+ # dummy step
3
+ end
4
+
5
+ When /^I invoke the utility with the following CSV file:?$/ do |string|
6
+ File.open(File.dirname(__FILE__) + '/tmp.csv', 'w') {|f| f.write(string) }
7
+ puts FasterCSV.parse(string).inspect
8
+ @erb = MovableErb.new({:csv => File.dirname(__FILE__) + '/tmp.csv', :separator => ''})
9
+ end
10
+
11
+ Then /^I should see the following output:?$/ do |string|
12
+ puts @erb.csv.to_hashes.inspect
13
+ @erb.convert.should == string
14
+ end
@@ -0,0 +1,4 @@
1
+ Title,Body,Body
2
+ John,773-123-1234,john@example.com
3
+ Abigail,,abby@example.com
4
+ Bernard,903-294-3921,
@@ -0,0 +1,20 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+
4
+ require File.expand_path(File.join(File.dirname(__FILE__), %w[.. .. lib movable_erb]))
5
+
6
+ require "mocha"
7
+
8
+ World(Mocha::API)
9
+
10
+ Before do
11
+ mocha_setup
12
+ end
13
+
14
+ After do
15
+ begin
16
+ mocha_verify
17
+ ensure
18
+ mocha_teardown
19
+ end
20
+ end
data/lib/movable_erb.rb CHANGED
@@ -1,49 +1,92 @@
1
+ class MovableErb
2
+ VERSION = "0.2.0"
3
+ attr_accessor :csv, :erb, :separator
1
4
 
2
- module MovableErb
5
+ DEFAULT_TEMPLATE = File.expand_path(File.dirname(__FILE__) + '/templates/mtimport.erb')
3
6
 
4
- # :stopdoc:
5
- VERSION = '0.1.3'
6
- LIBPATH = ::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
7
- PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
8
- # :startdoc:
7
+ def initialize(options = {})
8
+ @erb = MovableErb::Erb.setup do |erb|
9
+ erb.template = options[:template] || DEFAULT_TEMPLATE
10
+ end
11
+ if options[:csv]
12
+ @csv = MovableErb::CSV.setup do |csv|
13
+ csv.filename = options[:csv]
14
+ end
15
+ end
16
+ @separator = options[:separator] || ""
17
+ end
9
18
 
10
- # Returns the version string for the library.
11
- #
12
- def self.version
13
- VERSION
19
+ def convert
20
+ @results = []
21
+ csv.parse!
22
+ csv.hashes.each do |hash_data|
23
+ erb.data = hash_data
24
+ @results << erb.build!
25
+ end
26
+ @results.join(separator)
14
27
  end
28
+ end
29
+
30
+ class MovableErb::CSV
31
+ require 'fastercsv'
32
+
33
+ attr_accessor :filename, :hashes
15
34
 
16
- # Returns the library path for the module. If any arguments are given,
17
- # they will be joined to the end of the libray path using
18
- # <tt>File.join</tt>.
19
- #
20
- def self.libpath( *args )
21
- args.empty? ? LIBPATH : ::File.join(LIBPATH, args.flatten)
35
+ def setup(&block)
36
+ yield self
37
+ parse! if @filename
38
+ self
22
39
  end
23
40
 
24
- # Returns the lpath for the module. If any arguments are given,
25
- # they will be joined to the end of the path using
26
- # <tt>File.join</tt>.
27
- #
28
- def self.path( *args )
29
- args.empty? ? PATH : ::File.join(PATH, args.flatten)
41
+ def self.setup
42
+ csv = self.new
43
+ yield csv
44
+ csv.parse!
30
45
  end
31
46
 
32
- # Utility method used to require all files ending in .rb that lie in the
33
- # directory below this file that has the same name as the filename passed
34
- # in. Optionally, a specific _directory_ name can be passed in such that
35
- # the _filename_ does not have to be equivalent to the directory.
36
- #
37
- def self.require_all_libs_relative_to( fname, dir = nil )
38
- dir ||= ::File.basename(fname, '.*')
39
- search_me = ::File.expand_path(
40
- ::File.join(::File.dirname(fname), dir, '**', '*.rb'))
47
+ def parse!
48
+ @hashes = self.to_hashes
49
+ self
50
+ end
41
51
 
42
- Dir.glob(search_me).sort.each {|rb| require rb}
52
+ def to_hashes
53
+ array_of_arrays = FasterCSV.read(filename)
54
+ headers = array_of_arrays.shift
55
+ headers.each { |h| h.downcase! && h.gsub!(/\s/,"_") } if headers
56
+ hashes = Array.new(array_of_arrays.length) { Hash.new }
57
+ array_of_arrays.each_with_index do |row,i|
58
+ headers.each_with_index do |header, j|
59
+ unless row[j].nil?
60
+ hashes[i][header] = [] if hashes[i][header].nil?
61
+ hashes[i][header] << row[j]
62
+ end
63
+ end
64
+ end
65
+ hashes
43
66
  end
67
+ end
68
+
69
+ class MovableErb::Erb
70
+ require 'erb'
44
71
 
45
- end # module MovableErb
72
+ attr_accessor :template, :parsed_string, :data
46
73
 
47
- MovableErb.require_all_libs_relative_to(__FILE__)
74
+ def self.setup
75
+ erb = self.new
76
+ yield erb
77
+ erb
78
+ end
48
79
 
49
- # EOF
80
+ def template=(template_file)
81
+ @template = File.read template_file
82
+ end
83
+
84
+ def setup
85
+ yield self
86
+ end
87
+
88
+ def build!
89
+ erb = ERB.new(template, nil, '<>')
90
+ @parsed_string = erb.result(binding) if erb
91
+ end
92
+ end
@@ -0,0 +1,22 @@
1
+ TITLE: <%= data['title'].join(', ') if data['title'] %>
2
+ <% if data['author'] %>
3
+ AUTHOR: <%= data['author'].join(', ') %>
4
+ <% end %>
5
+ <% data['category'] && data['category'].each do |cat| %>
6
+ CATEGORY: <%= cat %>
7
+ <% end %>
8
+ -----
9
+ BODY:
10
+
11
+ <%= data['body'].join("\n") if data['body'] %>
12
+
13
+ <% if data['extended_body'] %>
14
+
15
+ -----
16
+ EXTENDED BODY:
17
+
18
+ <%= data['extended_body'] %>
19
+
20
+ <% end %>
21
+
22
+ --------
data/movable_erb.gemspec CHANGED
@@ -2,25 +2,23 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{movable_erb}
5
- s.version = "0.1.3"
5
+ s.version = "0.2.0"
6
6
 
7
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Joshua Davey"]
9
- s.date = %q{2009-04-18}
9
+ s.date = %q{2009-09-26}
10
10
  s.default_executable = %q{movable_erb}
11
- s.description = %q{Usage: movable_erb [options]}
11
+ s.description = %q{A General-purpose CSV to ERB template formatter}
12
12
  s.email = %q{josh@joshuadavey.com}
13
13
  s.executables = ["movable_erb"]
14
- s.extra_rdoc_files = ["History.txt", "bin/movable_erb", "lib/movable_erb/templates/default.erb"]
15
- s.files = ["History.txt", "README.textile", "Rakefile", "bin/movable_erb", "lib/movable_erb.rb", "lib/movable_erb/csv.rb", "lib/movable_erb/mtimport.rb", "lib/movable_erb/templates/default.erb", "movable_erb.gemspec", "spec/csv_spec.rb", "spec/fixtures/example.csv", "spec/movable_erb_spec.rb", "spec/mtimport_spec.rb", "spec/spec_helper.rb", "test/test_movable_erb.rb"]
16
- s.has_rdoc = true
17
- s.homepage = %q{http://github.com/jgdavey/movable_erb/}
18
- s.rdoc_options = ["--main", "README.textile"]
14
+ s.extra_rdoc_files = ["CHANGELOG", "LICENSE", "README.md", "bin/movable_erb", "lib/movable_erb.rb", "lib/templates/mtimport.erb", "tasks/rspec.rake"]
15
+ s.files = ["CHANGELOG", "LICENSE", "README.md", "Rakefile", "bin/movable_erb", "cucumber.yml", "features/csv.feature", "features/step_definitions/csv_steps.rb", "features/step_definitions/tmp.csv", "features/support/env.rb", "lib/movable_erb.rb", "lib/templates/mtimport.erb", "spec/csv_spec.rb", "spec/fixtures/advanced.csv", "spec/fixtures/example.csv", "spec/fixtures/template.erb", "spec/spec.opts", "spec/spec_helper.rb", "tasks/rspec.rake", "Manifest", "movable_erb.gemspec"]
16
+ s.homepage = %q{http://github.com/jgdavey/movable_erb}
17
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Movable_erb", "--main", "README.md"]
19
18
  s.require_paths = ["lib"]
20
19
  s.rubyforge_project = %q{movable_erb}
21
- s.rubygems_version = %q{1.3.2}
22
- s.summary = %q{A simple CSV to MTImport conversion utility}
23
- s.test_files = ["test/test_movable_erb.rb"]
20
+ s.rubygems_version = %q{1.3.5}
21
+ s.summary = %q{A General-purpose CSV to ERB template formatter}
24
22
 
25
23
  if s.respond_to? :specification_version then
26
24
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
@@ -28,13 +26,16 @@ Gem::Specification.new do |s|
28
26
 
29
27
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
30
28
  s.add_runtime_dependency(%q<fastercsv>, [">= 0"])
31
- s.add_development_dependency(%q<bones>, [">= 2.5.0"])
29
+ s.add_runtime_dependency(%q<trollop>, [">= 0"])
30
+ s.add_development_dependency(%q<rspec>, [">= 0"])
32
31
  else
33
32
  s.add_dependency(%q<fastercsv>, [">= 0"])
34
- s.add_dependency(%q<bones>, [">= 2.5.0"])
33
+ s.add_dependency(%q<trollop>, [">= 0"])
34
+ s.add_dependency(%q<rspec>, [">= 0"])
35
35
  end
36
36
  else
37
37
  s.add_dependency(%q<fastercsv>, [">= 0"])
38
- s.add_dependency(%q<bones>, [">= 2.5.0"])
38
+ s.add_dependency(%q<trollop>, [">= 0"])
39
+ s.add_dependency(%q<rspec>, [">= 0"])
39
40
  end
40
41
  end