Class: EpubTools::TextStyleClassFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/epub_tools/text_style_class_finder.rb

Instance Method Summary collapse

Constructor Details

#initialize(file_path, output_path = 'text_style_classes.yaml', verbose: false) ⇒ TextStyleClassFinder

Returns a new instance of TextStyleClassFinder.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
# File 'lib/epub_tools/text_style_class_finder.rb', line 7

def initialize(file_path, output_path = 'text_style_classes.yaml', verbose: false)
  @file_path = file_path
  @output_path = output_path
  @verbose = verbose
  raise ArgumentError, "File does not exist: #{@file_path}" unless File.exist?(@file_path)
end

Instance Method Details

#callObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/epub_tools/text_style_class_finder.rb', line 14

def call
  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)
end