Sha256: ac00942b60e3b35a86b66978b86af2d4bf47f69677d1891d0e90ff24a7aa875b

Contents?: true

Size: 1.59 KB

Versions: 21

Compression:

Stored size: 1.59 KB

Contents

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

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
infopark_fiona7-1.2.0.0.0 lib/fiona7/link_converter/scrivito_to_fiona.rb