class Bookwatch::Subnav::NavigationEntriesFromHtmlToc

Attributes

fs[R]
output_locations[R]
section[R]

Public Class Methods

new(fs) click to toggle source
# File lib/bookwatch/subnav/navigation_entries_from_html_toc.rb, line 7
def initialize(fs)
  @fs = fs
  @external_link_check = %r{\Ahttps?://}
end

Public Instance Methods

Private Instance Methods

gather_urls_and_texts(base_node) click to toggle source
# File lib/bookwatch/subnav/navigation_entries_from_html_toc.rb, line 43
def gather_urls_and_texts(base_node)
  top_level_li = base_node.css("> li")
  top_level_li.map do |li|
    anchor = li.css('a')[0]
    href = anchor['href']
    text = anchor.inner_text
    ul = li.css('> ul')
    if ul.size > 0
      {url: href, text: text, nested_links: gather_urls_and_texts(ul)}
    else
      {url: href, text: text}
    end
  end
end
parse_toc_file() click to toggle source
# File lib/bookwatch/subnav/navigation_entries_from_html_toc.rb, line 25
def parse_toc_file
  html = fs.read(
    File.join(
      output_locations.html_from_preprocessing_dir,
      section.destination_directory,
      'index.html')
  )
  Nokogiri::XML(html)
end
set_anchor_values(anchors) click to toggle source
# File lib/bookwatch/subnav/navigation_entries_from_html_toc.rb, line 35
def set_anchor_values(anchors)
  anchors.each do |anchor|
    unless @external_link_check.match(anchor['href'])
      anchor['href'] = "/#{section.destination_directory}/#{anchor['href']}"
    end
  end
end