-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathget-all-doc-paths-of-type
executable file
·282 lines (225 loc) · 6.19 KB
/
get-all-doc-paths-of-type
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/env bash
# Get all adoc paths of a certain :doctype: and :mn-document-class: in a
# directory
# (default: src-documents)
#
VERSION="0.0.1"
log() {
printf "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: %b\n" "$*"
} >&2
err() {
printf "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: \e[1;31mError:\e[m %b\n" "$*"
} >&2
ecdo() {
log "Running: $*"
"$@"
}
# BOOK KEEPING
DOC_TYPE="${DOC_TYPE:-}"
DOC_CLASS="${DOC_CLASS:-}"
FAILED_ADOCS=()
SUCCESS_ADOCS=()
ADOC_REFERENCES=()
COMMAND=
print-summary() {
if [[ "${COMMAND:-}" = flags ]]
then
return
fi
if [[ -z "${DOC_TYPE}" || -z "${DOC_CLASS}" ]]
then
show_usage
return
fi
local readable_docs=$(( ${#ADOC_REFERENCES[@]} - ${#FAILED_ADOCS[@]} ))
log "There are \e[1m${#ADOC_REFERENCES[@]}\e[m documents in total."
log "Successfully picked \e[1m${#SUCCESS_ADOCS[@]}\e[m out of \e[1m${readable_docs}\e[m readable documents that are of :mn-document-class: \e[1m${DOC_CLASS}\e[m and :doctype: \e[1m${DOC_TYPE}\e[m."
if [[ "${#FAILED_ADOCS[@]}" -gt 0 ]]
then
err "Failed to process \e[1m${#FAILED_ADOCS[@]}\e[m documents:"
for failure in "${FAILED_ADOCS[@]}"
do
err " ${failure}"
done
fi
}
trap print-summary EXIT
METANORMA_YAML_PATHS=()
get-all-metanorma-yaml-paths() {
local src_dir="${1:?Missing source directory}"
while read -r yml
do
METANORMA_YAML_PATHS+=("$yml")
done < <(find "$src_dir" -type f -name 'metanorma.y*ml')
log "Found ${#METANORMA_YAML_PATHS[@]} metanorma YAML files"
}
get-all-sources-from-metanorma-yaml() {
local yml="${1:?Missing metanorma YAML file}"
local path_prefix="${yml%/*}"
# log "pathPrefix is $pathPrefix"
# Only proceed if 'files' is not null (should be an array)
<"$yml" yq -r ".metanorma.source.files | select(null != .) | map(\"${path_prefix}/\" + .)"
}
get-all-sources-from-metanorma-yamls() {
for yml in "${METANORMA_YAML_PATHS[@]}"
do
get-all-sources-from-metanorma-yaml "$yml"
done
}
# Only search within the first n lines of the document
DOC_SEARCH_FIRST_N_LINES="${DOC_SEARCH_FIRST_N_LINES:-100}"
COMMON_DOC_TYPES=(
standard
directive
guide
specification
report
amendment
technical-corrigendum
administrative
international-standard
internet-draft
advisory
)
# See: https://github.com/CalConnect/VCARD/blob/4d17cb269cbe48cff9603d293e1374bc81fba7f1/presentations/20180926-calconnect-mn-authoring/index.adoc?plain=1#L220
# :doctype: can be standard, directive, guide, specification, report,
# amendment, technical-corrigendum.
#
# But these are also seen in the wild:
# administrative | international-standard | internet-draft | advisory
#
doc-is-of-doc-type() {
local doc="${1:?Missing asciidoc path}"
local doc_type="${2:?Missing doctype: standard | directive | guide | specification | report | amendment | technical-corrigendum | administrative | international-standard | internet-draft | advisory}"
<"$doc" \
head -n "${DOC_SEARCH_FIRST_N_LINES}" \
| grep -q ":doctype: $doc_type"
}
doc-is-of-doc-class() {
local doc="${1:?Missing asciidoc path}"
local doc_class="${2:?Missing mn-document-class}"
<"$doc" \
head -n "${DOC_SEARCH_FIRST_N_LINES}" \
| grep -q ":mn-document-class: $doc_class"
}
doc-is-of-common-doc_types() {
local doc="${1:?Missing asciidoc path}"
for doc_type in "${COMMON_DOC_TYPES[@]}"
do
if doc-is-of-doc-type "$doc" "$doc_type"
then
return 0
fi
done
local actual_doc_type
actual_doc_type=$(
<"$doc" head -n "${DOC_SEARCH_FIRST_N_LINES}" \
| grep ":doctype:" \
| cut -d' ' -f2-
)
err "Document $doc is not of any common doc types, got \`$actual_doc_type'."
err "List of common doc types: ${COMMON_DOC_TYPES[*]}"
return 1
}
# Paginated results are in separate arrays,
# so slurp them into one for a more uniform experience.
slurp-into-one-list() {
command jq -s -r '[.[][]] | .[]'
}
# Filter out all the documents that are of a certain :mn-document-class:
# and a certain :doctype:.
filter-docs() {
local doc
for doc in "${ADOC_REFERENCES[@]}"
do
if [[ -r "${doc}" ]]
then
if filter-doc "$doc"
then
SUCCESS_ADOCS+=("$doc")
echo "$doc"
fi
else
err "Document $doc is not readable"
FAILED_ADOCS+=("$doc")
fi
done
}
# If $DOC_CLASS is set, only include documents that are of that :mn-document-class:.
# If $DOC_TYPE is set, only include documents that are of that :doctype:.
filter-doc() {
local doc="${1:?Missing asciidoc path}"
if [[ -n "${DOC_CLASS}" ]] && \
! doc-is-of-doc-class "$doc" "${DOC_CLASS}"
then
return 1
fi
if [[ -n "${DOC_TYPE}" ]] && \
! doc-is-of-doc-type "$doc" "${DOC_TYPE}"
then
return 1
fi
return 0
}
# Bookkeeping
record-adoc-references() {
while read -r line
do
ADOC_REFERENCES+=("$line")
done < <( get-all-sources-from-metanorma-yamls \
| slurp-into-one-list
)
}
show_usage() {
COMMAND=flags
local prog_name="\e[1m${0}\e[22m"
printf "%b" "Usage: $prog_name [subdir]
$prog_name [-h|--help]
$prog_name --version
Return all adoc paths of a certain \e[4m:doctype:\e[24m and \e[4m:mn-document-class:\e[24m
in a directory, as a list of newline-separated paths.
\e[4mFlags:\e[24m
--version Print the version and exit
--help Print this help message and exit
\e[4mParameters:\e[24m
subdir The directory to search for metanorma YAML files
(default: src-documents)
\e[4mMandatory environment variables:\e[24m
DOC_TYPE The \e[4m:doctype:\e[24m to filter by
DOC_CLASS The \e[4m:mn-document-class:\e[24m to filter by
"
}
main() {
if [[ "$1" == "-h" || "$1" == "--help" ]]
then
show_usage
exit 0
elif [[ "$1" == "--version" ]]
then
COMMAND=flags
echo "${0} v${VERSION}"
exit 0
fi
local subdir="${1:-src-documents}"
DOC_TYPE="${DOC_TYPE-administrative}"
DOC_CLASS="${DOC_CLASS-cc}"
if [[ ! -d "${subdir}" ]]
then
err "Directory ${subdir} does not exist"
exit 1
fi
if [[ -z "${DOC_TYPE:-}" ]]
then
err "DOC_TYPE is not set"
exit 1
fi
if [[ -z "${DOC_CLASS:-}" ]]
then
err "DOC_CLASS is not set"
exit 1
fi
get-all-metanorma-yaml-paths "${subdir:?Missing doc subdir}"
record-adoc-references
filter-docs
}
main "$@"