Skip to content

Commit f3e7b25

Browse files
committed
Allow gh-md-toc to run on z/OS Unix System Services and remove non-EBCDIC friendly characters
1 parent 488f310 commit f3e7b25

File tree

1 file changed

+43
-18
lines changed

1 file changed

+43
-18
lines changed

gh-md-toc

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
# substr($0, match($0, "href=\"[^\"]+?\" ")+6, RLENGTH-8)
2424
#
2525

26-
gh_toc_version="0.6.2"
26+
gh_toc_version="0.6.3"
2727

2828
gh_user_agent="gh-md-toc v$gh_toc_version"
2929

@@ -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
@@ -182,7 +182,7 @@ gh_toc(){
182182
if [ -z $no_backup ]; then
183183
echo "!! Origin version of the file: '${gh_src}${ext}'"
184184
echo "!! TOC added into a separate file: '${toc_path}'"
185-
fi
185+
fi
186186
echo
187187
fi
188188
fi
@@ -191,31 +191,50 @@ gh_toc(){
191191
#
192192
# Grabber of the TOC from rendered html
193193
#
194-
# $1 a source url of document.
194+
# $1 - a source url of document.
195195
# It's need if TOC is generated for multiple documents.
196196
#
197197
gh_toc_grab() {
198-
# if closed <h[1-6]> is on the new line, then move it on the prev line
199-
# for example:
200-
# was: The command <code>foo1</code>
201-
# </h1>
202-
# became: The command <code>foo1</code></h1>
198+
if [ `uname -s` == "OS/390" ]; then
199+
grepcmd="pcregrep -o"
200+
echoargs=""
201+
awkscript='{
202+
level = substr($0, length($0), 1)
203+
text = substr($0, match($0, /a>.*<\/h/)+2, RLENGTH-5)
204+
href = substr($0, match($0, "href=\"([^\"]+)?\"")+6, RLENGTH-7)
205+
print sprintf("%*s", level*3, " ") "* [" text "](" gh_url href ")"
206+
}'
207+
else
208+
grepcmd="grep -Eo"
209+
echoargs="-e"
210+
awkscript='{
211+
level = substr($0, length($0), 1)
212+
text = substr($0, match($0, /a>.*<\/h/)+2, RLENGTH-5)
213+
href = substr($0, match($0, "href=\"[^\"]+?\"")+6, RLENGTH-7)
214+
print sprintf("%*s", level*3, " ") "* [" text "](" gh_url href ")"
215+
}'
216+
fi
217+
href_regex='href=\"[^\"]+?\"'
218+
219+
# if closed <h[1-6]> is on the new line, then move it on the prev line
220+
# for example:
221+
# was: The command <code>foo1</code>
222+
# </h1>
223+
# became: The command <code>foo1</code></h1>
203224
sed -e ':a' -e 'N' -e '$!ba' -e 's/\n<\/h/<\/h/g' |
225+
204226
# find strings that corresponds to template
205-
grep -E -o '<a.*id="user-content-[^"]*".*</h[1-6]' |
227+
$grepcmd '<a.*id="user-content-[^"]*".*</h[1-6]' |
228+
206229
# remove code tags
207230
sed 's/<code>//g' | sed 's/<\/code>//g' |
231+
208232
# now all rows are like:
209233
# <a id="user-content-..." href="..."><span ...></span></a> ... </h1
210234
# format result line
211-
# * $0 whole string
235+
# * $0 - whole string
212236
# * last element of each row: "</hN" where N in (1,2,3,...)
213-
echo -e "$(awk -v "gh_url=$1" '{
214-
level = substr($0, length($0), 1)
215-
text = substr($0, match($0, /a>.*<\/h/)+2, RLENGTH-5)
216-
href = substr($0, match($0, "href=\"[^\"]+?\"")+6, RLENGTH-7)
217-
print sprintf("%*s", level*3, " ") "* [" text "](" gh_url href ")" }' |
218-
sed 'y/+/ /; s/%/\\x/g')"
237+
echo $echoargs "$(awk -v "gh_url=$1" "$awkscript" | sed 'y/+/ /; s/%/\\x/g')"
219238
}
220239

221240
#
@@ -265,7 +284,12 @@ gh_toc_app() {
265284
mkdir -p "$TMPDIR"
266285
fi
267286
local gh_tmp_md
268-
gh_tmp_md=$(mktemp $TMPDIR/tmp.XXXXXX)
287+
if [ `uname -s` == "OS/390" ]; then
288+
local timestamp=$(date +%m%d%Y%H%M%S)
289+
gh_tmp_md="$TMPDIR/tmp.$timestamp"
290+
else
291+
gh_tmp_md=$(mktemp $TMPDIR/tmp.XXXXXX)
292+
fi
269293
while read input; do
270294
echo "$input" >> "$gh_tmp_md"
271295
done
@@ -297,3 +321,4 @@ gh_toc_app() {
297321
# Entry point
298322
#
299323
gh_toc_app "$@"
324+

0 commit comments

Comments
 (0)