Class: Shortcodes::TutsShortcodes

Inherits:
WordpressShortcodes show all
Defined in:
lib/shortcodes/tuts_shortcodes.rb

Constant Summary collapse

SUPPORTED_SYNTAXES =
%w(
  as3 actionscript3
  bash shell
  cf coldfusion
  c-sharp csharp
  cpp c
  objc obj-c
  css
  delphi pas pascal
  diff patch
  erl erlang
  groovy
  js jscript javascript
  java
  jfx javafx
  perl pl
  php
  plain text
  ps powershell
  py python
  rails ror ruby rb
  scala
  sql
  vb vbnet
  xml xhtml xslt html xhtml
)

Instance Method Summary collapse

Methods inherited from WordpressShortcodes

#add_shortcode, #do_shortcode_tag, #get_shortcode_regex, #process, #remove_shortcode, #shortcode_parse_atts, #strip_shortcode_tag, #strip_shortcodes

Constructor Details

#initializeTutsShortcodes

Returns a new instance of TutsShortcodes.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# 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