Commit 4268aee0 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Implement {@video} tag.

This allows easy embedding of videos to guides (and even to class
docs if there should be such need).

For now only supporting Vimeo videos, just like the separate videos
page:

    {@video <vimeo-video-id> Some comment}
parent 2e151556
Loading
Loading
Loading
Loading
+34 −0
Original line number Diff line number Diff line
@@ -29,6 +29,15 @@ module JsDuck
    # Default value: '<img src="%u" alt="%a"/>'
    attr_accessor :img_tpl

    # Template HTML that replaces {@video URL alt text}
    # Can contain placeholders:
    #
    # %u - URL from @video tag (e.g. "some/path.mpeg")
    # %a - alt text for video
    #
    # Default value: '<video src="%u">%a</video>'
    attr_accessor :video_tpl

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

@@ -61,10 +70,15 @@ module JsDuck
      @max_length = 120
      @relations = relations
      @images = []

      @link_tpl = opts[:link_tpl] || '<a href="%c%#%m">%a</a>'
      @img_tpl = opts[:img_tpl] || '<img src="%u" alt="%a"/>'
      @video_tpl = opts[:video_tpl] || '<video src="%u">%a</video>'

      @link_re = /\{@link\s+(\S*?)(?:\s+(.+?))?\}/m
      @img_re = /\{@img\s+(\S*?)(?:\s+(.+?))?\}/m
      @video_re = /\{@video\s+(\S*?)(?:\s+(.+?))?\}/m

      @example_annotation_re = /<pre><code>\s*@example( +[^\n]*)?\s+/m
    end

@@ -95,6 +109,8 @@ module JsDuck
          out += replace_link_tag(s.scan(@link_re))
        elsif s.check(@img_re)
          out += replace_img_tag(s.scan(@img_re))
        elsif s.check(@video_re)
          out += replace_video_tag(s.scan(@video_re))
        elsif s.check(/[{]/)
          # There might still be "{" that doesn't begin {@link} or {@img} - ignore it
          out += s.scan(/[{]/)
@@ -189,6 +205,10 @@ module JsDuck
      input.sub(@img_re) { img($1, $2) }
    end

    def replace_video_tag(input)
      input.sub(@video_re) { video($1, $2) }
    end

    # Looks input text for patterns like:
    #
    #  My.ClassName
@@ -273,6 +293,20 @@ module JsDuck
      end
    end

    # applies the video template
    def video(url, alt_text)
      @video_tpl.gsub(/(%\w)/) do
        case $1
        when '%u'
          url
        when '%a'
          CGI.escapeHTML(alt_text||"")
        else
          $1
        end
      end
    end

    # applies the link template
    def link(cls, member, anchor_text, type=nil, static=false)
      # Use the canonical class name for link (not some alternateClassName)
+13 −0
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ module JsDuck
    attr_accessor :images
    attr_accessor :link_tpl
    attr_accessor :img_tpl
    attr_accessor :video_tpl
    attr_accessor :export
    attr_accessor :seo
    attr_accessor :eg_iframe
@@ -92,6 +93,18 @@ module JsDuck
      # Note that we wrap image template inside <p> because {@img} often
      # appears inline within text, but that just looks ugly in HTML
      @img_tpl = '<p><img src="%u" alt="%a"></p>'
      # Template for embedding Vimeo videos.
      # For now there's no command line option to change this template.
      @video_tpl = [
        '<p><object width="640" height="360" id="video_player">',
          '<param name="allowfullscreen" value="true" />',
          '<param name="allowscriptaccess" value="always" />',
          '<param name="flashvars" value="api=1" />',
          '<param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=%u&amp;server=vimeo.com&amp;color=4CC208&amp;fullscreen=1" />',
          '<embed src="http://vimeo.com/moogaloop.swf?clip_id=%u&amp;server=vimeo.com&amp;color=4CC208&amp;fullscreen=1" ',
            'type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="640" height="360"></embed>',
        '</object></p>',
      ].join
      @export = nil
      @seo = false
      @eg_iframe = nil
+7 −0
Original line number Diff line number Diff line
@@ -113,6 +113,13 @@ describe JsDuck::DocFormatter do
        '<img src="some/image.png" alt="foo&quot;bar"/>'
    end

    # {@video ...}

    it "replaces {@video some/url.mpeg Alt text} with video markup" do
      @formatter.replace("{@video some/url.mpeg Alt text}").should ==
        '<video src="some/url.mpeg">Alt text</video>'
    end

    # auto-conversion of identifiable ClassNames to links
    describe "auto-detect" do
      before do