Finished syntactic conversion

Wed, 15 Oct 2008 23:14:54 +0000

author
andy
date
Wed, 15 Oct 2008 23:14:54 +0000
changeset 3
9c426fbf27d4
parent 2
03c561ae154e
child 4
406ab3438918

Finished syntactic conversion

emin file | annotate | diff | comparison | revisions
     1.1 --- a/emin	Fri Oct 03 23:00:37 2008 +0000
     1.2 +++ b/emin	Wed Oct 15 23:14:54 2008 +0000
     1.3 @@ -1,28 +1,25 @@
     1.4  #! /usr/bin/env python
     1.5  # -*- python -*-
     1.6  
     1.7 -## 
     1.8 -## emin - a static web gallery builder
     1.9 -## version 0.1
    1.10 -## by Andy Buckley
    1.11 -## http://www.insectnation.org
    1.12 -## 
    1.13 -## This is a weeny script for making static sets of Web
    1.14 -## pages for presenting lots of imagey things: photos, 
    1.15 -## PDFs, graphs...
    1.16 -## 
    1.17 -## As for the name, this is a program to make pretty 
    1.18 -## crappy galleries, so it's named after a pretty
    1.19 -## crappy artist. And, thankfully, it's not many characters
    1.20 -## to type --- trebles all round!
    1.21 -##
    1.22 -## TODO
    1.23 -##  * Use thickbox
    1.24 -##  * Specify Lightbox base path
    1.25 -##  * Split into blocks of N
    1.26 -##  * Rename images before starting
    1.27 -usage = """
    1.28 -Usage: $0 [-d|-F|-H|-h|-i|-I|-n|-r|-R|-t|-T|-v|-y|-z|-Z]
    1.29 +version = "%prog 0.2"
    1.30 +
    1.31 +usage="""
    1.32 +%prog - a static web gallery builder
    1.33 +
    1.34 +by Andy Buckley
    1.35 +http://www.insectnation.org
    1.36 +
    1.37 +This is a weeny script for making static sets of Web
    1.38 +pages for presenting lots of imagey things: photos, 
    1.39 +PDFs, graphs...
    1.40 +
    1.41 +As for the name, this is a program to make pretty 
    1.42 +crappy galleries, so it's named after a pretty
    1.43 +crappy artist. And, thankfully, it's not many characters
    1.44 +to type --- trebles all round!
    1.45 +
    1.46 +Usage: 
    1.47 +%prog [-d|-F|-H|-h|-i|-I|-n|-r|-R|-t|-T|-v|-y|-z|-Z]
    1.48   -d DIR       build gallery in given directory
    1.49   -F | -H FILE specify footer and header includes
    1.50   -h           displays this oh-so-helpful message
    1.51 @@ -34,14 +31,18 @@
    1.52   -T TAG       specifies the title tag (default is <h1>)
    1.53   -v           enables verbose output while building the gallery
    1.54   -y HEIGHT    specifies the max height of the generated thumbnails
    1.55 - -z | -Z      enable or disable the zip archive"""
    1.56 + -z | -Z      enable or disable the zip archive
    1.57  
    1.58 +TODO:
    1.59 + * Use thickbox, jQuery, Lightbox2 etc.
    1.60 + * Split into blocks of N
    1.61 + * Rename images before starting
    1.62 +"""
    1.63  
    1.64 -import sys, os, shutils, glob, re, commands
    1.65 +
    1.66  from optparse import OptionParser, OptionGroup
    1.67 -import logging
    1.68 -
    1.69 -
    1.70 +parser = OptionParser(usage=usage, version=version)
    1.71 +parser.add_option("-", "--", dest="", help="")
    1.72  ## Num per page / all on one page
    1.73  ## Zip file
    1.74  ## Num cols (or no cols)
    1.75 @@ -53,12 +54,17 @@
    1.76  ## JS
    1.77  ## Generate thumbs (+ force)
    1.78  ## Header & footer files; whole page / snippet; Cheetah format page template
    1.79 -
    1.80 +opts, args = parser.parse_args()
    1.81  
    1.82  ## Configure log levels
    1.83  logging.configRoot(level=opts.LOGLEVEL, format="$(message)")
    1.84  
    1.85  
    1.86 +## More stdlib imports
    1.87 +import sys, os, shutils, glob, re, commands
    1.88 +import logging, math
    1.89 +
    1.90 +
    1.91  ## Try to import Python Imaging Library
    1.92  try:
    1.93      import PIL
    1.94 @@ -67,20 +73,10 @@
    1.95      sys.exit(1)
    1.96  
    1.97  
    1.98 -
    1.99 -def safeencode(string):
   1.100 +def safeencode(s):
   1.101      """Encode a string for use as a filename."""
   1.102 -    #ZIPFILE=$( echo $TITLE | sed -e s/"\ "/"-"/g -e s/","//g -e s/"\/"//g -e s/"\."//g -e s/"'"//g )
   1.103 -    return string
   1.104 -
   1.105 -
   1.106 -ZIPFILE = "photo-album.zip"
   1.107 -if opts.TITLE is not None or len(opts.TITLE) > 0:
   1.108 -    ZIPFILE = safeencode(opts.TITLE)
   1.109 -
   1.110 -
   1.111 -
   1.112 -
   1.113 +    newstr = s.replace(" ", "-").replace(",", "").replace("/", "").replace(".", "")
   1.114 +    return newstr
   1.115  
   1.116  logging.debug("Title: $TITLE")
   1.117  logging.debug("Write header: $writeHeader $headerFile")
   1.118 @@ -88,15 +84,13 @@
   1.119  logging.debug("Thumb height: $THUMBMAXHEIGHT")
   1.120  logging.debug("Non-option args: $nonoptargs")
   1.121  
   1.122 -
   1.123  ## Go to the gallery directory and test if it's writeable
   1.124  if not os.access(opts.OUTDIR, os.W_OK):
   1.125      logging.error("Directory %s is not writeable" % opts.OUTDIR)
   1.126      sys.exit(1)
   1.127  
   1.128 -
   1.129 +## Rename images
   1.130  if opts.RENAME:
   1.131 -    ## Rename images
   1.132      ## TODO: types: PNG/GIF, JPEG, PDF
   1.133      ## TODO: match types
   1.134      for n, img in enumerate(glob.glob(["*.jpg", "*.jpeg", "*.JPG"])):
   1.135 @@ -105,182 +99,161 @@
   1.136              logging.debug("Renaming %s -> %s" % (img, newname))
   1.137              shutils.mv(img, newname)
   1.138  
   1.139 -
   1.140  ## TODO: Convert unviewables to JPG/PNG (keep link to originals)
   1.141  if opts.CONVERT:
   1.142      pass
   1.143  
   1.144 -
   1.145  ## Build the list of pictures to display
   1.146  pics = sorted(glob.glob(extensions))
   1.147  
   1.148 -
   1.149  ## Make thumbnails if needed
   1.150 -os.makedirs(os.path.join(os.OUTDIR, "thumbs")
   1.151 +os.makedirs(os.path.join(os.OUTDIR, "thumbs"))
   1.152  for pic in pics:
   1.153      thumbpic = os.path.join("thumbs", pic)
   1.154 -    if opts.regenThumbs
   1.155 +    if opts.REGENTHUMBS:
   1.156          logging.debug("Making new thumbnail for %s (max height %d)" % (pic, opts.THUMBMAXHEIGHT))
   1.157 +        ## TODO: This!
   1.158          # convert $pic $thumbpic
   1.159          # mogrify -resize "x${THUMBMAXHEIGHT}>" $thumbpic
   1.160  
   1.161 -
   1.162 -## Make a zipped tar archive
   1.163 -if opts.WRITEZIPFILE:
   1.164 -    import zipfiles
   1.165 -    #rm -f *.zip
   1.166 -    logging.debug("Making zipped picture archive")
   1.167 -    ZIPFILE = safename(opts.OUTDIR)
   1.168 -    ## TODO: Do the zipping
   1.169 -    ##zip $ZIPFILE $pics &> /dev/null
   1.170 -
   1.171 -
   1.172 -# Count the pictures
   1.173 +## Count the pictures
   1.174  logging.debug("Number of pictures = %d" % len(pics))
   1.175  if len(pics) == 0:
   1.176      logging.debug("No pictures from which to build a gallery...")
   1.177      sys.exit(2)
   1.178  
   1.179 +## Calculate how many pages will be needed
   1.180 +## TODO: allow all on one page
   1.181 +NUM_PER_PAGE = opts.NUMX * opts.NUMY
   1.182 +NUM_PAGES = math.ceil( len(pics)/float(NUM_PER_PAGE) )
   1.183  
   1.184 -# Calculate how many pages
   1.185 -let npages=$npics/$CHUNK_SIZE
   1.186 -let remainder=$npics-$npages*$CHUNK_SIZE
   1.187 -test $remainder -eq 0 || let npages++
   1.188  
   1.189 -# Make each index page
   1.190 -for page in `seq 1 $npages`; do
   1.191 -    pagefile=$INDEXFILE
   1.192 -    if [[ $page -ne 1 ]]; then pagefile=index${page}.html; fi
   1.193 +def getPageFilename(pagenum):
   1.194 +    if pagenum == 0:
   1.195 +        pagefile = "index.%s" % opts.EXTN
   1.196 +    else:
   1.197 +        pagefile = "index%02d.%s" % (pagenum, opts.EXTN)
   1.198 +
   1.199 +
   1.200 +def mkPageLinkStr(pagenum):
   1.201 +    "Write the linked page list"  
   1.202 +    global NUM_PAGES
   1.203 +    out = ""
   1.204 +    if NUM_PAGES > 1:
   1.205 +        out += "<div class=\"pagelinks\">Pages: "
   1.206 +        ## Previous
   1.207 +        prev = pagenum - 1
   1.208 +        if prev > 0:
   1.209 +            out += '[<a href="%s">prev</a>] ' % getPageFilename(prev)
   1.210 +        else:
   1.211 +            out += '[prev] '
   1.212 +        ## Numbers
   1.213 +        for n in range(NUM_PAGES):
   1.214 +            if n != pagenum:
   1.215 +            out += '[<a href="%s">%d</a>] ' % (getPageFilename(n), n)
   1.216 +        else:
   1.217 +            out += "[%d]" % n
   1.218 +        ## Next
   1.219 +        next = pagenum + 1
   1.220 +        if next <= NUM_PAGES:
   1.221 +            out += '[<a href="%s">prev</a>] ' % getPageFilename(next)
   1.222 +        else:
   1.223 +            out += '[next] '
   1.224 +        out = '</div>'
   1.225 +    return out
   1.226 +
   1.227 +
   1.228 +# Make a zip archive
   1.229 +if opts.WRITEZIPFILE:
   1.230 +    logging.debug("Making zipped picture archive")
   1.231 +    ZIPFILE = "photo-album.zip"
   1.232 +    if opts.TITLE is not None or len(opts.TITLE) > 0:
   1.233 +        ZIPFILE = safeencode(opts.TITLE)
   1.234 +        ZIPFILE = safename(opts.OUTDIR)
   1.235 +    import zipfiles
   1.236 +    #rm -f *.zip
   1.237 +    ## TODO: Do the zipping
   1.238 +    ##zip $ZIPFILE $pics &> /dev/null
   1.239 +
   1.240 +
   1.241 +## Make each index page
   1.242 +for n in range(NUM_PAGES):
   1.243 +    pagenum = n + 1
   1.244 +
   1.245 +    ## Choose and open page file
   1.246 +    pagefile = getPageFilename(pagenum)
   1.247 +    logging.info("Writing to index file %s" % pagefile)
   1.248 +    f = open(pagefile, "w")
   1.249      
   1.250 -    # Initialise empty index file
   1.251 -    if [[ $VERBOSE == "true" ]]; then echo "Writing to index file $pagefile"; fi
   1.252 -    > $pagefile
   1.253 +    ## Write the HTML header
   1.254 +    if opts.WRITEHEADER:
   1.255 +        ## TODO: Allow header and footer to come from user-specified file
   1.256 +        logging.debug("Writing default header")
   1.257 +        f.write('<html>\n')
   1.258 +        f.write('<head>\n')
   1.259 +        f.write('<title>%s</title>\n' % opts.TITLE)
   1.260 +        f.write('<style>\n')
   1.261 +        f.write('  img { border:0; margin:5px; }\n')
   1.262 +        f.write('  body { padding:1em; background:white; font-family:sans-serif; }\n')
   1.263 +        f.write('  h1 { font-family:sans-serif; }\n')
   1.264 +        f.write('</style>\n')
   1.265 +        ## TODO: thickbox / Lightbox2 / ...
   1.266 +        if opts.USE_JS:
   1.267 +            f.write('<script type="text/javascript" src="/lightbox/js/prototype.js"></script>\n')
   1.268 +            f.write('<script type="text/javascript" src="/lightbox/js/scriptaculous.js?load=effects"></script>\n')
   1.269 +            f.write('<script type="text/javascript" src="/lightbox/js/lightbox.js"></script>\n')
   1.270 +            f.write('<link rel="stylesheet" href="/lightbox/css/lightbox.css" type="text/css" media="screen" />\n')
   1.271 +        f.write('</head>\n')
   1.272 +        f.write('<body>\n')
   1.273      
   1.274 -    # Write the HTML header
   1.275 -    if [[ $writeHeader == "true" ]]; then
   1.276 -        if [[ -z $headerFile ]]; then
   1.277 -            if [[ $VERBOSE == "true" ]]; then echo "Writing default header"; fi
   1.278 -            echo "<html>" >> $pagefile
   1.279 -            echo "<head>" >> $pagefile
   1.280 -            echo "  <title>${TITLE}</title>" >> $pagefile
   1.281 -            echo "  <style>" >> $pagefile
   1.282 -            echo "    img { border:0; margin:5px; }" >> $pagefile
   1.283 -            echo "    body { padding:1em; background:white; font-family:sans-serif; }" >> $pagefile
   1.284 -            echo "    h1 { font-family:sans-serif; }" >> $pagefile
   1.285 -            echo "  </style>" >> $pagefile
   1.286 -            ## Lightbox2
   1.287 -            if [[ $lightbox == "true" ]]; then
   1.288 -                echo '  <script type="text/javascript" src="/lightbox/js/prototype.js"></script>' >> $pagefile
   1.289 -                echo '  <script type="text/javascript" src="/lightbox/js/scriptaculous.js?load=effects"></script>' >> $pagefile
   1.290 -                echo '  <script type="text/javascript" src="/lightbox/js/lightbox.js"></script>' >> $pagefile
   1.291 -                echo '  <link rel="stylesheet" href="/lightbox/css/lightbox.css" type="text/css" media="screen" />' >> $pagefile
   1.292 -            fi
   1.293 -            echo "</head>" >> $pagefile
   1.294 -            echo "<body>" >> $pagefile
   1.295 -        else
   1.296 -            if [[ $VERBOSE == "true" ]]; then echo "Writing header from $headerFile"; fi
   1.297 -            cat $headerFile >> $pagefile
   1.298 -        fi
   1.299 -    fi
   1.300 +    ## Write the title
   1.301 +    pagetitle=opts.TITLE
   1.302 +    if pagenum > 0:
   1.303 +        pagetitle += " (page %s)" % pagenum
   1.304 +    ## TODO: Allow overriding of title tag
   1.305 +    titletagstr = "<%s>%s</%s>" % ("h1", pagetitle, "h1")
   1.306 +    logging.debug("Writing title as %s" % titletagstr)
   1.307 +    f.write(titletagstr + "\n")
   1.308 +        
   1.309 +    ## Write the linked page list
   1.310 +    f.write(mkPageLinkStr(pagenum) + "\n")
   1.311      
   1.312 -    # Write the title
   1.313 -    pagetitle="$TITLE"
   1.314 -    if [[ $page -ne 1 ]]; then pagetitle="$pagetitle (page $page)"; fi
   1.315 -    if [[ $VERBOSE == "true" ]]; then echo "Writing title as <${TITLETAG}>${pagetitle}</${TITLETAG}>"; fi
   1.316 -    echo "<${TITLETAG}>${pagetitle}</${TITLETAG}>" >> $pagefile
   1.317 -        
   1.318 -    # Write the linked page list
   1.319 -    let prevpage=$page-1
   1.320 -    let nextpage=$page+1
   1.321 -    echo "<div class=\"pagelinks\">Pages: " >> $pagefile
   1.322 -    if [[ $prevpage -gt 0 ]]; then 
   1.323 -        echo "[<a href=\"index${prevpage}.html\">prev</a>] " >> $pagefile
   1.324 -    else
   1.325 -        echo "[prev] " >> $pagefile
   1.326 -    fi
   1.327 -    for pagelink in `seq 1 $npages`; do
   1.328 -        if [[ $pagelink -ne $page ]]; then
   1.329 -            pagelinkfile=$INDEXFILE
   1.330 -            if [[ $page -ne 1 ]]; then pagefile=index${page}.html; fi
   1.331 -            echo "[<a href=\"$pagelinkfile\">$pagelink</a>] " >> $pagefile
   1.332 -        else
   1.333 -            echo "[$pagelink]" >> $pagefile
   1.334 -        fi
   1.335 -    done
   1.336 -    if [[ $nextpage -lt $npages ]]; then 
   1.337 -        echo "[<a href=\"index${nextpage}.html\">next</a>] " >> $pagefile
   1.338 -    else
   1.339 -        echo "[next] " >> $pagefile
   1.340 -    fi
   1.341 -    echo "</div>" >> $pagefile
   1.342 -    
   1.343 -    # Make thumbnails if needed and write the image HTML
   1.344 -    echo "<p>" >> $pagefile
   1.345 -    let npic=0
   1.346 -    for pic in $pics; do
   1.347 -        let npic++
   1.348 -        thumbpic="thumbs/${pic}.jpg"
   1.349 -        
   1.350 -        let lowbound=($page-1)*$CHUNK_SIZE
   1.351 -        let highbound=$page*$CHUNK_SIZE
   1.352 -        if [[ $lightbox == "true" ]]; then
   1.353 -            echo -n "<a href=\"$pic\" rel=\"lightbox[set]\">" >> $pagefile
   1.354 -            if [[ $npic -gt $lowbound && $npic -lt $highbound ]]; then 
   1.355 -                echo -n "<img alt=\"$thumbpic\" src=\"$thumbpic\" style=\"border:0;\" width=\"`getWidth $thumbpic`\" height=\"`getHeight $thumbpic`\"/>" >> $pagefile
   1.356 -            fi
   1.357 -            echo "</a>" >> $pagefile
   1.358 -        else
   1.359 -            if [[ $npic -gt $lowbound && $npic -lt $highbound ]]; then 
   1.360 -                echo -n "<a href=\"$pic\">" >> $pagefile
   1.361 -                echo -n "<img alt=\"$thumbpic\" src=\"$thumbpic\" style=\"border:0;\" width=\"`getWidth $thumbpic`\" height=\"`getHeight $thumbpic`\"/>" >> $pagefile
   1.362 -                echo "</a>" >> $pagefile
   1.363 -            fi
   1.364 -        fi
   1.365 -    done
   1.366 -    echo "</p>" >> $pagefile
   1.367 +    ## Make thumbnails if needed and write the image HTML
   1.368 +    ## Display in table
   1.369 +    f.write('<p>\n')
   1.370 +    pics_start = n * NUM_PER_PAGE
   1.371 +    pics_end = (n+1) * NUM_PER_PAGE - 1
   1.372 +    if pics_end >= len(pics):
   1.373 +        pics_end = len(pics) - 1
   1.374 +    for npic, pic in enumerate(pics[pics_start, pics_end]):
   1.375 +        if not npic in (0, NUM_PER_PAGE-1) and npic % opts.NUMX == 0:
   1.376 +            f/write("<br/>\n")
   1.377 +        ## TODO: file extensions
   1.378 +        thumbpic = "thumbs/%s.jpg" % pic
   1.379 +        thumb_width = getWidth(thumbpic)
   1.380 +        thumb_height = getHeight(thumbpic)
   1.381 +        f.write('<a href="%s">' % pic)
   1.382 +        f.write('<img alt="$thumbpic" src="$thumbpic" ' % (thumbpic, thumbpic))
   1.383 +        f.write('style="border:0;" width="%d" height="%d"/>' % (thumb_width, thumb_height))
   1.384 +        f.write('</a>\n')
   1.385 +    f.write('</p>\n')
   1.386  
   1.387 -    # Write the linked page list
   1.388 -    let prevpage=$page-1
   1.389 -    let nextpage=$page+1
   1.390 -    echo "<div class=\"pagelinks\">Pages: " >> $pagefile
   1.391 -    if [[ $prevpage -gt 0 ]]; then 
   1.392 -        echo "[<a href=\"index${prevpage}.html\">prev</a>] " >> $pagefile
   1.393 -    else
   1.394 -        echo "[prev] " >> $pagefile
   1.395 -    fi
   1.396 -    for pagelink in `seq 1 $npages`; do
   1.397 -        if [[ $pagelink -ne $page ]]; then
   1.398 -            pagelinkfile=$INDEXFILE
   1.399 -            if [[ $page -ne 1 ]]; then pagefile=index${page}.html; fi
   1.400 -            echo "[<a href=\"$pagelinkfile\">$pagelink</a>] " >> $pagefile
   1.401 -        else
   1.402 -            echo "[$pagelink]" >> $pagefile
   1.403 -        fi
   1.404 -    done
   1.405 -    if [[ $nextpage -lt $npages ]]; then 
   1.406 -        echo "[<a href=\"index${nextpage}.html\">next</a>] " >> $pagefile
   1.407 -    else
   1.408 -        echo "[next] " >> $pagefile
   1.409 -    fi
   1.410 -    echo "</div>" >> $pagefile
   1.411 -    
   1.412 -    # Link to zip file
   1.413 -    if [[ $writeZipfile == "true" ]]; then
   1.414 -        echo "<p>All photos zipped up: <a href=\"$ZIPFILE\">$ZIPFILE</a></p>" >> $pagefile
   1.415 -    fi
   1.416 -    
   1.417 -    # Write the HTML footer
   1.418 -    if [[ $writeFooter == "true" ]]; then
   1.419 -        if [[ -z $footerFile ]]; then
   1.420 -            if [[ $VERBOSE == "true" ]]; then echo "Writing default footer"; fi
   1.421 -            echo "</body></html>" >> $pagefile
   1.422 -        else
   1.423 -            if [[ $VERBOSE == "true" ]]; then echo "Writing footer from $footerFile"; fi
   1.424 -            cat $footerFile >> $pagefile
   1.425 -        fi
   1.426 -    fi
   1.427 -done
   1.428 +    ## Write the linked page list
   1.429 +    f.write(mkPageLinkStr(pagenum) + "\n")
   1.430  
   1.431 -if [[ $VERBOSE == "true" ]]; then 
   1.432 -    echo "All done!"
   1.433 -fi
   1.434 +    ## Link to the zip file
   1.435 +    if opts.WRITEZIPFILE:
   1.436 +        f.write('<p>All photos zipped up: <a href="%s">%s</a></p>' % (ZIPFILE, ZIPFILE))
   1.437 +
   1.438 +    ## Write the HTML footer
   1.439 +    if opts.WRITEFOOTER:
   1.440 +        ## TODO: Allow user-specified footer file
   1.441 +        logging.debug("Writing default footer")
   1.442 +        f.write("</body>\n")
   1.443 +        f.write("</html>")
   1.444 +
   1.445 +    ## Close page file
   1.446 +    f.close()
   1.447 +
   1.448 +## It's over. Nothing to see here.
   1.449 +logging.debug("All done!")

mercurial