Skip to content

Instantly share code, notes, and snippets.

@autotelik
Created October 18, 2009 12:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save autotelik/212645 to your computer and use it in GitHub Desktop.
Save autotelik/212645 to your computer and use it in GitHub Desktop.
p = Product.seed( :name ) do |s|
s.name = <%= @name %>
s.available_on = ''
s.meta_keywords = ['training', 'training']
s.meta_description = ""
s.description = '<%= @description %>'
s.price = 0.00
s.sku = '<%= @sku %>'
end
# Author:: Tom Statter
# License:: MIT ?
#
# Simple wrapper around MS Word
# NOTES ON INVESTIGATING OLE METHODS in irb
#
# visible = @word_app.ole_method_help( 'Visible' ) # Get a Method Object
# log( visible.return_type_detail.to_s ) # => ["BOOL"]
# log( visible.invoke_kind.to_s ) # => "PROPERTYGET"
# log( visible.params.to_s ) # => []
# @fc.ole_method_help( 'Report' ).params[1].ole_type_detail
#
# prefs = @word_app.Preferences.Strings.ole_method_help( 'Set' ).params
# => [index, newVal]
#
# WORD_OLE_CONST.constants
#
# WORD_OLE_CONST.constants.sort.grep /CR/
# => ["ClHideCRLF", "LesCR", "LesCRLF"]
#
# WORD_OLE_CONST.const_get( 'LesCR' ) or WORD_OLE_CONST::LesCR
# => 1
require 'win32ole'
# Module for constants to be loaded int
module WORD_OLE_CONST
end
class Word
attr_reader :wd, :doc
def initialize( visible )
@wd = WIN32OLE.new('Word.Application')
WIN32OLE.const_load(@wd, WORD_OLE_CONST) if WORD_OLE_CONST.constants.empty?
@wd.Visible = visible
end
def open(file)
@doc = @wd.Documents.Open(file)
@doc
end
def save()
@doc.Save()
@doc
end
# Format : From WORD_OLE_CONST e.g WORD_OLE_CONST::WdFormatHTML
#
def save_as(name, format)
@doc.SaveAs(name, format)
return @doc
end
# WdFormatFilteredHTML
# WdFormatHTML
def save_as_html(name)
@doc.SaveAs(name, WORD_OLE_CONST::WdFormatHTML)
return @doc
end
def quit
@wd.quit()
end
end
# Author:: Tom Statter
# License:: Free, and do what you like with it ! ... MIT ?
#
# About:: Rake tasks to read Word documents, containing product descriptions,
# convert to HTML, tidy the HTML and then create seed_fu ready fixtures,
# from a template, with product description supplied by the HTML
#
# Note cleanest HTML is produced by this combination : saving with WdFormatHTML
# not WdFormatFilteredHTML and using the '--word-2000', 'y' option to tidy
# (don't use the '--bare' option)
#
# Not currently available for JRuby due to Win32Ole requirement
#
# Requires local exes available in PATH for :
# Microsoft Word
# HTML Tidy - http://tidy.sourceforge.net (Free)
#
require 'erb'
namespace :spree do
namespace :extensions do
namespace :site do
desc <<-EOS
Convert MS Word docs to HTML and seed_fu fixtures, by default searches for docs
in RAILS_ROOT/doc/copy
You can change the directory where Word document files are located
with the COPY_PATH environment variable.
Examples:
# default, to convert all Word files for the current environment
rake spree:extensions:site:word2seedfu
# to load seed files matching orders or customers
rake db:seed SEED=orders,customers
# to load files from RAILS_ROOT/features/fixtures
rake db:seed FIXTURE_PATH=features/fixtures
EOS
task :word2html => :environment do
site_extension_lib = File.join(SiteExtension.root, 'lib')
require File.join(site_extension_lib, 'word')
copy_path = ENV["COPY_PATH"] ? ENV["COPY_PATH"] : File.join(RAILS_ROOT, "doc", "copy")
fixtures_path = ENV["FIXTURES_PATH"] ? ENV["FIXTURES_PATH"] : File.join(RAILS_ROOT, "db", "fixtures")
copy_files = Dir[File.join(copy_path, '*.doc')]
copy_files.each do |file|
name = File.basename(file, '.doc')
puts "\n== Generate raw HTML from #{name}.doc =="
@word = Word.new(true)
@word.open( file )
html_file = File.join(copy_path, "#{name}.ms.html")
@word.save_as_html( html_file )
tidy_file = File.join(copy_path, "#{name}.html")
tidy_config = File.join(site_extension_lib, 'tasks', 'tidy_config.txt')
puts "tidy cmd line:", "tidy -config #{tidy_config} -clean --show-body-only y --word-2000 y --indent-spaces 2 -output #{tidy_file} #{html_file}"
result = system("tidy", '-config', "#{tidy_config}", '-clean', '--show-body-only', 'y', '--word-2000', 'y', '--indent-spaces', '2', '-output', "#{tidy_file}", "#{html_file}")
# TODO maybe report on result, $?
File.open( tidy_file ) do |f|
puts f.read
end
@word.quit
end
end
task :word2seedfu => :environment do
site_extension_lib = File.join(SiteExtension.root, 'lib')
require File.join(site_extension_lib, 'word')
sku_id = ENV["INITIAL_SKU_ID"] ? ENV["INITIAL_SKU_ID"] : 0
sku_prefix = ENV["SKU_PREFIX"] ? ENV["SKU_PREFIX"] : File.basename( RAILS_ROOT )
seedfu_template = File.join(site_extension_lib, 'tasks', 'seed_fu_product_template.erb')
begin
File.open( seedfu_template ) do |f|
@template = ERB.new(f.read)
end
rescue => e
puts "ERROR: #{e.inspect}"
puts "Cannot open or read template #{seedfu_template}"
raise e
end
copy_path = ENV["COPY_PATH"] ? ENV["COPY_PATH"] : File.join(RAILS_ROOT, "doc", "copy")
fixtures_path = ENV["FIXTURES_PATH"] ? ENV["FIXTURES_PATH"] : File.join(RAILS_ROOT, "db", "fixtures")
copy_files = Dir[File.join(copy_path, '*.doc')]
copy_files.each do |file|
name = File.basename(file, '.doc')
puts "\n== Generate raw HTML from #{name}.doc =="
@word = Word.new(true)
@word.open( file )
html_file = File.join(copy_path, "#{name}.ms.html")
@word.save_as_html( html_file )
tidy_file = File.join(copy_path, "#{name}.html")
tidy_config = File.join(site_extension_lib, 'tasks', 'tidy_config.txt')
puts "tidy cmd line:", "tidy -config #{tidy_config} -clean --show-body-only y --word-2000 y --indent-spaces 2 -output #{tidy_file} #{html_file}"
result = system("tidy", '-config', "#{tidy_config}", '-clean', '--show-body-only', 'y', '--word-2000', 'y', '--indent-spaces', '2', '-output', "#{tidy_file}", "#{html_file}")
# TODO maybe report on result, $?
File.open( tidy_file ) do |f|
@description = f.read
end
sku_id_str = "%03d" % sku_id
seed_file = "#{sku_id_str}_#{name.gsub(' ', '_')}.rb"
puts "\n== Generate seed fu file #{seed_file} =="
@sku = "#{sku_prefix}_#{sku_id_str}"
@name = 'TODO'
File.open( File.join(fixtures_path, seed_file), 'w' ) do |f|
f.write @template.result(binding)
puts "\nFile created: #{File.join(fixtures_path, seed_file)}"
end
sku_id += 1
@word.quit
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment