-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmakedoc
executable file
·162 lines (162 loc) · 3.17 KB
/
makedoc
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
#!/bin/bash
shopt -s lastpipe
IFS=$'\n'
declare -i i=1
file="${1:-ebashs}"
doc="doc/README.bml"
function print-stdin {
while read -r; do echo "$REPLY"; done
}
function readme {
function table-init {
divide=1
table_columns="$1"
shift
local -i i=0
for col in "$@"; do
local -n n="table_column_$i"
n="$col"
((i++))
done
}
function table-char {
table_char=('-' '|' X X X '|' '|' '|' X X X)
}
function table-top {
true
}
function table-bottom {
true
}
function title {
printf '## %s' "$@"
}
function header {
printf '# %s' "$@"
}
function code-block {
printf '```%s\n' "$1"
while read -r; do echo "$REPLY"; done
printf '```'
}
function text {
while read -r; do echo "$REPLY"; done
}
function table-divider {
printf '%s' "${table_char[5]}"
for ((i=0; i<table_columns; i++)); do
local -n n="table_column_$i"
for ((ii=1; ii-1<n; ii++)); do
printf ' '
done
((i==table_columns-1)) || printf '%s' "${table_char[6]}"
((i==table_columns-1)) && printf '%s' "${table_char[7]}"
done
printf '\n'
}
function table-title-line {
printf '%s' "${table_char[5]}"
for ((i=0; i<table_columns; i++)); do
local -n n="table_column_$i"
for ((ii=1; ii-1<n; ii++)); do
printf '%s' "${table_char[0]}"
done
((i==table_columns-1)) || printf '%s' "${table_char[6]}"
((i==table_columns-1)) && printf '%s' "${table_char[7]}"
done
printf '\n'
}
function table-item {
local -i i=0
for item in "$@"; do
local -n n="table_column_$i"
printf '%s' "${table_char[1]}"
printf '%s' "$item"
printf '%*s' $(( n - ${#item} )) ''
((i++))
done
printf '%s' "${table_char[1]}"
printf '\n'
}
function table-title {
local -i i=0
for title in "$@"; do
local -n n="table_column_$i"
printf '%s' "${table_char[1]}"
printf '%*s' $(( n / 2 - ( ${#title} / 2 ) )) ''
printf '%s' "$title"
if ((${#title}%2==0)) && ((n%2==1)); then
printf '%*s' $(( ( n / 2 - ( ${#title} / 2 ) ) + 1 )) ''
else
printf '%*s' $(( n / 2 - ( ${#title} / 2 ) )) ''
fi
((i++))
done
printf '%s' "${table_char[1]}"
printf '\n'
}
function code {
printf '`%s`' "$@"
}
unset buffer
skip=0
function remove-indentation {
[[ "${1:0:3}" = '```' ]] && {
printf '%s\n' "$1"
if ((skip)); then
skip=0
else
skip=1
fi
return
}
((skip)) && {
printf '%s\n' "$1"
return
}
IFS=''
line_start=1
while read -rsn1 char; do
if ((line_start)); then
case "${char}" in
' ') continue;;
*) line_start=0 && printf '%s' "$char";;
esac
else
printf '%s' "$char"
fi
done <<< "$1"
printf '\n'
}
source "$doc"
true > README.md
for line in "${buffer[@]}"; do
remove-indentation "${line}" >> README.md
done
}
function file {
exit # temporary
{
mapfile -t -O 1 fileload < "$file"
for line in "${fileload[@]}"; do
((skip)) || printf '%s\n' "$line"
case "$line" in
' # MAKEDOC INSERT README START')
print-stdin < "$doc"
skip=1
;;
' # MAKEDOC INSERT README STOP')
skip=0
printf '%s\n' "$line"
;;
esac
((i++))
done
} | mapfile -t -O 1 output
true > "$file"
for line in "${output[@]}"; do
printf '%s\n' "$line" >> "$file"
done
}
readme
file