class Shortcodes::TutsShortcodes

Constants

SUPPORTED_SYNTAXES

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/shortcodes/tuts_shortcodes.rb, line 31
def initialize
  super

  add_shortcode 'sessions_post' do |att|
    att = {
      'href'  => 'http://sessions.tutsplus.com/creative/',
      'title' => 'Creative Sessions',
      'day'   => '1',
      'prev'  => '',
      'next'  => ''
    }.merge att

    navigation = ''

    if att['prev'].present? || att['next'].present?
      p = att['prev'].present? ? '<a href="' + att['prev'] + '" class="left">&laquo; Session Day ' + (att['day'].to_i - 1).to_s + '</a>' : ''
      n = att['next'].present? ? '<a href="' + att['next'] + '" class="right">Session Day ' + (att['day'].to_i + 1).to_s + ' &raquo;</a>' : ''
      navigation = '<div class="session_navigation" style="overflow:hidden;clear:both;padding-top:10px;">' + p + n + '</div>'
    end

    '<div class="sessions_post">This Post is Day ' + att['day'] + ' of our <a href="' + att['href'] + '">' + att['title'] + ' Session</a>. <a href="http://sessions.tutsplus.com/creative/" class="cs_span">Creative Sessions</a>' + navigation + '</div>'
  end

  add_shortcode 'sponsored_review' do
    '<div class="review-shortcode"><strong>Sponsored Content</strong><p>This is a sponsored review or giveaway of a product/service that\'s particularly relevant to our readers.</p></div>'
  end

  add_shortcode 'sponsored_content' do |attr, content|
    '<div class="sponsored-shortcode"><strong>Sponsored Content</strong><p>This content was commissioned by ' + content.strip + ' and was written and/or edited by the Tuts+ team. Our aim with sponsored content is to publish relevant and objective tutorials, case studies, and inspirational interviews that offer genuine educational value to our readers and enable us to fund the creation of more useful content.</p></div>'
  end

  add_shortcode 'disclaimer' do
    '<div class="disclaimer-shortcode"><strong>Disclaimer</strong><p>You should always seek independent financial advice and thoroughly read terms and conditions relating to any insurance, tax, legal, or financial issue, service, or product. This article is intended as a guide only.</p></div>'
  end

  add_shortcode 'republished' do |attr, content|
    '<div class="republished-shortcode"><strong>Republished Tutorial</strong><p>Every few weeks, we revisit some of our reader\'s favorite posts from throughout the history of the site. This tutorial was first published in ' + content + '.</p></div>'
  end

  add_shortcode 'changed' do |attr, content|
    '<div class="changed-shortcode"><strong>Subsequent Changes to Techniques &amp; Software</strong><p>Certain aspects of applications or techniques used in this tutorial have changed since it was originally published. This might make it a little difficult to follow along. We\'d recommend looking at these more recent tutorials on the same topic:</p>' + content + '</div>'
  end

  add_shortcode 'videodownload' do |attr, content|
    '<div class="videodownload-shortcode"><strong>Download Video</strong>' + content + '</div>'
  end

  add_shortcode 'tip' do |attr, content|
    if content
      '<div class="tip-shortcode">' + content + '</div>';
    else
      ''
    end
  end

  add_shortcode 'related' do |attr, content|
    '<div class="related-shortcode"><strong>Related Posts</strong>' + content + '</div>';
  end

  add_shortcode 'audio' do |att|
    if att.respond_to? :last
      att.delete(":")
      audio_url = att.join(' ').gsub(/^:/, '')
      type = nil
    else
      audio_url = att.values.last
      codec = att.keys.first
      type = case codec
      when 'mp3'
        %Q[type='audio/mpeg; codecs="mp3"']
      when 'ogg','vorbis'
        %Q[type='audio/ogg; codecs="vorbis"']
      else
        nil
      end
    end
    %Q[<audio src="#{audio_url}" #{type} />]
  end

  add_shortcode 'mathjax' do
    '<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>'
  end

  handle_sourcecode = Proc.new do |att, content|
    att = { 'language' => 'plain' } unless att.is_a?(Hash) # Use plain as the default

    att['brush'] = att.delete('language') || att.delete('lang')
    attrs = att.map do |k,v|
      v = "[#{v}]" if k == 'highlight'
      "#{k}: #{v}"
    end.join('; ')

    if content
      # Not left-stripping content because we want to preserve any indentations
      # at the start.
      %(<pre class="#{attrs}">#{content.rstrip}</pre>)
    else
      ''
    end
  end

  add_shortcode 'sourcecode', &handle_sourcecode
  add_shortcode 'code', &handle_sourcecode

  SUPPORTED_SYNTAXES.each do |lang|
    add_shortcode lang do |att, content|
      new_att = { 'language' => lang }
      new_att.merge!(att) if att.is_a? Hash
      handle_sourcecode.call(new_att, content)
    end
  end

  add_shortcode 'quiz' do |att|
    '<div id="quiz-container" style="margin-bottom: 30px;"></div>
    <link rel="stylesheet" href="http://nettuts.s3.amazonaws.com/jquizzy-1.0/css/styles-cloud-version1.30.css" />
    <script src="http://nettuts.s3.amazonaws.com/jquizzy-1.0/js/jquery.jquizzyv1.35.js"></script>
    <script src="' + att['url'] + '"></script>

    <script>
      function initQuiz() {
        if (window.init) {
          $("#quiz-container").jquizzy({
            questions: init.questions,
            resultComments: init.resultComments,
            twitterStatus: "Woo! I got {score} on the @envatopsd quiz. Try it! http://bit.ly/linky",
            twitterUsername: "envatopsd",
            twitterImage: "http://nettuts.s3.amazonaws.com/jquizzy-1.0/img/share.png",
            splashImage: "' + att['image'] + '"
          });
        } else {
          setTimeout(initQuiz, 100);
        }
      }
      $(document).ready(function() { initQuiz() });
    </script>'
  end

  add_shortcode 'polldaddy' do |att|
    %(<script type="text/javascript" charset="utf-8" src="http://static.polldaddy.com/p/#{att['id']}.js"></script>)
  end

  add_shortcode 'WP_UnityObject' do |att|
    # The WP plugin generates an iframe tag to load another php script that embeds the actual content with some JS.
    # To makes things simpler for us, we're going to load the object in place using this method instead (loosely):
    # http://docs.unity3d.com/Documentation/Manual/HTMLcodetoloadUnityWebPlayercontent.html
    #
    # TODO: allow more than one of this in the same page.
    #
    # TODO: (less urgent) load the js from https://ssl-webplayer... when on SSL, could've used '//webplayer.unity3d.com'
    # if unity3d were smarter :/
    %Q{
      <script src="http://webplayer.unity3d.com/download_webplayer-3.x/3.0/uo/UnityObject2.js"></script>
      <script async>$(function(){new UnityObject2().initPlugin($('.unity-player')[0], "#{att['src']}");});</script>
      <div class="unity-player" style="width:#{att['width']}px;height:#{att['height']}px">
        <div class="unity-player__missing">
          <a href="http://unity3d.com/webplayer/">
            <img alt="Unity Web Player. Install now!" src="http://webplayer.unity3d.com/installation/getunity.png" width="193" height="63" />
          </a>
        </div>
      </div>
    }.gsub(/\s+/, ' ').gsub(/>\s+</, '><').strip
  end

  add_shortcode 'caption' do |att, content|
    if match = content.match(/^\s*(<img[^>]*>)\s*(.*)$/)
      %(<figure class="post_image">#{match[1]}<figcaption>#{match[2]}</figcaption></figure>)
    else
      content
    end
  end

  add_shortcode 'player' do |att|
    if src = att['href']
      width = att['width'] || "100%"
      height = att['height'] || "auto"
      %Q{
        <video class="post__content-video" src="#{src}" width="#{width}" height="#{height}" />
      }.strip
    end
  end
end