Serving
    Code Samples

Lachstock
I was recently on holidays.
Holidays are boring
Holidays are boring




So I wrote code.
Holidays are boring




Then I thought I should blog about it.
Holidays are boring




But I didn’t want to write a long article.
Holidays are boring




Just a snippet.
Holidays are boring




I needed   code section
Holidays are boring




I needed a   code section!
Five busy days later...
The Problem




I needed to display the actual code I wrote.
The Problem




Ordered list with codey bits in LIs?
The Problem




Gist?
The Problem




Surely someone has solved this?
The Problem




Surely someone has solved this?
NO!
The Post
 %h2 HTML

 = code_sample("anchor.html")
The Helper
  def code_sample(filename)
    @filename = filename
    file = "#{options.views}/#{@category}/#{@name}/
  files/#{fi...
The Partial
 .code
   %pre
    %code{:class => @code_class}= @code_snippet
   %span.download
    Download file:
    %a{:hre...
The Browser
  <h2>HTML</h2>
  <div class="code">
   <pre> <code class="html">&lt;a href=&quot;/
  feeds/articles/&quot; id...
The Controller
  get '/*/files/:filename.:filetype' do

 filetype = params[:filetype] == "zip" ? "zip" :
"#{params[:filetype]}.t...
Questions?
Upcoming SlideShare
Loading in …5
×

Serving Code Samples

1,093 views

Published on

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

Published in: Technology, Travel
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,093
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Serving Code Samples

    1. 1. Serving Code Samples Lachstock
    2. 2. I was recently on holidays.
    3. 3. Holidays are boring
    4. 4. Holidays are boring So I wrote code.
    5. 5. Holidays are boring Then I thought I should blog about it.
    6. 6. Holidays are boring But I didn’t want to write a long article.
    7. 7. Holidays are boring Just a snippet.
    8. 8. Holidays are boring I needed code section
    9. 9. Holidays are boring I needed a code section!
    10. 10. Five busy days later...
    11. 11. The Problem I needed to display the actual code I wrote.
    12. 12. The Problem Ordered list with codey bits in LIs?
    13. 13. The Problem Gist?
    14. 14. The Problem Surely someone has solved this?
    15. 15. The Problem Surely someone has solved this?
    16. 16. NO!
    17. 17. The Post %h2 HTML = code_sample("anchor.html")
    18. 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. 19. The Partial .code %pre %code{:class => @code_class}= @code_snippet %span.download Download file: %a{:href => "files/#{@filename}"}= @filename
    20. 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; &lt;span&gt;Subscribe&lt;/span&gt; &lt;/ a&gt;</code></pre> <span class="download"> Download file: <a href="files/anchor.html"> anchor.html </a> </span> </div>
    21. 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. 22. Questions?

    ×