From 35e8603d1fa25b8b388875ca743480ec4eb4e1cc Mon Sep 17 00:00:00 2001 From: George Date: Fri, 27 Sep 2013 21:41:23 +0400 Subject: [PATCH 1/2] AsciiDoc template language support --- Gemfile | 1 + README.md | 21 ++++++++++++ README.ru.md | 20 +++++++++++ lib/sinatra/base.rb | 4 +++ test/asciidoctor_test.rb | 72 +++++++++++++++++++++++++++++++++++++++ test/views/hello.asciidoc | 1 + 6 files changed, 119 insertions(+) create mode 100644 test/asciidoctor_test.rb create mode 100644 test/views/hello.asciidoc diff --git a/Gemfile b/Gemfile index 2cf99165..c7f0ec00 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/README.md b/README.md index 8b7eafeb..aae2083f 100644 --- a/README.md +++ b/README.md @@ -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 + + + + + + + + + + + + + + +
DependencyAsciidoctor
File Extension.asciidoc, .adoc and .ad
Exampleasciidoc :README, :layout_engine => :erb
+ +Since you cannot call Ruby methods directly from an AsciiDoc template, you almost +always want to pass locals to it. + #### Radius Templates diff --git a/README.ru.md b/README.ru.md index 20bf48c2..72e4f78f 100644 --- a/README.ru.md +++ b/README.ru.md @@ -702,6 +702,26 @@ erb :overview, :locals => { :text => rdoc(:introduction) } для отображения шаблона, а другой для лэйаута с помощью опции `:layout_engine`. +#### AsciiDoc шаблоны + +
+ + + + + + + + + + + + +
ЗависимостиAsciidoctor
Расширения файлов.asciidoc, .adoc и .ad
Примерasciidoc :README, :layout_engine => :erb
+ +Так как в Radius шаблонах невозможно вызывать методы из Ruby напрямую, то вы +почти всегда будете передавать в шаблон локальные переменные. + #### Radius шаблоны diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index d20e7b46..2411ec02 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -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 diff --git a/test/asciidoctor_test.rb b/test/asciidoctor_test.rb new file mode 100644 index 00000000..02e05ba9 --- /dev/null +++ b/test/asciidoctor_test.rb @@ -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{Hiya}, 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{Hello from AsciiDoc}, 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, '

SPARTA

' + 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, '

Hello World

' + end + + it 'can be used in a nested fashion for partials and whatnot' do + mock_app do + template(:inner) { 'hi' } + template(:outer) { '<%= asciidoc :inner %>' } + get('/') { erb :outer } + end + get '/' + assert ok? + assert_match %r{.*hi

.*
}m, body + end + end +rescue LoadError + warn "#{$!.to_s}: skipping asciidoc tests" +end diff --git a/test/views/hello.asciidoc b/test/views/hello.asciidoc new file mode 100644 index 00000000..1afd1605 --- /dev/null +++ b/test/views/hello.asciidoc @@ -0,0 +1 @@ +== Hello from AsciiDoc From b09f70f66a0b3451ccf081a574d72450c9107b21 Mon Sep 17 00:00:00 2001 From: George Date: Sat, 28 Sep 2013 00:37:10 +0400 Subject: [PATCH 2/2] Fixed typo in AsciiDoc description in russian README Mentioned by @sullenel, thank you. --- README.ru.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.ru.md b/README.ru.md index 72e4f78f..7e5f0141 100644 --- a/README.ru.md +++ b/README.ru.md @@ -719,7 +719,7 @@ erb :overview, :locals => { :text => rdoc(:introduction) }
-Так как в Radius шаблонах невозможно вызывать методы из Ruby напрямую, то вы +Так как в AsciiDoc шаблонах невозможно вызывать методы из Ruby напрямую, то вы почти всегда будете передавать в шаблон локальные переменные. #### Radius шаблоны