class Bookbinder::Subnav::NavigationEntriesFromMarkdownRoot

Constants

Attributes

config[R]
fs[R]
renderer[R]
source_for_site_gen[R]

Public Class Methods

new(fs, require_valid_subnav_links) click to toggle source
# File lib/bookbinder/subnav/navigation_entries_from_markdown_root.rb, line 12
def initialize(fs, require_valid_subnav_links)
  @fs = fs
  @require_valid_subnav_links = require_valid_subnav_links
  @renderer = Redcarpet::Markdown.new(Redcarpet::Render::HTML.new)
end

Public Instance Methods

Private Instance Methods

absolute_source_from_path(path) click to toggle source
# File lib/bookbinder/subnav/navigation_entries_from_markdown_root.rb, line 45
def absolute_source_from_path(path)
  full_sources = fs.find_files_extension_agnostically(path, source_for_site_gen)
  full_sources.first
end
gather_urls_and_texts(source) click to toggle source

href: ./cat/index.html expanded href: my/cat/index.html full source: my/cat/index.html.md.erb

# File lib/bookbinder/subnav/navigation_entries_from_markdown_root.rb, line 53
def gather_urls_and_texts(source)
  toc_md = fs.read(source)
  base_node = get_html(toc_md).css('html')

  nav_items(base_node).map do |element|
    href = element['href']
    expanded_href = (source.dirname + href).relative_path_from(source_for_site_gen)
    next_source = absolute_source_from_path(expanded_href)
    nested_links = {}

    no_children = false
    no_children ||= validate_no_broken_link(expanded_href, next_source, source)
    no_children ||= validate_no_duplicate_link(expanded_href, next_source, source)

    unless no_children
      @parsed_files[next_source] = source
      nested_urls_and_texts = gather_urls_and_texts(next_source)
      nested_links.merge!(nested_links: nested_urls_and_texts) unless nested_urls_and_texts.empty?
    end

    {url: '/' + expanded_href.to_s, text: element.inner_text}.merge(nested_links)
  end
end
get_html(md) click to toggle source
# File lib/bookbinder/subnav/navigation_entries_from_markdown_root.rb, line 41
def get_html(md)
  Nokogiri::HTML(renderer.render(md))
end
nav_items(base_node) click to toggle source