Total Pageviews

Sunday 9 October 2016

用三个bash脚本建立一个静态网站

Publish a Web site with three bash scripts


This is a follow on from an earlier article that used a much more complex scheme based on a published script. I've found that devising my own mess means that, at least, I understand the mess I have made. These scripts will run on any recent Linux distribution and possibly Mac OS X
I write my Web pages for this site using markdown format. I use three bash scripts on my desktop PC to convert, index and publish the Web pages.
Any improvements you can suggest to the scripts, or better commands to use, would be appreciated. Or suggestions for functions to read up on. Just e-mail me on ping fullstop keith atthing gmail dot com.
  • makepage.sh is called from within the 'pages' directory, runs the markdown script and adds a header and footer, saving the result as an html file
  • makeindex.sh builds an index of all the html files in the 'pages' directory and writes an index.html file. This command is run from the directory above the pages directory and index.html ends up in the 'top' of the Web server public_html directory
  • publish.sh calls lftp and mirrors the pages directory to the remote server, and then separately copies up the index and style sheet as that's all I want in the top level.
Thanks to ubuntu forum member sisco311 for sorting out aspects of my bash script style. You can find the original horrible hacks on the forum thread page.

Make page script

The makepage.sh script assumes that the markdown perl script is on the bash search path. I rename the Markdown.pl file downloaded from daringfireball.net as just 'markdown', then I put that in ~/bin, set the permissions to 755 so that the file is executable, and then add the ~/bin directory to my search path by adding the line export PATH=$PATH:/home/keith/bin to.bashrc. Note that the commands used in this script depend on the bash shell, see the first line of the script.
 #!/bin/bash
 # usage: ./dopage.sh filename (no extension on the file that holds the markdown text)
 TEXTFILE=${1}
 # the first line of the markdown file is always the title of the page, we save that for html title
 TITLE=`head -1 "$TEXTFILE"`
 DATETEXT=$(date '+%a %b %d %Y')
 # write out the web page, including the html generated by markdown running on the text file
 cat << EOF > "$TEXTFILE".html
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 <html xmlns="http://www.w3.org/1999/xhtml"> 
 <head> 
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> 
 <title> 
 $TITLE
 </title>
 <link rel="stylesheet" href="../style.css" type="text/css" media="screen" /> 
 </head> 
 <body>
 <p>[ <a href="../index.html">home</a> ]</p>
 $(markdown "$TEXTFILE")
 <hr /><p><small>Last update: $DATETEXT</small></p>
 </body>
 </html>
 EOF
 # set permissions for the web page as lftp preserves the permissions during the sftp upload
 chmod 777  "$TEXTFILE".html
 exit 0

Make index script

The html title of the page is always on line 6 of the converted html file when converted using makepage.sh. Note that the commands used in this script depend on the bash shell, see the first line of the script. I'm sure that it is possible to wrap the whole of this script in a 'here' block (cat << EOF ... lines ... EOF) but I'm still trying to crack that so the echo block remains for now.
 #!/bin/bash
 datetext=$(date '+%a %b %d %Y')
 dir=pages
 cat << EOF 
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 <html xmlns="http://www.w3.org/1999/xhtml"> 
 <head> 
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> 
 <title> 
 Web site title
 </title>
 <link rel="stylesheet" href="style.css" type="text/css" media="screen" /> 
 </head> 
 <body> 
 <h1>Web site title</h1>
 <p>Tagline</p>
 <h2>Recently modified pages</h2>
 <ul>
 EOF
 while IFS= read -r -d '' file
 do
     echo "<li><a href=\"${file#* }\">$(sed -n "6{p;q;}" "${file#* }")</a></li>"
 done< <(find "$dir" -maxdepth 1 -name "*.html" -type f -printf '%T@ %p\0' | sort -znr)
 cat << EOF
 </ul>
 <hr />
 <p>
 <small>Last update: $datetext</small>
 </p>
 </body>
 </html>
 EOF
 # set the permission for the Web page as lftp preserves permissions...
 chmod 777 index.html
 exit 0

Publish script

This is a script file that provides a series of commands to lftp. Lftp needs to be installed on Debian and on Ubuntu.
  #!/bin/sh
  HOST=web.server.com
  USER=usernameonserver
  PASS=secret
  ./makeindex.sh > index.html
  echo "Starting to sftp..."
  lftp -u ${USER},${PASS} sftp://${HOST} <<EOF
  cd public_html_folder
  put index.html
  put style.css
  mirror -e -n -R pages
  bye
  EOF
  echo "done"
  exit 0

from  http://sohcahtoa.org.uk/pages/publish-a-web-site-with-bash-scripts.html