Guest User

s.cgi shell blog script

a guest
Feb 22nd, 2011
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. BLOGTITLE="test blog"
  4. BLOGSUBTITLE="super cool!!!"
  5. POSTSDIR="posts"
  6. DEFTAG="uncategorized"
  7.  
  8. getparam() {
  9. # Thanks to the site below for this bit of sed:
  10. # http://foxlx.acmesystems.it/?id=165
  11. echo $QUERY_STRING | sed -n "s/^.*$1=\([^&]*\).*$/\1/p" | sed 's/%20/ /g'
  12. }
  13.  
  14. getposts() {
  15. ls -t $(find -iname '*.md' | sed 's,^\./,,')
  16. }
  17.  
  18. posttitle() {
  19. echo $1 | sed -n 's/^\([^-]*\).*\.md$/\1/p'
  20. }
  21.  
  22. posttags() {
  23. TAGS=$(echo $1 | sed -n 's/^[^-]*-\(.*\)\.md$/\1/p' | sed 's/-/ /g')
  24. [ -z "$TAGS" ] && TAGS="$DEFTAG" # Apply default tag to untagged posts
  25. echo $TAGS
  26. }
  27.  
  28. recentposts() {
  29. POSTS=$(getposts | head -$1)
  30.  
  31. for p in $POSTS; do
  32. TITLE=$(posttitle $p)
  33.  
  34. cat << POSTLINK
  35. <a href="s.cgi?post=$TITLE">$(echo $TITLE | sed 's/_/ /g')</a>
  36. <br>
  37. POSTLINK
  38. done
  39. }
  40.  
  41. gettags() {
  42. POSTS=$(getposts)
  43. TAGS="$DEFTAG"
  44.  
  45. for p in $POSTS; do
  46. for t in $(posttags $p); do
  47. [ -z "$(echo $TAGS | grep $t)" ] && TAGS="$TAGS $t"
  48. done
  49. done
  50.  
  51. echo $TAGS
  52. }
  53.  
  54. taglinks() {
  55. for t in $*; do
  56. echo "<a href=\"s.cgi?tag=$t\">$t</a>"
  57. done
  58. }
  59.  
  60. showpost() {
  61. TITLE=$(posttitle $1)
  62. TAGS=$(posttags $1)
  63.  
  64. cat << BLOGPOST
  65. <h3 style="display: inline; float: left; margin: 0;">
  66. $(echo $TITLE | sed 's/_/ /g')
  67. </h3>
  68. <p style="display: inline; float: right; font-size: 80%; margin: 0;">
  69. $(taglinks $TAGS)
  70. </p>
  71. <div style="clear: both;">
  72. <!-- markdown output begins... -->
  73. $(../md2html.awk $1)
  74. <!-- ...and ends -->
  75. </div>
  76. BLOGPOST
  77. }
  78.  
  79. showtag() {
  80. echo "<h3>Posts tagged \"$TAGNAME\"</h3>"
  81.  
  82. for p in $(getposts); do
  83. if [ -n "$(posttags $p | grep $TAGNAME)" ]; then
  84. TITLE=$(posttitle $p)
  85. cat << POSTLINK
  86. <a href="s.cgi?post=$TITLE">$(echo $TITLE | sed 's/_/ /g')</a>
  87. <br>
  88. POSTLINK
  89. fi
  90. done
  91. }
  92.  
  93.  
  94. echo "Content-type: text/html"
  95. echo
  96.  
  97. # Nobody wants to see an empty blog...
  98. if [ -z "$POSTSDIR" ] || [ ! -d $POSTSDIR ]; then
  99. echo "Nothing to see here!"
  100. exit 1
  101. fi
  102.  
  103. cd $POSTSDIR
  104.  
  105. cat << HEADER
  106. <!DOCTYPE html>
  107. <html>
  108. <head>
  109. <title>$BLOGTITLE</title>
  110. <meta charset="utf-8">
  111. <!-- link to user-defined stylesheet will go here -->
  112. </head>
  113. <body>
  114. <h1 style="margin-bottom: 0;">
  115. <a href="s.cgi" style="color: #000; text-decoration: none;">$BLOGTITLE</a>
  116. </h1>
  117. <p style="margin: 0; font-style: italic;">$BLOGSUBTITLE</p>
  118. <hr>
  119. <div style="display: inline; float: left; width: 65%">
  120. HEADER
  121.  
  122. POSTNAME=$(getparam post)
  123. TAGNAME=$(getparam tag)
  124.  
  125. if [ -n "$POSTNAME" ]; then
  126. P=$(ls $POSTNAME*.md)
  127.  
  128. if [ -n "$P" ]; then
  129. showpost $P
  130. else
  131. echo "Post ($POSTNAME) doesn't exist"
  132. fi
  133. elif [ -n "$TAGNAME" ]; then
  134. showtag $TAGNAME
  135. else
  136. showpost $(ls -t *.md | head -1)
  137. fi
  138.  
  139. cat << FOOTER
  140. </div>
  141. <div style="display: inline; float: right; width: 33%;">
  142. <h4>Recent posts</h4>
  143. <p>$(recentposts 10)<p>
  144. <hr>
  145. <h4>Tags</h4>
  146. <p>$(taglinks $(gettags))</p>
  147. </div>
  148. <div style="clear: both; text-align: center;">
  149. <hr>
  150. <p>Pretty footer text goes here</p>
  151. </div>
  152. </body>
  153. </html>
  154. FOOTER
Advertisement
Add Comment
Please, Sign In to add comment