Skip to content

Instantly share code, notes, and snippets.

@chatman-media
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save chatman-media/9568099 to your computer and use it in GitHub Desktop.

Select an option

Save chatman-media/9568099 to your computer and use it in GitHub Desktop.
convert fb2 to html
module ConvertingTool
extend ActiveSupport::Concern
def self.convert(file_path)
# TODO: make schema file cache
schema_path = Rails.root.join('data', 'schemes', 'FB2_2_html.xsl')
require 'nokogiri'
begin
# converting
doc = Nokogiri::XML(File.read file_path)
xslt = Nokogiri::XSLT(File.read schema_path)
html = xslt.transform(doc)
# remove invalid parts
html = html.to_s.gsub(/l:href/, 'href').gsub(/ xmlns(:\w+)*="[^\"]+"/, '')
rescue Exception => e
Rails.logger.debug "Error while converting fb2 to html:: #{e}"
return nil
end
html
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment