Skip to content

Commit 14141ec

Browse files
ebeahanMathieu Martin
andauthored
[1.x] Add usage docs section (#988) (#1024)
Co-authored-by: Mathieu Martin <[email protected]>
1 parent 501d404 commit 14141ec

File tree

7 files changed

+85
-3
lines changed

7 files changed

+85
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ All notable changes to this project will be documented in this file based on the
3737
* Introduced `--strict` flag to perform stricter schema validation when running the generator script. #937
3838
* Added check under `--strict` that ensures composite types in example fields are quoted. #966
3939
* Added `ignore_above` and `normalizer` support for keyword multi-fields. #971
40+
* Added ability to supply free-form usage documentation per fieldset. #988
4041
* Added `--oss` flag for users who want to generate ECS templates for use on OSS clusters. #991
4142
* Added a new directory with experimental artifacts, which includes all changes
4243
from RFCs that have reached stage 2. #993

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ docs:
4444
if [ ! -d $(PWD)/build/docs ]; then \
4545
git clone --depth=1 https://github.com/elastic/docs.git ./build/docs ; \
4646
fi
47-
./build/docs/build_docs --asciidoctor --doc ./docs/index.asciidoc --chunk=1 $(OPEN_DOCS) --out ./build/html_docs
47+
./build/docs/build_docs --asciidoctor --doc ./docs/index.asciidoc --chunk=2 $(OPEN_DOCS) --out ./build/html_docs
4848

4949
# Alias to generate experimental artifacts
5050
.PHONY: experimental

docs/usage/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Usage Docs
2+
3+
ECS fields can benefit from additional context and examples which describe their real-world usage. This directory provides a place in the documentation to capture these usage details. AsciiDoc markdown files can be added for any fieldset defined in ECS.
4+
5+
## Adding a Usage Doc
6+
7+
1. Create an AsciiDoc formatted file with the `.asciidoc` file extension.
8+
2. Save the file in this directory (`docs/usage`), naming it after its associated field set (e.g. a usage document for the fields defined in `schemas/base.yml` fields would be named `docs/usage/base.asciidoc`).
9+
3. The anchor at the top of the file (e.g. `[[ecs-base-usage]]`) must use the following convention for valid link references in the generated docs: `[[ecs-<<fieldset-name>>-usage]]`.
10+
4. Run `make`. The asciidoc generator will generate the ECS field reference, including the present usage docs.
11+
12+
If the filename doesn't match a currently defined fieldset, the usage document will not appear on the ECS docs site. This logic is handled in the AsciiDoc generator scripts, `scripts/generators/asciidoc_fields.py`.
13+
14+
## Template
15+
16+
The following is a simple AsciiDoc template as a starting point:
17+
18+
```asciidoc
19+
20+
[[ecs-fieldset-usage]]
21+
==== Fieldset Usage
22+
23+
Add relevant text here.
24+
25+
[discrete]
26+
===== New Section header
27+
28+
Text for the new section.
29+
30+
[discrete]
31+
===== Examples
32+
33+
[source,sh]
34+
-----------
35+
{
36+
"key": "value"
37+
}
38+
-----------
39+
40+
```

scripts/generators/asciidoc_fields.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ def sort_fields(fieldset):
7272
return sorted(fields_list, key=lambda field: field['name'])
7373

7474

75+
def check_for_usage_doc(fieldset_name, usage_file_list=ecs_helpers.usage_doc_files()):
76+
"""Checks if a usage doc exists for the specified
77+
fieldset.
78+
79+
:param fieldset_name: The name of the target fieldset
80+
"""
81+
return f"{fieldset_name}.asciidoc" in usage_file_list
82+
83+
7584
def templated(template_name):
7685
"""Decorator function to simplify rendering a template.
7786
@@ -138,10 +147,12 @@ def generate_field_details_page(fieldset):
138147
sorted_reuse_fields = render_fieldset_reuse_text(fieldset)
139148
render_nestings_reuse_fields = render_nestings_reuse_section(fieldset)
140149
sorted_fields = sort_fields(fieldset)
150+
usage_doc = check_for_usage_doc(fieldset.get('name'))
141151
return dict(fieldset=fieldset,
142152
sorted_reuse_fields=sorted_reuse_fields,
143153
render_nestings_reuse_section=render_nestings_reuse_fields,
144-
sorted_fields=sorted_fields)
154+
sorted_fields=sorted_fields,
155+
usage_doc=usage_doc)
145156

146157

147158
# Allowed values section

scripts/generators/ecs_helpers.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import yaml
44
import git
5+
import pathlib
56
import warnings
67

78
from collections import OrderedDict
@@ -113,6 +114,14 @@ def get_tree_by_ref(ref):
113114
return commit.tree
114115

115116

117+
def usage_doc_files():
118+
usage_docs_dir = os.path.join(os.path.dirname(__file__), '../../docs/usage')
119+
usage_docs_path = pathlib.Path(usage_docs_dir)
120+
if usage_docs_path.is_dir():
121+
return [x.name for x in usage_docs_path.glob('*.asciidoc') if x.is_file()]
122+
return []
123+
124+
116125
def ecs_files():
117126
"""Return the schema file list to load"""
118127
schema_glob = os.path.join(os.path.dirname(__file__), '../../schemas/*.yml')

scripts/templates/field_details.j2

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
{{ fieldset['description']|replace("\n", "\n\n") }}
66

7+
{%- if usage_doc %}
8+
9+
Find additional usage and examples in the {{ fieldset['name'] }} fields <<ecs-{{ fieldset['name']|replace("_", "-") }}-usage,usage>> section.
10+
11+
{% endif %}
12+
713
{# Field Details Table Header -#}
814
[discrete]
915
==== {{ fieldset['title'] }} Field Details
@@ -113,4 +119,9 @@ Note also that the `{{ fieldset['name'] }}` fields are not expected to be used d
113119
|=====
114120

115121
{% endif %}{# if 'nestings' #}
116-
{%- endif %}{# if 'nestings' or 'reusable' in fieldset #}
122+
{%- endif -%}{# if 'nestings' or 'reusable' in fieldset #}
123+
{%- if usage_doc %}
124+
125+
include::usage/{{ fieldset['name'] }}.asciidoc[]
126+
127+
{% endif %}

scripts/tests/test_asciidoc_fields.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,16 @@ def test_rendering_fieldset_nesting(self):
127127
self.assertEqual('as', foo_nesting_fields[0]['name'])
128128
self.assertEqual('Fields describing an AS', foo_nesting_fields[0]['short'])
129129

130+
def test_check_for_usage_doc_true(self):
131+
usage_files = ["foo.asciidoc"]
132+
foo_name = self.foo_fieldset.get('name')
133+
self.assertTrue(asciidoc_fields.check_for_usage_doc(foo_name, usage_file_list=usage_files))
134+
135+
def test_check_for_usage_doc_false(self):
136+
usage_files = ["notfoo.asciidoc"]
137+
foo_name = self.foo_fieldset.get('name')
138+
self.assertFalse(asciidoc_fields.check_for_usage_doc(foo_name, usage_file_list=usage_files))
139+
130140

131141
if __name__ == '__main__':
132142
unittest.main()

0 commit comments

Comments
 (0)