Skip to content
Snippets Groups Projects
Unverified Commit ac72eeac authored by Sébastien Gaya's avatar Sébastien Gaya
Browse files

replace links wp-uploads

parent 353f9108
No related branches found
No related tags found
No related merge requests found
......@@ -5,16 +5,22 @@ module Communication::Website::Imported::WithRichText
def rich_text_with_attachments(text)
fragment = Nokogiri::HTML.fragment(text)
images = fragment.css("img[src*=\"#{website.website.uploads_url}\"]")
images.each do |image|
fragment = replace_tags_with_attachments(fragment, 'a', 'href')
fragment = replace_tags_with_attachments(fragment, 'img', 'src')
fragment.to_html
end
def replace_tags_with_attachments(fragment, tag_name, attribute_name)
nodes = fragment.css("#{tag_name}[#{attribute_name}*=\"#{website.website.uploads_url}\"]")
nodes.each do |node|
begin
url = image.attr('src')
url = node.attr(attribute_name)
blob = load_blob_from_url(url)
image.replace ActionText::Attachment.from_attachable(blob).node.to_s
node.replace ActionText::Attachment.from_attachable(blob).node.to_s
rescue
end
end
fragment.to_html
fragment
end
def load_blob_from_url(url)
......
<figure class="attachment attachment--<%= blob.representable? ? "preview" : "file" %> attachment--<%= blob.filename.extension %>">
<% if blob.representable? %>
<% if blob.image? %>
<%= image_tag blob.representation(resize_to_limit: local_assigns[:in_gallery] ? [ 800, 600 ] : [ 1024, 768 ]) %>
<% elsif blob.video? %>
<video>
<source src="<%= rails_blob_path(blob) %>" type="<%= blob.content_type %>">
</video>
<% else %>
<p>
<span class="attachment__name"><%= blob.filename %></span>
<span class="attachment__size"><%= number_to_human_size blob.byte_size %></span>
</p>
<%= link_to blob, target: :blank do %>
<% if blob.representable? %>
<%= image_tag blob.representation(resize_to_limit: local_assigns[:in_gallery] ? [ 800, 600 ] : [ 1024, 768 ]) %>
<% else %>
<p>
<span class="attachment__name"><%= blob.filename %></span>
<span class="attachment__size"><%= number_to_human_size blob.byte_size %></span>
</p>
<% end %>
<% end %>
<% end %>
<% if caption = blob.try(:caption) %>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment