forked from stdlib-js/stdlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadme_files_to_html
More file actions
executable file
·156 lines (128 loc) · 3.02 KB
/
Copy pathreadme_files_to_html
File metadata and controls
executable file
·156 lines (128 loc) · 3.02 KB
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
#!/usr/bin/env bash
#
# @license Apache-2.0
#
# Copyright (c) 2017 The Stdlib Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Converts package README files to HTML.
#
# This script is called with the following arguments:
#
# * `$1` - destination directory
# VARIABLES #
# Destination directory:
output_dir="$1"
if [[ -z "${output_dir}" ]]; then
echo 'ERROR: Must provide a destination directory.' >&2
exit 1
fi
# Determine root directory:
root_dir="$(git rev-parse --show-toplevel)"
# Define the path to a utility for finding packages:
find_packages="${root_dir}/tools/scripts/find_packages"
# Define the path to the source library:
source_dir="${root_dir}/lib/node_modules"
# Define the path to a utility for converting Markdown to HTML:
to_html="${source_dir}/@stdlib/_tools/markdown/to-html/bin/cli"
# FUNCTIONS #
# Defines an error handler.
#
# $1 - error status
on_error() {
echo 'ERROR: An error was encountered during execution.' >&2
cleanup
exit "$1"
}
# Runs clean-up tasks.
cleanup() {
echo ''
}
# Creates a directory.
#
# $1 - directory path
create_dir() {
mkdir -p "$1"
}
# Prints a success message.
print_success() {
echo 'Success!' >&2
}
# Extracts a package name from an absolute file path.
#
# $1 - file path
package_name() {
local name
# Remove the source directory path from the package path (using POSIX shell variable expansion):
if [[ "$1" == "${root_dir}" ]]; then
name='@stdlib/stdlib'
else
name="${1#${source_dir}/}"
fi
echo "${name}"
}
# Converts READMEs to HTML.
#
# $1 - packages
convert_files() {
local files
local dest
local name
local src
echo 'Converting README files...' >&2
echo '' >&2
for pkg in $(echo "$1"); do
echo "Package: ${pkg}" >&2
src="${pkg}/README.md"
name=$(package_name "${pkg}")
if [[ -f "${src}" ]]; then
dest="${output_dir}/${name}"
create_dir "${dest}"
convert_file "${src}" "${dest}/index.html"
if [[ "$?" -ne 0 ]]; then
echo "Encountered an error when converting package README: ${pkg}.". >&2
return 1
fi
echo 'Successfully converted package README.' >&2
else
echo 'No README. Skipping package.' >&2
fi
echo '' >&2
done
echo 'Finished converting README files.' >&2
return 0
}
# Converts a README to HTML.
#
# $1 - source file
# $2 - destination
convert_file() {
cat "$1" | "${to_html}" > "$2"
}
# Main execution sequence:
main() {
local pkgs
pkgs=$("${find_packages}")
if [[ "$?" -ne 0 ]]; then
on_error 1
fi
convert_files "${pkgs}"
if [[ "$?" -ne 0 ]]; then
on_error 1
fi
print_success
cleanup
exit 0
}
# Run main:
main