Last active
August 29, 2015 13:57
-
-
Save chatman-media/9568099 to your computer and use it in GitHub Desktop.
convert fb2 to html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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