1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
|
# encoding: UTF-8
unless defined? ASCIIDOCTOR_PROJECT_DIR
$: << File.dirname(__FILE__); $:.uniq!
require 'test_helper'
end
require 'tilt' unless defined? ::Tilt
context 'Converter' do
context 'View options' do
test 'should set Haml format to html5 for html5 backend' do
doc = Asciidoctor::Document.new [], :template_dir => File.join(File.dirname(__FILE__), 'fixtures', 'custom-backends', 'haml'), :template_cache => false
assert doc.converter.is_a?(Asciidoctor::Converter::CompositeConverter)
selected = doc.converter.find_converter('paragraph')
assert selected.is_a? Asciidoctor::Converter::TemplateConverter
assert selected.templates['paragraph'].is_a? Tilt::HamlTemplate
assert_equal :html5, selected.templates['paragraph'].options[:format]
end
test 'should set Haml format to xhtml for docbook backend' do
doc = Asciidoctor::Document.new [], :backend => 'docbook45', :template_dir => File.join(File.dirname(__FILE__), 'fixtures', 'custom-backends', 'haml'), :template_cache => false
assert doc.converter.is_a?(Asciidoctor::Converter::CompositeConverter)
selected = doc.converter.find_converter('paragraph')
assert selected.is_a? Asciidoctor::Converter::TemplateConverter
assert selected.templates['paragraph'].is_a? Tilt::HamlTemplate
assert_equal :xhtml, selected.templates['paragraph'].options[:format]
end
test 'should set Slim format to html for html5 backend' do
doc = Asciidoctor::Document.new [], :template_dir => File.join(File.dirname(__FILE__), 'fixtures', 'custom-backends', 'slim'), :template_cache => false
assert doc.converter.is_a?(Asciidoctor::Converter::CompositeConverter)
selected = doc.converter.find_converter('paragraph')
assert selected.is_a? Asciidoctor::Converter::TemplateConverter
assert selected.templates['paragraph'].is_a? Slim::Template
assert_equal :html, selected.templates['paragraph'].options[:format]
end
test 'should set Slim format to nil for docbook backend' do
doc = Asciidoctor::Document.new [], :backend => 'docbook45', :template_dir => File.join(File.dirname(__FILE__), 'fixtures', 'custom-backends', 'slim'), :template_cache => false
assert doc.converter.is_a?(Asciidoctor::Converter::CompositeConverter)
selected = doc.converter.find_converter('paragraph')
assert selected.is_a? Asciidoctor::Converter::TemplateConverter
assert selected.templates['paragraph'].is_a? Slim::Template
assert_nil selected.templates['paragraph'].options[:format]
end
test 'should set safe mode of Slim AsciiDoc engine to match document safe mode when Slim >= 3' do
doc = Asciidoctor::Document.new [], :template_dir => File.join(File.dirname(__FILE__), 'fixtures', 'custom-backends', 'slim'), :template_cache => false, :safe => :unsafe
assert doc.converter.is_a?(Asciidoctor::Converter::CompositeConverter)
selected = doc.converter.find_converter('paragraph')
assert selected.is_a? Asciidoctor::Converter::TemplateConverter
slim_asciidoc_opts = selected.instance_variable_get(:@engine_options)[:slim][:asciidoc]
if ::Slim::VERSION >= '3.0'
assert_equal({ :safe => Asciidoctor::SafeMode::UNSAFE }, slim_asciidoc_opts)
else
assert_nil slim_asciidoc_opts
end
end
test 'should support custom template engine options for known engine' do
doc = Asciidoctor::Document.new [], :template_dir => File.join(File.dirname(__FILE__), 'fixtures', 'custom-backends', 'slim'), :template_cache => false, :template_engine_options => { :slim => { :pretty => true } }
assert doc.converter.is_a?(Asciidoctor::Converter::CompositeConverter)
selected = doc.converter.find_converter('paragraph')
assert selected.is_a? Asciidoctor::Converter::TemplateConverter
assert selected.templates['paragraph'].is_a? Slim::Template
assert_equal true, selected.templates['paragraph'].options[:pretty]
end
test 'should support custom template engine options' do
doc = Asciidoctor::Document.new [], :template_dir => File.join(File.dirname(__FILE__), 'fixtures', 'custom-backends', 'slim'), :template_cache => false, :template_engine_options => { :slim => { :pretty => true } }
assert doc.converter.is_a?(Asciidoctor::Converter::CompositeConverter)
selected = doc.converter.find_converter('paragraph')
assert selected.is_a? Asciidoctor::Converter::TemplateConverter
assert selected.templates['paragraph'].is_a? Slim::Template
assert_equal false, selected.templates['paragraph'].options[:sort_attrs]
assert_equal true, selected.templates['paragraph'].options[:pretty]
end
end
context 'Custom backends' do
test 'should load Haml templates for default backend' do
doc = Asciidoctor::Document.new [], :template_dir => File.join(File.dirname(__FILE__), 'fixtures', 'custom-backends', 'haml'), :template_cache => false
assert doc.converter.is_a?(Asciidoctor::Converter::CompositeConverter)
['paragraph', 'sidebar'].each do |node_name|
selected = doc.converter.find_converter node_name
assert selected.is_a? Asciidoctor::Converter::TemplateConverter
assert selected.templates[node_name].is_a? Tilt::HamlTemplate
assert_equal %(block_#{node_name}.html.haml), File.basename(selected.templates[node_name].file)
end
end
test 'should set outfilesuffix according to backend info' do
doc = Asciidoctor.load 'content'
doc.render
assert_equal '.html', doc.attributes['outfilesuffix']
doc = Asciidoctor.load 'content', :template_dir => File.join(File.dirname(__FILE__), 'fixtures', 'custom-backends', 'haml'), :template_cache => false
doc.render
assert_equal '.html', doc.attributes['outfilesuffix']
end
test 'should not override outfilesuffix attribute if locked' do
doc = Asciidoctor.load 'content', :attributes => {'outfilesuffix' => '.foo'}
doc.render
assert_equal '.foo', doc.attributes['outfilesuffix']
doc = Asciidoctor.load 'content', :template_dir => File.join(File.dirname(__FILE__), 'fixtures', 'custom-backends', 'haml'), :template_cache => false, :attributes => {'outfilesuffix' => '.foo'}
doc.render
assert_equal '.foo', doc.attributes['outfilesuffix']
end
test 'should load Haml templates for docbook45 backend' do
doc = Asciidoctor::Document.new [], :backend => 'docbook45', :template_dir => File.join(File.dirname(__FILE__), 'fixtures', 'custom-backends', 'haml'), :template_cache => false
assert doc.converter.is_a?(Asciidoctor::Converter::CompositeConverter)
['paragraph'].each do |node_name|
selected = doc.converter.find_converter node_name
assert selected.is_a? Asciidoctor::Converter::TemplateConverter
assert selected.templates[node_name].is_a? Tilt::HamlTemplate
assert_equal %(block_#{node_name}.xml.haml), File.basename(selected.templates[node_name].file)
end
end
test 'should use Haml templates in place of built-in templates' do
input = <<-EOS
= Document Title
Author Name
== Section One
Sample paragraph
.Related
****
Sidebar content
****
EOS
output = render_embedded_string input, :template_dir => File.join(File.dirname(__FILE__), 'fixtures', 'custom-backends', 'haml'), :template_cache => false
assert_xpath '/*[@class="sect1"]/*[@class="sectionbody"]/p', output, 1
assert_xpath '//aside', output, 1
assert_xpath '/*[@class="sect1"]/*[@class="sectionbody"]/p/following-sibling::aside', output, 1
assert_xpath '//aside/header/h1[text()="Related"]', output, 1
assert_xpath '//aside/header/following-sibling::p[text()="Sidebar content"]', output, 1
end
test 'should use built-in global cache to cache templates' do
begin
# clear out any cache, just to be sure
Asciidoctor::Converter::TemplateConverter.clear_caches if defined? Asciidoctor::Converter::TemplateConverter
template_dir = File.join(File.dirname(__FILE__), 'fixtures', 'custom-backends', 'haml')
doc = Asciidoctor::Document.new [], :template_dir => template_dir
doc.converter
caches = Asciidoctor::Converter::TemplateConverter.caches
if defined? ::ThreadSafe::Cache
assert caches[:templates].is_a?(::ThreadSafe::Cache)
assert !caches[:templates].empty?
paragraph_template_before = caches[:templates].values.find {|t| File.basename(t.file) == 'block_paragraph.html.haml' }
assert !paragraph_template_before.nil?
# should use cache
doc = Asciidoctor::Document.new [], :template_dir => template_dir
template_converter = doc.converter.find_converter('paragraph')
paragraph_template_after = template_converter.templates['paragraph']
assert !paragraph_template_after.nil?
assert paragraph_template_before.eql?(paragraph_template_after)
# should not use cache
doc = Asciidoctor::Document.new [], :template_dir => template_dir, :template_cache => false
template_converter = doc.converter.find_converter('paragraph')
paragraph_template_after = template_converter.templates['paragraph']
assert !paragraph_template_after.nil?
assert !paragraph_template_before.eql?(paragraph_template_after)
else
assert caches.empty?
end
ensure
# clean up
Asciidoctor::Converter::TemplateConverter.clear_caches if defined? Asciidoctor::Converter::TemplateConverter
end
end
test 'should use custom cache to cache templates' do
template_dir = File.join(File.dirname(__FILE__), 'fixtures', 'custom-backends', 'haml')
Asciidoctor::PathResolver.new.system_path(File.join(template_dir, 'html5', 'block_paragraph.html.haml'), nil)
caches = { :scans => {}, :templates => {} }
doc = Asciidoctor::Document.new [], :template_dir => template_dir, :template_cache => caches
doc.converter
assert !caches[:scans].empty?
assert !caches[:templates].empty?
paragraph_template = caches[:templates].values.find {|t| File.basename(t.file) == 'block_paragraph.html.haml' }
assert !paragraph_template.nil?
assert paragraph_template.is_a? ::Tilt::HamlTemplate
end
test 'should be able to disable template cache' do
begin
# clear out any cache, just to be sure
Asciidoctor::Converter::TemplateConverter.clear_caches if defined? Asciidoctor::Converter::TemplateConverter
doc = Asciidoctor::Document.new [], :template_dir => File.join(File.dirname(__FILE__), 'fixtures', 'custom-backends', 'haml'),
:template_cache => false
doc.converter
caches = Asciidoctor::Converter::TemplateConverter.caches
assert caches.empty? || caches[:scans].empty?
assert caches.empty? || caches[:templates].empty?
ensure
# clean up
Asciidoctor::Converter::TemplateConverter.clear_caches if defined? Asciidoctor::Converter::TemplateConverter
end
end
test 'should load ERB templates using ERBTemplate if eruby is not set' do
doc = Asciidoctor::Document.new [], :template_dir => File.join(File.dirname(__FILE__), 'fixtures', 'custom-backends', 'erb'), :template_cache => false
assert doc.converter.is_a?(Asciidoctor::Converter::CompositeConverter)
['paragraph'].each do |node_name|
selected = doc.converter.find_converter node_name
assert selected.is_a? Asciidoctor::Converter::TemplateConverter
template = selected.templates[node_name]
assert template.is_a? Tilt::ERBTemplate
assert !(template.is_a? Tilt::ErubisTemplate)
assert template.instance_variable_get('@engine').is_a? ::ERB
assert_equal %(block_#{node_name}.html.erb), File.basename(selected.templates[node_name].file)
end
end
test 'should load ERB templates using ErubisTemplate if eruby is set to erubis' do
doc = Asciidoctor::Document.new [], :template_dir => File.join(File.dirname(__FILE__), 'fixtures', 'custom-backends', 'erb'), :template_cache => false, :eruby => 'erubis'
assert doc.converter.is_a?(Asciidoctor::Converter::CompositeConverter)
['paragraph'].each do |node_name|
selected = doc.converter.find_converter node_name
assert selected.is_a? Asciidoctor::Converter::TemplateConverter
template = selected.templates[node_name]
assert template.is_a? Tilt::ERBTemplate
assert template.is_a? Tilt::ErubisTemplate
assert template.instance_variable_get('@engine').is_a? ::Erubis::FastEruby
assert_equal %(block_#{node_name}.html.erb), File.basename(selected.templates[node_name].file)
end
end
test 'should load Slim templates for default backend' do
doc = Asciidoctor::Document.new [], :template_dir => File.join(File.dirname(__FILE__), 'fixtures', 'custom-backends', 'slim'), :template_cache => false
assert doc.converter.is_a?(Asciidoctor::Converter::CompositeConverter)
['paragraph', 'sidebar'].each do |node_name|
selected = doc.converter.find_converter node_name
assert selected.is_a? Asciidoctor::Converter::TemplateConverter
assert selected.templates[node_name].is_a? Slim::Template
assert_equal %(block_#{node_name}.html.slim), File.basename(selected.templates[node_name].file)
end
end
test 'should load Slim templates for docbook45 backend' do
doc = Asciidoctor::Document.new [], :backend => 'docbook45', :template_dir => File.join(File.dirname(__FILE__), 'fixtures', 'custom-backends', 'slim'), :template_cache => false
assert doc.converter.is_a?(Asciidoctor::Converter::CompositeConverter)
['paragraph'].each do |node_name|
selected = doc.converter.find_converter node_name
assert selected.is_a? Asciidoctor::Converter::TemplateConverter
assert selected.templates[node_name].is_a? Slim::Template
assert_equal %(block_#{node_name}.xml.slim), File.basename(selected.templates[node_name].file)
end
end
test 'should use Slim templates in place of built-in templates' do
input = <<-EOS
= Document Title
Author Name
== Section One
Sample paragraph
.Related
****
Sidebar content
****
EOS
output = render_embedded_string input, :template_dir => File.join(File.dirname(__FILE__), 'fixtures', 'custom-backends', 'slim'), :template_cache => false
assert_xpath '/*[@class="sect1"]/*[@class="sectionbody"]/p', output, 1
assert_xpath '//aside', output, 1
assert_xpath '/*[@class="sect1"]/*[@class="sectionbody"]/p/following-sibling::aside', output, 1
assert_xpath '//aside/header/h1[text()="Related"]', output, 1
assert_xpath '//aside/header/following-sibling::p[text()="Sidebar content"]', output, 1
end
test 'should use custom converter if specified' do
input = <<-EOS
= Document Title
preamble
== Section
content
EOS
class CustomConverterA
def initialize backend, opts = {}
end
def convert node, name = nil
'document'
end
def self.converts? backend
true
end
end
output = render_string input, :converter => CustomConverterA
assert 'document', output
end
test 'should use converter registered for backend' do
input = <<-EOS
content
EOS
begin
Asciidoctor::Converter::Factory.unregister_all
class CustomConverterB
include Asciidoctor::Converter
register_for 'foobar'
def convert node, name = nil
'foobar content'
end
end
converters = Asciidoctor::Converter::Factory.converters
assert converters.size == 1
assert converters['foobar'] == CustomConverterB
output = render_string input, :backend => 'foobar'
assert 'foobar content', output
ensure
Asciidoctor::Converter::Factory.unregister_all
end
end
test 'should fall back to catch all converter' do
input = <<-EOS
content
EOS
begin
Asciidoctor::Converter::Factory.unregister_all
class CustomConverterC
include Asciidoctor::Converter
register_for '*'
def convert node, name = nil
'foobaz content'
end
end
converters = Asciidoctor::Converter::Factory.converters
assert converters['*'] == CustomConverterC
output = render_string input, :backend => 'foobaz'
assert 'foobaz content', output
ensure
Asciidoctor::Converter::Factory.unregister_all
end
end
end
end
|