Commit 4913e2ab authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Move InlineImg and InlineVideo classes to subdir.

A bit less stuff in the main JsDuck namespace.
parent 1c186adf
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -3,8 +3,8 @@ require 'rubygems'
require 'strscan'
require 'rdiscount'
require 'jsduck/logger'
require 'jsduck/inline_img'
require 'jsduck/inline_video'
require 'jsduck/inline/img'
require 'jsduck/inline/video'
require 'jsduck/html'

module JsDuck
@@ -49,8 +49,8 @@ module JsDuck
      @relations = relations
      @images = []

      @inline_img = InlineImg.new(opts)
      @inline_video = InlineVideo.new(opts)
      @inline_img = Inline::Img.new(opts)
      @inline_video = Inline::Video.new(opts)

      @link_tpl = opts[:link_tpl] || '<a href="%c%#%m">%a</a>'
      @link_re = /\{@link\s+(\S*?)(?:\s+(.+?))?\}/m
+55 −0
Original line number Diff line number Diff line
require 'jsduck/html'
require 'jsduck/logger'

module JsDuck
  module Inline

    # Implementation of inline tag {@img}
    class Img
      # Base path to prefix images from {@img} tags.
      # Defaults to no prefix.
      attr_accessor :base_path

      # This will hold list of all image paths gathered from {@img} tags.
      attr_accessor :images

      def initialize(opts={})
        @tpl = opts[:img_tpl] || '<img src="%u" alt="%a"/>'

        @re = /\{@img\s+(\S*?)(?:\s+(.+?))?\}/m

        @base_path = nil
        @images = []
      end

      # Takes StringScanner instance.
      #
      # Looks for inline tag at the current scan pointer position, when
      # found, moves scan pointer forward and performs the apporpriate
      # replacement.
      def replace(input)
        if input.check(@re)
          input.scan(@re).sub(@re) { apply_tpl($1, $2) }
        else
          false
        end
      end

      # applies the image template
      def apply_tpl(url, alt_text)
        @images << url
        @tpl.gsub(/(%\w)/) do
          case $1
          when '%u'
            @base_path ? (@base_path + "/" + url) : url
          when '%a'
            HTML.escape(alt_text||"")
          else
            $1
          end
        end
      end
    end

  end
end
+60 −0
Original line number Diff line number Diff line
@@ -2,9 +2,10 @@ require 'jsduck/html'
require 'jsduck/logger'

module JsDuck
  module Inline

    # Implementation of inline tag {@video}
  class InlineVideo
    class Video
      def initialize(opts={})
        @templates = {
          "html5" => '<video src="%u">%a</video>',
@@ -56,3 +57,4 @@ module JsDuck
    end

  end
end

lib/jsduck/inline_img.rb

deleted100644 → 0
+0 −53
Original line number Diff line number Diff line
require 'jsduck/html'
require 'jsduck/logger'

module JsDuck

  # Implementation of inline tag {@img}
  class InlineImg
    # Base path to prefix images from {@img} tags.
    # Defaults to no prefix.
    attr_accessor :base_path

    # This will hold list of all image paths gathered from {@img} tags.
    attr_accessor :images

    def initialize(opts={})
      @tpl = opts[:img_tpl] || '<img src="%u" alt="%a"/>'

      @re = /\{@img\s+(\S*?)(?:\s+(.+?))?\}/m

      @base_path = nil
      @images = []
    end

    # Takes StringScanner instance.
    #
    # Looks for inline tag at the current scan pointer position, when
    # found, moves scan pointer forward and performs the apporpriate
    # replacement.
    def replace(input)
      if input.check(@re)
        input.scan(@re).sub(@re) { apply_tpl($1, $2) }
      else
        false
      end
    end

    # applies the image template
    def apply_tpl(url, alt_text)
      @images << url
      @tpl.gsub(/(%\w)/) do
        case $1
        when '%u'
          @base_path ? (@base_path + "/" + url) : url
        when '%a'
          HTML.escape(alt_text||"")
        else
          $1
        end
      end
    end
  end

end