Serving Code Samples
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Serving Code Samples

on

  • 1,325 views

A lightning talk I gave at RORO Sydney on 12 January 2010 about how I set up code samples on http://lachstock.com.au

A lightning talk I gave at RORO Sydney on 12 January 2010 about how I set up code samples on http://lachstock.com.au

Statistics

Views

Total Views
1,325
Views on SlideShare
1,325
Embed Views
0

Actions

Likes
0
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Serving Code Samples Presentation Transcript

  • 1. Serving Code Samples Lachstock
  • 2. I was recently on holidays.
  • 3. Holidays are boring
  • 4. Holidays are boring So I wrote code.
  • 5. Holidays are boring Then I thought I should blog about it.
  • 6. Holidays are boring But I didn’t want to write a long article.
  • 7. Holidays are boring Just a snippet.
  • 8. Holidays are boring I needed code section
  • 9. Holidays are boring I needed a code section!
  • 10. Five busy days later...
  • 11. The Problem I needed to display the actual code I wrote.
  • 12. The Problem Ordered list with codey bits in LIs?
  • 13. The Problem Gist?
  • 14. The Problem Surely someone has solved this?
  • 15. The Problem Surely someone has solved this?
  • 16. NO!
  • 17. The Post %h2 HTML = code_sample("anchor.html")
  • 18. The Helper def code_sample(filename) @filename = filename file = "#{options.views}/#{@category}/#{@name}/ files/#{filename}.txt" if File.exist? file @code_snippet= preserve( escape_once(File.read(file)) ) extension = filename.split('.')[1] @code_class = ( extension == "js" ? "javascript" : extension ) end haml(:"_code_sample", :layout => false) end
  • 19. The Partial .code %pre %code{:class => @code_class}= @code_snippet %span.download Download file: %a{:href => "files/#{@filename}"}= @filename
  • 20. The Browser <h2>HTML</h2> <div class="code"> <pre> <code class="html">&lt;a href=&quot;/ feeds/articles/&quot; id=&quot;feed- button&quot;&gt;&#x000A; &lt;span&gt;Subscribe&lt;/span&gt;&#x000A;&lt;/ a&gt;</code></pre> <span class="download"> Download file: <a href="files/anchor.html"> anchor.html </a> </span> </div>
  • 21. The Controller get '/*/files/:filename.:filetype' do filetype = params[:filetype] == "zip" ? "zip" : "#{params[:filetype]}.txt" file = "#{options.views}/#{params[:splat]}/files/ #{params[:filename]}.#{filetype}" if File.exists? file content_type 'text/plain', :charset => 'utf-8' send_file(file) else raise not_found end end
  • 22. Questions?