Skip to content

Commit 33913d4

Browse files
Rework Hammer reference generation (#4234)
Co-authored-by: Maximilian Kolb <[email protected]>
1 parent 0cd623f commit 33913d4

File tree

2 files changed

+89
-54
lines changed

2 files changed

+89
-54
lines changed
Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,40 @@
1-
# Generated Hammer reference
1+
# Creating Hammer reference
22

3-
Use the `generate-hammer-reference.sh` script to update the generated reference.
3+
## Generating the reference
44

5-
## Prerequisite
5+
Use the `generate-hammer-reference.sh` script to generate the reference.
66

7+
### Prerequisites
8+
9+
- Ensure all Foreman plugins are enabled on your Foreman instance.
710
- You have got the full help output from Hammer CLI in MarkDown format:
811
```
912
hammer full-help --md >hammer.full-help.md
1013
```
1114

12-
## Usage
15+
### Usage
1316
```
1417
cd guides/
18+
mkdir --parents common/hammer-reference
1519
scripts/generate-hammer-reference.sh hammer.full-help.md
1620
```
21+
22+
## Manual editing
23+
24+
Move `content-view component` and `content-view filter` sections, which are level 3 and have level 4 sections, to **level 2** to decrease section nesting to 3 levels.
25+
26+
Generated structure:
27+
```
28+
2.1 content-view
29+
2.1.x content-view component
30+
2.1.y content-view filter
31+
2.2 content-view-environment
32+
```
33+
34+
Manually updated structure:
35+
```
36+
2.1 content-view
37+
2.2 content-view component
38+
2.3 content-view-environment
39+
2.4 content-view filter
40+
```

guides/scripts/generate-hammer-reference.sh

Lines changed: 61 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
#!/usr/bin/bash
22

33
SRC=
4-
ASMB_DIR=common # assembly destination folder
5-
MOD_DIR=modules
4+
MOD_DIR=common/hammer-reference/
65
MOD_TYPE=":_mod-docs-content-type: REFERENCE"
7-
ASMB_FILENAME=assembly_hammer-reference.adoc
6+
ROLE_ABSTRACT='[role="_abstract"]'
7+
ASMB_FILENAME=doc-Hammer_Reference/master.adoc
88
NOOP= # dry run
99
DEBUG=
1010

1111
print_help() {
1212
echo -e "Generate AsciiDoc reference from a Hammer full-help MarkDown file"
13-
echo -e " Generates one assembly and one module per hammer command."
13+
echo -e "Generates $ASMB_FILENAME and one module per hammer subsubcommand (3 levels)."
1414
echo
1515
echo -e "Usage:"
1616
echo -e " $0 [OPTIONS ...] SOURCE_FILE"
@@ -19,9 +19,8 @@ print_help() {
1919
echo
2020
echo -e "Options:"
2121
echo -e " --target-dir, -T DIR"
22-
echo -e " Path to a folder where the reference assembly"
23-
echo -e " will be placed. Default: $ASMB_DIR"
24-
echo -e " Modules will be placed in $ASMB_DIR/$MOD_DIR/"
22+
echo -e " Path to a folder where the reference modules"
23+
echo -e " will be placed. Default: $MOD_DIR"
2524
echo -e " --help, -h Print help and exit"
2625
echo -e " --noop Dry run (don't do anything, only preview output)"
2726
echo -e " --debug, -d Debug mode (verbose output and keep tmp file)"
@@ -37,10 +36,10 @@ bye() {
3736

3837
# Process user arguments
3938
if [ $# -eq 0 ]; then print_help; exit 1; fi
40-
while [ $# -gt 1 ]; do
39+
while [ $# -gt 0 ]; do
4140
case "$1" in
4241
--target-dir|-T)
43-
ASMB_DIR="$2"
42+
MOD_DIR="$2"
4443
shift 2
4544
;;
4645
--noop)
@@ -64,32 +63,37 @@ while [ $# -gt 1 ]; do
6463
print_help
6564
exit 0
6665
;;
67-
*)
66+
-*)
6867
bye "E: Invalid option: $1"
6968
;;
69+
*)
70+
break
7071
esac
7172
done
7273
SRC=$1
7374

74-
# Define internal variables
75-
if [ -n "$ASMB_DIR" ]; then
76-
mod_path=$ASMB_DIR/$MOD_DIR # module destination folder
77-
asmb_file=$ASMB_DIR/$ASMB_FILENAME
78-
else
79-
mod_path=$MOD_DIR
80-
asmb_file=$ASMB_FILENAME
75+
# Exit if source file doesn't exist
76+
if [ ! -f "$SRC" ]; then
77+
bye "E: Source file not found: $SRC"
8178
fi
79+
80+
# Define internal variables
81+
mod_path=${MOD_DIR%/}
82+
asmb_file=$ASMB_FILENAME
8283
details_tmp="$mod_path/tmp_hammer-option-details"
83-
echo -e >$details_tmp # reset
8484

85-
details_file="$mod_path/ref_hammer-option-details.adoc"
86-
details_header='[id="hammer-option-details"]\n= Option details\n\nHammer options accept the following option types and values:'
85+
details_file="$mod_path/ref_data-representation-in-hammer-options.adoc"
86+
details_header="$MOD_TYPE\n\n[id="data-representation-in-hammer-options"]\n= Data representation in Hammer options\n\n$ROLE_ABSTRACT\nHammer options accept the following types and values."
8787

88-
asmb_header=':_mod-docs-content-type: ASSEMBLY\n\n// DO NOT EDIT MANUALLY\n// Use the generate-hammer-reference.sh script to update\n\ninclude::modules/con_hammer-reference.adoc[]\n\n'
89-
asmb_footer='include::modules/ref_hammer-option-details.adoc[leveloffset=+1]\n'
88+
asmb_header="include::common/attributes.adoc[]\ninclude::common/header.adoc[]\n:context: hammer-reference\n:hammer-reference:\n\n= {HammerRefDocTitle}\n\nifdef::satellite[]\ninclude::common/modules/proc_providing-feedback-on-red-hat-documentation.adoc[leveloffset=+1]\nendif::[]\n\n// DO NOT EDIT MANUALLY!\n// Use the generate-hammer-reference.sh script to update\n\ninclude::$mod_path/ref_data-representation-in-hammer-options.adoc[leveloffset=+1]\n"
89+
asmb_footer='ifndef::orcharhino,satellite[]\ninclude::common/ribbons.adoc[]\nendif::[]\n'
9090

91-
# Create folders
92-
{ [ -n "$NOOP" ] || mkdir -p $mod_path ; } && echo -e "Created folders: " $mod_path
91+
# Exit if module path doesn't exist
92+
if [ -d "$mod_path" ]; then
93+
echo "I: Module path exists: $mod_path"
94+
else
95+
bye "E: Module path not found: $mod_path"
96+
fi
9397

9498
# Output headers
9599
if [ -z "$NOOP" ]; then
@@ -98,8 +102,7 @@ if [ -z "$NOOP" ]; then
98102
fi
99103
if [ -z "$ASSEMBLY_ONLY" ]; then
100104
echo -e "$details_header" >$details_file && \
101-
{ [ -n "$DEBUG" ] && echo -e "I: Option details header written" ; } || \
102-
echo -e "W: Something went wrong writing option details header"
105+
[ -n "$DEBUG" ] && echo -e "I: Data representation header written"
103106
fi
104107

105108
# Read lines from the source file
@@ -115,31 +118,31 @@ cat "$SRC" | while read line ; do
115118
'- ['*) # Skip MD TOC
116119
continue
117120
;;
118-
"# hammer") # top-command
121+
"# hammer") # program
119122
echo -e "I: [cmd-1]: $line"
120123
id_core=hammer
121124
mod_file="$mod_path/ref_$id_core.adoc"
122125
id='[id="hammer"]'
123126
heading='= hammer'
124127
[ -n "$NOOP" ] || [ -n "$ASSEMBLY_ONLY" ] || \
125-
echo -e "$MOD_TYPE\n\n$id\n$heading" >$mod_file
128+
echo -e -n "$MOD_TYPE\n\n$id\n$heading\n\n$ROLE_ABSTRACT" >$mod_file
126129
{ [ -z "$NOOP" ] || [ -n "$ASSEMBLY_ONLY" ] ; } && \
127-
echo -e "include::modules/ref_hammer.adoc[leveloffset=+1]\n" >>$asmb_file
130+
echo -e "include::$mod_path/ref_hammer.adoc[leveloffset=+1]\n" >>$asmb_file
128131
begin_section=yes
129132
skip_section=
130133
continue
131134
;;
132135
"## hammer"*) # command
133136
echo -e "I: [cmd-2]: $line"
134137
id_core=`echo -e $line | sed 's/^## //' | sed 's/ /-/g'`
135-
filename=ref_$id_core.adoc
138+
filename=ref_${id_core}.adoc
136139
mod_file="$mod_path/$filename"
137-
id=[id=\"$id_core\"]
140+
id=[id=\"${id_core}\"]
138141
heading=`echo -e $line | sed 's/^## hammer/=/'`
139142
[ -n "$NOOP" ] || [ -n "$ASSEMBLY_ONLY" ] || \
140-
echo -e "$MOD_TYPE\n\n$id\n$heading" >$mod_file
143+
echo -e -n "$MOD_TYPE\n\n$id\n$heading\n\n$ROLE_ABSTRACT" >$mod_file
141144
{ [ -z "$NOOP" ] || [ -n "$ASSEMBLY_ONLY" ] ; } && \
142-
echo -e "include::modules/$filename[leveloffset=+1]\n" >>$asmb_file
145+
echo -e "include::$mod_path/$filename[leveloffset=+1]\n" >>$asmb_file
143146
begin_section=yes
144147
skip_section=
145148
options=
@@ -152,10 +155,14 @@ cat "$SRC" | while read line ; do
152155
"### hammer"*) # subcommand
153156
[ -n "$DEBUG" ] && echo -e "I: [cmd-3]: $line"
154157
id_core=`echo -e $line | sed 's/^### //' | sed 's/ /-/g'`
155-
id=[id=\"$id_core\"]
156-
heading=`echo -e $line | sed 's/^### hammer/==/'`
158+
id=[id=\"${id_core}\"]
159+
filename=ref_${id_core}.adoc
160+
mod_file="$mod_path/$filename"
161+
heading=`echo -e $line | sed 's/^### hammer/=/'`
157162
[ -n "$NOOP" ] || [ -n "$ASSEMBLY_ONLY" ] || \
158-
echo -e "$MOD_TYPE\n\n$id\n$heading" >>$mod_file
163+
echo -e -n "$MOD_TYPE\n\n$id\n$heading\n\n$ROLE_ABSTRACT" >>$mod_file
164+
{ [ -z "$NOOP" ] || [ -n "$ASSEMBLY_ONLY" ] ; } && \
165+
echo -e "include::$mod_path/$filename[leveloffset=+2]\n" >>$asmb_file
159166
begin_section=yes
160167
skip_section=
161168
options=
@@ -168,10 +175,14 @@ cat "$SRC" | while read line ; do
168175
"#### hammer"*) # subsubcommand
169176
[ -n "$DEBUG" ] && echo -e "I: [cmd-4]: $line"
170177
id_core=`echo -e $line | sed 's/^#### //' | sed 's/ /-/g'`
171-
id=[id=\"$id_core\"]
172-
heading=`echo -e $line | sed 's/^#### hammer/===/'`
178+
id=[id=\"${id_core}\"]
179+
filename=ref_${id_core}.adoc
180+
mod_file="$mod_path/$filename"
181+
heading=`echo -e $line | sed 's/^#### hammer/=/'`
173182
[ -n "$NOOP" ] || [ -n "$ASSEMBLY_ONLY" ] || \
174-
echo -e "$MOD_TYPE\n\n$id\n$heading" >>$mod_file
183+
echo -e -n "$MOD_TYPE\n\n$id\n$heading\n\n$ROLE_ABSTRACT" >>$mod_file
184+
{ [ -z "$NOOP" ] || [ -n "$ASSEMBLY_ONLY" ] ; } && \
185+
echo -e "include::$mod_path/$filename[leveloffset=+3]\n" >>$asmb_file
175186
begin_section=yes
176187
skip_section=
177188
options=
@@ -184,10 +195,10 @@ cat "$SRC" | while read line ; do
184195
"##### hammer"*) # subsubsubcommand
185196
[ -n "$DEBUG" ] && echo -e "I: [cmd-5]: $line"
186197
id_core=`echo -e $line | sed 's/^##### //' | sed 's/ /-/g'`
187-
id=[id=\"$id_core\"]
188-
heading=`echo -e $line | sed 's/^##### hammer/====/'`
198+
id=[id=\"${id_core}\"]
199+
heading=`echo -e $line | sed 's/^##### hammer/==/'`
189200
[ -n "$NOOP" ] || [ -n "$ASSEMBLY_ONLY" ] || \
190-
echo -e "$MOD_TYPE\n\n$id\n$heading" >>$mod_file
201+
echo -e "$id\n$heading" >>$mod_file
191202
begin_section=yes
192203
skip_section=
193204
options=
@@ -200,7 +211,7 @@ cat "$SRC" | while read line ; do
200211
"Usage:")
201212
[ -n "$DEBUG" ] && echo -e "I: [Usage]ID: $id_core"
202213
[ -n "$NOOP" ] || [ -n "$ASSEMBLY_ONLY" ] || \
203-
echo -e ".Usage" >>$mod_file
214+
echo -e "*Usage*\n" >>$mod_file
204215
skip_section=
205216
continue
206217
;;
@@ -223,15 +234,15 @@ cat "$SRC" | while read line ; do
223234
"Options:")
224235
[ -n "$DEBUG" ] && echo -e "I: [Options]ID: $id_core"
225236
[ -n "$NOOP" ] || [ -n "$ASSEMBLY_ONLY" ] || \
226-
echo -e ".Options" >>$mod_file
237+
echo -e "*Options*\n" >>$mod_file
227238
options=yes
228239
skip_section=
229240
continue
230241
;;
231242
*"Predefined field sets"*)
232243
[ -n "$DEBUG" ] && echo -e "I: [Predefined field sets]ID: $id_core"
233244
[ -n "$NOOP" ] || [ -n "$ASSEMBLY_ONLY" ] || \
234-
echo -e ".Predefined field sets" >>$mod_file
245+
echo -e "*Predefined field sets*\n" >>$mod_file
235246
field_sets=yes
236247
table_delim=0
237248
skip_section=
@@ -249,7 +260,7 @@ cat "$SRC" | while read line ; do
249260
"Provider specific options:")
250261
[ -n "$DEBUG" ] && echo -e "I: [Provider specific options]ID: $id_core"
251262
[ -n "$NOOP" ] || [ -n "$ASSEMBLY_ONLY" ] || \
252-
echo -e ".Provider specific options\n" >>$mod_file
263+
echo -e "*Provider-specific options*\n" >>$mod_file
253264
provider_specific=yes
254265
skip_section=
255266
options=
@@ -260,7 +271,7 @@ cat "$SRC" | while read line ; do
260271
*Search*fields*)
261272
[ -n "$DEBUG" ] && echo -e "I: [Search / Order fields]ID: $id_core"
262273
[ -n "$NOOP" ] || [ -n "$ASSEMBLY_ONLY" ] || \
263-
echo -e ".Search / Order fields" >>$mod_file
274+
echo -e "*Search and order fields*\n" >>$mod_file
264275
search_fields=yes
265276
skip_section=
266277
options=
@@ -328,7 +339,7 @@ cat "$SRC" | while read line ; do
328339
fixed_line=`echo -e "$fixed_line" | sed -E 's/..1m|..0m/\*/g'` #bold
329340
if echo -e "${line}" | grep -Eq '^-{1,2}[a-z\[]' - ; then # option line
330341
fixed_line=`echo -e "$fixed_line" | sed -E 's/^(-[a-z])/\`\1\`/' | sed -E 's/( )(-[a-z])/\1\`\2\`/g'`
331-
fixed_line=`echo -e "$fixed_line" | sed -E 's/(--\S+\s[A-Z\_]{3,})/\`\1\`/' | sed -E 's/([A-Z\_]{3,})(\`)/xref:hammer-option-details-\L\1\E\[\1\]\2/'`
342+
fixed_line=`echo -e "$fixed_line" | sed -E 's/(--\S+\s[A-Z\_]{3,})/\`\1\`/' `
332343
fixed_line=`echo -e "$fixed_line" | sed -E 's/^(--\S+)/\`\1\`/' | sed -E 's/( )(--\S+)/\1\`\2\`/g'`
333344
fixed_line=`echo -e "$fixed_line" | sed -E 's/ +/ \{endash\} /'`
334345
fixed_line="* $fixed_line"
@@ -426,9 +437,9 @@ echo -e "I: [End of input file]"
426437
echo
427438

428439
# Process option details
429-
echo -e "Processing option details..."
440+
echo -e "Processing data representation module..."
430441
sort $details_tmp | uniq | \
431-
sed -E 's/^([A-Z\_]+)\s+/\n\[id="hammer-option-details-\L\1\E"\]\n\1:: /' >>$details_file
442+
sed -E 's/^([A-Z\_]+)\s+/\n\1:: /' >>$details_file
432443
[ -n "$DEBUG" ] || rm $details_tmp # Delete tmp file
433444

434445
echo

0 commit comments

Comments
 (0)