Skip to content

Instantly share code, notes, and snippets.

@ggshily
Last active December 12, 2015 00:08
Show Gist options
  • Save ggshily/4681576 to your computer and use it in GitHub Desktop.
Save ggshily/4681576 to your computer and use it in GitHub Desktop.
parse wiki notation to html tag
#!/bin/sh
# line start with "h1. " title 1
# line start with "h2. " title 2
# !image_url!
# [link_description|link_address]
# line start with "* " is a list
# line start with "** " is a sub list
DEBUG=1
file=$1
list=0
while read -r line
do
#echo "$line"
#parse link
line=$(echo "$line" | sed -e 's/\[\([^]|]*\)\(|\)\([^]]*\)\]/<a href=\"\3\">\1<\/a>/g')
#parse image
line=$(echo "$line" | sed -e 's/\!\([^!]*\)\!/<img src=\"\1\">/g')
line=$(echo "$line" | sed -e 's/(?)//g')
star=$(expr "$line" : "^\*\+ ")
#trace("star:$star")
(( star > 0 )) && {
(( star - 1 == list )) && {
echo "</li>"
}
(( star - 1 > list )) && {
echo "<ul>"
(( list=list+1 ))
}
(( star -1 < list )) && {
echo "</li>"
echo "</ul>"
echo "</li>"
(( list=list-1 ))
}
echo "<li>"${line:$star}
continue
}
(( list > 0 )) && {
echo "</li>"
echo "</ul>"
(( list=list-1 ))
}
head=$(expr "$line" : "^h[0-9]\. ")
#echo "head"$head
(( head > 0 )) && {
echo "<"${line:0:2}">"${line:$head}"</"${line:0:2}">"
continue
}
len=$(expr length "$line")
(( len > 0 )) && {
echo "<p>$line</p>"
}
done < $file
(( list > 0 )) && {
echo "</li>"
echo "</ul>"
}
trace() {
(( DEBUG == 1 )) && {
echo $1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment