Skip to content

Instantly share code, notes, and snippets.

@chucker
Created April 23, 2010 17:09
Show Gist options
  • Save chucker/376816 to your computer and use it in GitHub Desktop.
Save chucker/376816 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require 'rubygems'
require 'nokogiri'
require 'ftools'
require 'base64'
HG_PATH = '/usr/local/bin/hg'
def clean_up(dirpath)
if File.exist?(dirpath) && File.directory?(dirpath)
Dir.chdir(dirpath)
FileUtils.rm_r Dir.glob("{*.{xml,css,html,.gz},skin_*}")
else
Dir.mkdir(dirpath)
Dir.chdir(dirpath)
`#{HG_PATH} init`
end
end
def extract_file(node)
name = node.css('filename').text
content = Base64.decode64(node.css('content').text)
case name.gsub(/ipb_(.*).xml/, '\1')
when "css"
extract_css_or_wrapper('css', 'style.css', content)
when "templates"
extract_templates(content)
when "wrapper"
extract_css_or_wrapper('wrapper', 'wrapper.html', content)
else
File.open(name, "w+") do |out_file|
out_file.write(content)
end
end
end
def extract_css_or_wrapper(prefix, filename, content)
doc = Nokogiri.XML(content)
doc.css("#{prefix}export #{prefix}group #{prefix} #{prefix}content").each do |node|
File.open(filename, "w+") do |out_file|
out_file.write(node.text)
end
end
end
def extract_templates(content)
doc = Nokogiri.XML(content)
doc.css("templateexport templategroup template").each do |node|
extract_template(node)
end
end
def extract_template(node)
dir_name = node.css('group_name').text
if ! File.exist?(dir_name)
Dir.mkdir(dir_name)
end
name = node.css('func_name').text
content = node.css('section_content').text
File.open("#{dir_name}/#{name}.html", "w+") do |out_file|
out_file.write(content)
end
end
path = ARGV[0]
if path == nil
path = "/Users/chucker/Downloads/23-04-2010/ipb_skin-mystcommun.xml"
end
dirpath = path + '_Files'
clean_up(dirpath)
File.open(path) do |in_file|
doc = Nokogiri.XML(in_file)
doc.css('xmlarchive fileset file').each {|node| extract_file(node)}
end
File.copy(path, dirpath)
system('gzip', '-f', "#{dirpath}/#{File.basename(path)}")
`#{HG_PATH} addremove`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment