summaryrefslogtreecommitdiff
path: root/docs/modules/api/pages/catalog-assets.adoc
blob: 5aa721dea46317b17e4372c2443ce7a82d6bbaf8 (plain) (blame)
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
= Catalog Assets

Since Asciidoctor's primary focus is on converting documents efficiently, it does not attempt to store information about all assets it comes across while processing the document.
However, such information can be useful for analyzing the document or for use in extensions.
Therefore, Asciidoctor provides a flag to catalog certain additional assets.
This page explains how to enable this catalog and how to access it.

== What assets are cataloged?

The asset catalog provides a table of select assets grouped by type that are discovered while processing the document.
This table is stored on the `catalog` property of the document model.

When this feature is enabled, the processor catalogs the following assets:

* links (but not xrefs) (key: `:links`)
* block or inline images (key: `:images`)

The processor always cataloged the following assets, regardless of this setting:

* block or inline anchors (key: `:refs`)
* footnotes (key: `:footnotes`)

Generally speaking, inline elements are not cataloged until conversion.
Therefore, they're only available after the document has been converted, not after it has been loaded.
However, inline anchors are an exception.

Inline anchors in paragraphs and list items are cataloged when the document is parsed.
However, the scan for inline anchors happens before inline passthroughs are processed.
Therefore, to escape an inline anchor, you must use a backslash instead of an inline passthrough.

== Set :catalog_assets option

Whether the processor catalogs assets (specifically links and images) is controlled from the API using the `:catalog_assets` option.
The value of this option is a boolean.
If the value is `false` (default), assets are not cataloged.
If the value is `true`, assets are cataloged.
The `:catalog_assets` option is accepted by all xref:index.adoc#entrypoints[entrypoint methods] (e.g., Asciidoctor#load_file).

Here's an example of how to catalog assets when using the API:

[,ruby]
----
doc = Asciidoctor.convert_file 'doc.adoc', safe: :safe, catalog_assets: true
----

Notice that we've used the `convert_file` method instead of the `load_file` method.
This ensures that inline assets are included in the catalog as well.

If you need to enable the `:catalog_assets` option when using the CLI, you'll need to require the following extension:

.enable-catalog-assets-preprocessor.rb
[,ruby]
----
Asciidoctor::Extensions.register do
  preprocessor do
    process do |doc, reader|
      doc.instance_variable_set :@options, ((doc.instance_variable_get :@options).merge catalog_assets: true)
      nil
    end
  end
end
----

Now that you've configured the processor to catalog assets, you can access them from the document object.
Let's explore it.

== Use the asset catalog

Assets which have been cataloged are available from the `catalog` property on the document model (i.e., the parsed document).
The catalog is a Ruby hash.
The keys are the asset families (e.g., `:links`, `:images`, etc).
The value of each key is an array of assets.
The asset object varies by family.

Let's look at an example of how to access links, images, and refs from the asset catalog.

=== :links

Start by creating the following AsciiDoc file named [.path]_doc.adoc_.

.doc.adoc
[,asciidoc]
----
You can learn about Asciidoctor at https://docs.asciidoctor.org.
The Asciidoctor source repo is hosted on https://github.com[GitHub].

image::screenshot.png[]

If you see image:green-check.png[], it means the job was successful.
----

Now, convert this file using Asciidoctor with the `:catalog_assets` option enabled:

[,ruby]
----
doc = Asciidoctor.convert_file 'doc.adoc', safe: :safe, catalog_assets: true
----

Let's see what links the processor found:

[,ruby]
----
links = doc.catalog[:links]
puts "Found #{links.size} links:"
puts links
----

You'll see the following output:

[.output]
....
Found 2 links:
https://docs.asciidoctor.org
https://github.com
....

The value of the `:links` key is an array of unique URLs found in the document.
The entry does not include the link text, only the URL itself.

=== :images

Let's add some images to the document.

.doc.adoc
[,asciidoc]
----
//...

image::screenshot.png[]

If you see image:green-check.png[], it means the job was successful.
----

Now, convert this file using Asciidoctor with the `:catalog_assets` option enabled:

[,ruby]
----
doc = Asciidoctor.convert_file 'doc.adoc', safe: :safe, catalog_assets: true
----

Let's see what images the processor found:

[,ruby]
----
images = doc.catalog[:images]
puts "Found #{images.size} images:"
puts images
----

You'll see the following output:

[.output]
....
Found 2 images:
screenshot.png
green-check.png
....

The value of the `:images` key is an array of images (by occurrence) found in the document.
While it looks like the value of each entry is a relative path string, there's actually more information there.

Each entry in the `:images` collection is an ImageReference object.
This object appears as a relative path string when printed (which explains the observed behavior).
An ImageReference contains the following properties:

target:: The image path relative to the value of imagesdir.
imagesdir:: The value of the imagesdir attribute at the time the image was processed.

Let's assume the images are located in the [.path]_images_ folder, and we have set the `imagesdir` attribute on the document accordingly.

.doc.adoc
[,asciidoc]
----
= Document Title
:imagesdir: images

//...

image::screenshot.png[]

If you see image:green-check.png[], it means the job was successful.
----

You can print the full location to the images as follows:

[,ruby]
----
images = doc.catalog[:images]
puts "Found #{images.size} images:"
docdir = doc.attr 'docdir'
puts images.map {|image| File.join docdir, image.imagesdir.to_s, image.target }
----

In the output, the image references will be shown as absolute paths.

=== :refs

In addition to images and links, you can also access all targetable references (i.e., elements that have an ID).
First, let's add some referenceable elements to our document.

[,asciidoc]
----
= Document Title

== Quickstart

You can learn about Asciidoctor at https://docs.asciidoctor.org.
The Asciidoctor source repo is hosted on https://github.com[GitHub].

.Screenshot
[#screenshot]
image::screenshot.png[]

== CI

If you see image:green-check.png[], it means the job was successful.
----

Let's see what references the processor found:

[,ruby]
----
refs = doc.catalog[:refs]
puts "Found #{refs.size} references:"
puts refs.keys
----

You'll see the following output:

[.output]
....
Found 3 references:
_quickstart
screenshot
_ci
....

The value of the `:refs` key is a map of unique references found in the document.
The key names are the unique IDs.
The values are the element nodes to which these IDs are bound.
The API for the node depends on the type of element.
The most common property is the reftext of the node.

[,ruby]
----
refs = doc.catalog[:refs]
puts "Found #{refs.size} references:"
puts refs.map {|id, node| %(#{node.context}: #{id} => #{node.xreftext || "[#{id}]"}) }
----

Now you'll see the following output:

[.output]
....
Found 3 references:
section: _quickstart => Quickstart
image: screenshot => Screenshot
section: _ci => CI
....

An idea of something you can do with the refs table is validate deep xrefs across documents.