Skip to content

Commit a80ce50

Browse files
authored
Merge pull request #99 from djfitzgerald/master
Add z/OS support to gh-md-toc
2 parents 3dcedc4 + d8a8c9b commit a80ce50

File tree

1 file changed

+42
-17
lines changed

1 file changed

+42
-17
lines changed

gh-md-toc

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ gh_toc_load() {
4747
#
4848
# Converts local md file into html by GitHub
4949
#
50-
# curl -X POST --data '{"text": "Hello world github/linguist#1 **cool**, and #1!"}' https://api.github.com/markdown
50+
# -> curl -X POST --data '{"text": "Hello world github/linguist#1 **cool**, and #1!"}' https://api.github.com/markdown
5151
# <p>Hello world github/linguist#1 <strong>cool</strong>, and #1!</p>'"
5252
gh_toc_md2html() {
5353
local gh_file_md=$1
@@ -184,7 +184,7 @@ gh_toc(){
184184
if [ -z $no_backup ]; then
185185
echo "!! Origin version of the file: '${gh_src}${ext}'"
186186
echo "!! TOC added into a separate file: '${toc_path}'"
187-
fi
187+
fi
188188
echo
189189
fi
190190
fi
@@ -193,31 +193,50 @@ gh_toc(){
193193
#
194194
# Grabber of the TOC from rendered html
195195
#
196-
# $1 a source url of document.
196+
# $1 - a source url of document.
197197
# It's need if TOC is generated for multiple documents.
198198
#
199199
gh_toc_grab() {
200-
# if closed <h[1-6]> is on the new line, then move it on the prev line
201-
# for example:
202-
# was: The command <code>foo1</code>
203-
# </h1>
204-
# became: The command <code>foo1</code></h1>
200+
if [ `uname -s` == "OS/390" ]; then
201+
grepcmd="pcregrep -o"
202+
echoargs=""
203+
awkscript='{
204+
level = substr($0, length($0), 1)
205+
text = substr($0, match($0, /a>.*<\/h/)+2, RLENGTH-5)
206+
href = substr($0, match($0, "href=\"([^\"]+)?\"")+6, RLENGTH-7)
207+
print sprintf("%*s", level*3, " ") "* [" text "](" gh_url href ")"
208+
}'
209+
else
210+
grepcmd="grep -Eo"
211+
echoargs="-e"
212+
awkscript='{
213+
level = substr($0, length($0), 1)
214+
text = substr($0, match($0, /a>.*<\/h/)+2, RLENGTH-5)
215+
href = substr($0, match($0, "href=\"[^\"]+?\"")+6, RLENGTH-7)
216+
print sprintf("%*s", level*3, " ") "* [" text "](" gh_url href ")"
217+
}'
218+
fi
219+
href_regex='href=\"[^\"]+?\"'
220+
221+
# if closed <h[1-6]> is on the new line, then move it on the prev line
222+
# for example:
223+
# was: The command <code>foo1</code>
224+
# </h1>
225+
# became: The command <code>foo1</code></h1>
205226
sed -e ':a' -e 'N' -e '$!ba' -e 's/\n<\/h/<\/h/g' |
227+
206228
# find strings that corresponds to template
207-
grep -E -o '<a.*id="user-content-[^"]*".*</h[1-6]' |
229+
$grepcmd '<a.*id="user-content-[^"]*".*</h[1-6]' |
230+
208231
# remove code tags
209232
sed 's/<code>//g' | sed 's/<\/code>//g' |
233+
210234
# now all rows are like:
211235
# <a id="user-content-..." href="..."><span ...></span></a> ... </h1
212236
# format result line
213-
# * $0 whole string
237+
# * $0 - whole string
214238
# * last element of each row: "</hN" where N in (1,2,3,...)
215-
echo -e "$(awk -v "gh_url=$1" '{
216-
level = substr($0, length($0), 1)
217-
text = substr($0, match($0, /a>.*<\/h/)+2, RLENGTH-5)
218-
href = substr($0, match($0, "href=\"[^\"]+?\"")+6, RLENGTH-7)
219-
print sprintf("%*s", level*3, " ") "* [" text "](" gh_url href ")" }' |
220-
sed 'y/+/ /; s/%/\\x/g')"
239+
echo $echoargs "$(awk -v "gh_url=$1" "$awkscript" | sed 'y/+/ /; s/%/\\x/g')"
221240
}
222241

223242
#
@@ -267,7 +286,12 @@ gh_toc_app() {
267286
mkdir -p "$TMPDIR"
268287
fi
269288
local gh_tmp_md
270-
gh_tmp_md=$(mktemp $TMPDIR/tmp.XXXXXX)
289+
if [ `uname -s` == "OS/390" ]; then
290+
local timestamp=$(date +%m%d%Y%H%M%S)
291+
gh_tmp_md="$TMPDIR/tmp.$timestamp"
292+
else
293+
gh_tmp_md=$(mktemp $TMPDIR/tmp.XXXXXX)
294+
fi
271295
while read input; do
272296
echo "$input" >> "$gh_tmp_md"
273297
done
@@ -299,3 +323,4 @@ gh_toc_app() {
299323
# Entry point
300324
#
301325
gh_toc_app "$@"
326+

0 commit comments

Comments
 (0)