-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathh2md
executable file
·118 lines (118 loc) · 5.31 KB
/
h2md
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
#!/bin/bash
shopt -s extglob
# Convert a header file into a markdown file.
# Pass the header file to convert as first argument.
# Markdown text sent to standard output.
typeset -i comment=0
typeset -i endcomment=0
typeset -i newpar=0
typeset -i verbatim=0
preline=
while IFS= read -r newline ; do # Read standard input stream
preline="$preline${newline%"\\"}" # Aggregate lines ending with \
if [[ "$newline" == *"\\" ]] ; then
continue
else
line="$preline"
preline=
fi
if (( comment )) && [[ "$line" == *([[:blank:]]) ]] ; then # An empty line starts a new paragraph.
echo
newpar=1
verbatim=0
fi
if (( comment )) && (( newpar )) && [[ "$line" == +([[:blank:]])!([[:blank:]])* ]] ; then # A new paragraph starting with spaces is treated as verbatim (snippets of code inside comments).
newpar=0
verbatim=1
fi
while ! [[ "$line" == *([[:blank:]]) ]] ; do # Not a blank line
if ! (( comment )) && [[ "$line" == *"/*"*"*/"* ]] ; then
a="${line#*"/*"}" # what is after /*
a="${a%%"*/"*}" # what if before */
a="${a##+("*")*([[:blank:]])}" # what if after ****
a="${a%%*([[:blank:]])+("*")}" # what if before ****
a="${a/%"."*([[:blank:]])/".\n\n"}" # add a \n after . followed by spaces
echo -e "${a#+([[:blank:]])}"
code="$code ${line%%"/*"*}" # what is before /* is code, a space replaces the comment in the code
line="${line#*"*/"}" # what is after */ is code or comments
elif ! (( comment )) && [[ "$line" == *"/*"* ]] ; then
comment=1
a="${line#*"/*"}" # what is after /*
a="${a##+("*")*([[:blank:]])}" # what if after ****
a="${a%%*([[:blank:]])+("*")}" # what if before ****
a="${a/%"."*([[:blank:]])/".\n\n"}" # add a \n after . followed by spaces
echo -e "${a#+([[:blank:]])}"
code="$code${line%%"/*"*}" # what is before /* is code
line=""
elif (( comment )) && [[ "$line" == *"*/"* ]] ; then
comment=0
a="${line%%"*/"*}" # what if before */
a="${a%%*([[:blank:]])+("*")}" # what if before ****
if ! (( verbatim )) ; then
a="${a/%"."*([[:blank:]])/".\n\n"}" # add a \n after . followed by spaces
echo -e "${a#+([[:blank:]])}"
else # verbatim
echo -n -e "\t" ; echo -E "${a%%+([[:blank:]])}"
fi
line="${line#*"*/"}" # what is after */ is code or comments
code="$code " # a space replaces the comment in the code
elif (( comment )) ; then
a="$line"
if ! (( verbatim )) ; then
a="${a##*([[:blank:]])+("*")*([[:blank:]])}" # what if after ****
a="${a%%*([[:blank:]])+("*")}" # what if before ****
a="${a/%"."*([[:blank:]])/".\n\n"}" # add a \n after . followed by spaces
echo -e "${a#+([[:blank:]])}"
else # verbatim
echo -n -e "\t" ; echo -E "${a%%+([[:blank:]])}"
fi
line=""
elif ! (( comment )) && [[ "$line" == *"//"* ]] ; then
a="${line#*"//"}" # what is after //
a="${a/%"."*([[:blank:]])/".\n\n"}" # add a \n after . followed by spaces
echo -e "${a#+([[:blank:]])}"
code="$code${line%%"//"*}"
line=""
else
code="$code$line"
line=""
fi
done
## Code between comments
if (( comment )) ; then continue ; fi
if [[ "$code" == "#"*([[:blank:]])"include"+([[:blank:]])* ]] ; then # '#include'
code="${code//"\\"/"\\\\"}"
code="${code//"*"/"\\*"}"
code="${code//"_"/"\\_"}"
echo -n -e "\n| Include |\n| - |\n| "
echo -n -E "${code#"#"*([[:blank:]])"include"+([[:blank:]])}"
echo -e " |\n"
elif [[ "$code" == "#"*([[:blank:]])"define"+([[:blank:]])* ]] ; then # '#define'
code="${code//"\\"/"\\\\"}"
code="${code//"*"/"\\*"}"
code="${code//"_"/"\\_"}"
code="${code#"#"*([[:blank:]])"define"+([[:blank:]])}"
echo -n -e "\n| Define | Value |\n| - | - |\n| "
echo -n -E "${code/+([[:blank:]])/" | "}"
echo -e " |\n"
elif [[ "$code" == "#"* ]] ; then
:
elif [[ "$code" == *([[:blank:]])"typedef"+([[:blank:]])* ]] ; then # 'typedef'
code="${code//"\\"/"\\\\"}"
code="${code//"*"/"\\*"}"
code="${code//"_"/"\\_"}"
code="${code#*([[:blank:]])"typedef"+([[:blank:]])}"
code="${code%";"*([[:blank:]])}"
echo -n -e "\n| Type definition |\n| - |\n| "
echo -n -E "$code"
echo -e " |\n"
elif [[ "$code" != *([[:blank:]]) ]] ; then # code
echo -e '```c'
echo -E "$code"
echo -e '```'
fi
code=""
done < "$1"
echo
echo -----
echo -e "*This page was generated automatically from \`${1##*/}\` by \`${0##*/}\`.*"