Sha256: 69a1116b70c4870eea9c8c5eee61ff2e7403a03076bdc0d94b0eeb9cd038a0d6

Contents?: true

Size: 1.34 KB

Versions: 9

Compression:

Stored size: 1.34 KB

Contents

module Parade
  module Parsers

    #
    # Within the markdown file the image paths are relative to the markdown
    # file. This parser will convert any image paths specified within the
    # markdown and convert them to relative to the presentation root.
    #
    # This parsing functionality ensures that the image path when this is
    # later rendered to HTML will have the correct path.
    #
    # @example ![octocat](octocat.png)
    #
    #     "![octocat](octocat.png)" # => "![octocat](section/octocat.png)"
    #
    class MarkdownImagePaths

      #
      # Convert all the image paths within the markdown content with the
      # specified path parameter.
      #
      # @example Update all image paths to be prefixed with 'section'
      #
      #     MarkdownImagePaths.parse(markdown_content, :path => 'section')
      #
      #
      # @param [String] content markdown content that may or may not contain
      #   image tags.
      # @param [Hash] options that contains parameters to help to properly
      #   convert the image path.
      #
      def self.parse(content,options = {})
        return content unless options[:path]

        content.gsub(/!\[([^\]]*)\]\((?!https?:\/\/)(.+)\)/) do |match|
          updated_image_path = File.join(options[:path],$2)
          %{![#{$1}](#{updated_image_path})}
        end

      end
    end

  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
parade-0.10.2 lib/parade/parsers/markdown_image_paths.rb
parade-0.10.1 lib/parade/parsers/markdown_image_paths.rb
parade-0.10.0 lib/parade/parsers/markdown_image_paths.rb
parade-0.9.2 lib/parade/parsers/markdown_image_paths.rb
parade-0.9.1 lib/parade/parsers/markdown_image_paths.rb
parade-0.9.0 lib/parade/parsers/markdown_image_paths.rb
parade-0.8.2 lib/parade/parsers/markdown_image_paths.rb
parade-0.8.1 lib/parade/parsers/markdown_image_paths.rb
parade-0.8.0 lib/parade/parsers/markdown_image_paths.rb