Sha256: a38119261aabaf1bba61290e156b190a7e4adb73bc5fcc7f3b80a8bb12c245f7

Contents?: true

Size: 1.42 KB

Versions: 9

Compression:

Stored size: 1.42 KB

Contents

module Thredded
  module Filter
    module Attachment
      def filtered_content
        content = super
        matches = content.scan(/(?<full>\[t:(?<tag>\w+)=?(?<img_nmb>\d+)? ?(?<attribs>[^\]]+)?\])/)

        matches.each do |match|
          str_buff = ''
          full = match[0]
          tag = match[1]
          img_number = match[2] ? match[2].to_i - 1 : 0 # default to first attachment
          attribs = match[3]

          if(tag != 'img' || !self.attachments[img_number])
            next
          end

          # start with img tag
          str_buff += '<img '

          # get attachment object at spot img_number - 1
          attachment = self.attachments[img_number]
          str_buff += 'src="' + attachment.attachment.to_s + '" '

          # do attribute stuff, left right first
          if !attribs.nil?
            if align = attribs.match(/(left|right)/)
              str_buff += 'class="align_' + align[0] + '" '
            end

            # attributes, width x height
            if wxh = attribs.match(/(\d+)x?(\d+)?/)
              str_buff += 'width="' + wxh[1] + '" '
              height = wxh[2].nil? ? wxh[1] : wxh[2]
              str_buff += 'height="' + height + '" '
            end
          end

          # end img tag
          str_buff += '/>'

          # replace in post content
          content = content.sub(full, str_buff)
        end

        content.html_safe
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
thredded-0.0.10 lib/thredded/filter/attachment.rb
thredded-0.0.9 lib/thredded/filter/attachment.rb
thredded-0.0.8 lib/thredded/filter/attachment.rb
thredded-0.0.7 lib/thredded/filter/attachment.rb
thredded-0.0.6 lib/thredded/filter/attachment.rb
thredded-0.0.5 lib/thredded/filter/attachment.rb
thredded-0.0.4 lib/thredded/filter/attachment.rb
thredded-0.0.3 lib/thredded/filter/attachment.rb
thredded-0.0.1 lib/thredded/filter/attachment.rb