AsciiDoc template language support

This commit is contained in:
George 2013-09-27 21:41:23 +04:00
parent 7948494fdf
commit 35e8603d1f
6 changed files with 119 additions and 0 deletions

View File

@ -41,6 +41,7 @@ gem 'maruku'
gem 'creole'
gem 'markaby'
gem 'radius'
gem 'asciidoctor'
unless RUBY_ENGINE =~ /jruby|maglev/
gem 'rabl'
gem 'activesupport', '< 4.0.0' if RUBY_VERSION < '1.9.3'

View File

@ -52,6 +52,7 @@ pick up if available.
* [Markdown Templates](#markdown-templates)
* [Textile Templates](#textile-templates)
* [RDoc Templates](#rdoc-templates)
* [AsciiDoc Templates](#asciidoc-templates)
* [Radius Templates](#radius-templates)
* [Markaby Templates](#markaby-templates)
* [RABL Templates](#rabl-templates)
@ -785,6 +786,26 @@ Since you cannot call Ruby from RDoc, you cannot use layouts written in
RDoc. However, it is possible to use another rendering engine for the
template than for the layout by passing the `:layout_engine` option.
#### AsciiDoc Templates
<table>
<tr>
<td>Dependency</td>
<td><a href="http://asciidoctor.org/" title="Asciidoctor">Asciidoctor</a></td>
</tr>
<tr>
<td>File Extension</td>
<td><tt>.asciidoc</tt>, <tt>.adoc</tt> and <tt>.ad</tt></td>
</tr>
<tr>
<td>Example</td>
<td><tt>asciidoc :README, :layout_engine => :erb</tt></td>
</tr>
</table>
Since you cannot call Ruby methods directly from an AsciiDoc template, you almost
always want to pass locals to it.
#### Radius Templates
<table>

View File

@ -702,6 +702,26 @@ erb :overview, :locals => { :text => rdoc(:introduction) }
для отображения шаблона, а другой для лэйаута с помощью опции
`:layout_engine`.
#### AsciiDoc шаблоны
<table>
<tr>
<td>Зависимости</td>
<td><a href="http://asciidoctor.org/" title="Asciidoctor">Asciidoctor</a></td>
</tr>
<tr>
<td>Расширения файлов</td>
<td><tt>.asciidoc</tt>, <tt>.adoc</tt> и <tt>.ad</tt></td>
</tr>
<tr>
<td>Пример</td>
<td><tt>asciidoc :README, :layout_engine => :erb</tt></td>
</tr>
</table>
Так как в Radius шаблонах невозможно вызывать методы из Ruby напрямую, то вы
почти всегда будете передавать в шаблон локальные переменные.
#### Radius шаблоны
<table>

View File

@ -715,6 +715,10 @@ module Sinatra
render :rdoc, template, options, locals
end
def asciidoc(template, options = {}, locals = {})
render :asciidoc, template, options, locals
end
def radius(template, options = {}, locals = {})
render :radius, template, options, locals
end

72
test/asciidoctor_test.rb Normal file
View File

@ -0,0 +1,72 @@
require File.expand_path('../helper', __FILE__)
begin
require 'asciidoctor'
class AsciidoctorTest < Test::Unit::TestCase
def asciidoc_app(&block)
mock_app do
set :views, File.dirname(__FILE__) + '/views'
get('/', &block)
end
get '/'
end
it 'renders inline AsciiDoc strings' do
asciidoc_app { asciidoc '== Hiya' }
assert ok?
assert_match %r{<h2.*?>Hiya</h2>}, body
end
it 'uses the correct engine' do
engine = Tilt::AsciidoctorTemplate
assert_equal engine, Tilt[:ad]
assert_equal engine, Tilt[:adoc]
assert_equal engine, Tilt[:asciidoc]
end
it 'renders .asciidoc files in views path' do
asciidoc_app { asciidoc :hello }
assert ok?
assert_match %r{<h2.*?>Hello from AsciiDoc</h2>}, body
end
it 'raises error if template not found' do
mock_app { get('/') { asciidoc :no_such_template } }
assert_raise(Errno::ENOENT) { get('/') }
end
it 'renders with inline layouts' do
mock_app do
layout { 'THIS. IS. #{yield.upcase}!' }
get('/') { asciidoc 'Sparta', :layout_engine => :str }
end
get '/'
assert ok?
assert_include body, 'THIS. IS.'
assert_include body, '<P>SPARTA</P>'
end
it 'renders with file layouts' do
asciidoc_app do
asciidoc 'Hello World', :layout => :layout2, :layout_engine => :erb
end
assert ok?
assert_include body, 'ERB Layout!'
assert_include body, '<p>Hello World</p>'
end
it 'can be used in a nested fashion for partials and whatnot' do
mock_app do
template(:inner) { 'hi' }
template(:outer) { '<outer><%= asciidoc :inner %></outer>' }
get('/') { erb :outer }
end
get '/'
assert ok?
assert_match %r{<outer>.*<p.*?>hi</p>.*</outer>}m, body
end
end
rescue LoadError
warn "#{$!.to_s}: skipping asciidoc tests"
end

View File

@ -0,0 +1 @@
== Hello from AsciiDoc