Sha256: 598084be4dc8ad7d187358248658e176eab76a20df4fe24e49afe8d0cc4938d7

Contents?: true

Size: 972 Bytes

Versions: 2

Compression:

Stored size: 972 Bytes

Contents

require 'jekyll'

module Jekyll
  module Asciinema
    class AsciicastTag < Liquid::Tag
      def render(context)
        if tag_contents = parse_tag(@markup.strip)
          asciicast_id = tag_contents[0]
          render_tag(asciicast_id)
        else
          raise ArgumentError.new <<-EOS.gsub(/^ {12}/, '')
            Syntax error in tag 'asciicast' while parsing the following markup:

              #{@markup}

            Valid syntax:
              {% asciicast 123456 %}
          EOS
        end
      end

      private

      def parse_tag(input)
        matched = input.match(/\A\s*(\d+)\s*\Z/)
        [matched[1].strip] if matched && matched.length >= 2
      end

      def render_tag(asciicast_id)
         %Q{<script type="text/javascript" src="https://asciinema.org/a/#{asciicast_id}.js" id="asciicast-#{asciicast_id}" async="async"></script>}
      end
    end
  end
end

Liquid::Template.register_tag('asciicast', Jekyll::Asciinema::AsciicastTag)

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
jekyll-asciinema-0.2.0 lib/jekyll/asciinema/asciicast_tag.rb
jekyll-asciinema-0.1.0 lib/jekyll/asciinema/asciicast_tag.rb