asciidoctor-doctest 1.5.2.0 → 2.0.0.beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/LICENSE +1 -1
- data/README.adoc +48 -68
- data/features/fixtures/html-slim/Rakefile +5 -11
- data/features/generator_html.feature +6 -6
- data/features/test_html.feature +70 -28
- data/lib/asciidoctor/doctest.rb +11 -14
- data/lib/asciidoctor/doctest/asciidoc_converter.rb +85 -0
- data/lib/asciidoctor/doctest/{base_example.rb → example.rb} +6 -27
- data/lib/asciidoctor/doctest/factory.rb +36 -0
- data/lib/asciidoctor/doctest/generator.rb +30 -23
- data/lib/asciidoctor/doctest/html/converter.rb +64 -0
- data/lib/asciidoctor/doctest/{html/normalizer.rb → html_normalizer.rb} +4 -4
- data/lib/asciidoctor/doctest/io.rb +14 -0
- data/lib/asciidoctor/doctest/{asciidoc/examples_suite.rb → io/asciidoc.rb} +4 -8
- data/lib/asciidoctor/doctest/{base_examples_suite.rb → io/base.rb} +28 -46
- data/lib/asciidoctor/doctest/io/xml.rb +69 -0
- data/lib/asciidoctor/doctest/no_fallback_template_converter.rb +42 -0
- data/lib/asciidoctor/doctest/rake_tasks.rb +229 -0
- data/lib/asciidoctor/doctest/test_reporter.rb +110 -0
- data/lib/asciidoctor/doctest/tester.rb +134 -0
- data/lib/asciidoctor/doctest/version.rb +1 -1
- data/spec/asciidoc_converter_spec.rb +64 -0
- data/spec/{base_example_spec.rb → example_spec.rb} +4 -5
- data/spec/factory_spec.rb +46 -0
- data/spec/html/converter_spec.rb +95 -0
- data/spec/{html/normalizer_spec.rb → html_normalizer_spec.rb} +1 -1
- data/spec/{asciidoc/examples_suite_spec.rb → io/asciidoc_spec.rb} +3 -8
- data/spec/{html/examples_suite_spec.rb → io/xml_spec.rb} +3 -106
- data/spec/no_fallback_template_converter_spec.rb +38 -0
- data/spec/shared_examples/{base_examples_suite.rb → base_examples.rb} +25 -28
- data/spec/spec_helper.rb +4 -0
- data/spec/tester_spec.rb +153 -0
- metadata +52 -59
- data/features/fixtures/html-slim/test/html_test.rb +0 -6
- data/features/fixtures/html-slim/test/test_helper.rb +0 -5
- data/lib/asciidoctor/doctest/asciidoc_renderer.rb +0 -111
- data/lib/asciidoctor/doctest/generator_task.rb +0 -115
- data/lib/asciidoctor/doctest/html/example.rb +0 -21
- data/lib/asciidoctor/doctest/html/examples_suite.rb +0 -118
- data/lib/asciidoctor/doctest/test.rb +0 -125
- data/spec/asciidoc_renderer_spec.rb +0 -103
- data/spec/test_spec.rb +0 -164
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: adc5657c9e7bcd1f4dfda82cdbce7789414b7129
|
4
|
+
data.tar.gz: 1549da48c04eb0b58148e7e0e37b81483274d366
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 412eb45db40fe2eec88e7a8507be9531a855ecb8efe898bc22f4fed70a12e7d3b4a044110a39fdd5706a9a647b9371e86c301edaa8042b9d3e0d112186f17316
|
7
|
+
data.tar.gz: a49cc812d65eade759965016fb5a816726af906b18f0367f90adde476c4f373aa0ab10ea4c1f9c00086c9caa51525f342438870fe37ddefffbdee5f6b8ea0170
|
data/LICENSE
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
The MIT License
|
2
2
|
|
3
|
-
Copyright 2014-
|
3
|
+
Copyright 2014-2017 Jakub Jirutka <jakub@jirutka.cz> and the Asciidoctor Project.
|
4
4
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
data/README.adoc
CHANGED
@@ -1,11 +1,6 @@
|
|
1
1
|
= Asciidoctor::DocTest
|
2
2
|
Jakub Jirutka <https://github.com/jirutka[@jirutka]>
|
3
|
-
:page-layout: base
|
4
|
-
:idprefix:
|
5
|
-
ifdef::env-github[:idprefix: user-content-]
|
6
|
-
:idseparator: -
|
7
3
|
:source-language: ruby
|
8
|
-
:language: {source-language}
|
9
4
|
// custom
|
10
5
|
:gem-name: asciidoctor-doctest
|
11
6
|
:gh-name: asciidoctor/{gem-name}
|
@@ -55,83 +50,62 @@ mkdir -p test/examples/asciidoc
|
|
55
50
|
|
56
51
|
. Add development dependency on `asciidoctor-doctest` to your gemspec:
|
57
52
|
+
|
58
|
-
[source]
|
59
|
-
s.add_development_dependency 'asciidoctor-doctest', '~>
|
53
|
+
[source, ruby]
|
54
|
+
s.add_development_dependency 'asciidoctor-doctest', '~> 2.0.0'
|
60
55
|
+
|
61
56
|
or Gemfile if you’re not distributing the backend as a gem:
|
62
57
|
+
|
63
|
-
[source]
|
64
|
-
gem 'asciidoctor-doctest', '~>
|
58
|
+
[source, ruby]
|
59
|
+
gem 'asciidoctor-doctest', '~> 2.0.0'
|
65
60
|
+
|
66
61
|
and run `bundle install`.
|
67
62
|
|
68
|
-
. Create or edit `
|
63
|
+
. Create or edit `Rakefile` and configure DocTest tasks:
|
69
64
|
+
|
70
|
-
[source]
|
65
|
+
[source, ruby]
|
71
66
|
----
|
72
67
|
require 'asciidoctor/doctest'
|
73
|
-
require '
|
74
|
-
|
75
|
-
# used to colorize output
|
76
|
-
require 'minitest/rg'
|
77
|
-
|
78
|
-
# needed if you're testing templates-based backend
|
68
|
+
require 'thread_safe'
|
79
69
|
require 'tilt'
|
80
70
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
class TestTemplates < DocTest::Test
|
95
|
-
converter_opts template_dirs: 'data/templates'
|
96
|
-
generate_tests! DocTest::HTML::ExamplesSuite
|
71
|
+
DocTest::RakeTasks.new(:doctest) do |t|
|
72
|
+
t.output_suite = DocTest::HTML::ExamplesSuite
|
73
|
+
t.output_suite_opts = {
|
74
|
+
examples_path: 'test/examples/shiny'
|
75
|
+
}
|
76
|
+
# add extra input examples (optional)
|
77
|
+
t.input_suite_opts = {
|
78
|
+
examples_path: [ *DocTest.examples_path, 'test/examples/asciidoc' ]
|
79
|
+
}
|
80
|
+
t.converter_opts = {
|
81
|
+
template_dirs: 'data/templates'
|
82
|
+
}
|
97
83
|
end
|
98
84
|
----
|
99
85
|
|
100
|
-
.
|
86
|
+
. Check if rake loads the DocTest tasks _doctest_, _doctest:test_ and _doctest:generate_.
|
101
87
|
+
|
102
|
-
[source]
|
103
|
-
|
104
|
-
require 'asciidoctor/doctest'
|
105
|
-
require 'rake/testtask'
|
106
|
-
require 'thread_safe'
|
107
|
-
require 'tilt'
|
88
|
+
[source, sh]
|
89
|
+
bundle exec rake -D
|
108
90
|
|
109
|
-
Rake::TestTask.new(:test) do |task|
|
110
|
-
task.description = 'Run tests for templates'
|
111
|
-
task.pattern = 'test/templates_test.rb'
|
112
|
-
task.libs << 'test'
|
113
|
-
end
|
114
91
|
|
115
|
-
|
116
|
-
task.output_suite = DocTest::HTML::ExamplesSuite.new(examples_path: 'test/examples/shiny')
|
117
|
-
task.converter_opts[:template_dirs] = 'data/templates'
|
118
|
-
#
|
119
|
-
# add extra input examples (optional)
|
120
|
-
task.examples_path.unshift 'test/examples/asciidoc'
|
121
|
-
end
|
92
|
+
== Run tests
|
122
93
|
|
123
|
-
|
124
|
-
|
125
|
-
----
|
94
|
+
Assume that you have defined the Rake tasks in the default namespace _doctest_ (see above).
|
95
|
+
Then you can simply run:
|
126
96
|
|
97
|
+
[source, sh]
|
98
|
+
bundle exec rake doctest:test
|
127
99
|
|
128
|
-
|
100
|
+
To test only specific examples, use `PATTERN` with glob-like expression:
|
129
101
|
|
130
|
-
|
131
|
-
|
102
|
+
[source, sh]
|
103
|
+
bundle exec rake doctest:test PATTERN='block_*:*'
|
104
|
+
|
105
|
+
For verbose output, use `VERBOSE=yes`:
|
132
106
|
|
133
107
|
[source, sh]
|
134
|
-
bundle exec rake test
|
108
|
+
bundle exec rake doctest:test VERBOSE=yes
|
135
109
|
|
136
110
|
image::doc/img/failing-test-term.gif[Failing test in term, align="center"]
|
137
111
|
|
@@ -225,11 +199,17 @@ NOTE: The trailing new line (below this) will be removed.
|
|
225
199
|
HtmlTest assumes that paragraphs are enclosed in `<p></p>` tags and implicitly sets the _include_ option to `./p/node()` for `inline_*:*` examples (if _include_ is not already set).
|
226
200
|
If it’s not your case, then you must overwrite it:
|
227
201
|
|
228
|
-
[source]
|
202
|
+
[source, ruby]
|
229
203
|
----
|
230
|
-
|
231
|
-
|
232
|
-
|
204
|
+
DocTest::RakeTasks.new(:doctest) do |t|
|
205
|
+
t.output_suite = DocTest::HTML::ExamplesSuite
|
206
|
+
t.output_suite_opts = {
|
207
|
+
examples_path: 'test/examples/shiny',
|
208
|
+
paragraph_xpath: './div/p/node()' //<1>
|
209
|
+
}
|
210
|
+
t.converter_opts = {
|
211
|
+
template_dirs: 'data/templates'
|
212
|
+
}
|
233
213
|
end
|
234
214
|
----
|
235
215
|
|
@@ -290,27 +270,27 @@ Therefore DocTest provides a generator.
|
|
290
270
|
When you have at least partially working Asciidoctor _backend_ (converter or a set of templates), you can pass the input examples through it and generate your output examples.
|
291
271
|
Then you should verify them and modify if needed.
|
292
272
|
|
293
|
-
Assume that you have defined the
|
273
|
+
Assume that you have defined the Rake tasks in the default namespace _doctest_ (see <<setup-doctest>>).
|
294
274
|
|
295
275
|
Now you can generate output examples from all the input examples (those with `.adoc` extension) found on the examples_path that doesn’t already exist (i.e. it doesn’t rewrite existing):
|
296
276
|
|
297
277
|
[source, sh]
|
298
|
-
bundle exec rake generate
|
278
|
+
bundle exec rake doctest:generate
|
299
279
|
|
300
280
|
Same as previous, but rewrite existing tested examples:
|
301
281
|
|
302
282
|
[source, sh]
|
303
|
-
bundle exec rake generate FORCE=yes
|
283
|
+
bundle exec rake doctest:generate FORCE=yes
|
304
284
|
|
305
285
|
Generate just examples for `block_ulist` node (i.e. all examples in `block_ulist.adoc` file(s) found on the examples_path) that doesn’t exist yet:
|
306
286
|
|
307
287
|
[source, sh]
|
308
|
-
bundle exec rake generate PATTERN='block_ulist:*'
|
288
|
+
bundle exec rake doctest:generate PATTERN='block_ulist:*'
|
309
289
|
|
310
290
|
(Re)generate examples which name starts with `basic` for all _block_ nodes (i.e. files that starts with `block_`):
|
311
291
|
|
312
292
|
[source, sh]
|
313
|
-
bundle exec rake generate PATTERN='block_*:basic*' FORCE=yes
|
293
|
+
bundle exec rake doctest:generate PATTERN='block_*:basic*' FORCE=yes
|
314
294
|
|
315
295
|
|
316
296
|
== How to extend it
|
@@ -1,15 +1,9 @@
|
|
1
1
|
require 'asciidoctor/doctest'
|
2
|
-
require 'rake/testtask'
|
3
2
|
require 'tilt'
|
4
3
|
|
5
|
-
DocTest::
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
Rake::TestTask.new(:test) do |task|
|
12
|
-
task.description = 'Run tests for custom HTML backend'
|
13
|
-
task.pattern = 'test/html_test.rb'
|
14
|
-
task.libs << 'test'
|
4
|
+
DocTest::RakeTasks.new do |t|
|
5
|
+
t.output_examples :html, path: 'examples/html'
|
6
|
+
t.input_examples :asciidoc, path: 'examples/asciidoc'
|
7
|
+
t.converter = DocTest::HTML::Converter
|
8
|
+
t.converter_opts = { template_dirs: 'templates' }
|
15
9
|
end
|
@@ -4,10 +4,10 @@ Feature: Generating output examples for a custom HTML backend
|
|
4
4
|
Given I do have a template-based HTML backend with DocTest
|
5
5
|
|
6
6
|
Scenario: Generate missing output examples
|
7
|
-
When I run `bundle exec rake generate`
|
7
|
+
When I run `bundle exec rake doctest:generate`
|
8
8
|
Then the output should contain:
|
9
9
|
"""
|
10
|
-
|
10
|
+
Generating test examples *:* in examples/html
|
11
11
|
--> Unchanged block_quote:with_id_and_role
|
12
12
|
--> Generating block_quote:with_title
|
13
13
|
--> Skipping block_quote:with_attribution
|
@@ -71,10 +71,10 @@ Feature: Generating output examples for a custom HTML backend
|
|
71
71
|
"""
|
72
72
|
|
73
73
|
Scenario: Regenerate all outdated output examples
|
74
|
-
When I run `bundle exec rake generate FORCE=yes`
|
74
|
+
When I run `bundle exec rake doctest:generate FORCE=yes`
|
75
75
|
Then the output should contain:
|
76
76
|
"""
|
77
|
-
|
77
|
+
Generating test examples *:* in examples/html
|
78
78
|
--> Unchanged block_quote:with_id_and_role
|
79
79
|
--> Generating block_quote:with_title
|
80
80
|
--> Rewriting block_quote:with_attribution
|
@@ -132,10 +132,10 @@ Feature: Generating output examples for a custom HTML backend
|
|
132
132
|
"""
|
133
133
|
|
134
134
|
Scenario: Regenerate outdated output examples specified by filter
|
135
|
-
When I run `bundle exec rake generate PATTERN="*:*attribution" FORCE=yes`
|
135
|
+
When I run `bundle exec rake doctest:generate PATTERN="*:*attribution" FORCE=yes`
|
136
136
|
Then the output should contain:
|
137
137
|
"""
|
138
|
-
|
138
|
+
Generating test examples *:*attribution in examples/html
|
139
139
|
--> Rewriting block_quote:with_attribution
|
140
140
|
|
141
141
|
"""
|
data/features/test_html.feature
CHANGED
@@ -4,53 +4,95 @@ Feature: Testing a custom HTML backend
|
|
4
4
|
Given I do have a template-based HTML backend with DocTest
|
5
5
|
|
6
6
|
Scenario: Some examples do not match the expected output
|
7
|
-
When I run `bundle exec rake test`
|
7
|
+
When I run `bundle exec rake doctest:test`
|
8
8
|
Then the output should contain:
|
9
9
|
"""
|
10
|
-
|
11
|
-
TestHtml :: block_quote : with_attribution:
|
12
|
-
Failing example..
|
10
|
+
Running DocTest for the templates: templates.
|
13
11
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
12
|
+
.SFFS
|
13
|
+
"""
|
14
|
+
Then the output should contain:
|
15
|
+
"""
|
16
|
+
✗ Failure: block_quote:with_attribution
|
17
|
+
Failing example..
|
18
|
+
|
19
|
+
<div class="quoteblock">
|
20
|
+
<blockquote>A person who never made a mistake <em>never</em> tried anything new.</blockquote>
|
21
|
+
E <div>Albert Einstein</div>
|
22
|
+
A <div class="attribution">— Albert Einstein</div>
|
23
|
+
</div>
|
19
24
|
"""
|
20
25
|
And the output should contain:
|
21
26
|
"""
|
22
|
-
|
23
|
-
|
24
|
-
This example should fail..
|
27
|
+
✗ Failure: document:title_with_author
|
28
|
+
This example should fail..
|
25
29
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
30
|
+
<div id="header">
|
31
|
+
<h1>The Dangerous and Thrilling Documentation Chronicles</h1>
|
32
|
+
E <div id="author">Kismet Rainbow Chameleon</div>
|
33
|
+
A <div class="details"><span id="author">Kismet Rainbow Chameleon</span></div>
|
34
|
+
</div>
|
35
|
+
"""
|
36
|
+
And the output should contain:
|
37
|
+
"""
|
38
|
+
5 examples (1 passed, 2 failed, 2 skipped)
|
31
39
|
"""
|
32
40
|
And the output should contain:
|
33
41
|
"""
|
34
|
-
|
42
|
+
You have skipped tests. Run with VERBOSE=yes for details.
|
43
|
+
"""
|
44
|
+
|
45
|
+
When I run `bundle exec rake doctest:test VERBOSE=yes`
|
46
|
+
Then the output should contain:
|
47
|
+
"""
|
48
|
+
Running DocTest for the templates: templates.
|
49
|
+
|
50
|
+
✓ block_quote:with_id_and_role
|
51
|
+
∅ block_quote:with_title
|
52
|
+
✗ block_quote:with_attribution
|
53
|
+
✗ document:title_with_author
|
54
|
+
∅ inline_quoted:emphasis
|
55
|
+
|
56
|
+
"""
|
57
|
+
And the output should contain:
|
58
|
+
"""
|
59
|
+
∅ Skipped: block_quote:with_title
|
60
|
+
No expected output found
|
61
|
+
"""
|
62
|
+
And the output should contain:
|
63
|
+
"""
|
64
|
+
∅ Skipped: inline_quoted:emphasis
|
65
|
+
No expected output found
|
66
|
+
"""
|
67
|
+
|
68
|
+
Scenario: Test only examples matching the pattern
|
69
|
+
When I run `bundle exec rake doctest:test PATTERN=block_*:* VERBOSE=yes`
|
70
|
+
Then the output should contain:
|
71
|
+
"""
|
72
|
+
Running DocTest for the templates: templates.
|
73
|
+
|
74
|
+
✓ block_quote:with_id_and_role
|
75
|
+
∅ block_quote:with_title
|
76
|
+
✗ block_quote:with_attribution
|
77
|
+
|
35
78
|
"""
|
36
79
|
|
37
80
|
Scenario: A necessary template is missing and fallback to the built-in converter is disabled
|
38
81
|
When I remove the file "templates/inline_quoted.html.slim"
|
39
|
-
And I run `bundle exec rake test`
|
82
|
+
And I run `bundle exec rake doctest:test`
|
40
83
|
Then the output should contain:
|
41
84
|
"""
|
42
85
|
Could not find a custom template to handle template_name: inline_quoted
|
43
86
|
"""
|
44
87
|
And the output should contain:
|
45
88
|
"""
|
46
|
-
|
47
|
-
|
48
|
-
Failing example..
|
89
|
+
✗ Failure: block_quote:with_attribution
|
90
|
+
Failing example..
|
49
91
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
92
|
+
<div class="quoteblock">
|
93
|
+
E <blockquote>A person who never made a mistake <em>never</em> tried anything new.</blockquote>
|
94
|
+
E <div>Albert Einstein</div>
|
95
|
+
A <blockquote>A person who never made a mistake --TEMPLATE NOT FOUND-- tried anything new.</blockquote>
|
96
|
+
A <div class="attribution">— Albert Einstein</div>
|
97
|
+
</div>
|
56
98
|
"""
|
data/lib/asciidoctor/doctest.rb
CHANGED
@@ -3,16 +3,13 @@ require 'pathname'
|
|
3
3
|
module Asciidoctor
|
4
4
|
module DocTest
|
5
5
|
|
6
|
-
|
6
|
+
@examples_path = Pathname.new(
|
7
7
|
'../../data/examples/asciidoc').expand_path(__dir__).to_s.freeze
|
8
8
|
|
9
|
-
@
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
# examples suites. Use +unshift+ to add your paths before the built-in
|
14
|
-
# reference input examples (default: +["{asciidoctor-doctest}/data/examples/asciidoc"]+).
|
15
|
-
attr_accessor :examples_path
|
9
|
+
# @return [Array<String>] paths of the built-in input examples. It always
|
10
|
+
# returns a new array.
|
11
|
+
def self.examples_path
|
12
|
+
[ @examples_path ]
|
16
13
|
end
|
17
14
|
end
|
18
15
|
end
|
@@ -21,10 +18,10 @@ end
|
|
21
18
|
DocTest = Asciidoctor::DocTest unless defined? DocTest
|
22
19
|
|
23
20
|
require 'asciidoctor/doctest/version'
|
24
|
-
require 'asciidoctor/doctest/
|
25
|
-
require 'asciidoctor/doctest/base_examples_suite'
|
21
|
+
require 'asciidoctor/doctest/example'
|
26
22
|
require 'asciidoctor/doctest/generator'
|
27
|
-
require 'asciidoctor/doctest/
|
28
|
-
require 'asciidoctor/doctest/
|
29
|
-
require 'asciidoctor/doctest/
|
30
|
-
require 'asciidoctor/doctest/
|
23
|
+
require 'asciidoctor/doctest/rake_tasks'
|
24
|
+
require 'asciidoctor/doctest/test_reporter'
|
25
|
+
require 'asciidoctor/doctest/tester'
|
26
|
+
require 'asciidoctor/doctest/io'
|
27
|
+
require 'asciidoctor/doctest/html/converter'
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'asciidoctor'
|
2
|
+
require 'asciidoctor/doctest/no_fallback_template_converter'
|
3
|
+
require 'corefines'
|
4
|
+
|
5
|
+
using Corefines::Hash[:rekey, :+]
|
6
|
+
using Corefines::Object::blank?
|
7
|
+
|
8
|
+
module Asciidoctor
|
9
|
+
module DocTest
|
10
|
+
##
|
11
|
+
# This class is basically a wrapper for +Asciidoctor.convert+ that allows to
|
12
|
+
# preset and validate some common parameters.
|
13
|
+
class AsciidocConverter
|
14
|
+
|
15
|
+
# @return [Hash] the default options to be passed to Asciidoctor.
|
16
|
+
attr_reader :default_opts
|
17
|
+
|
18
|
+
##
|
19
|
+
# @param opts [Hash] the default options to be passed to Asciidoctor.
|
20
|
+
# For a complete list of all available options see the
|
21
|
+
# Asciidoctor's documentation
|
22
|
+
#
|
23
|
+
# @option opts :backend [#to_s, nil] the name of the tested backend.
|
24
|
+
# @option opts :backend_name [#to_s, nil] alias for the +:backend+.
|
25
|
+
#
|
26
|
+
# @option opts :converter [Class, Asciidoctor::Converter::Base, nil]
|
27
|
+
# the backend's converter class (or its instance). When not
|
28
|
+
# specified, the default converter for the specified backend is
|
29
|
+
# used.
|
30
|
+
#
|
31
|
+
# @option opts :template_dirs [Array<String>, String] path of the
|
32
|
+
# directory (or multiple directories) where to look for the
|
33
|
+
# backend's templates. Relative paths are referenced from the
|
34
|
+
# working directory.
|
35
|
+
#
|
36
|
+
# @option opts :templates_fallback [Boolean] allow to fall back to using
|
37
|
+
# an appropriate built-in converter when there is no required
|
38
|
+
# template in the tested backend?
|
39
|
+
# This is actually a default behaviour in Asciidoctor, but since
|
40
|
+
# it's inappropriate for testing of custom backends, it's
|
41
|
+
# disabled by default.
|
42
|
+
#
|
43
|
+
# @option opts :safe [Symbol] the safe mode, one of +:unsafe+, +:safe+,
|
44
|
+
# +:server+, or +:secure+. Default is +:safe+.
|
45
|
+
#
|
46
|
+
# @raise [ArgumentError] if some path from the +template_dirs+ doesn't
|
47
|
+
# exist or is not a directory.
|
48
|
+
#
|
49
|
+
def initialize(opts = {})
|
50
|
+
opts = opts.rekey(&:to_sym).rekey(:backend_name => :backend)
|
51
|
+
|
52
|
+
template_dirs = Array(opts[:template_dirs]).freeze
|
53
|
+
template_dirs.each do |path|
|
54
|
+
fail ArgumentError, "Templates directory '#{path}' doesn't exist!" unless Dir.exist? path
|
55
|
+
end
|
56
|
+
|
57
|
+
unless template_dirs.empty?
|
58
|
+
opts[:template_dirs] = template_dirs
|
59
|
+
opts[:converter] ||= NoFallbackTemplateConverter unless opts[:templates_fallback]
|
60
|
+
end
|
61
|
+
|
62
|
+
opts[:safe] ||= :safe
|
63
|
+
opts.delete(:backend) if opts[:backend].blank?
|
64
|
+
|
65
|
+
@default_opts = opts
|
66
|
+
end
|
67
|
+
|
68
|
+
##
|
69
|
+
# Converts the given +text+ into AsciiDoc syntax with Asciidoctor using
|
70
|
+
# the tested backend.
|
71
|
+
#
|
72
|
+
# @param text [#to_s] the input text in AsciiDoc syntax.
|
73
|
+
# @param opts [Hash] options to pass to Asciidoctor. This will be merged
|
74
|
+
# with {#default_opts} with precedence of +opts+.
|
75
|
+
# @return [String] converted input.
|
76
|
+
#
|
77
|
+
def convert(text, opts = {})
|
78
|
+
Asciidoctor.convert(text.to_s, @default_opts + opts)
|
79
|
+
end
|
80
|
+
|
81
|
+
alias_method :opts, :default_opts
|
82
|
+
alias_method :call, :convert
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|