Method: Verku::Exporter::Epub#write_sections!

Defined in:
lib/verku/exporter/epub.rb

#write_sections!Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/verku/exporter/epub.rb', line 95

def write_sections!
  # First we need to get all ids, which are used as
  # the anchor target.
  links = sections.inject({}) do |buffer, section|
    section.html.css("[id]").each do |element|
      anchor = "##{element["id"]}"
      buffer[anchor] = "#{section.filename}#{anchor}"
    end

    buffer
  end

  # Then we can normalize all links and
  # manipulate other paths.
  #
  sections.each do |section|
    section.html.css("a[href^='#']").each do |link|
      href = link["href"]
      link.set_attribute("href", links.fetch(href, href))
    end

    # Replace all srcs.
    #
    section.html.css("[src]").each do |element|
      src = File.basename(element["src"]).gsub(/\.svg$/, ".png")
      element.set_attribute("src", src)
      element.set_attribute("alt", "")
      element.node_name = "img"
    end

    FileUtils.mkdir_p(tmp_dir)
    File.open(section.filepath, "w") do |file|
      body = section.html.css("body").to_xhtml.gsub(%r[<body>(.*?)</body>]m, "\\1")
      file << render_chapter(body)
    end
  end
end