Commit f816cd88 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Rewrite of {@link} and {@img} tags replacement.

Doing a proper parsing now, so automatic link-creation won't be
done inside {@link} and {@img}.
parent 74c6be0a
Loading
Loading
Loading
Loading
+21 −5
Original line number Diff line number Diff line
require 'rubygems'
require 'rdiscount'
require 'strscan'

module JsDuck

@@ -34,6 +35,8 @@ module JsDuck
      @url_template = "%cls%"
      @max_length = 120
      @relations = {}
      @link_re = /\{@link\s+(\S*?)(?:\s+(.+?))?\}/m
      @img_re = /\{@img\s+(\S*?)(?:\s+(.+?))?\}/m
    end

    # Replaces {@link} and {@img} tags, auto-generates links for
@@ -50,11 +53,24 @@ module JsDuck
    # links to these classes.  So one doesn even need to use the @link
    # tag to create a link.
    def replace(input)
      replace_class_names(replace_img_tags(replace_link_tags(input)))
      s = StringScanner.new(input)
      out = ""
      while !s.eos? do
        if s.check(@link_re)
          out += replace_link_tag(s.scan(@link_re))
        elsif s.check(@img_re)
          out += replace_img_tag(s.scan(@img_re))
        elsif s.check(/\{/)
          out += s.scan(/\{/)
        else
          out += replace_class_names(s.scan(/[^{]+/))
        end
      end
      out
    end

    def replace_link_tags(input)
      input.gsub(/\{@link\s+(\S*?)(?:\s+(.+?))?\}/m) do
    def replace_link_tag(input)
      input.sub(@link_re) do
        target = $1
        text = $2
        if target =~ /^(.*)#(.*)$/
@@ -78,8 +94,8 @@ module JsDuck
      end
    end

    def replace_img_tags(input)
      input.gsub(/\{@img\s+(\S*?)(?:\s+(.+?))?\}/m) do
    def replace_img_tag(input)
      input.sub(@img_re) do
        src = $1
        alt = $2
        "<img src=\"#{src}\" alt=\"#{alt}\"/>"
+10 −0
Original line number Diff line number Diff line
@@ -125,6 +125,16 @@ describe JsDuck::DocFormatter do
        @formatter.replace("Look at Ext.form.Field#getValues").should ==
          "Look at <a href=\"Ext.form.Field#getValues\" rel=\"Ext.form.Field#getValues\">Ext.form.Field.getValues</a>"
      end

      it "doesn't create links inside {@link} tag" do
        @formatter.replace("{@link MyClass a MyClass link}").should ==
          '<a href="MyClass" rel="MyClass">a MyClass link</a>'
      end

      it "doesn't create links inside {@img} tag" do
        @formatter.replace("{@img some/file.jpg a MyClass image}").should ==
          '<img src="some/file.jpg" alt="a MyClass image"/>'
      end
    end
  end