lib/bookchef/tree_merger.rb
# Converts links 'href' attr from relative to absolute path
# according to the sections 'name' attrs.
Converts links 'href' attr from relative to absolute path
according to the sections 'name' attrs.
Converts links ' href ' attr from relative to absolute path according to the sections ' name ' attrs .
def convert_links!(document, current_path)
        document.search("a").each do |link|
          next if link[:href].nil? || link[:href] =~ /\Ahttps?:\/\//
          link_path_arr    = link[:href].split("/")
          current_path_arr = current_path.split("/").reject { |i| i.empty? }
          uplevels_count   = (link_path_arr.map { |i| i if i == ".." }).compact.size
          link_path_arr.delete_if { |i| i == ".." }
          if uplevels_count > current_path_arr.size
            raise LinkLevelOutOfReach, "for link #{link.to_s} in \"#{current_path}\""
          end
          current_path_arr = current_path_arr.reverse.drop(uplevels_count).reverse
          link[:href] = "#/#{current_path_arr.join("/")}/#{path_with_filename(link_path_arr.join("/"))}".gsub(/\/+/, '/')
        end
      end
def convert_links! ( document , current_path ) document . search ( "a" ) . each do | link | next if link [ :href ] . nil? || link [ :href ] =~ / \A https?: \/ \/ / link_path_arr = link [ :href ] . split ( "/" ) current_path_arr = current_path . split ( "/" ) . reject { | i | i . empty? } uplevels_count = ( link_path_arr . map { | i | i if i == ".." } ) . compact . size link_path_arr . delete_if { | i | i == ".." } if uplevels_count > current_path_arr . size raise LinkLevelOutOfReach , "for link #{link.to_s} in \"#{current_path}\"" end current_path_arr = current_path_arr . reverse . drop ( uplevels_count ) . reverse link [ :href ] = "#/#{current_path_arr.join("/")}/#{path_with_filename(link_path_arr.join("/"))}" . gsub ( / \/ + / , '/' ) end end
Converts links 'href' attr from relative to absolute path
according to the sections 'name' attrs.
Converts links ' href ' attr from relative to absolute path according to the sections ' name ' attrs .
document
<not_specific>
current_path
<not_specific>