Skip to content
This repository

Refactored gist embedding #67

Merged
merged 2 commits into from almost 2 years ago

2 participants

Craig Wickesser Nathan Wienert
Craig Wickesser

Cleaned up the gist/youtube embedding, I think it's a bit cleaner now.

Nathan Wienert natew merged commit 989f066 into from
Nathan Wienert natew closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

Apr 27, 2012
Craig Wickesser mindscratch refactored gist embedding 26379ab
Craig Wickesser mindscratch removed TODO cc08b20
This page is out of date. Refresh to see the latest.
1  app/assets/javascripts/application.js
@@ -12,6 +12,7 @@
12 12 //
13 13 //= require jquery
14 14 //= require jquery_ujs
  15 +//= require obtvse.js
15 16 //= require fitvids.js
16 17 //= require posts.js
17 18 //= require showdown.js
29 app/assets/javascripts/obtvse.js
... ... @@ -0,0 +1,29 @@
  1 +if (typeof Obtvse === 'undefined') {
  2 + var displayGist = function(selector, gistResponse) {
  3 + $(selector).html(gistResponse.div);
  4 + };
  5 +
  6 + var getGist = function(id, success) {
  7 + $.ajax({
  8 + url: 'https://gist.github.com/' + id + '.json',
  9 + dataType: 'jsonp',
  10 + success: function(gist) {
  11 + success("#" + id, gist);
  12 + }
  13 + });
  14 + };
  15 +
  16 + Obtvse = {
  17 + /**
  18 + * Find all divs that should have gists fetched and rendered.
  19 + *
  20 + * The "id" attribute of the elements that match the given
  21 + * selector should be equal to the gist id.
  22 + */
  23 + renderGists: function(selector) {
  24 + $(selector).each(function() {
  25 + getGist(this.id, displayGist)
  26 + });
  27 + }
  28 + };
  29 +}
26 app/assets/javascripts/posts.js
... ... @@ -1,29 +1,5 @@
1 1 $(function() {
2 2 $('.post').fitVids();
3 3
4   - var printGist = function(gist) {
5   - console.log(gist.repo, ' (' + gist.description + ') :');
6   - console.log(gist.div);
7   - };
8   -
9   - var displayGist = function(selector, gistResponse) {
10   - $(selector).html(gistResponse.div);
11   - };
12   -
13   - // TODO: bad, don't add functions to jQuery
14   - $.getGist = function(id, success) {
15   - $.ajax({
16   - url: 'https://gist.github.com/' + id + '.json',
17   - dataType: 'jsonp',
18   - success: function(gist) {
19   - success("#" + id, gist);
20   - }
21   - });
22   - };
23   -
24   - // fetch the gists
25   - $('.gist').each(function() {
26   - $.getGist(this.id, displayGist);
27   - });
28   -
  4 + Obtvse.renderGists('.gist-files');
29 5 });
37 app/helpers/application_helper.rb
@@ -4,27 +4,28 @@ def is_admin?
4 4 end
5 5
6 6 def markdown(text)
7   - text = youtube_embed(text)
8   - text = gist_embed(text)
9   - RedcarpetCompat.new(text, :fenced_code, :gh_blockcode)
  7 + output = text.lines.map do |line|
  8 + process_line line
  9 + end.join
  10 + RedcarpetCompat.new(output, :fenced_code, :gh_blockcode)
10 11 end
11 12
12   - # TODO refactor these filters so they don't each iterate over all the lines
13   - def gist_embed(str)
14   - output = str.lines.map do |line|
15   - match = nil
16   - match = line.match(/\{\{gist\s+(.*)\}\}/)
17   - match ? "<div id=\"#{match[1]}\" class=\"gist\">Loading gist...</div>" : line
18   - end
19   - output.join
  13 + def process_line(line)
  14 + match = match_gist line
  15 + return "<div id=\"#{match[1]}\" class=\"gist-files\">Loading gist...</div>" if match
  16 +
  17 + match = match_youtube line
  18 + return render(:partial => 'youtube', :locals => { :video => match[1] }) if match
  19 +
  20 + line
20 21 end
21 22
22   - def youtube_embed(str)
23   - output = str.lines.map do |line|
24   - match = nil
25   - match = line.match(/^http.*(?:youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=)([^#\&\?]*).*/)
26   - match ? render(:partial => 'youtube', :locals => { :video => match[1] }) : line
27   - end
28   - output.join
  23 + def match_gist(line)
  24 + line.match(/\{\{gist\s+(.*)\}\}/)
29 25 end
  26 +
  27 + def match_youtube(line)
  28 + line.match(/^http.*(?:youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=)([^#\&\?]*).*/)
  29 + end
  30 +
30 31 end

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.