Sha256: 2f0b002009af233b80dc4e564abb0da20fc905fd813996f45312c1c3b6371837

Contents?: true

Size: 1.32 KB

Versions: 6

Compression:

Stored size: 1.32 KB

Contents

require 'nokogiri'

module Scrivito

  class ContentConversion
    def self.convert_html_links(input, request_host, request_port)
      doc = Nokogiri::HTML.parse("<div class='internal_jump_point'>#{input}</div>")

      doc.css('body a').each do |a_tag|
        if href = a_tag.attributes['href']
          link = Link.parse(href.value, request_host, request_port)
          href.value = link.to_cms_api_html_url
        end
      end

      doc.css('body img').each do |img_tag|
        if src = img_tag.attributes['src']
          link = Link.parse(src.value, request_host, request_port)
          src.value = link.to_cms_api_html_url
        end
      end

      new_html = doc.css('body div.internal_jump_point').inner_html

      nbsp = Nokogiri::HTML("&nbsp;").text
      new_html.gsub(nbsp, "&nbsp;")
    end

    def self.convert_linklist_urls(linklist, request_host, request_port)
      linklist.map do |link_data|
        convert_link(link_data, request_host, request_port)
      end
    end

    def self.convert_link(link_data, request_host, request_port)
      if link_data && link_data.has_key?(:url)
        link_data.delete(:obj_id)
        url = link_data.delete(:url)
        link = Link.parse(url, request_host, request_port)
        link_data.merge!(link.to_cms_api_linklist_params)
      end

      link_data
    end
  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
scrivito_sdk-0.40.0.rc1 lib/scrivito/content_conversion.rb
scrivito_sdk-0.30.0 lib/scrivito/content_conversion.rb
scrivito_sdk-0.30.0.rc1 lib/scrivito/content_conversion.rb
scrivito_sdk-0.18.1 lib/scrivito/content_conversion.rb
scrivito_sdk-0.18.0 lib/scrivito/content_conversion.rb
scrivito_sdk-0.17.0 lib/scrivito/content_conversion.rb