module Fiona7 module LinkConverter class FionaToScrivito include ERB::Util def initialize(obj) @obj = obj end def convert(content) return content unless content.present? convert_links(content) end protected LINK_PATTERN = '?' LINK_EXPRESSION = %r(#{LINK_PATTERN}) def link_map @link_map ||= Hash[@obj.attr_values["text_links"].map{|l| [l["link_id"].to_s, l]}] end def convert_links(content_attribute) return content_attribute unless content_attribute =~ LINK_EXPRESSION content_attribute.gsub(%r(#{LINK_PATTERN}(['"]?))) do link = link_map[$1.to_s] if link.blank? raise "Unable to convert links" "#{RailsConnector::DefaultCmsRoutingHelper::LINK_TO_UNREACHABLE}#{$2}" else uri = "#{cms_path(link)}#{$2}" html_link(link, uri) end end end def cms_path(link) if link["type"] == "internal" base = "objid:#{link['destination']}" base += "?#{link['search']}" if link['search'].present? base += "##{link['fragment']}" if link['fragment'].present? base else # This cleans-up pseudo-external links. # Such links have a external: scheme. link["url"].to_s.sub(/^external:/, '') end end def html_link(link, uri) subst = uri name, value = case link["tag_name"] when 'img', 'input' ['alt', link["title"] || ""] when 'a', 'link' ['title', begin link["title"] unless link["title"].blank? end] end if value subst << %( #{name}="#{h(value)}") end subst << %( target="#{h(link['target'])}") unless link['target'].blank? subst end end end end