Class: EpubTools::StyleFinder
- Inherits:
-
Object
- Object
- EpubTools::StyleFinder
- Includes:
- Loggable
- Defined in:
- lib/epub_tools/style_finder.rb
Overview
Finds css classes for bold and italic texts in Google Docs-generated EPUBs. Used by XHTMLCleaner and SplitChapters.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ StyleFinder
constructor
Initializes the class.
-
#run ⇒ Hash
Runs the finder.
Methods included from Loggable
Constructor Details
#initialize(options = {}) ⇒ StyleFinder
Initializes the class
17 18 19 20 21 22 |
# File 'lib/epub_tools/style_finder.rb', line 17 def initialize( = {}) @file_path = .fetch(:file_path) @output_path = [:output_path] || 'text_style_classes.yaml' @verbose = [:verbose] || false raise ArgumentError, "File does not exist: #{@file_path}" unless File.exist?(@file_path) end |
Instance Method Details
#run ⇒ Hash
Runs the finder
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/epub_tools/style_finder.rb', line 26 def run doc = Nokogiri::HTML(File.read(@file_path)) style_blocks = doc.xpath('//style').map(&:text).join("\n") italics = extract_classes(style_blocks, /font-style\s*:\s*italic/) bolds = extract_classes(style_blocks, /font-weight\s*:\s*700/) print_summary(italics, bolds) if @verbose data = { 'italics' => italics, 'bolds' => bolds } File.write(@output_path, data.to_yaml) data end |