shuffling code so it can be reused outside the engine

This commit is contained in:
Torsten 2023-01-15 21:23:36 +02:00
parent ab1f8ef4a8
commit c58b5fc4ba
4 changed files with 45 additions and 43 deletions

View File

@ -1,35 +1,8 @@
require "redcarpet"
module Merged
module MergedHelper
include OptionsHelper
def renderer
options = {hard_wrap: true , autolink: true, no_intra_emphasis: true ,
safe_links_only: true, no_styles: true ,
link_attributes: { target: '_blank' }}
html = Redcarpet::Render::HTML.new(options)
Redcarpet::Markdown.new(html, options)
end
def prose_classes
classes = "prose lg:prose-lg "
classes += "prose-headings:text-inherit "
{ class: classes }
end
def markdown_image(section)
return "" unless section.text
down = self.renderer.render(section.text)
image = image_for(section)
down.gsub("IMAGE" , image).html_safe
end
def markdown(text)
text = text.text unless text.is_a?(String)
return "" if text.blank?
self.renderer.render(text).html_safe
end
include SharedHelper
def aspect_ratio image
x , y = image.aspect_ratio

View File

@ -25,34 +25,28 @@ module Merged
end
def copy()
image = Image.new name: "#{name}_copy" , type: type , tags: (tags || "")
image = Image.new name: "#{name}_copy" , type: type , tags: (tags || "") ,
width: width , height: height, size: size
Image.insert(image) # assigns next id
FileUtils.cp full_filename , image.full_filename
image.init_file_data
image
end
#save an io as new image. The filename is the id, type taken from io
def self.create_new(name , tags, io)
original , ending = io.original_filename.split("/").last.split(".")
original , end_ = io.original_filename.split("/").last.split(".")
magick_image = MiniMagick::Image.read(io)
ending = magick_image.type.downcase
name = original if( name.blank? )
image = Image.new name: name , type: ending , tags: (tags || "")
image = Image.new name: name , type: ending , tags: (tags || "") ,
width: magick_image.width, height: magick_image.height ,
size: (magick_image.size/1024).to_i
self.insert(image) # assigns next id
full_filename = image.id.to_s + "." + ending
File.open(Rails.root.join("app/assets/images/cms/", full_filename), "wb") do |file|
file.write( io.read )
end
image.init_file_data
magick_image.write(Rails.root.join("app/assets/images/cms/", full_filename))
image
end
def init_file_data
image = MiniMagick::Image.new(full_filename)
self.width = image.width
self.height = image.height
self.size = (image.size/1024).to_i
end
def destroy(editor)
File.delete self.full_filename
delete_save!(editor)

View File

@ -4,6 +4,7 @@ require "ruby2js/es2015"
require "ruby2js/filter/functions"
require "ruby2js/haml"
require "ruby2js/filter/vue"
require "merged/shared_helper"
module Merged
class Engine < ::Rails::Engine

View File

@ -0,0 +1,34 @@
require "redcarpet"
module Merged
module SharedHelper
def renderer
options = {hard_wrap: true , autolink: true, no_intra_emphasis: true ,
safe_links_only: true, no_styles: true ,
link_attributes: { target: '_blank' }}
html = Redcarpet::Render::HTML.new(options)
Redcarpet::Markdown.new(html, options)
end
def prose_classes
classes = "prose lg:prose-lg "
classes += "prose-headings:text-inherit "
{ class: classes }
end
def markdown_image(section)
return "" unless section.text
down = self.renderer.render(section.text)
image = image_for(section)
down.gsub("IMAGE" , image).html_safe
end
def markdown(text)
text = text.text unless text.is_a?(String)
return "" if text.blank?
self.renderer.render(text).html_safe
end
end
end