-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer
executable file
·177 lines (134 loc) · 3.66 KB
/
renderer
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
#!/usr/bin/env bash
show_help() {
printf "%s" "
$(f_heading "renderer" | f_fg_yellow)
reads the formatted output of a program, and writes it to a file.
before the actual write, the output is passed through some transformers that \
strip-down and dress-up the text, e.g. get rid of redundant blank lines, or \
append an auto-generated message to the end of the file.
if you don't want to use the transformers, you can just skip the renderer and \
redirect your program's output directly to a file, after setting the desired format. for example:
$(f_code_block \
"sh" \
'env FORMAT=markdown ./my-command -h > ./usage/my-command.md')
$(f_subheading "usage")
$(f_code_block \
"sh" \
'( export FORMAT=<format> OUTPUT_PATH=<output_path> ; <my-command> | ./styli.sh/renderer )')
$(f_code_block \
"sh" \
'./renderer -h')
$(f_subheading "flags")
$(f_code_definition \
"-h" \
"shows this help message.")
$(f_subheading "environment")
environment variables for the renderer must be declared using $(f_code 'export') \
(rather than $(f_code 'env')), in order to share them both with the renderer and the formatter in your program.
$(f_code_definition \
"FORMAT" \
"which format to use. possible values: $(f_code 'markdown'), $(f_code 'terminal'), $(f_code 'raw').")
$(f_code_definition \
"OUTPUT_PATH" \
"where to write to. value should be a full path, including file name and extension.")
$(f_subheading "examples")
assuming $(f_code './foo') is a program that $(f_code 'source')s the $(f_italics 'styli.sh') \
formatter, and its output is formatted when using the $(f_code '-h') flag:
$(f_list_item \
"render a markdown file" \
"$(f_code_block \
"sh" \
'( export FORMAT="markdown" OUTPUT_PATH='./usage/foo.md' ; ./foo -h | ./styli.sh/renderer )')
writes the markdown-formatted output of $(f_code './foo -h') to $(f_code './usage/foo.md')")
"
exit 0
}
init_renderer() {
local output_dir
require_var "FORMAT"
require_var "OUTPUT_PATH"
echo
echo "> > > start rendering"
echo "> format: '$FORMAT'"
echo "> output path: '$OUTPUT_PATH'"
echo "> ensuring output directory..."
output_dir="$( dirname "$OUTPUT_PATH" )"
ensure_dir "$output_dir"
echo "> directory ensured: '$output_dir'"
echo "> redirecting all input to the output path"
cat - | strip_down | dress_up > "$OUTPUT_PATH"
# a little trick if this script will ever be sourced
# shellcheck disable=SC2128
if [[ $BASH_SOURCE != "$0" ]]; then
exec 1> "$OUTPUT_PATH" > >( strip_down | dress_up )
fi
}
require_var() {
if [[ -z "${!1}" ]]; then
echo "$1 is mandatory!"
exit 1
fi
}
ensure_dir() {
if [[ ! -d $1 ]]; then
mkdir -p "$1"
fi
}
dress_up() {
add_message | eof
}
strip_down() {
case $FORMAT in
markdown)
squeeze_blanks
;;
*)
cat -
;;
esac
}
add_message() {
local repo_url="https://github.com/eliranmal/styli.sh"
case $FORMAT in
markdown)
printf "%s\n\n\n\n%s\n%s\n%s" "$(cat -)" '<br/><br/>' '---' \
'<sup><i>created with <b><a href="'"$repo_url"'">styli.sh</a></b></i></sup>'
;;
raw)
printf "%s\n\n\n\n\n%s\n%s" "$(cat -)" '---' 'created with styli.sh ('"$repo_url"')'
;;
*)
cat -
;;
esac
}
eof() {
case $FORMAT in
markdown)
# shellcheck disable=SC2005
echo "$(cat -)" # add newline at end of file
;;
*)
cat -
;;
esac
}
squeeze_blanks() {
cat -s
}
main() {
local source_dir
# test if this script is executed (i.e. not sourced)
# shellcheck disable=SC2128
if [[ $BASH_SOURCE = "$0" ]]; then
if [[ $1 = "-h" ]]; then
# shellcheck disable=SC2164
source_dir="$( cd "$(dirname "${BASH_SOURCE}")" ; pwd -P )"
# shellcheck source=/dev/null
source "$source_dir"/formatter
show_help
fi
init_renderer
fi
}
main "$@"