The automatic gist!

One thing I wish for any blog platform is the ability to automatically gist all of the code snippets found in a particular blog entry.

I find it very strange that NOBODY (that I know of) has really publicly implemented this as a solution to “gists” for bloggers. The only implementation that I know of was privately announced on a post by Ben Nadel but since then I’ve not found anyone else…

The reason Ben and I like to handle Gists this way is because sometimes your blogs code snippets are better served from your blog… giving you the freedom to later switch syntax highlighters and keep your code indexed within your own blog database or flat files. Removing this dependency on github’s javascript loader, and lettings your code snippets render regardless if github is a slug today or not.

Since I just started using Octopress I’ve decided I’m going to take a stab at implementing this.

Project requirements

  • Fastest and simplest way is to just use the gist gem which offers a command line method for posting gists.
  • Each gist is unique to the post
  • Some sort of unique identifier needs to be referenced everytime a gist is created / updated as to not keep creating new unique gists everytime you compile your Octopress blog.
  • rake generate should handle the gist creation commands, I’m thinking this would be somewhere in the backtick_code_block.rb

WARNING: All code is conceptual, do not use it yet! I’m just brainstorming in this entry…

Modify Octopress Backtick Code Block Plugin

I’m not certain I should be modifying this plugin to make this work but I’m a total Ruby n00b so I’m just finding a nice place to put my thoughts. I’m certain this will end up becoming it’s own plugin.

#!ruby
require './plugins/pygments_code'

module BacktickCodeBlock
  include HighlightCode
  AllOptions = /([^\s]+)\s+(.+?)(https?:\/\/\S+)\s*(.+)?/i
  LangCaption = /([^\s]+)\s*(.+)?/i
  def render_code_block(input)
    @options = nil
    @caption = nil
    @lang = nil
    @url = nil
    @title = nil
    input.gsub(/^`{3} *([^\n]+)?\n(.+?)\n`{3}/m) do
      @options = $1 || ''
      str = $2


      if @options =~ AllOptions
        @lang = $1
        @caption = "<figcaption><span>#{$2}</span><a href='#{$3}'>#{$4 || 'link'}</a></figcaption>"
      elsif @options =~ LangCaption
        @lang = $1
        @caption = "<figcaption><span>#{$2}</span></figcaption>"
      end

      if str.match(/\A( {4}|\t)/)
        str = str.gsub(/^( {4}|\t)/, '')
      end
      if @lang.nil? || @lang == 'plain'
        code = tableize_code(str.gsub('<','&lt;').gsub('>','&gt;'))
        "<figure class='code'>#{@caption}#{code}</figure>"

        system "echo ""#{code}"" | gist -t plain"
      else
        if @lang.include? "-raw"
          raw = "``` #{@options.sub('-raw', '')}\n"
          raw += str
          raw += "\n```\n"
        else
          code = highlight(str, @lang)
          "<figure class='code'>#{@caption}#{code}</figure>"

          system "echo ""#{str}"" | gist -t #{@lang}"
        end
      end
    end
  end
end

A new Octopress AutoGist plugin

No idea how to implement my own Octopress plugin yet but it may look like this?
I’m not sure how to do this but we will need to gather all code blocks into an array…
Save each one as a separate file in a temp directory with a file format based on the title of the code block?

#!ruby #contents of file 1

Would create a file like “_gist_temp/932423_file1.rb

      #contents of file 2

Would create a file like “_gist_temp/932423_file2.rb”

The 932423 would be some sort of “post” id (not sure if one of those exists)
Possibly a hash of the post filename?

#!ruby
require './plugins/pygments_code'

module AutoGist
  include HighlightCode
  AllOptions = /([^\s]+)\s+(.+?)(https?:\/\/\S+)\s*(.+)?/i
  LangCaption = /([^\s]+)\s*(.+)?/i
  def render_code_block(input)
    @options = nil
    @caption = nil
    @lang = nil
    @url = nil
    @title = nil
    input.gsub(/^`{3} *([^\n]+)?\n(.+?)\n`{3}/m) do
      @options = $1 || ''
      str = $2

      if @options =~ AllOptions
        @lang = $1
      elsif @options =~ LangCaption
        @lang = $1
      end

      if str.match(/\A( {4}|\t)/)
        str = str.gsub(/^( {4}|\t)/, '')
      end
      if @lang.nil? || @lang == 'plain'
          system "echo ""#{str}"" | gist -t plain"
      else
          system "echo ""#{str}"" | gist -t #{@lang}"
        end
      end
    end
  end
end

Well, that’s all the time I have for today so I’ll be thinking more about this and make some changes appropriately. If I get a new plugin released, I’ll definitely share it! Let me know if anyone has any input on how they would invision this working.

Okay so here it is…

So I’ve been meaning to start a blog for 6 years now and many attempts have failed thus far.
I can’t make any promises on this one.

Truth is, it’s hard to find time between work and my family time… so I’ve decided to try something new.
I wanted to be able to blog from anywhere, but not necessarily have to be connected to a wordpress account or server to post a new entry.

Decided to try out Octopress since I’m addicted to github and well… why not…

One of the key times during my day that I have free to blog would be during my long commute to and from the office. The only problem is, I’m driving…

I’m going to try to blog by using speech to text dictation and then clean it up when I arrive at my destination.

I would compose a new post using rake new_post[“title”] but then I would just start talking and let it type for me.
I’m thinking if I get my thoughts in a file, I’ll be more likely to follow through with finishing a blog entry and publishing.

So let’s give this a try… stay tuned!

Living in a world without $()

Recently we were presented an opportunity to redevelop and application we originally developed in a very traditional manner.

I say traditional because the most revolutionary difference between it and it’s initial 1.0 release was the strings of jQuery globs in view pages, and some flashy new CSS / UI. We called it 2.0! Oh we thought we had made life spectacular… How awesome is this jQuery stuff…

Of course, it wasn’t our fault… The entire world took jQuery and ran with it like it was the savior of all things JS.

Of course, this post isn’t going to be about jQuery or moving to some other new thing… It’s mostly about what it represents. It’s a cog that filled my time indirectly preventing growth. It’s not it’s fault, or Resig’s fault (even tho I’d like it if I could put it all on him :p) … It’s just life I suppose.

All I can do is try to stay relevant in what seems to be a train of web tech that I’m running beside. No matter which car I leap to, I’m still never caught up.

That’s why I’m chasing the bleeding edge. I’ll never catch it.

Am I a UX Designer? A CTO? a web researcher? a database engineer (is that real?)? a web developer? Front end developer? Today I love coffee-script, yesterday, you loved it. Tomorrow, I think I’m going to like Ruby, three years ago you loved it. Java is dead? I better learn it? ColdFusion? Seriously?

I still feel like the web is so under developed and primitive. Everything that comes along is either a patch or a rigged method to a much larger problem. How can I get my views to stay accurately bound to my models… What if the model changes… Ut oh!

Ok this entry took a very strange turn… I’m just fried.

It’s been a never ending cycle lately with me.

Decide on a framework, or boilerplate my day away…

Should I spend a week trying to figure out how to make commonjs work in the browser? Or spend a week trying to figure out how to use sprockets in a java servlet app?

Should I approach this problem with my own brain? Or scan github’s useless search engine for code that magically solves mine and the worlds problems?

Or maybe I should just make my app work by stringing globs of jquery in my views and focus on delivering my product and live a life regretting I was too tired and lazy to stay relevant.

Or by researching, am I being lazy? Too terrified of the real world of JavaScript to pull the trigger on something and progress?

Am I addicted to preparing to code? Am I so afraid of doing it wrong that I don’t do it at all? Where do I draw the line?

Do I lean on Ember? Or lean on myself with Backbone?

Do I care more about what legacy my code leaves behind me, or what other developers will think of me for how I’ve written it? Do I feel like by taking these extra steps, that someday it will pay off and I’ll stand out in the crowd? Who am I kidding, I’m weaning myself off of jQuery… Am I ahead of the game? Or behind it? Am I above average at what I do? Or below?

What’s the meaning of life?

Okay I’m done.

If you’re still here, I’m sorry!

Hopefully there won’t be many more of these entries… Where is my JavaScript robot?

:-/

2 of 2
12