Class: UndriveGoogle::Transformations::FixHtml

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/undrive_google/transformations/fix_html.rb

Overview

In Google Doc generated HTML, optionally replace:

<html><head>

with

<html lang="en"><head><title>Peter Boling's Bounce to the Ounce</title>

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(unzip) ⇒ FixHtml

Returns a new instance of FixHtml.



21
22
23
24
25
# File 'lib/undrive_google/transformations/fix_html.rb', line 21

def initialize(unzip)
  @unzip = unzip
  @lang = Options.instance.lang
  @title = Options.instance.title
end

Instance Attribute Details

#langObject

Returns the value of attribute lang.



16
17
18
# File 'lib/undrive_google/transformations/fix_html.rb', line 16

def lang
  @lang
end

#titleObject

Returns the value of attribute title.



16
17
18
# File 'lib/undrive_google/transformations/fix_html.rb', line 16

def title
  @title
end

#unzipObject

Returns the value of attribute unzip.



16
17
18
# File 'lib/undrive_google/transformations/fix_html.rb', line 16

def unzip
  @unzip
end

Instance Method Details

#lang_valid?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/undrive_google/transformations/fix_html.rb', line 45

def lang_valid?
  lang && lang.length >= 2
end

#processObject

Returns nil.

Returns:

  • nil

Raises:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/undrive_google/transformations/fix_html.rb', line 28

def process
  return unless lang_valid? || title_valid?
  raise UndriveGoogle::Error, "No file at #{html_path}" unless File.exist?(html_path)

  puts "Checking html in #{html_path}" if Options.instance.verbose
  file = File.open(html_path)
  html = file.read
  return unless html.length >= 12 # open & close <html> or <head> is at least 12 chars

  puts "Fixing html in #{html_path}" if Options.instance.verbose
  html.sub!("<html>", "<html lang=\"#{lang}\">") if lang_valid?
  html.sub!("<head>", "<head><title>#{title}</title>") if title_valid?
  File.write(html_path, html)

  nil
end

#title_valid?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/undrive_google/transformations/fix_html.rb', line 49

def title_valid?
  title && title.length >= 1
end