-
Notifications
You must be signed in to change notification settings - Fork 1
/
markdown-generator-for-menu-items.sh
executable file
·47 lines (44 loc) · 2.01 KB
/
markdown-generator-for-menu-items.sh
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
#!/usr/bin/env bash
set -e
#set -x
lib_dir="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
name="$(basename "${BASH_SOURCE[0]}")"
perl -ne '
/^[^#]*key=/ and not /\bOFF\b/ and print;
/^[^#]*$key-link/ and print;
/^\s*#\s+(start|end)/ and print;
/^[^#]*description="[^"]/ and print;
' "$lib_dir/menu-items.sh" |
perl -pe '
s/.*key="([^"]+)".*/$1/;
s/.*key-link[^=]+="([^"]+)"/[link]($1)/;
s/.*(#\s+start.*submenu)/\n$1/i;
s/.*(#\s+end.*submenu)/$1\n/i;
' |
perl -0777 -pe '
s/\n\s*(description="[^"]+")/| |$1/g; # join description to previous line
s/\n\[link/ |[link/g; # join link to previous line
s/(#\s+start.*\n.+\n)/$1|:---|:-:|:--|\n/g; # create MD header separator on submenus
s/\n\n([^#\n].*)/\n\nTop Level Menu Items\n|:---|:-:|:--|\n$1/g; # create header on top levem menu items
s/^/\n\nTop Level Menu Items\n|:---|:-:|:--|\n$1/; # create header on top levem menu items
' |
perl -pe '
/^[^#|].+$/ and not /description=/ and s/^/|/ and s/$/| | |/; # make each w/o descriptions -> row 3 cols
/\[link\]/ and s/\| \|/\|/; # make 2nd col link for links
s/^(.*)\|description="(.*?)"(.*)$/|$1$3|$2|/; # reorder descriptions after links
s/^(#.*)/<!--- $1 --->/; # use MD comments to hide submenu delimiters
s/\bON\b/ON\\|OFF/; # show both toggle values in the menu item
' |
perl -0777 -pe '
s/\|([^|]+)(\|[^|]+\|)([^|]+\|\n)(\|:---)/### $1 \n\n|$1$2$3$4/g; # add MD headers for TOC above each table
# fix table col width by appending row with nobr tags enclosing spaces that will be translated to unicode
s{(\|\n(?!\|))}{sprintf "$1|<nobr>%-*s</nobr>|<nobr>%-*s</nobr>| |\n", 18, "", 3, "" }ge;
' |
perl -pe '
/<nobr>/ and tr/ /\x{2003}/; # replace reg spaces with unicode space to prevent whitespace collapse
s/\\`/`/g; # remove shell escaped backtick (valid for MD)
' |
perl -0777 -pe "
s/^/<!--- START AUTOGENERATED MD by $name --->/;
s/$/\n<!--- END AUTOGENERATED MD by $name --->/;
"