Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Refactored gist embedding #67

Merged
merged 2 commits into from

2 participants

Craig Wickesser Nate Wienert
Craig Wickesser

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

Craig Wickesser added some commits
Craig Wickesser refactored gist embedding 26379ab
Craig Wickesser removed TODO cc08b20
Nate Wienert natew merged commit 989f066 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 27, 2012
  1. refactored gist embedding

    Craig Wickesser authored
  2. removed TODO

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