get_doc_contents

in rakelib/build_pdf.rake [73:164]


def get_doc_contents
  source_dir = CONFIG[:source_dir]
  tmp_dir = CONFIG[:tmp_dir]
  formatter = REXML::Formatters::Default.new
  toc = YAML::load_file("#{source_dir}/_data/_nav.yml")
  doc_content = []

  toc["reference"].each do |toc_data|
    section = {
        'id' => ERB::Util.url_encode(toc_data['title'].sub(' ', '_')),
        'title' => toc_data['title'],
        'content' => []
    }

    if toc_data["content"]
      toc_data['content'].each do |item|
        item.each do |file_path, title|
          puts "Processing #{file_path}"

          file_path = "#{tmp_dir}/_site/#{file_path}"
          file_basename = Pathname.new(file_path).basename

          if File.file?(file_path) == false
            next
          end

          page_content = File.read(file_path)
          page = {
              'id' => file_basename,
              'title' => title,
          }

          doc = REXML::Document.new page_content

          
          doc.elements.each("//*[@id]") do |node|
            node_id = node.attributes['id']
            node.attributes['id'] = "#{page['id']}_#{node_id}"
          end

          
          doc.elements.each("//a[@href]") do |link|
            link_href = CGI.escape(link.attributes['href'])
            uri = URI.parse(link_href)

            
            if uri.scheme == nil
              new_anchor_id = link_href

              
              if link_href.match(/^
                new_anchor_id = "#{page['id']}_#{uri.fragment}"
                
              else
                new_anchor_id = "#{uri.path}" + ((uri.fragment != nil) ? "_#{uri.fragment}" : "")
              end

              link.attributes['href'] = "#" + new_anchor_id
            end
          end

          
          doc.elements.each("//a[count(child::node())=0]") do |link|
            link.add_text ' '
          end

          
          5.downto(1) do |i|
            doc.elements.each("//h#{i}") do |node|
              node_name = node.name
              node.name = "h#{i + 1}"
            end
          end

          out = String.new
          doc.elements.each("//body/*") do |node|
            formatter.write(node, out)
          end

          page['content'] = out
          section['content'].push(page)
        end
      end

    end

    doc_content.push(section)
  end

  doc_content
end