module Fiona7 module LinkConverter class ScrivitoToFiona def initialize(klass, value) @klass, @value = klass, value.to_str end def convert serialize_html end private def serialize_html # TODO: this an incomplete list of possible tags if Fiona7.mode == :legacy link_expressions = [/(src|href)\s*=\s*"([^"]*)"/, /(src|href)\s*=\s*'([^']*)'/] else # in standalone mode image links cannot be stored correctly link_expressions = [/(href)\s*=\s*"([^"]*)"/, /(href)\s*=\s*'([^']*)'/] end link_expressions.each do |expr| @value.gsub!(expr) do |string| attr = $1 target = $2 objid = objid_from_target(target) if objid # This is a normal objid link "#{attr}=\"#{obj_path(objid)}\"" elsif target =~ /^#/ # This is a link with anchor only if such link # is placed within a widget it would become # invalid (because it would produce a self-link # in the widget) # # This works around this problem by producing # an external link instead. "#{attr}=\"external:#{target}\"" else string end end end @value end def objid_from_target(target) if (match = target.match(/\Aobjid:([0-9a-f]+)\Z/)) match[1] end end def obj_path(id) @klass.find(id).try(:path) end end end end