Your SlideShare is downloading. ×
  • Like
Serving Code Samples
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Serving Code Samples

  • 694 views
Published

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

Published in Technology , Travel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
694
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
1
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

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?