Commit 74c6be0a authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Initial support for {@img} tag in doc-comments.

Simple-minded implementation with regex search-replace.
parent 2971197e
Loading
Loading
Loading
Loading
+14 −1
Original line number Diff line number Diff line
@@ -36,16 +36,21 @@ module JsDuck
      @relations = {}
    end

    # Replaces {@link} and {@img} tags, auto-generates links for
    # recognized classnames.
    #
    # Replaces {@link Class#member link text} in given string with
    # HTML links pointing to documentation.  In addition to the href
    # attribute links will also contain ext:cls and ext:member
    # attributes.
    #
    # Replaces {@img path/to/image.jpg Alt text} with HTML <img> tag.
    #
    # Additionally replaces strings recognized as ClassNames with
    # 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_link_tags(input))
      replace_class_names(replace_img_tags(replace_link_tags(input)))
    end

    def replace_link_tags(input)
@@ -73,6 +78,14 @@ module JsDuck
      end
    end

    def replace_img_tags(input)
      input.gsub(/\{@img\s+(\S*?)(?:\s+(.+?))?\}/m) do
        src = $1
        alt = $2
        "<img src=\"#{src}\" alt=\"#{alt}\"/>"
      end
    end

    def replace_class_names(input)
      input.gsub(/(\A|\s)([A-Z][A-Za-z0-9.]*[A-Za-z0-9])(?:(#)([A-Za-z0-9]+))?([.,]?(?:\s|\Z))/m) do
        before = $1
+14 −0
Original line number Diff line number Diff line
@@ -9,6 +9,8 @@ describe JsDuck::DocFormatter do

  describe "#replace" do

    # {@link ...}

    it "replaces {@link Ext.Msg} with link to class" do
      @formatter.replace("Look at {@link Ext.Msg}").should ==
        'Look at <a href="Ext.Msg" rel="Ext.Msg">Ext.Msg</a>'
@@ -49,6 +51,18 @@ describe JsDuck::DocFormatter do
        "Look at <a href=\"Ext.Msg\" rel=\"Ext.Msg\">some\ntext</a>"
    end

    # {@img ...}

    it "replaces {@img some/image.png Alt text} with <img> element" do
      @formatter.replace("Look at {@img some/image.png Alt text}").should ==
        'Look at <img src="some/image.png" alt="Alt text"/>'
    end

    it "replaces {@img some/image.png} with <img> element with empty alt tag" do
      @formatter.replace("Look at {@img some/image.png}").should ==
        'Look at <img src="some/image.png" alt=""/>'
    end

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