Commit 04e04a1a authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Extract Inline::Example class from DocFormatter.

parent 397c42fa
Loading
Loading
Loading
Loading
+4 −9
Original line number Diff line number Diff line
require 'rubygems'
require 'strscan'
require 'rdiscount'
require 'jsduck/logger'
require 'jsduck/inline/link'
require 'jsduck/inline/img'
require 'jsduck/inline/video'
require 'jsduck/inline/example'

module JsDuck

@@ -18,8 +18,7 @@ module JsDuck
      @inline_link.relations = relations
      @inline_img = Inline::Img.new(opts)
      @inline_video = Inline::Video.new(opts)

      @example_annotation_re = /<pre><code>\s*@example( +[^\n]*)?\s+/m
      @inline_example = Inline::Example.new(opts)
    end

    # Sets base path to prefix images from {@img} tags.
@@ -92,12 +91,8 @@ module JsDuck
        elsif s.check(/[{]/)
          # There might still be "{" that doesn't begin {@link} or {@img} - ignore it
          out += s.scan(/[{]/)
        elsif s.check(@example_annotation_re)
          # Match possible classnames following @example and add them
          # as CSS classes inside <pre> element.
          s.scan(@example_annotation_re) =~ @example_annotation_re
          css_classes = ($1 || "").strip
          out += "<pre class='inline-example #{css_classes}'><code>"
        elsif substitute = @inline_example.replace(s)
          out += substitute
        elsif s.check(/<a\b/)
          # Increment number of open <a> tags.
          open_a_tags += 1
+42 −0
Original line number Diff line number Diff line
module JsDuck
  module Inline

    # Implementation of @example tag.
    #
    # Looks for @example tag at the beginning of code blocks. When
    # found, adds an "inline-example" CSS class to the <pre> element.
    #
    # Unlike other Inline:: classes this doesn't implement an
    # {@example} tag as could be expected.  But it fits nicely along
    # with other inline tags as it's processed inside DocFormatter, so
    # it mostly fits here along with the others.
    #
    class Example
      # Constructor takes opts parameter for consistency with other
      # JsDuck::Inline::* classes.
      def initialize(opts={})
        @re = /<pre><code>\s*@example( +[^\n]*)?\s+/m
      end

      # Takes StringScanner instance.
      #
      # Looks for "<pre><code>@example" at the current scan pointer
      # position, when found, moves scan pointer forward and performs
      # the apporpriate replacement.
      def replace(input)
        if input.check(@re)
          # Match possible classnames following @example and add them
          # as CSS classes inside <pre> element.
          input.scan(@re) =~ @re
          css_classes = ($1 || "").strip

          return "<pre class='inline-example #{css_classes}'><code>"
        else
          false
        end
      end

    end

  end
end