SlideShare a Scribd company logo
1 of 22
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/#{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
The Partial
 .code
   %pre
    %code{:class => @code_class}= @code_snippet
   %span.download
    Download file:
    %a{:href => "files/#{@filename}"}= @filename
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>
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
Questions?

More Related Content

Similar to Serving Code Samples

php-mysql-tutorial-part-3
php-mysql-tutorial-part-3php-mysql-tutorial-part-3
php-mysql-tutorial-part-3
tutorialsruby
 
&lt;b>PHP&lt;/b>/MySQL &lt;b>Tutorial&lt;/b> webmonkey/programming/
&lt;b>PHP&lt;/b>/MySQL &lt;b>Tutorial&lt;/b> webmonkey/programming/&lt;b>PHP&lt;/b>/MySQL &lt;b>Tutorial&lt;/b> webmonkey/programming/
&lt;b>PHP&lt;/b>/MySQL &lt;b>Tutorial&lt;/b> webmonkey/programming/
tutorialsruby
 
php-mysql-tutorial-part-3
php-mysql-tutorial-part-3php-mysql-tutorial-part-3
php-mysql-tutorial-part-3
tutorialsruby
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
tutorialsruby
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
tutorialsruby
 
&lt;b>PHP&lt;/b> Reference: Beginner to Intermediate &lt;b>PHP5&lt;/b>
&lt;b>PHP&lt;/b> Reference: Beginner to Intermediate &lt;b>PHP5&lt;/b>&lt;b>PHP&lt;/b> Reference: Beginner to Intermediate &lt;b>PHP5&lt;/b>
&lt;b>PHP&lt;/b> Reference: Beginner to Intermediate &lt;b>PHP5&lt;/b>
tutorialsruby
 

Similar to Serving Code Samples (20)

Wordpress(css,php,js,ajax)
Wordpress(css,php,js,ajax)Wordpress(css,php,js,ajax)
Wordpress(css,php,js,ajax)
 
Le Wagon Tokyo | Build your Landing Page in 2 hours
Le Wagon Tokyo | Build your Landing Page in 2 hoursLe Wagon Tokyo | Build your Landing Page in 2 hours
Le Wagon Tokyo | Build your Landing Page in 2 hours
 
php-mysql-tutorial-part-3
php-mysql-tutorial-part-3php-mysql-tutorial-part-3
php-mysql-tutorial-part-3
 
&lt;b>PHP&lt;/b>/MySQL &lt;b>Tutorial&lt;/b> webmonkey/programming/
&lt;b>PHP&lt;/b>/MySQL &lt;b>Tutorial&lt;/b> webmonkey/programming/&lt;b>PHP&lt;/b>/MySQL &lt;b>Tutorial&lt;/b> webmonkey/programming/
&lt;b>PHP&lt;/b>/MySQL &lt;b>Tutorial&lt;/b> webmonkey/programming/
 
php-mysql-tutorial-part-3
php-mysql-tutorial-part-3php-mysql-tutorial-part-3
php-mysql-tutorial-part-3
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 
Le Wagon - Build your Landing Page in 2 hours
Le Wagon - Build your Landing Page in 2 hoursLe Wagon - Build your Landing Page in 2 hours
Le Wagon - Build your Landing Page in 2 hours
 
Twig internals - Maksym MoskvychevTwig internals maksym moskvychev
Twig internals - Maksym MoskvychevTwig internals   maksym moskvychevTwig internals - Maksym MoskvychevTwig internals   maksym moskvychev
Twig internals - Maksym MoskvychevTwig internals maksym moskvychev
 
Le Wagon - 2h Landing
Le Wagon - 2h LandingLe Wagon - 2h Landing
Le Wagon - 2h Landing
 
Instagram filters (8 24)
Instagram filters (8 24)Instagram filters (8 24)
Instagram filters (8 24)
 
Professional Help for PowerShell Modules
Professional Help for PowerShell ModulesProfessional Help for PowerShell Modules
Professional Help for PowerShell Modules
 
Coder son site web
Coder son site webCoder son site web
Coder son site web
 
Instagram filters
Instagram filters Instagram filters
Instagram filters
 
Listen afup 2010
Listen afup 2010Listen afup 2010
Listen afup 2010
 
Web 101 intro to html
Web 101  intro to htmlWeb 101  intro to html
Web 101 intro to html
 
WordPress Theme Design - Rich Media Institute Workshop
WordPress Theme Design - Rich Media Institute WorkshopWordPress Theme Design - Rich Media Institute Workshop
WordPress Theme Design - Rich Media Institute Workshop
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 
&lt;b>PHP&lt;/b> Reference: Beginner to Intermediate &lt;b>PHP5&lt;/b>
&lt;b>PHP&lt;/b> Reference: Beginner to Intermediate &lt;b>PHP5&lt;/b>&lt;b>PHP&lt;/b> Reference: Beginner to Intermediate &lt;b>PHP5&lt;/b>
&lt;b>PHP&lt;/b> Reference: Beginner to Intermediate &lt;b>PHP5&lt;/b>
 
Intro to WordPress theme development
Intro to WordPress theme developmentIntro to WordPress theme development
Intro to WordPress theme development
 
HTML Tutorial
HTML TutorialHTML Tutorial
HTML Tutorial
 

More from Lachlan Hardy

Give Your JavaScript Apps Some Spine
Give Your JavaScript Apps Some SpineGive Your JavaScript Apps Some Spine
Give Your JavaScript Apps Some Spine
Lachlan Hardy
 

More from Lachlan Hardy (11)

Give Your JavaScript Apps Some Spine
Give Your JavaScript Apps Some SpineGive Your JavaScript Apps Some Spine
Give Your JavaScript Apps Some Spine
 
Give Your JavaScript Apps A Spine
Give Your JavaScript Apps A SpineGive Your JavaScript Apps A Spine
Give Your JavaScript Apps A Spine
 
SydJS.com
SydJS.comSydJS.com
SydJS.com
 
Yql && Raphaël
Yql && RaphaëlYql && Raphaël
Yql && Raphaël
 
GitHub for JavaScripters
GitHub for JavaScriptersGitHub for JavaScripters
GitHub for JavaScripters
 
The Open Web
The Open WebThe Open Web
The Open Web
 
The Open Web
The Open WebThe Open Web
The Open Web
 
The Open Web
The Open WebThe Open Web
The Open Web
 
Improving Dashboards with open content sharing
Improving Dashboards with open content sharingImproving Dashboards with open content sharing
Improving Dashboards with open content sharing
 
Local Government on the Open Web
Local Government on the Open WebLocal Government on the Open Web
Local Government on the Open Web
 
Welcome to IE8 - Integrating Your Site With Internet Explorer 8
Welcome to IE8 - Integrating Your Site With Internet Explorer 8Welcome to IE8 - Integrating Your Site With Internet Explorer 8
Welcome to IE8 - Integrating Your Site With Internet Explorer 8
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 

Serving Code Samples