Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refactored gist embedding

  • Loading branch information...
commit 26379ab59892bcf374dd6d70f1c98ff7c2c25225 1 parent f5ce330
Craig Wickesser authored
View
1  app/assets/javascripts/application.js
@@ -12,6 +12,7 @@
//
//= require jquery
//= require jquery_ujs
+//= require obtvse.js
//= require fitvids.js
//= require posts.js
//= require showdown.js
View
30 app/assets/javascripts/obtvse.js
@@ -0,0 +1,30 @@
+if (typeof Obtvse === 'undefined') {
+ var displayGist = function(selector, gistResponse) {
+ $(selector).html(gistResponse.div);
+ };
+
+ // TODO: bad, don't add functions to jQuery
+ 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)
+ });
+ }
+ };
+}
View
26 app/assets/javascripts/posts.js
@@ -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');
});
View
37 app/helpers/application_helper.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.