Browse files

Markdown parsing is done by Kramdown now instead of Redcarpet.

Redcarpet does not parse inline HTML tags properly. Syntax
highlighting comes free with Kramdown in the form of Coderay gem.

The previous commit's generated html files were error prone. These are
due to improper parsing of Redcarpet gem and because of errors in
Readmes which have been corrected now.
  • Loading branch information...
1 parent e4c31bd commit 1089a9cf7282f47c53bf82e21d4a026deb1fa9b2 @kgrz kgrz committed Mar 12, 2013
Showing with 14,506 additions and 7,638 deletions.
  1. +9 −14 Rakefile
  2. +1,726 −926 _includes/README.de.html
  3. +1,662 −855 _includes/README.es.html
  4. +1,823 −1,039 _includes/README.fr.html
  5. +1,771 −933 _includes/README.html
  6. +508 −268 _includes/README.hu.html
  7. +416 −206 _includes/README.jp.html
  8. +1,701 −885 _includes/README.ko.html
  9. +824 −420 _includes/README.pt-br.html
  10. +673 −327 _includes/README.pt-pt.html
  11. +1,707 −918 _includes/README.ru.html
  12. +1,686 −847 _includes/README.zh.html
View
23 Rakefile
@@ -3,15 +3,9 @@ require 'rake/clean'
require 'rdoc/encoding'
require 'rdoc/markup/to_html'
require 'redcarpet'
-require 'rouge'
-require 'rouge/plugins/redcarpet'
require 'uri'
require 'nokogiri'
-
-
-class HTML < Redcarpet::Render::HTML
- include Rouge::Plugins::Redcarpet # yep, that's it.
-end
+require 'kramdown'
def cleanup(html)
@@ -72,7 +66,7 @@ end
task :default => ['_sinatra', '_contrib', :build]
desc "Build outdated static files and API docs"
-task :build => ['build:static']
+task :build => [:pull, 'build:static']
desc "Build outdated static files"
task 'build:static' => readme("_includes/%s.html") + contrib("_includes/%s.html")
@@ -109,16 +103,17 @@ task 'pull:contrib' => "_contrib" do
sh "cd _contrib && git pull &>/dev/null"
end
-readme("_sinatra/%s") { |fn| file fn => '_sinatra' }
+readme("_sinatra/%s.md") { |fn| file fn => '_sinatra' }
file 'AUTHORS' => '_sinatra'
-
readme do |fn|
- file "_includes/#{fn}.html" => ["_sinatra/#{fn}", "Rakefile"] do |f|
- rndr = HTML.new(:safe_links_only => true)
- markdown = Redcarpet::Markdown.new(rndr, :lax_spacing => true, :fenced_code_blocks => true)
+ file "_includes/#{fn}.html" => ["_sinatra/#{fn}.md", "Rakefile"] do |f|
markdown_string = File.read("_sinatra/#{fn}.md").encode('UTF-16le', :invalid => :replace, :replace => "").encode("UTF-8")
- html = cleanup(markdown.render(markdown_string))
+ markdown_string.gsub!(/```(\s?(\w+\n))?/) do |match|
+ match =~ /```\s?\n/ ? "~~~~~\n" : match.sub(/```\s?/, "~~~~")
+ end
+ markdown = Kramdown::Document.new(markdown_string, :fenced_code_blocks => true, :coderay_line_numbers => nil, :auto_ids => false)
+ html = cleanup(markdown.to_html)
File.open(f.name, 'w') { |io| io.write with_toc(html) }
end
end
View
2,652 _includes/README.de.html
1,726 additions, 926 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
2,517 _includes/README.es.html
1,662 additions, 855 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
2,862 _includes/README.fr.html
1,823 additions, 1,039 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
2,704 _includes/README.html
1,771 additions, 933 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
776 _includes/README.hu.html
@@ -34,104 +34,157 @@
</div>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
-
<p><em>Fontos megjegyzés: Ez a dokumentum csak egy fordítása az angol nyelvű
változat, és lehet, hogy nem naprakész.</em></p>
<p>A Sinatra egy <a href="http://en.wikipedia.org/wiki/Domain-specific_language">DSL</a>
webalkalmazások Ruby nyelven történő fejlesztéséhez, minimális
energiabefektetéssel:</p>
-<pre class="highlight ruby"> <span class="c1"># myapp.rb</span>
- <span class="nb">require</span> <span class="s1">'sinatra'</span>
- <span class="n">get</span> <span class="s1">'/'</span> <span class="k">do</span>
- <span class="s1">'Helló Világ!'</span>
- <span class="k">end</span>
-</pre>
+
+<div>
+<div class="CodeRay">
+ <div class="code"><pre> <span style="color:#777"># myapp.rb</span>
+ require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">sinatra</span><span style="color:#710">'</span></span>
+ get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
+ <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Helló Világ!</span><span style="color:#710">'</span></span>
+ <span style="color:#080;font-weight:bold">end</span>
+</pre></div>
+</div>
+</div>
+
<p>Telepítsd a gem-et és indítsd el az alkalmazást a következőképpen:</p>
-<pre class="highlight ruby"> <span class="n">sudo</span> <span class="n">gem</span> <span class="n">install</span> <span class="n">sinatra</span>
- <span class="n">ruby</span> <span class="n">myapp</span><span class="nf">.rb</span>
-</pre>
+
+<div>
+<div class="CodeRay">
+ <div class="code"><pre> sudo gem install sinatra
+ ruby myapp.rb
+</pre></div>
+</div>
+</div>
+
<p>Az alkalmazás elérhető lesz itt: <code>http://localhost:4567</code></p>
<a name='%C3%9Atvonalak%20(routes)'></a>
<h2>Útvonalak (routes)</h2>
<p>A Sinatrában az útvonalat egy HTTP metódus és egy URL-re illeszkedő minta
párosa alkotja. Minden egyes útvonalhoz tartozik egy blokk:</p>
-<pre class="highlight ruby"> <span class="n">get</span> <span class="s1">'/'</span> <span class="k">do</span>
- <span class="nf">..</span> <span class="n">megjelen</span><span class="err">í</span><span class="n">t</span><span class="err">ü</span><span class="n">nk</span> <span class="n">valamit</span> <span class="nf">..</span>
- <span class="k">end</span>
-
- <span class="n">post</span> <span class="s1">'/'</span> <span class="k">do</span>
- <span class="nf">..</span> <span class="n">l</span><span class="err">é</span><span class="n">trehozunk</span> <span class="n">valamit</span> <span class="nf">..</span>
- <span class="k">end</span>
-
- <span class="n">put</span> <span class="s1">'/'</span> <span class="k">do</span>
- <span class="nf">..</span> <span class="n">friss</span><span class="err">í</span><span class="n">t</span><span class="err">ü</span><span class="n">nk</span> <span class="n">valamit</span> <span class="nf">..</span>
- <span class="k">end</span>
-
- <span class="n">delete</span> <span class="s1">'/'</span> <span class="k">do</span>
- <span class="nf">..</span> <span class="n">t</span><span class="err">ö</span><span class="n">rl</span><span class="err">ü</span><span class="n">nk</span> <span class="n">valamit</span> <span class="nf">..</span>
- <span class="k">end</span>
-</pre>
+
+<div>
+<div class="CodeRay">
+ <div class="code"><pre> get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
+ .. megjelenítünk valamit ..
+ <span style="color:#080;font-weight:bold">end</span>
+
+ post <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
+ .. létrehozunk valamit ..
+ <span style="color:#080;font-weight:bold">end</span>
+
+ put <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
+ .. frissítünk valamit ..
+ <span style="color:#080;font-weight:bold">end</span>
+
+ delete <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
+ .. törlünk valamit ..
+ <span style="color:#080;font-weight:bold">end</span>
+</pre></div>
+</div>
+</div>
+
<p>Az útvonalak illeszkedését a rendszer a definiálásuk sorrendjében
ellenőrzi. Sorrendben mindig az első illeszkedő útvonalhoz tartozó metódus kerül
meghívásra.</p>
<p>Az útvonalminták tartalmazhatnak paramétereket is, melyeket a <code>params</code>
hash-ből érhetünk el:</p>
-<pre class="highlight ruby"> <span class="n">get</span> <span class="s1">'/hello/:name'</span> <span class="k">do</span>
- <span class="c1"># illeszkedik a "GET /hello/foo" és a "GET /hello/bar" útvonalakra</span>
- <span class="c1"># ekkor params[:name] értéke 'foo' vagy 'bar' lesz</span>
- <span class="s2">"Helló </span><span class="si">#{</span><span class="n">params</span><span class="o">[</span><span class="ss">:name</span><span class="o">]</span><span class="si">}</span><span class="s2">!"</span>
- <span class="k">end</span>
-</pre>
+
+<div>
+<div class="CodeRay">
+ <div class="code"><pre> get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/hello/:name</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
+ <span style="color:#777"># illeszkedik a "GET /hello/foo" és a "GET /hello/bar" útvonalakra</span>
+ <span style="color:#777"># ekkor params[:name] értéke 'foo' vagy 'bar' lesz</span>
+ <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">Helló </span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span>params[<span style="color:#A60">:name</span>]<span style="font-weight:bold;color:#666">}</span></span><span style="color:#D20">!</span><span style="color:#710">"</span></span>
+ <span style="color:#080;font-weight:bold">end</span>
+</pre></div>
+</div>
+</div>
+
<p>A kulcsszavas argumentumokat (named parameters) blokk paraméterek útján
is el tudod érni:</p>
-<pre class="highlight ruby"> <span class="n">get</span> <span class="s1">'/hello/:name'</span> <span class="k">do</span> <span class="o">|</span><span class="n">n</span><span class="o">|</span>
- <span class="s2">"Helló </span><span class="si">#{</span><span class="n">n</span><span class="si">}</span><span class="s2">!"</span>
- <span class="k">end</span>
-</pre>
+
+<div>
+<div class="CodeRay">
+ <div class="code"><pre> get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/hello/:name</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span> |n|
+ <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">Helló </span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span>n<span style="font-weight:bold;color:#666">}</span></span><span style="color:#D20">!</span><span style="color:#710">"</span></span>
+ <span style="color:#080;font-weight:bold">end</span>
+</pre></div>
+</div>
+</div>
+
<p>Az útvonalmintákban szerepelhetnek joker paraméterek is, melyeket a
<code>params[:splat]</code> tömbön keresztül tudunk elérni.</p>
-<pre class="highlight ruby"> <span class="n">get</span> <span class="s1">'/say/*/to/*'</span> <span class="k">do</span>
- <span class="c1"># illeszkedik a /say/hello/to/world mintára</span>
- <span class="n">params</span><span class="o">[</span><span class="ss">:splat</span><span class="o">]</span> <span class="c1"># =&gt; ["hello", "world"]</span>
- <span class="k">end</span>
-
- <span class="n">get</span> <span class="s1">'/download/*.*'</span> <span class="k">do</span>
- <span class="c1"># illeszkedik a /download/path/to/file.xml mintára</span>
- <span class="n">params</span><span class="o">[</span><span class="ss">:splat</span><span class="o">]</span> <span class="c1"># =&gt; ["path/to/file", "xml"]</span>
- <span class="k">end</span>
-</pre>
+
+<div>
+<div class="CodeRay">
+ <div class="code"><pre> get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/say/*/to/*</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
+ <span style="color:#777"># illeszkedik a /say/hello/to/world mintára</span>
+ params[<span style="color:#A60">:splat</span>] <span style="color:#777"># =&gt; ["hello", "world"]</span>
+ <span style="color:#080;font-weight:bold">end</span>
+
+ get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/download/*.*</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
+ <span style="color:#777"># illeszkedik a /download/path/to/file.xml mintára</span>
+ params[<span style="color:#A60">:splat</span>] <span style="color:#777"># =&gt; ["path/to/file", "xml"]</span>
+ <span style="color:#080;font-weight:bold">end</span>
+</pre></div>
+</div>
+</div>
+
<p>Reguláris kifejezéseket is felvehetünk az útvonalba:</p>
-<pre class="highlight ruby"> <span class="n">get</span> <span class="sr">%r{/hello/([</span><span class="se">\w</span><span class="sr">]+)}</span> <span class="k">do</span>
- <span class="s2">"Helló, </span><span class="si">#{</span><span class="n">params</span><span class="o">[</span><span class="ss">:captures</span><span class="o">]</span><span class="nf">.first</span><span class="si">}</span><span class="s2">!"</span>
- <span class="k">end</span>
-</pre>
+
+<div>
+<div class="CodeRay">
+ <div class="code"><pre> get <span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">%r{</span><span style="color:#808">/hello/([</span><span style="color:#D20">\w</span><span style="color:#808">]+)</span><span style="color:#404">}</span></span> <span style="color:#080;font-weight:bold">do</span>
+ <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">Helló, </span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span>params[<span style="color:#A60">:captures</span>].first<span style="font-weight:bold;color:#666">}</span></span><span style="color:#D20">!</span><span style="color:#710">"</span></span>
+ <span style="color:#080;font-weight:bold">end</span>
+</pre></div>
+</div>
+</div>
+
<p>Vagy blokk paramétereket:</p>
-<pre class="highlight ruby"> <span class="n">get</span> <span class="sr">%r{/hello/([</span><span class="se">\w</span><span class="sr">]+)}</span> <span class="k">do</span> <span class="o">|</span><span class="n">c</span><span class="o">|</span>
- <span class="s2">"Helló, </span><span class="si">#{</span><span class="n">c</span><span class="si">}</span><span class="s2">!"</span>
- <span class="k">end</span>
-</pre>
+
+<div>
+<div class="CodeRay">
+ <div class="code"><pre> get <span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">%r{</span><span style="color:#808">/hello/([</span><span style="color:#D20">\w</span><span style="color:#808">]+)</span><span style="color:#404">}</span></span> <span style="color:#080;font-weight:bold">do</span> |c|
+ <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">Helló, </span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span>c<span style="font-weight:bold;color:#666">}</span></span><span style="color:#D20">!</span><span style="color:#710">"</span></span>
+ <span style="color:#080;font-weight:bold">end</span>
+</pre></div>
+</div>
+</div>
+
<p>Az útvonalak azonban számos egyéb illeszkedési feltétel szerint is
tervezhetők, így például az user agent karakterláncot alapul véve:</p>
-<pre class="highlight ruby"> <span class="n">get</span> <span class="s1">'/foo'</span><span class="p">,</span> <span class="ss">:agent</span> <span class="o">=&gt;</span> <span class="sr">/Songbird (\d\.\d)[\d\/]*?/</span> <span class="k">do</span>
- <span class="s2">"A Songbird </span><span class="si">#{</span><span class="n">params</span><span class="o">[</span><span class="ss">:agent</span><span class="o">][</span><span class="mi">0</span><span class="o">]</span><span class="si">}</span><span class="s2"> verzióját használod"</span>
- <span class="k">end</span>
-
- <span class="n">get</span> <span class="s1">'/foo'</span> <span class="k">do</span>
- <span class="c1"># illeszkedik az egyéb user agentekre</span>
- <span class="k">end</span>
-</pre>
+
+<div>
+<div class="CodeRay">
+ <div class="code"><pre> get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/foo</span><span style="color:#710">'</span></span>, <span style="color:#A60">:agent</span> =&gt; <span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#808">Songbird (</span><span style="color:#D20">\d</span><span style="color:#D20">\.</span><span style="color:#D20">\d</span><span style="color:#808">)[</span><span style="color:#D20">\d</span><span style="color:#D20">\/</span><span style="color:#808">]*?</span><span style="color:#404">/</span></span> <span style="color:#080;font-weight:bold">do</span>
+ <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">A Songbird </span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span>params[<span style="color:#A60">:agent</span>][<span style="color:#00D">0</span>]<span style="font-weight:bold;color:#666">}</span></span><span style="color:#D20"> verzióját használod</span><span style="color:#710">"</span></span>
+ <span style="color:#080;font-weight:bold">end</span>
+
+ get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/foo</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
+ <span style="color:#777"># illeszkedik az egyéb user agentekre</span>
+ <span style="color:#080;font-weight:bold">end</span>
+</pre></div>
+</div>
+</div>
+
<a name='Statikus%20%C3%A1llom%C3%A1nyok'></a>
<h2>Statikus állományok</h2>
<p>A statikus fájlok kiszolgálása a <code>./public</code> könyvtárból
történik, de természetesen más könyvtárat is megadhatsz erre a célra,
mégpedig a :public_folder kapcsoló beállításával:</p>
-<p>set :public<em>folder, File.dirname(</em><em>FILE</em>_) + '/static'</p>
+<p>set :public_folder, File.dirname(<strong>FILE</strong>) + /static</p>
<p>Fontos mgejegyezni, hogy a nyilvános könyvtár neve nem szerepel az URL-ben.
A ./public/css/style.css fájl az
@@ -143,46 +196,64 @@
<p>A sablonfájlokat rendszerint a <code>./views</code> könyvtárba helyezzük, de
itt is lehetőség nyílik egyéb könyvtár használatára:</p>
-<p>set :views, File.dirname(<strong>FILE</strong>) + '/templates'</p>
+<p>set :views, File.dirname(<strong>FILE</strong>) + /templates</p>
<p>Nagyon fontos észben tartani, hogy a sablononkra mindig szimbólumokkal
hivatkozunk, még akkor is, ha egyéb (ebben az esetben a
-:'subdir/template') könyvtárban tároljuk őket. A renderelő
+:subdir/template) könyvtárban tároljuk őket. A renderelő
metódusok minden, nekik közvetlenül átadott karakterláncot megjelenítenek.</p>
<a name='Haml%20sablonok'></a>
<h3>Haml sablonok</h3>
<p>HAML sablonok rendereléséhez szükségünk lesz a haml gem-re vagy könyvtárra:</p>
-<pre class="highlight ruby"> <span class="c1"># Importáljuk be a haml-t az alkalmazásba</span>
- <span class="nb">require</span> <span class="s1">'haml'</span>
- <span class="n">get</span> <span class="s1">'/'</span> <span class="k">do</span>
- <span class="n">haml</span> <span class="ss">:index</span>
- <span class="k">end</span>
-</pre>
+<div>
+<div class="CodeRay">
+ <div class="code"><pre> <span style="color:#777"># Importáljuk be a haml-t az alkalmazásba</span>
+ require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">haml</span><span style="color:#710">'</span></span>
+
+ get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
+ haml <span style="color:#A60">:index</span>
+ <span style="color:#080;font-weight:bold">end</span>
+</pre></div>
+</div>
+</div>
+
<p>Ez szépen lerendereli a <code>./views/index.haml</code> sablont.</p>
<p>A <a href="http://haml.hamptoncatlin.com/docs/rdoc/classes/Haml.html">Haml kapcsolói</a>
globálisan is beállíthatók a Sinatra konfigurációi között, lásd az
<a href="http://www.sinatrarb.com/configuration.html">Options and Configurations</a> lapot.
A globális beállításokat lehetőségünk van felülírni metódus szinten is.</p>
-<pre class="highlight ruby"> <span class="n">set</span> <span class="ss">:haml</span><span class="p">,</span> <span class="p">{</span><span class="ss">:format</span> <span class="o">=&gt;</span> <span class="ss">:html5</span> <span class="p">}</span> <span class="c1"># az alapértelmezett Haml formátum az :xhtml</span>
- <span class="n">get</span> <span class="s1">'/'</span> <span class="k">do</span>
- <span class="n">haml</span> <span class="ss">:index</span><span class="p">,</span> <span class="ss">:haml_options</span> <span class="o">=&gt;</span> <span class="p">{</span><span class="ss">:format</span> <span class="o">=&gt;</span> <span class="ss">:html4</span> <span class="p">}</span> <span class="c1"># immár felülírva</span>
- <span class="k">end</span>
-</pre>
+<div>
+<div class="CodeRay">
+ <div class="code"><pre> set <span style="color:#A60">:haml</span>, {<span style="color:#A60">:format</span> =&gt; <span style="color:#A60">:html5</span> } <span style="color:#777"># az alapértelmezett Haml formátum az :xhtml</span>
+
+ get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
+ haml <span style="color:#A60">:index</span>, <span style="color:#A60">:haml_options</span> =&gt; {<span style="color:#A60">:format</span> =&gt; <span style="color:#A60">:html4</span> } <span style="color:#777"># immár felülírva</span>
+ <span style="color:#080;font-weight:bold">end</span>
+</pre></div>
+</div>
+</div>
+
<a name='Erb%20sablonok'></a>
<h3>Erb sablonok</h3>
<p># Importáljuk be az erb-t az alkalmazásba</p>
-<pre class="highlight ruby"> <span class="nb">require</span> <span class="s1">'erb'</span>
- <span class="n">get</span> <span class="s1">'/'</span> <span class="k">do</span>
- <span class="n">erb</span> <span class="ss">:index</span>
- <span class="k">end</span>
-</pre>
+<div>
+<div class="CodeRay">
+ <div class="code"><pre> require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">erb</span><span style="color:#710">'</span></span>
+
+ get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
+ erb <span style="color:#A60">:index</span>
+ <span style="color:#080;font-weight:bold">end</span>
+</pre></div>
+</div>
+</div>
+
<p>Ez a <code>./views/index.erb</code> sablont fogja lerenderelni.</p>
<a name='Builder%20sablonok'></a>
@@ -192,12 +263,18 @@
rendereléséhez:</p>
<p># Importáljuk be a builder-t az alkalmazásba</p>
-<pre class="highlight ruby"> <span class="nb">require</span> <span class="s1">'builder'</span>
- <span class="n">get</span> <span class="s1">'/'</span> <span class="k">do</span>
- <span class="n">builder</span> <span class="ss">:index</span>
- <span class="k">end</span>
-</pre>
+<div>
+<div class="CodeRay">
+ <div class="code"><pre> require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">builder</span><span style="color:#710">'</span></span>
+
+ get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
+ builder <span style="color:#A60">:index</span>
+ <span style="color:#080;font-weight:bold">end</span>
+</pre></div>
+</div>
+</div>
+
<p>Ez pedig a <code>./views/index.builder</code> állományt fogja renderelni.</p>
<a name='Sass%20sablonok'></a>
@@ -206,30 +283,48 @@
<p>Sass sablonok használatához szükség lesz a haml gem-re vagy könyvtárra:</p>
<p># Be kell importálni a haml, vagy a sass könyvtárat</p>
-<pre class="highlight ruby"> <span class="nb">require</span> <span class="s1">'sass'</span>
- <span class="n">get</span> <span class="s1">'/stylesheet.css'</span> <span class="k">do</span>
- <span class="n">sass</span> <span class="ss">:stylesheet</span>
- <span class="k">end</span>
-</pre>
+<div>
+<div class="CodeRay">
+ <div class="code"><pre> require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">sass</span><span style="color:#710">'</span></span>
+
+ get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/stylesheet.css</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
+ sass <span style="color:#A60">:stylesheet</span>
+ <span style="color:#080;font-weight:bold">end</span>
+</pre></div>
+</div>
+</div>
+
<p>Így a <code>./views/stylesheet.sass</code> fájl máris renderelhető.</p>
<p>A <a href="http://haml.hamptoncatlin.com/docs/rdoc/classes/Sass.html">Sass kapcsolói</a>
globálisan is beállíthatók a Sinatra konfigurációi között, lásd az
<a href="http://www.sinatrarb.com/configuration.html">Options and Configurations</a> lapot.
A globális beállításokat lehetőségünk van felülírni metódus szinten is.</p>
-<pre class="highlight ruby"> <span class="n">set</span> <span class="ss">:sass</span><span class="p">,</span> <span class="p">{</span><span class="ss">:style</span> <span class="o">=&gt;</span> <span class="ss">:compact</span> <span class="p">}</span> <span class="c1"># az alapértelmezett Sass stílus a :nested</span>
- <span class="n">get</span> <span class="s1">'/stylesheet.css'</span> <span class="k">do</span>
- <span class="n">sass</span> <span class="ss">:stylesheet</span><span class="p">,</span> <span class="ss">:sass_options</span> <span class="o">=&gt;</span> <span class="p">{</span><span class="ss">:style</span> <span class="o">=&gt;</span> <span class="ss">:expanded</span> <span class="p">}</span> <span class="c1"># felülírva</span>
- <span class="k">end</span>
-</pre>
+<div>
+<div class="CodeRay">
+ <div class="code"><pre> set <span style="color:#A60">:sass</span>, {<span style="color:#A60">:style</span> =&gt; <span style="color:#A60">:compact</span> } <span style="color:#777"># az alapértelmezett Sass stílus a :nested</span>
+
+ get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/stylesheet.css</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
+ sass <span style="color:#A60">:stylesheet</span>, <span style="color:#A60">:sass_options</span> =&gt; {<span style="color:#A60">:style</span> =&gt; <span style="color:#A60">:expanded</span> } <span style="color:#777"># felülírva</span>
+ <span style="color:#080;font-weight:bold">end</span>
+</pre></div>
+</div>
+</div>
+
<a name='Be%C3%A1gyazott%20sablonok'></a>
<h3>Beágyazott sablonok</h3>
-<pre class="highlight ruby"> <span class="n">get</span> <span class="s1">'/'</span> <span class="k">do</span>
- <span class="n">haml</span> <span class="s1">'%div.title Helló Világ'</span>
- <span class="k">end</span>
-</pre>
+
+<div>
+<div class="CodeRay">
+ <div class="code"><pre> get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
+ haml <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">%div.title Helló Világ</span><span style="color:#710">'</span></span>
+ <span style="color:#080;font-weight:bold">end</span>
+</pre></div>
+</div>
+</div>
+
<p>Lerendereli a beágyazott sablon karakerláncát.</p>
<a name='V%C3%A1ltoz%C3%B3k%20el%C3%A9r%C3%A9se%20a%20sablonokban'></a>
@@ -238,17 +333,29 @@
<p>A sablonok ugyanabban a kontextusban kerülnek kiértékelésre, mint az
útvonal metódusok (route handlers). Az útvonal metódusokban megadott
változók közvetlenül elérhetőek lesznek a sablonokban:</p>
-<pre class="highlight ruby"> <span class="n">get</span> <span class="s1">'/:id'</span> <span class="k">do</span>
- <span class="vi">@foo</span> <span class="o">=</span> <span class="no">Foo</span><span class="nf">.find</span><span class="p">(</span><span class="n">params</span><span class="o">[</span><span class="ss">:id</span><span class="o">]</span><span class="p">)</span>
- <span class="n">haml</span> <span class="s1">'%h1= @foo.name'</span>
- <span class="k">end</span>
-</pre>
+
+<div>
+<div class="CodeRay">
+ <div class="code"><pre> get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/:id</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
+ <span style="color:#33B">@foo</span> = <span style="color:#036;font-weight:bold">Foo</span>.find(params[<span style="color:#A60">:id</span>])
+ haml <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">%h1= @foo.name</span><span style="color:#710">'</span></span>
+ <span style="color:#080;font-weight:bold">end</span>
+</pre></div>
+</div>
+</div>
+
<p>De megadhatod egy lokális változókat tartalmazó explicit hash-ben is:</p>
-<pre class="highlight ruby"> <span class="n">get</span> <span class="s1">'/:id'</span> <span class="k">do</span>
- <span class="n">foo</span> <span class="o">=</span> <span class="no">Foo</span><span class="nf">.find</span><span class="p">(</span><span class="n">params</span><span class="o">[</span><span class="ss">:id</span><span class="o">]</span><span class="p">)</span>
- <span class="n">haml</span> <span class="s1">'%h1= foo.name'</span><span class="p">,</span> <span class="ss">:locals</span> <span class="o">=&gt;</span> <span class="p">{</span> <span class="ss">:foo</span> <span class="o">=&gt;</span> <span class="n">foo</span> <span class="p">}</span>
- <span class="k">end</span>
-</pre>
+
+<div>
+<div class="CodeRay">
+ <div class="code"><pre> get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/:id</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
+ foo = <span style="color:#036;font-weight:bold">Foo</span>.find(params[<span style="color:#A60">:id</span>])
+ haml <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">%h1= foo.name</span><span style="color:#710">'</span></span>, <span style="color:#A60">:locals</span> =&gt; { <span style="color:#A60">:foo</span> =&gt; foo }
+ <span style="color:#080;font-weight:bold">end</span>
+</pre></div>
+</div>
+</div>
+
<p>Ezt leginkább akkor érdemes megtenni, ha partial-eket akarunk renderelni
valamely más sablonból.</p>
@@ -257,75 +364,102 @@
<p>Sablonokat úgy is megadhatunk, hogy egyszerűen az alkalmazás fájl
végére begépeljük őket:</p>
-<pre class="highlight ruby"> <span class="nb">require</span> <span class="s1">'rubygems'</span>
- <span class="nb">require</span> <span class="s1">'sinatra'</span>
- <span class="n">get</span> <span class="s1">'/'</span> <span class="k">do</span>
- <span class="n">haml</span> <span class="ss">:index</span>
- <span class="k">end</span>
+<div>
+<div class="CodeRay">
+ <div class="code"><pre> require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">rubygems</span><span style="color:#710">'</span></span>
+ require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">sinatra</span><span style="color:#710">'</span></span>
+
+ get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
+ haml <span style="color:#A60">:index</span>
+ <span style="color:#080;font-weight:bold">end</span>
- <span class="cp">__END__
+ __END__
- @@ layout
+ <span style="color:#F00;background-color:#FAA">@</span><span style="color:#F00;background-color:#FAA">@</span> layout
%html
- = yield
+ = <span style="color:#080;font-weight:bold">yield</span>
+
+ <span style="color:#F00;background-color:#FAA">@</span><span style="color:#F00;background-color:#FAA">@</span> index
+ %div.title <span style="color:#036;font-weight:bold">Helló</span> Világ!!!!!
+</pre></div>
+</div>
+</div>
- @@ index
- %div.title Helló Világ!!!!!
-</span></pre>
<p>Megjegyzés: azok a fájlon belüli sablonok, amelyek az alkalmazás fájl végére
kerülnek és függnek a sinatra könyvtártól, automatikusan betöltődnek.
Ha ugyanezt más alkalmazásfájlban is szeretnéd megtenni, hívd meg
-a <tt>use<em>in</em>file_templates!</tt> metódust az adott fájlban.</p>
+a <tt>use_in_file_templates!</tt> metódust az adott fájlban.</p>
<a name='Kulcsszavas%20sablonok'></a>
<h3>Kulcsszavas sablonok</h3>
<p>Sablonokat végül a felsőszintű <tt>template</tt> metódussal is
definiálhatunk: </p>
-<pre class="highlight ruby"> <span class="n">template</span> <span class="ss">:layout</span> <span class="k">do</span>
- <span class="s2">"%html</span><span class="se">\n</span><span class="s2"> =yield</span><span class="se">\n</span><span class="s2">"</span>
- <span class="k">end</span>
-
- <span class="n">template</span> <span class="ss">:index</span> <span class="k">do</span>
- <span class="s1">'%div.title Helló Világ!'</span>
- <span class="k">end</span>
-
- <span class="n">get</span> <span class="s1">'/'</span> <span class="k">do</span>
- <span class="n">haml</span> <span class="ss">:index</span>
- <span class="k">end</span>
-</pre>
-<p>Ha létezik "layout" nevű sablon, akkor az minden esetben meghívódik, amikor
+
+<div>
+<div class="CodeRay">
+ <div class="code"><pre> template <span style="color:#A60">:layout</span> <span style="color:#080;font-weight:bold">do</span>
+ <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">%html</span><span style="color:#b0b">\n</span><span style="color:#D20"> =yield</span><span style="color:#b0b">\n</span><span style="color:#710">"</span></span>
+ <span style="color:#080;font-weight:bold">end</span>
+
+ template <span style="color:#A60">:index</span> <span style="color:#080;font-weight:bold">do</span>
+ <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">%div.title Helló Világ!</span><span style="color:#710">'</span></span>
+ <span style="color:#080;font-weight:bold">end</span>
+
+ get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
+ haml <span style="color:#A60">:index</span>
+ <span style="color:#080;font-weight:bold">end</span>
+</pre></div>
+</div>
+</div>
+
+<p>Ha létezik “layout” nevű sablon, akkor az minden esetben meghívódik, amikor
csak egy sablon renderelésre kerül. A layoutokat ki lehet kapcsolni a
<code>:layout =&gt; false</code> meghívásával.</p>
-<pre class="highlight ruby"> <span class="n">get</span> <span class="s1">'/'</span> <span class="k">do</span>
- <span class="n">haml</span> <span class="ss">:index</span><span class="p">,</span> <span class="ss">:layout</span> <span class="o">=&gt;</span> <span class="o">!</span><span class="n">request</span><span class="nf">.xhr?</span>
- <span class="k">end</span>
-</pre>
+
+<div>
+<div class="CodeRay">
+ <div class="code"><pre> get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
+ haml <span style="color:#A60">:index</span>, <span style="color:#A60">:layout</span> =&gt; !request.xhr?
+ <span style="color:#080;font-weight:bold">end</span>
+</pre></div>
+</div>
+</div>
+
<a name='Helperek'></a>
<h2>Helperek</h2>
<p>Használd a felső szintű <tt>helpers</tt> metódust azokhoz a helper
függvényekhez, amiket az útvonal metódusokban és a sablonokban akarsz
használni:</p>
-<pre class="highlight ruby"> <span class="n">helpers</span> <span class="k">do</span>
- <span class="k">def</span> <span class="nf">bar</span><span class="p">(</span><span class="nb">name</span><span class="p">)</span>
- <span class="s2">"</span><span class="si">#{</span><span class="nb">name</span><span class="si">}</span><span class="s2">bar"</span>
- <span class="k">end</span>
- <span class="k">end</span>
-
- <span class="n">get</span> <span class="s1">'/:name'</span> <span class="k">do</span>
- <span class="n">bar</span><span class="p">(</span><span class="n">params</span><span class="o">[</span><span class="ss">:name</span><span class="o">]</span><span class="p">)</span>
- <span class="k">end</span>
-</pre>
+
+<div>
+<div class="CodeRay">
+ <div class="code"><pre> helpers <span style="color:#080;font-weight:bold">do</span>
+ <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">bar</span>(name)
+ <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span>name<span style="font-weight:bold;color:#666">}</span></span><span style="color:#D20">bar</span><span style="color:#710">"</span></span>
+ <span style="color:#080;font-weight:bold">end</span>
+ <span style="color:#080;font-weight:bold">end</span>
+
+ get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/:name</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
+ bar(params[<span style="color:#A60">:name</span>])
+ <span style="color:#080;font-weight:bold">end</span>
+</pre></div>
+</div>
+</div>
+
<a name='Sz%C5%B1r%C5%91k%20(filters)'></a>
<h2>Szűrők (filters)</h2>
<p>Az előszűrők (before filter) az adott hívás kontextusában minden egyes
kérés alkalmával kiértékelődnek, így módosíthatják a kérést és a
választ egyaránt. A szűrőkbe felvett példányváltozók elérhetőek lesznek
az útvonalakban és a sablonokban is:</p>
-<pre class="highlight text"> before do
+
+<div>
+<div class="CodeRay">
+ <div class="code"><pre> before do
@note = 'Csá!'
request.path_info = '/foo/bar/baz'
end
@@ -334,15 +468,24 @@
@note #=&gt; 'Szeva!'
params[:splat] #=&gt; 'bar/baz'
end
-</pre>
+</pre></div>
+</div>
+</div>
+
<p>Az utószűrők az egyes kérések után, az adott kérés kontextusában kerülnek
kiértékelésre, így ezek is képesek módosítani a kérést és a választ egyaránt.
Az előszűrőkben és úvonalakban létrehozott példányváltozók elérhetőek lesznek
az utószűrők számára:</p>
-<pre class="highlight ruby"> <span class="n">after</span> <span class="k">do</span>
- <span class="nb">puts</span> <span class="n">response</span><span class="nf">.status</span>
- <span class="k">end</span>
-</pre>
+
+<div>
+<div class="CodeRay">
+ <div class="code"><pre> after <span style="color:#080;font-weight:bold">do</span>
+ puts response.status
+ <span style="color:#080;font-weight:bold">end</span>
+</pre></div>
+</div>
+</div>
+
<a name='Meg%C3%A1ll%C3%ADt%C3%A1s'></a>
<h2>Megállítás</h2>
@@ -351,28 +494,34 @@
<p>halt</p>
-<p>A megállításkor egy blokktörzset is megadhatsz ...</p>
+<p>A megállításkor egy blokktörzset is megadhatsz </p>
-<p>halt 'ez fog megjelenni a törzsben'</p>
+<p>halt ez fog megjelenni a törzsben</p>
-<p>Vagy állítsd be a HTTP státuszt és a törzset is egyszerre ...</p>
+<p>Vagy állítsd be a HTTP státuszt és a törzset is egyszerre </p>
-<p>halt 401, 'menj innen!'</p>
+<p>halt 401, menj innen!</p>
<a name='Passzol%C3%A1s'></a>
<h2>Passzolás</h2>
<p>Az útvonalak továbbadhatják a végrehajtást egy másik útvonalnak
a <code>pass</code> függvényhívással:</p>
-<pre class="highlight ruby"> <span class="n">get</span> <span class="s1">'/guess/:who'</span> <span class="k">do</span>
- <span class="n">pass</span> <span class="k">unless</span> <span class="n">params</span><span class="o">[</span><span class="ss">:who</span><span class="o">]</span> <span class="o">==</span> <span class="s1">'Frici'</span>
- <span class="s2">"Elkaptál!"</span>
- <span class="k">end</span>
-
- <span class="n">get</span> <span class="s1">'/guess/*'</span> <span class="k">do</span>
- <span class="s2">"Elhibáztál!"</span>
- <span class="k">end</span>
-</pre>
+
+<div>
+<div class="CodeRay">
+ <div class="code"><pre> get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/guess/:who</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
+ pass <span style="color:#080;font-weight:bold">unless</span> params[<span style="color:#A60">:who</span>] == <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Frici</span><span style="color:#710">'</span></span>
+ <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">Elkaptál!</span><span style="color:#710">"</span></span>
+ <span style="color:#080;font-weight:bold">end</span>
+
+ get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/guess/*</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
+ <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">Elhibáztál!</span><span style="color:#710">"</span></span>
+ <span style="color:#080;font-weight:bold">end</span>
+</pre></div>
+</div>
+</div>
+
<p>Az útvonal blokkja azonnal kilép és átadja a vezérlést a következő
illeszkedő útvonalnak. Ha nem talál megfelelő útvonalat, a Sinatra
egy 404-es hibával tér vissza.</p>
@@ -381,21 +530,39 @@
<h2>Beállítások</h2>
<p>Csak indításkor, de minden környezetre érvényesen fusson le:</p>
-<pre class="highlight ruby"> <span class="n">configure</span> <span class="k">do</span>
- <span class="nf">..</span><span class="o">.</span>
- <span class="k">end</span>
-</pre>
+
+<div>
+<div class="CodeRay">
+ <div class="code"><pre> configure <span style="color:#080;font-weight:bold">do</span>
+ ...
+ <span style="color:#080;font-weight:bold">end</span>
+</pre></div>
+</div>
+</div>
+
<p>Csak akkor fusson le, ha a környezet (a RACK_ENV környezeti változóban)
<code>:production</code>-ra van állítva:</p>
-<pre class="highlight ruby"> <span class="n">configure</span> <span class="ss">:production</span> <span class="k">do</span>
- <span class="nf">..</span><span class="o">.</span>
- <span class="k">end</span>
-</pre>
+
+<div>
+<div class="CodeRay">
+ <div class="code"><pre> configure <span style="color:#A60">:production</span> <span style="color:#080;font-weight:bold">do</span>
+ ...
+ <span style="color:#080;font-weight:bold">end</span>
+</pre></div>
+</div>
+</div>
+
<p>Csak akkor fusson le, ha a környezet <tt>:production</tt> vagy <tt>:test</tt>:</p>
-<pre class="highlight ruby"> <span class="n">configure</span> <span class="ss">:production</span><span class="p">,</span> <span class="ss">:test</span> <span class="k">do</span>
- <span class="nf">..</span><span class="o">.</span>
- <span class="k">end</span>
-</pre>
+
+<div>
+<div class="CodeRay">
+ <div class="code"><pre> configure <span style="color:#A60">:production</span>, <span style="color:#A60">:test</span> <span style="color:#080;font-weight:bold">do</span>
+ ...
+ <span style="color:#080;font-weight:bold">end</span>
+</pre></div>
+</div>
+</div>
+
<a name='Hibakezel%C3%A9s'></a>
<h2>Hibakezelés</h2>
@@ -409,33 +576,57 @@
<p>Amikor a <code>Sinatra::NotFound</code> kivétel fellép, vagy a válasz HTTP
státuszkódja 404-es, mindig a <code>not_found</code> metódus hívódik meg.</p>
-<pre class="highlight ruby"> <span class="n">not_found</span> <span class="k">do</span>
- <span class="s1">'Sehol sem találom, amit keresel'</span>
- <span class="k">end</span>
-</pre>
+
+<div>
+<div class="CodeRay">
+ <div class="code"><pre> not_found <span style="color:#080;font-weight:bold">do</span>
+ <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Sehol sem találom, amit keresel</span><span style="color:#710">'</span></span>
+ <span style="color:#080;font-weight:bold">end</span>
+</pre></div>
+</div>
+</div>
+
<a name='Hiba'></a>
<h3>Hiba</h3>
<p>Az <code>error</code> metódus hívódik meg olyankor, amikor egy útvonal, blokk vagy
előszűrő kivételt vált ki. A kivétel objektum lehívható a
<code>sinatra.error</code> Rack változótól:</p>
-<pre class="highlight ruby"> <span class="n">error</span> <span class="k">do</span>
- <span class="s1">'Elnézést, de valami szörnyű hiba lépett fel - '</span> <span class="o">+</span> <span class="n">env</span><span class="o">[</span><span class="s1">'sinatra.error'</span><span class="o">]</span><span class="nf">.name</span>
- <span class="k">end</span>
-</pre>
+
+<div>
+<div class="CodeRay">
+ <div class="code"><pre> error <span style="color:#080;font-weight:bold">do</span>
+ <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Elnézést, de valami szörnyű hiba lépett fel - </span><span style="color:#710">'</span></span> + env[<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">sinatra.error</span><span style="color:#710">'</span></span>].name
+ <span style="color:#080;font-weight:bold">end</span>
+</pre></div>
+</div>
+</div>
+
<p>Egyéni hibakezelés:</p>
-<pre class="highlight ruby"> <span class="n">error</span> <span class="no">MyCustomError</span> <span class="k">do</span>
- <span class="s1">'Szóval az van, hogy...'</span> <span class="o">+</span> <span class="n">env</span><span class="o">[</span><span class="s1">'sinatra.error'</span><span class="o">]</span><span class="nf">.message</span>
- <span class="k">end</span>
-</pre>
+
+<div>
+<div class="CodeRay">
+ <div class="code"><pre> error <span style="color:#036;font-weight:bold">MyCustomError</span> <span style="color:#080;font-weight:bold">do</span>
+ <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Szóval az van, hogy...</span><span style="color:#710">'</span></span> + env[<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">sinatra.error</span><span style="color:#710">'</span></span>].message
+ <span style="color:#080;font-weight:bold">end</span>
+</pre></div>
+</div>
+</div>
+
<p>És amikor fellép:</p>
-<pre class="highlight ruby"> <span class="n">get</span> <span class="s1">'/'</span> <span class="k">do</span>
- <span class="k">raise</span> <span class="no">MyCustomError</span><span class="p">,</span> <span class="s1">'valami nem stimmel!'</span>
- <span class="k">end</span>
-</pre>
+
+<div>
+<div class="CodeRay">
+ <div class="code"><pre> get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
+ raise <span style="color:#036;font-weight:bold">MyCustomError</span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">valami nem stimmel!</span><span style="color:#710">'</span></span>
+ <span style="color:#080;font-weight:bold">end</span>
+</pre></div>
+</div>
+</div>
+
<p>Ez fog megjelenni:</p>
-<p>Szóval az van, hogy... valami nem stimmel!</p>
+<p>Szóval az van, hogy valami nem stimmel!</p>
<p>A Sinatra speciális <code>not_found</code> és <code>error</code> hibakezelőket
használ, amikor a futtatási környezet fejlesztői módba van kapcsolva.</p>
@@ -447,40 +638,58 @@
kiszolgálásakor előfordulhat, hogy a Sinatra nem ismeri fel a fájlok
mime típusát. Ilyenkor használd a +mime_type+ kapcsolót a fájlkiterjesztés
bevezetéséhez:</p>
-<pre class="highlight ruby"> <span class="n">mime_type</span> <span class="ss">:foo</span><span class="p">,</span> <span class="s1">'text/foo'</span>
-</pre>
+
+<div>
+<div class="CodeRay">
+ <div class="code"><pre> mime_type <span style="color:#A60">:foo</span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">text/foo</span><span style="color:#710">'</span></span>
+</pre></div>
+</div>
+</div>
+
<a name='Rack%20Middleware'></a>
<h2>Rack Middleware</h2>
<p>A Sinatra egy Ruby keretrendszerek számára kifejlesztett egyszerű és szabványos
interfészre, a <a href="http://rack.rubyforge.org/">Rack</a> -re épül. A Rack fejlesztői
szempontból egyik legérdekesebb jellemzője, hogy támogatja az úgynevezett
-"middleware" elnevezésű komponenseket, amelyek beékelődnek a szerver és az
+middleware elnevezésű komponenseket, amelyek beékelődnek a szerver és az
alkalmazás közé, így képesek megfigyelni és/vagy módosítani a HTTP
kéréseket és válaszokat. Segítségükkel különféle, egységesen működő
funkciókat építhetünk be rendszerünkbe.</p>
<p>A Sinatra keretrendszerben gyerekjáték a Rack middleware-ek behúzása a
<code>use</code> metódus segítségével:</p>
-<pre class="highlight ruby"> <span class="nb">require</span> <span class="s1">'sinatra'</span>
- <span class="nb">require</span> <span class="s1">'my_custom_middleware'</span>
- <span class="n">use</span> <span class="no">Rack</span><span class="o">::</span><span class="no">Lint</span>
- <span class="n">use</span> <span class="no">MyCustomMiddleware</span>
+<div>
+<div class="CodeRay">
+ <div class="code"><pre> require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">sinatra</span><span style="color:#710">'</span></span>
+ require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">my_custom_middleware</span><span style="color:#710">'</span></span>
+
+ use <span style="color:#036;font-weight:bold">Rack</span>::<span style="color:#036;font-weight:bold">Lint</span>
+ use <span style="color:#036;font-weight:bold">MyCustomMiddleware</span>
+
+ get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/hello</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
+ <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Helló Világ</span><span style="color:#710">'</span></span>
+ <span style="color:#080;font-weight:bold">end</span>
+</pre></div>
+</div>
+</div>
- <span class="n">get</span> <span class="s1">'/hello'</span> <span class="k">do</span>
- <span class="s1">'Helló Világ'</span>
- <span class="k">end</span>
-</pre>
<p>A <code>use</code> metódus szemantikája megegyezik a
<a href="http://rack.rubyforge.org/doc/classes/Rack/Builder.html">Rack::Builder</a> DSL-ben
használt +use+ metóduséval (az említett DSL-t leginkább rackup állományokban
használják). Hogy egy példát említsünk, a <code>use</code> metódus elfogad
változókat és blokkokat egyaránt, akár kombinálva is ezeket:</p>
-<pre class="highlight ruby"> <span class="n">use</span> <span class="no">Rack</span><span class="o">::</span><span class="no">Auth</span><span class="o">::</span><span class="no">Basic</span> <span class="k">do</span> <span class="o">|</span><span class="n">username</span><span class="p">,</span> <span class="n">password</span><span class="o">|</span>
- <span class="n">username</span> <span class="o">==</span> <span class="s1">'admin'</span> <span class="o">&amp;&amp;</span> <span class="n">password</span> <span class="o">==</span> <span class="s1">'titkos'</span>
- <span class="k">end</span>
-</pre>
+
+<div>
+<div class="CodeRay">
+ <div class="code"><pre> use <span style="color:#036;font-weight:bold">Rack</span>::<span style="color:#036;font-weight:bold">Auth</span>::<span style="color:#036;font-weight:bold">Basic</span> <span style="color:#080;font-weight:bold">do</span> |username, password|
+ username == <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">admin</span><span style="color:#710">'</span></span> &amp;&amp; password == <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">titkos</span><span style="color:#710">'</span></span>
+ <span style="color:#080;font-weight:bold">end</span>
+</pre></div>
+</div>
+</div>
+
<p>A Rack terjesztéssel egy csomó alap middleware komponens is érkezik,
amelyekkel a naplózás, URL útvonalak megadása, autentikáció és
munkamenet-kezelés könnyen megvalósítható. A Sinatra ezek közül elég
@@ -493,32 +702,38 @@
<p>Sinatra teszteket bármely Rack alapú tesztelő könyvtárral vagy
keretrendszerrel készíthetsz. Mi a <a href="http://gitrdoc.com/brynary/rack-test">Rack::Test</a>
könyvtárat ajánljuk:</p>
-<pre class="highlight ruby"> <span class="nb">require</span> <span class="s1">'my_sinatra_app'</span>
- <span class="nb">require</span> <span class="s1">'rack/test'</span>
-
- <span class="k">class</span> <span class="nc">MyAppTest</span> <span class="o">&lt;</span> <span class="no">Test</span><span class="o">::</span><span class="no">Unit</span><span class="o">::</span><span class="no">TestCase</span>
- <span class="kp">include</span> <span class="no">Rack</span><span class="o">::</span><span class="no">Test</span><span class="o">::</span><span class="no">Methods</span>
-
- <span class="k">def</span> <span class="nf">app</span>
- <span class="no">Sinatra</span><span class="o">::</span><span class="no">Application</span>
- <span class="k">end</span>
-
- <span class="k">def</span> <span class="nf">test_my_default</span>
- <span class="n">get</span> <span class="s1">'/'</span>
- <span class="n">assert_equal</span> <span class="s1">'Helló Világ!'</span><span class="p">,</span> <span class="n">last_response</span><span class="nf">.body</span>
- <span class="k">end</span>
-
- <span class="k">def</span> <span class="nf">test_with_params</span>
- <span class="n">get</span> <span class="s1">'/meet'</span><span class="p">,</span> <span class="ss">:name</span> <span class="o">=&gt;</span> <span class="s1">'Frici'</span>
- <span class="n">assert_equal</span> <span class="s1">'Helló Frici!'</span><span class="p">,</span> <span class="n">last_response</span><span class="nf">.body</span>
- <span class="k">end</span>
-
- <span class="k">def</span> <span class="nf">test_with_rack_env</span>
- <span class="n">get</span> <span class="s1">'/'</span><span class="p">,</span> <span class="p">{},</span> <span class="s1">'HTTP_USER_AGENT'</span> <span class="o">=&gt;</span> <span class="s1">'Songbird'</span>
- <span class="n">assert_equal</span> <span class="s2">"Songbird-öt használsz!"</span><span class="p">,</span> <span class="n">last_response</span><span class="nf">.body</span>
- <span class="k">end</span>
- <span class="k">end</span>
-</pre>
+
+<div>
+<div class="CodeRay">
+ <div class="code"><pre> require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">my_sinatra_app</span><span style="color:#710">'</span></span>
+ require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">rack/test</span><span style="color:#710">'</span></span>
+
+ <span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">MyAppTest</span> &lt; <span style="color:#036;font-weight:bold">Test</span>::<span style="color:#036;font-weight:bold">Unit</span>::<span style="color:#036;font-weight:bold">TestCase</span>
+ include <span style="color:#036;font-weight:bold">Rack</span>::<span style="color:#036;font-weight:bold">Test</span>::<span style="color:#036;font-weight:bold">Methods</span>
+
+ <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">app</span>
+ <span style="color:#036;font-weight:bold">Sinatra</span>::<span style="color:#036;font-weight:bold">Application</span>
+ <span style="color:#080;font-weight:bold">end</span>
+
+ <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">test_my_default</span>
+ get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span>
+ assert_equal <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Helló Világ!</span><span style="color:#710">'</span></span>, last_response.body
+ <span style="color:#080;font-weight:bold">end</span>
+
+ <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">test_with_params</span>
+ get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/meet</span><span style="color:#710">'</span></span>, <span style="color:#A60">:name</span> =&gt; <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Frici</span><span style="color:#710">'</span></span>
+ assert_equal <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Helló Frici!</span><span style="color:#710">'</span></span>, last_response.body
+ <span style="color:#080;font-weight:bold">end</span>
+
+ <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">test_with_rack_env</span>
+ get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span>, {}, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">HTTP_USER_AGENT</span><span style="color:#710">'</span></span> =&gt; <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Songbird</span><span style="color:#710">'</span></span>
+ assert_equal <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">Songbird-öt használsz!</span><span style="color:#710">"</span></span>, last_response.body
+ <span style="color:#080;font-weight:bold">end</span>
+ <span style="color:#080;font-weight:bold">end</span>
+</pre></div>
+</div>
+</div>
+
<p>Megjegyzés: A beépített Sinatra::Test és Sinatra::TestHarness osztályok
a 0.9.2-es kiadástól kezdve elavultnak számítanak.</p>
@@ -535,24 +750,36 @@
<code>./public</code>
és <code>./views</code> könyvtár meglétét, naplózást, kivételkezelő oldalt stb.).
Itt jön a képbe a Sinatra::Base osztály:</p>
-<pre class="highlight ruby"> <span class="nb">require</span> <span class="s1">'sinatra/base'</span>
- <span class="k">class</span> <span class="nc">MyApp</span> <span class="o">&lt;</span> <span class="no">Sinatra</span><span class="o">::</span><span class="no">Base</span>
- <span class="n">set</span> <span class="ss">:sessions</span><span class="p">,</span> <span class="kp">true</span>
- <span class="n">set</span> <span class="ss">:foo</span><span class="p">,</span> <span class="s1">'bar'</span>
+<div>
+<div class="CodeRay">
+ <div class="code"><pre> require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">sinatra/base</span><span style="color:#710">'</span></span>
+
+ <span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">MyApp</span> &lt; <span style="color:#036;font-weight:bold">Sinatra</span>::<span style="color:#036;font-weight:bold">Base</span>
+ set <span style="color:#A60">:sessions</span>, <span style="color:#069">true</span>
+ set <span style="color:#A60">:foo</span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">bar</span><span style="color:#710">'</span></span>
+
+ get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
+ <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Helló Világ!</span><span style="color:#710">'</span></span>
+ <span style="color:#080;font-weight:bold">end</span>
+ <span style="color:#080;font-weight:bold">end</span>
+</pre></div>
+</div>
+</div>
- <span class="n">get</span> <span class="s1">'/'</span> <span class="k">do</span>
- <span class="s1">'Helló Világ!'</span>
- <span class="k">end</span>
- <span class="k">end</span>
-</pre>
<p>A MyApp osztály immár önálló Rack komponensként, mondjuk Rack middleware-ként
vagy alkalmazásként, esetleg Rails metal-ként is tud működni. Közvetlenül
használhatod (<code>use</code>) vagy futtathatod (<code>run</code>) az osztályodat egy rackup
konfigurációs állományban (<code>config.ru</code>), vagy egy szerverkomponenst
tartalmazó könyvtár vezérlésekor:</p>
-<pre class="highlight ruby"> <span class="no">MyApp</span><span class="nf">.run!</span> <span class="ss">:host</span> <span class="o">=&gt;</span> <span class="s1">'localhost'</span><span class="p">,</span> <span class="ss">:port</span> <span class="o">=&gt;</span> <span class="mi">9090</span>
-</pre>
+
+<div>
+<div class="CodeRay">
+ <div class="code"><pre> <span style="color:#036;font-weight:bold">MyApp</span>.run! <span style="color:#A60">:host</span> =&gt; <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">localhost</span><span style="color:#710">'</span></span>, <span style="color:#A60">:port</span> =&gt; <span style="color:#00D">9090</span>
+</pre></div>
+</div>
+</div>
+
<p>A Sinatra::Base gyermekosztályaiban elérhető metódusok egyúttal a felső
szintű DSL-en keresztül is hozzáférhetők. A legtöbb felső szintű
alkalmazás átalakítható Sinatra::Base alapú komponensekké két lépésben:</p>
@@ -576,8 +803,10 @@
<h2>Parancssori lehetőségek</h2>
<p>Sinatra alkalmazásokat közvetlenül futtathatunk:</p>
-<pre class="highlight text"> ruby myapp.rb [-h] [-x] [-e ENVIRONMENT] [-p PORT] [-s HANDLER]
-</pre>
+
+<pre><code> ruby myapp.rb [-h] [-x] [-e ENVIRONMENT] [-p PORT] [-s HANDLER]
+</code></pre>
+
<p>Az alábbi kapcsolókat ismeri fel a rendszer:</p>
<p>-h # segítség
@@ -593,38 +822,49 @@
készíts egy helyi másolatot és indítsd az alkalmazásodat úgy,
hogy a <code>sinatra/lib</code> könyvtár elérhető legyen a
<code>LOAD_PATH</code>-on:</p>
-<pre class="highlight text"> cd myapp
+
+<pre><code> cd myapp
git clone git://github.com/sinatra/sinatra.git
ruby -Isinatra/lib myapp.rb
-</pre>
+</code></pre>
+
<p>De hozzá is adhatod a <tt>sinatra/lib</tt> könyvtárat a <tt>LOAD_PATH</tt>-hoz
az alkalmazásodban:</p>
-<pre class="highlight ruby"> <span class="vg">$LOAD_PATH</span><span class="nf">.unshift</span> <span class="no">File</span><span class="nf">.dirname</span><span class="p">(</span><span class="kp">__FILE__</span><span class="p">)</span> <span class="o">+</span> <span class="s1">'/sinatra/lib'</span>
- <span class="nb">require</span> <span class="s1">'rubygems'</span>
- <span class="nb">require</span> <span class="s1">'sinatra'</span>
-
- <span class="n">get</span> <span class="s1">'/about'</span> <span class="k">do</span>
- <span class="s2">"A következő változatot futtatom "</span> <span class="o">+</span> <span class="no">Sinatra</span><span class="o">::</span><span class="no">VERSION</span>
- <span class="k">end</span>
-</pre>
+
+<div>
+<div class="CodeRay">
+ <div class="code"><pre> <span style="color:#d70">$LOAD_PATH</span>.unshift <span style="color:#036;font-weight:bold">File</span>.dirname(<span style="color:#069">__FILE__</span>) + <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/sinatra/lib</span><span style="color:#710">'</span></span>
+ require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">rubygems</span><span style="color:#710">'</span></span>
+ require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">sinatra</span><span style="color:#710">'</span></span>
+
+ get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/about</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
+ <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">A következő változatot futtatom </span><span style="color:#710">"</span></span> + <span style="color:#036;font-weight:bold">Sinatra</span>::<span style="color:#036;font-weight:bold">VERSION</span>
+ <span style="color:#080;font-weight:bold">end</span>
+</pre></div>
+</div>
+</div>
+
<p>A Sinatra frissítését később így végezheted el:</p>
-<pre class="highlight text"> cd myproject/sinatra
+
+<pre><code> cd myproject/sinatra
git pull
-</pre>
+</code></pre>
+
<a name='Tov%C3%A1bbi%20inform%C3%A1ci%C3%B3k'></a>
<h2>További információk</h2>
<ul>
<li>
<a href="http://sinatra.github.com/">A projekt weboldala</a> - Kiegészítő dokumentáció,
hírek, hasznos linkek</li>
-<li>
+ <li>
<a href="http://sinatra.github.com/contributing.html">Közreműködés</a> - Hibát találtál?
Segítségre van szükséged? Foltot küldenél be?</li>
-<li>
+ <li>
<a href="http://sinatra.lighthouseapp.com">Lighthouse</a> - Hibakövetés és kiadások</li>
-<li><a href="http://twitter.com/sinatra">Twitter</a></li>
-<li><a href="http://groups.google.com/group/sinatrarb">Levelezőlista</a></li>
-<li>[IRC: #sinatra](irc://chat.freenode.net/#sinatra) a http://freenode.net címen</li>
+ <li><a href="http://twitter.com/sinatra">Twitter</a></li>
+ <li><a href="http://groups.google.com/group/sinatrarb">Levelezőlista</a></li>
+ <li>
+<a href="irc://chat.freenode.net/#sinatra">IRC: #sinatra</a> a http://freenode.net címen</li>
</ul>
</body></html>
View
622 _includes/README.jp.html
@@ -67,24 +67,29 @@
本文書は英語から翻訳したものであり、その内容が最新でない場合もあります。最新の情報はオリジナルの英語版を参照して下さい。</em></p>
<p><a href="http://ja.wikipedia.org/wiki/%E3%83%89%E3%83%A1%E3%82%A4%E3%83%B3%E5%9B%BA%E6%9C%89%E8%A8%80%E8%AA%9E">DSL</a>です。</p>
-<pre class="highlight text"># myapp.rb
+
+<pre><code># myapp.rb
require 'sinatra'
get '/' do
'Hello world!'
end
-</pre>
+</code></pre>
+
<p>gemをインストールして動かしてみる。</p>
-<pre class="highlight text">gem install sinatra
+
+<pre><code>gem install sinatra
ruby myapp.rb
-</pre>
+</code></pre>
+
<p><a href="http://localhost:4567">localhost:4567</a> を見る。</p>
<a name='%E3%83%AB%E3%83%BC%E3%83%88'></a>
<h2>ルート</h2>
<p>Sinatraでは、ルートはHTTPメソッドとURLマッチングパターンがペアになっています。
ルートはブロックに結び付けられています。</p>
-<pre class="highlight text">get '/' do
+
+<pre><code>get '/' do
.. 何か見せる ..
end
@@ -99,26 +104,32 @@
delete '/' do
.. 何か削除する ..
end
-</pre>
+</code></pre>
+
<p>ルートは定義された順番にマッチします。
リクエストに最初にマッチしたルートが呼び出されます。</p>
<p>ルートのパターンは名前付きパラメータを含むことができ、
<code>params</code>ハッシュで取得できます。</p>
-<pre class="highlight text">get '/hello/:name' do
+
+<pre><code>get '/hello/:name' do
# matches "GET /hello/foo" and "GET /hello/bar"
# params[:name] is 'foo' or 'bar'
"Hello #{params[:name]}!"
end
-</pre>
+</code></pre>
+
<p>また、ブロックパラメータで名前付きパラメータにアクセスすることもできます。</p>
-<pre class="highlight text">get '/hello/:name' do |n|
+
+<pre><code>get '/hello/:name' do |n|
"Hello #{n}!"
end
-</pre>
+</code></pre>
+
<p>ルートパターンはsplat(またはワイルドカード)を含むこともでき、
<code>params[:splat]</code> で取得できます。</p>
-<pre class="highlight text">get '/say/*/to/*' do
+
+<pre><code>get '/say/*/to/*' do
# matches /say/hello/to/world
params[:splat] # =&gt; ["hello", "world"]
end
@@ -127,36 +138,46 @@
# matches /download/path/to/file.xml
params[:splat] # =&gt; ["path/to/file", "xml"]
end
-</pre>
+</code></pre>
+
<p>ブロックパラーメータを使用した場合:</p>
-<pre class="highlight text">get '/download/*.*' do |path, ext|
+
+<pre><code>get '/download/*.*' do |path, ext|
[path, ext] # =&gt; ["path/to/file", "xml"]
end
-</pre>
+</code></pre>
+
<p>正規表現を使ったルート:</p>
-<pre class="highlight text">get %r{/hello/([\w]+)} do
+
+<pre><code>get %r{/hello/([\w]+)} do
"Hello, #{params[:captures].first}!"
end
-</pre>
+</code></pre>
+
<p>ブロックパラーメータを使用した場合:</p>
-<pre class="highlight text">get %r{/hello/([\w]+)} do |c|
+
+<pre><code>get %r{/hello/([\w]+)} do |c|
"Hello, #{c}!"
end
-</pre>
+</code></pre>
+
<a name='%E6%9D%A1%E4%BB%B6'></a>
<h3>条件</h3>
<p>ルートにはユーザエージェントのようなさまざまな条件を含めることができます。</p>
-<pre class="highlight text">get '/foo', :agent =&gt; /Songbird (\d\.\d)[\d\/]*?/ do
+
+<pre><code>get '/foo', :agent =&gt; /Songbird (\d\.\d)[\d\/]*?/ do
"You're using Songbird version #{params[:agent][0]}"
end
get '/foo' do
# Matches non-songbird browsers
end
-</pre>
+</code></pre>
+
<p>ほかに<code>host_name</code>と<code>provides</code>条件が利用可能です:</p>
-<pre class="highlight text">get '/', :host_name =&gt; /^admin\./ do
+
+<pre><code>get '/', :host_name =&gt; /^admin\./ do
"Admin Area, Access denied!"
end
@@ -167,9 +188,11 @@
get '/', :provides =&gt; ['rss', 'atom', 'xml'] do
builder :feed
end
-</pre>
+</code></pre>
+
<p>独自の条件を定義することも簡単にできます:</p>
-<pre class="highlight text">set(:probability) { |value| condition { rand &lt;= value } }
+
+<pre><code>set(:probability) { |value| condition { rand &lt;= value } }
get '/win_a_car', :probability =&gt; 0.1 do
"You won!"
@@ -178,7 +201,8 @@
get '/win_a_car' do
"Sorry, you lost."
end
-</pre>
+</code></pre>
+
<a name='%E6%88%BB%E3%82%8A%E5%80%A4'></a>
<h3>戻り値</h3>
@@ -190,21 +214,25 @@
妥当なオブジェクトであればどのようなオブジェクトでも返すことができます:</p>
<p>そのように、例えばストリーミングの例を簡単に実装することができます:</p>
-<pre class="highlight text">class Stream
+
+<pre><code>class Stream
def each
100.times { |i| yield "#{i}\n" }
end
end
get('/') { Stream.new }
-</pre>
+</code></pre>
+
<a name='%E9%9D%99%E7%9A%84%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB'></a>
<h2>静的ファイル</h2>
<p>静的ファイルは<code>./public</code>ディレクトリから配信されます。
<code>:public_folder</code>オプションを指定することで別の場所を指定することができます。</p>
-<pre class="highlight text">set :public_folder, File.dirname(__FILE__) + '/static'
-</pre>
+
+<pre><code>set :public_folder, File.dirname(__FILE__) + '/static'
+</code></pre>
+
<p>注意: この静的ファイル用のディレクトリ名はURL中に含まれません。
例えば、<code>./public/css/style.css</code>は<code>http://example.com/css/style.css</code>でアクセスできます。</p>
@@ -213,8 +241,10 @@
<p>テンプレートは<code>./views</code>ディレクトリ下に配置されています。
他のディレクトリを使用する場合の例:</p>
-<pre class="highlight text">set :views, File.dirname(__FILE__) + '/templates'
-</pre>
+
+<pre><code>set :views, File.dirname(__FILE__) + '/templates'
+</code></pre>
+
<p>テンプレートはシンボルを使用して参照させることを覚えておいて下さい。
サブデレクトリでもこの場合は<code>:'subdir/template'</code>のようにします。
レンダリングメソッドは文字列が渡されると、そのまま文字列を出力します。</p>
@@ -223,306 +253,366 @@
<h3>Haml テンプレート</h3>
<p>hamlを使うにはhamlライブラリが必要です:</p>
-<pre class="highlight text"># hamlを読み込みます
+
+<pre><code># hamlを読み込みます
require 'haml'
get '/' do
haml :index
end
-</pre>
+</code></pre>
+
<p><code>./views/index.haml</code>を表示します。</p>
<p><a href="http://haml.info/docs/yardoc/file.HAML_REFERENCE.html#options">Haml’s
options</a>
はSinatraの設定でグローバルに設定することができます。 <a href="http://www.sinatrarb.com/configuration.html">Options and
Configurations</a>,
を参照してそれぞれ設定を上書きして下さい。</p>
-<pre class="highlight text">set :haml, {:format =&gt; :html5 } # デフォルトのフォーマットは:xhtml
+
+<pre><code>set :haml, {:format =&gt; :html5 } # デフォルトのフォーマットは:xhtml
get '/' do
haml :index, :haml_options =&gt; {:format =&gt; :html4 } # 上書き
end
-</pre>
+</code></pre>
+
<a name='Erb%20%E3%83%86%E3%83%B3%E3%83%97%E3%83%AC%E3%83%BC%E3%83%88'></a>
<h3>Erb テンプレート</h3>
-<pre class="highlight text"># erbを読み込みます
+
+<pre><code># erbを読み込みます
require 'erb'
get '/' do
erb :index
end
-</pre>
+</code></pre>
+
<p><code>./views/index.erb</code>を表示します。</p>
<a name='Erubis'></a>
<h3>Erubis</h3>
<p>erubisテンプレートを表示するには、erubisライブラリが必要です:</p>
-<pre class="highlight text"># erubisを読み込みます
+
+<pre><code># erubisを読み込みます
require 'erubis'
get '/' do
erubis :index
end
-</pre>
+</code></pre>
+
<p><code>./views/index.erubis</code>を表示します。</p>
<a name='Builder%20%E3%83%86%E3%83%B3%E3%83%97%E3%83%AC%E3%83%BC%E3%83%88'></a>
<h3>Builder テンプレート</h3>
<p>builderを使うにはbuilderライブラリが必要です:</p>
-<pre class="highlight text"># builderを読み込みます
+
+<pre><code># builderを読み込みます
require 'builder'
get '/' do
builder :index
end
-</pre>
+</code></pre>
+
<p><code>./views/index.builder</code>を表示します。</p>
<a name='%E9%8B%B8%20%E3%83%86%E3%83%B3%E3%83%97%E3%83%AC%E3%83%BC%E3%83%88'></a>
<h3>鋸 テンプレート</h3>
<p>鋸を使うには鋸ライブラリが必要です:</p>
-<pre class="highlight text"># 鋸を読み込みます
+
+<pre><code># 鋸を読み込みます
require 'nokogiri'
get '/' do
nokogiri :index
end
-</pre>
+</code></pre>
+
<p><code>./views/index.nokogiri</code>を表示します。</p>
<a name='Sass%20%E3%83%86%E3%83%B3%E3%83%97%E3%83%AC%E3%83%BC%E3%83%88'></a>
<h3>Sass テンプレート</h3>
<p>Sassテンプレートを使うにはsassライブラリが必要です:</p>
-<pre class="highlight text"># hamlかsassを読み込みます
+
+<pre><code># hamlかsassを読み込みます
require 'sass'
get '/stylesheet.css' do
sass :stylesheet
end
-</pre>
+</code></pre>
+
<p><code>./views/stylesheet.sass</code>を表示します。</p>
<p><a href="http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#options">Sass’
options</a>
はSinatraの設定でグローバルに設定することができます。 see <a href="http://www.sinatrarb.com/configuration.html">Options and
Configurations</a>,
を参照してそれぞれ設定を上書きして下さい。</p>
-<pre class="highlight text">set :sass, {:style =&gt; :compact } # デフォルトのSass styleは :nested
+
+<pre><code>set :sass, {:style =&gt; :compact } # デフォルトのSass styleは :nested
get '/stylesheet.css' do
sass :stylesheet, :sass_options =&gt; {:style =&gt; :expanded } # 上書き
end
-</pre>
+</code></pre>
+
<a name='Scss%20%E3%83%86%E3%83%B3%E3%83%97%E3%83%AC%E3%83%BC%E3%83%88'></a>
<h3>Scss テンプレート</h3>
<p>Scssテンプレートを使うにはsassライブラリが必要です:</p>
-<pre class="highlight text"># hamlかsassを読み込みます
+
+<pre><code># hamlかsassを読み込みます
require 'sass'
get '/stylesheet.css' do
scss :stylesheet
end
-</pre>
+</code></pre>
+
<p><code>./views/stylesheet.scss</code>を表示します。</p>
<p><a href="http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#options">Sass’
options</a>
はSinatraの設定でグローバルに設定することができます。 see <a href="http://www.sinatrarb.com/configuration.html">Options and
Configurations</a>,
を参照してそれぞれ設定を上書きして下さい。</p>
-<pre class="highlight text">set :scss, :style =&gt; :compact # デフォルトのScss styleは:nested
+
+<pre><code>set :scss, :style =&gt; :compact # デフォルトのScss styleは:nested
get '/stylesheet.css' do
scss :stylesheet, :style =&gt; :expanded # 上書き
end
-</pre>
+</code></pre>
+
<a name='Less%20%E3%83%86%E3%83%B3%E3%83%97%E3%83%AC%E3%83%BC%E3%83%88'></a>
<h3>Less テンプレート</h3>
<p>Lessテンプレートを使うにはlessライブラリが必要です:</p>
-<pre class="highlight text"># lessを読み込みます
+
+<pre><code># lessを読み込みます
require 'less'
get '/stylesheet.css' do
less :stylesheet
end
-</pre>
+</code></pre>
+
<p><code>./views/stylesheet.less</code>を表示します。</p>
<a name='Liquid%20%E3%83%86%E3%83%B3%E3%83%97%E3%83%AC%E3%83%BC%E3%83%88'></a>
<h3>Liquid テンプレート</h3>
<p>Liquidテンプレートを使うにはliquidライブラリが必要です:</p>
-<pre class="highlight text"># liquidを読み込みます
+
+<pre><code># liquidを読み込みます
require 'liquid'
get '/' do
liquid :index
end
-</pre>
+</code></pre>
+
<p><code>./views/index.liquid</code>を表示します。</p>
<p>LiquidテンプレートからRubyのメソッド(<code>yield</code>を除く)を呼び出すことができないため、
ほぼ全ての場合にlocalsを指定する必要があるでしょう:</p>
-<pre class="highlight text">liquid :index, :locals =&gt; { :key =&gt; 'value' }
-</pre>
+
+<pre><code>liquid :index, :locals =&gt; { :key =&gt; 'value' }
+</code></pre>
+
<a name='Markdown%20%E3%83%86%E3%83%B3%E3%83%97%E3%83%AC%E3%83%BC%E3%83%88'></a>
<h3>Markdown テンプレート</h3>
<p>Markdownテンプレートを使うにはrdiscountライブラリが必要です:</p>
-<pre class="highlight text"># rdiscountを読み込みます
+
+<pre><code># rdiscountを読み込みます
require "rdiscount"
get '/' do
markdown :index
end
-</pre>
+</code></pre>
+
<p><code>./views/index.markdown</code>を表示します。(<code>md</code>と<code>mkd</code>も妥当な拡張子です)</p>
<p>markdownからメソッドを呼び出すことも、localsに変数を渡すこともできません。
それゆえ、他のレンダリングエンジンとの組み合わせで使うのが普通です:</p>
-<pre class="highlight text">erb :overview, :locals =&gt; { :text =&gt; markdown(:introduction) }
-</pre>
+
+<pre><code>erb :overview, :locals =&gt; { :text =&gt; markdown(:introduction) }
+</code></pre>
+
<p>他のテンプレートからmarkdownメソッドを呼び出してもよいことに注意してください:</p>
-<pre class="highlight text">%h1 Hello From Haml!
+
+<pre><code>%h1 Hello From Haml!
%p= markdown(:greetings)
-</pre>
+</code></pre>
+
<a name='Textile%20%E3%83%86%E3%83%B3%E3%83%97%E3%83%AC%E3%83%BC%E3%83%88'></a>
<h3>Textile テンプレート</h3>
<p>Textileテンプレートを使うにはRedClothライブラリが必要です:</p>
-<pre class="highlight text"># redclothを読み込みます
+
+<pre><code># redclothを読み込みます
require "redcloth"
get '/' do
textile :index
end
-</pre>
+</code></pre>
+
<p><code>./views/index.textile</code>を表示します。</p>
<p>textileからメソッドを呼び出すことも、localsに変数を渡すこともできません。
それゆえ、他のレンダリングエンジンとの組み合わせで使うのが普通です:</p>
-<pre class="highlight text">erb :overview, :locals =&gt; { :text =&gt; textile(:introduction) }
-</pre>
+
+<pre><code>erb :overview, :locals =&gt; { :text =&gt; textile(:introduction) }
+</code></pre>
+
<p>他のテンプレートからtextileメソッドを呼び出してもよいことに注意してください:</p>
-<pre class="highlight text">%h1 Hello From Haml!
+
+<pre><code>%h1 Hello From Haml!
%p= textile(:greetings)
-</pre>
+</code></pre>
+
<a name='RDoc%20%E3%83%86%E3%83%B3%E3%83%97%E3%83%AC%E3%83%BC%E3%83%88'></a>
<h3>RDoc テンプレート</h3>
<p>RDocテンプレートを使うにはRDocライブラリが必要です:</p>
-<pre class="highlight text"># rdoc/markup/to_htmlを読み込みます
+
+<pre><code># rdoc/markup/to_htmlを読み込みます
require "rdoc"
require "rdoc/markup/to_html"
get '/' do
rdoc :index
end
-</pre>
+</code></pre>
+
<p><code>./views/index.rdoc</code>を表示します。</p>
<p>rdocからメソッドを呼び出すことも、localsに変数を渡すこともできません。
それゆえ、他のレンダリングエンジンとの組み合わせで使うのが普通です:</p>
-<pre class="highlight text">erb :overview, :locals =&gt; { :text =&gt; rdoc(:introduction) }
-</pre>
+
+<pre><code>erb :overview, :locals =&gt; { :text =&gt; rdoc(:introduction) }
+</code></pre>
+
<p>他のテンプレートからrdocメソッドを呼び出してもよいことに注意してください:</p>
-<pre class="highlight text">%h1 Hello From Haml!
+
+<pre><code>%h1 Hello From Haml!
%p= rdoc(:greetings)
-</pre>
+</code></pre>
+
<a name='Radius%20%E3%83%86%E3%83%B3%E3%83%97%E3%83%AC%E3%83%BC%E3%83%88'></a>
<h3>Radius テンプレート</h3>
<p>Radiusテンプレートを使うにはradiusライブラリが必要です:</p>
-<pre class="highlight text"># radiusを読み込みます
+
+<pre><code># radiusを読み込みます
require 'radius'
get '/' do
radius :index
end
-</pre>
+</code></pre>
+
<p><code>./views/index.radius</code>を表示します。</p>
<p>RadiusテンプレートからRubyのメソッド(<code>yield</code>を除く)を呼び出すことができないため、
ほぼ全ての場合にlocalsを指定する必要があるでしょう:</p>
-<pre class="highlight text">radius :index, :locals =&gt; { :key =&gt; 'value' }
-</pre>
+
+<pre><code>radius :index, :locals =&gt; { :key =&gt; 'value' }
+</code></pre>
+
<a name='Markaby%20%E3%83%86%E3%83%B3%E3%83%97%E3%83%AC%E3%83%BC%E3%83%88'></a>
<h3>Markaby テンプレート</h3>
<p>Markabyテンプレートを使うにはmarkabyライブラリが必要です:</p>
-<pre class="highlight text"># markabyを読み込みます
+
+<pre><code># markabyを読み込みます
require 'markaby'
get '/' do
markaby :index
end
-</pre>
+</code></pre>
+
<p><code>./views/index.mab</code>を表示します。</p>
<a name='RABL%20%E3%83%86%E3%83%B3%E3%83%97%E3%83%AC%E3%83%BC%E3%83%88'></a>
<h3>RABL テンプレート</h3>
<p>RABLテンプレートを使うにはrablライブラリが必要です:</p>
-<pre class="highlight text"># rablを読み込みます
+
+<pre><code># rablを読み込みます
require 'rabl'
get '/' do
rabl :index
end
-</pre>
+</code></pre>
+
<p><code>./views/index.rabl</code>を表示します。</p>
<a name='Slim%20%E3%83%86%E3%83%B3%E3%83%97%E3%83%AC%E3%83%BC%E3%83%88'></a>
<h3>Slim テンプレート</h3>
<p>Slimテンプレートを使うにはslimライブラリが必要です:</p>
-<pre class="highlight text"># slimを読み込みます
+
+<pre><code># slimを読み込みます
require 'slim'
get '/' do
slim :index