Wednesday, December 10, 2008


This one's easy.

I needed something to put together really simple galleries of JPG files (battle reports, actually, but nevermind) so put together this glorious little bit of AWK code. It just made a list of files into a big HTML file, which basically consists of images and paragraphs underneath, which are empty and need to be filled out.

So then I though: "hey! why not fill it out on-the-fly?" So I figured this could be done in two ways, basically - with zenity or the bash read command. So why not implement them both? One for GUI-like action, and the other for a straightforward command-line thing.

Zenity, was easy (line 57), just as soon as I figured out how to make system calls from AWK... so cool! With a little bit of checking whether ok or cancel was pressed, it all came together, and you can even stop the process, if you get bored, or something.

Bash read didn't work for some reason - couldn't get the read command to actually take input from the user... oh well, 2 out of 3 ain't bad.

I will take suggestions gladly, though.

You run the thing like this, if you want to run in vanilla mode, on the current directory, and you have the script saved as gallerizer.awk:

ls | ./gallerizer.awk > gallery.html

Then just go through the gallery.html file and put all the descriptions into the paragraphs...

And if you want to run it all with zenity-powered interactive mode, go:

ls | ./gallerizer.awk -v interactive=true > gallery.html

And there you go.

The code:
#!/usr/bin/awk -f
# Gallerizer
# Create an extremely simple gallery, with places to insert 
# descriptions, or even insert descriptions on the fly with 
# the bash read command or zenity.

# Parameters
10#   interactive: 'yes' or 'true' turns on the interctive
11#               mode with zenity input windows.
12# Requires
13#   zenity (for displaying dialogs in interactive mode)
14# Author
15#   Konrad Siek
17# Print HTML header
19    print "<html>"
20    print "\t<head>"
21    print "\t\t<title>"title"</title>"
22    print "\t\t<style>"
23    print "\t\t\tbody {"
24    print "\t\t\t\ttext-align: center;"
25    print "\t\t\t}"
26    print "\t\t\tp {"
27    print "\t\t\t\tmargin-bottom: 50px;"
28    print "\t\t\t}"
29    print "\t\t</style>"
30    print "\t</head>"
31    print "\t<body>"
32    if (title !~ /^[ \t\n]*$/) {
33        print "\t\t<h1>"title"</h1>"
34    }
36    is_interactive = interactive ~ /^(yes|true)$/
39# Print HTML footer
40END {
41    print "\t</body>"
42    print "</html>"
45# Ignore all temporary (files ending in '~')
46/~$/ {
47    next;
50# Include all PNG or JPG files as images
51/\.(png|PNG|jpg|JPG)$/    {
52    print "\t\t<img id=\""$0"\" src=\""$0"\" alt=\""$0"\" />"
53    print "\t\t<p>";    
54    # Prompt for description in interactive mode
55    if (is_interactive) {
56        printf("\t\t\t")
57        r = system("zenity --entry --title=\""$0"\"")
58        if (!= 0) {
59            printf("\n")
60        }
61    }
62    print "\t\t</p>";    
63    # Kill off the script on cancel
64    if(!= 0 && is_interactive) {
65        exit
66    }

The code is also available at GitHub as awk/gallerizer.awk.

No comments: