Sha256: 3f70c7d1d0c218cbb4ae51dd621b9d9e9929fd960369372c1305d3618f478d28

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

module Nanoc::Toolbox::Helpers

  # NANOC Helper for Github Gists
  #
  # This module contains helper functions to embed gists to your content
  #
  # @see https://gist.github.com/
  #
  # @author Anouar ADLANI <anouar@adlani.com>
  module GithubGist
    include Nanoc::Toolbox::Helpers::HtmlTag

    # GIST script base URL
    GIST_HOST = "https://gist.github.com"
    GIST_EXT  = "js"

    # Generates the script tag for the supplied Gist ID
    # optionaly displays only the specified file
    #
    # @example
    #   gist(1)
    #   #=> <script src="https://gist.github.com/1.js"> </script>
    #   gist(1, "gistfile1.txt")
    #   #=> <script src="https://gist.github.com/1.js?file=gistfile1.txt"></script>
    #
    # @param [Integer] gist_id - the ID of the Gist
    # @param [String] filename - the optional filename to display
    def gist(gist_id, filename=nil)
      raise ArgumentError, "Gist ID should be a hex number" unless (gist_id.to_s).match(/^\h+$/)
      url = "#{GIST_HOST}/#{gist_id}.#{GIST_EXT}"

      if filename
        raise ArgumentError, "Filename should be a string" unless filename.is_a? String
        url += "?file=#{filename}"
      end

      content_tag :script, "", :src => url
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nanoc-toolbox-0.2.1 lib/nanoc/toolbox/helpers/github_gist.rb