include-tagged-regions.adoc 5.21 KB
Newer Older
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
= Include Content by Tagged Regions
// aka Select Portions of a Document to Include

The include directive enables you to select portions of a file to include instead of including the whole file.
Use the `lines` attribute to include individual lines or a range of lines (by line number).
Use the `tags` attribute (or `tag` attribute for the singular case) to select lines that fall between regions marked with user-defined tags.

When including multiple line ranges or multiple tags, each entry in the list must be separated by either a comma or a semi-colon.
If commas are used, the entire value must be enclosed in quotes.
You can eliminate this requirement by using the semi-colon as the data separator instead.

== Tagging regions

Tags are useful when you want to identify specific regions of a file to include.
You can then select the lines between the boundaries of the include tag/end directives to include using the `tags` attribute.

TIP: If the target file has tagged lines, and you just want to ignore those lines, use the `tags` attribute to filter them out.
See <<tag-filtering>> for details.

The example below shows how you tag a region of content inside a file containing multiple code examples.

.Tagged code snippets in a file named core.rb
[source,ruby,subs=attributes+]
----
include::example$include.adoc[tag=tag-co]
----
<.> To indicate the start of a tagged region, insert a comment line in the code.
<.> Assign a unique name to the `tag` directive. In this example the tag is called _timings_.
<.> Insert another comment line where you want the tagged region to end.
<.> Assign the name of the region you want to terminate to the `end` directive.
31
<.> This is the start of a tagged snippet named _parse_.
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
<.> This is the end of the tagged snippet named _parse_.

IMPORTANT: The `tag::[]` and `end::[]` directives should be placed after a line comment as defined by the language of the source file.
The directives must also appear at the _end_ of the line.
In the previous example, we choose to prefix the lines with a hash (`#`) because that's the start of a line comment in Ruby.

TIP: For languages that only have circumfix comments, such as XML, you can enclose the tag and end directives in the respective circumfix comment markers.
For example, in XML files, you can use `+<!-- tag::name[] -->+` and `+<!-- end::name[] -->+` (the spaces around the tag are required).

In the next example, the tagged region named _parse_ is selected by the `include` directive.

.Selecting the _parse_ code snippet from a document
[source]
....
include::example$include.adoc[tag=target-co]
....
<.> In the directive's brackets, set the `tag` attribute and assign it the unique name of the code snippet you tagged in your code file.

You can include multiple tags from the same file.

.Selecting the _timings_ and the _parse_ code snippets from a document
[source]
....
include::example$include.adoc[tag=target-co-multiple]
....

It is also possible to have fine-grained tagged regions inside larger tagged regions.

For example, if your include file has the following content:

[source]
----
// tag::snippets[]
// tag::snippet-a[]
snippet a
// end::snippet-a[]

// tag::snippet-b[]
snippet b
// end::snippet-b[]
// end::snippets[]
----

And you include this file using the following include directive:

[source]
----
\include::file-with-snippets.adoc[tag=snippets]
----

The following lines will be selected and displayed:

....
snippet a

snippet b
....

Notice that none of the lines with the tag directives are displayed.

[#tag-filtering]
== Tag filtering

The previous section showed how to select tagged regions explicitly, but you can also use wildcards and exclusions.
These expressions give you the ability to include or exclude tags in bulk.
Even if you're not including by tag, you can use the double wildcard (`**`) to ignore all lines with an include tag/end directive.

The modifiers you can use for filtering are as follows:

`*`::
Select all tagged regions.

`**`::
Select all the lines in the document *except for lines with an include tag/end directive*.
Use this symbol if you want to include a file that has tag/end directives, but you want to ignore all those lines.

`!`::
Negate the match.

The wildcards are applied first, then the exclusions are applied.
Technically, the order you list them doesn't matter, but it's customary to list them in this order.

Here are some of the permutations that you can use:

`*`:: Selects all tagged regions in the document.

`**`:: Selects all the lines in the document except for lines containing a tag directive.

`+**;*+`:: Selects all the lines outside and inside of tagged regions, but not the lines containing a tag directive.

`foo;!bar`:: Selects regions tagged _foo_, but excludes any nested regions tagged _bar_.

`*;!foo`:: Selects all tagged regions, but excludes any regions tagged _foo_ (nested or otherwise).

`**;!foo`:: Selects all the lines of the document except for regions tagged _foo_.

`+**;!*+`:: Selects only the regions of the document outside of tags (i.e., non-tagged regions).

There are also some shorthands if only exclusions are specified:

`!foo`:: Equivalent to `**;!foo`; an exclusion without an inclusion implicitly starts by selecting all the lines.
`!*`:: Equivalent to `+**;!*+`; only selects regions that are not tagged.