Fixpoint

2019-11-16

Blogging photos and chat logs: some handy scripts

Filed under: Software — Jacob Welsh @ 20:37

For last night's article I had to pay the debt of having deferred my ever so carefully planned dig into photo blogging for quite so long. I started from Popescu's recipe and made it into a script, including doing the necessary jiggling so I wouldn't have to recall the details every time.

One detail I wasn't sure about was the function of the "size-large" and "wp-image-###" classes on the <img> tag, so I left them out. If I wanted to style an individually numbered image, I can't imagine why I'd want to do it in a global CSS file rather than a local style attribute but perhaps there's some other reason. Ideally I should have asked in advance.

Without further ado, the blog-photos script:(i)

#!/bin/sh
echo -n "Output file prefix: "
read pfx
root="/wp-content/uploads/`date +%Y/%m`"
let cnt=0
for f in "$@" ; do
	let cnt++
	echo -n "<a href=\"$root/$pfx-$cnt.jpg\">"
	echo "<img src=\"$root/$pfx-$cnt-720px.jpg\" alt=\"$pfx-$cnt\" title=\"$pfx-$cnt\" width=\"720\" class=\"aligncenter\" /></a>"
	echo # paragraph break for spacing between photos
	convert "$f" -auto-orient -geometry 1920 "$pfx-$cnt.jpg"
	convert "$f" -auto-orient -geometry 720 "$pfx-$cnt-720px.jpg"
done

The chief dependency here is of course ImageMagick. Typical usage, from a directory full of pre-selected images:

blog-photos *.JPG > draft.html

You could of course use "tee" instead of redirection if you'd like to see the progress. Once you've uploaded the resulting images to the proper directory, paste the draft as a new article (I like "xclip" for this; of course a GUI text editor would work too) and load the preview; this allows you to identify image numbers by mouseover so as to find where to put your text.

Next up, a simpler one for importing chat logs in the presently prevailing style from a browser, with proper escaping of special HTML characters, logotron2wp:

#!/bin/sh
sed -re 's/&/\&amp;/g' -e 's/</\&lt;/g' -e 's/>/\&gt;/g' -e 's/^([^: ]*)/<strong>\1<\/strong>/'

Come to think of it, the first three substitutions there also work for any sort of code import that might contain those pesky &<> characters - such as this one right here.

The comment box awaits any questions, comments, flames etc.

  1. And now we'll get to see how badly long code lines will render... [^]

2 Comments »

  1. [...] stuff. Except that I wanted to try out Mircea Popescu's image processor for blog articles, and also jfw's version of the same. Except! It turns out the box I'm using for publishing doesn't have Image Magick, [...]

    Pingback by A Little Bit of TinyScheme, a Lot of Cozonac « The Whet — 2019-11-25 @ 14:30

  2. A somewhat tidied and improved version, now renamed to blog-imgs because not all images one might want to feed it will be photos:

    #!/bin/sh
    echo -n "Output file prefix: " >/dev/tty
    # ^ allows redirecting output to capture the generated html
    read pfx
    root="/wp-content/uploads/`date +%Y/%m`"
    let i=0
    for f in "$@" ; do
    	let i++
    	base="$pfx-$i"
    	path="$root/$base"
    	# paragraph break for spacing between photos
    	printf '\n<a href="%s.jpg"><img src="%s-640px.jpg" alt="%s" title="%s" class="aligncenter" /></a>\n' "$path" "$path" "$base" "$base"
    	convert "$f" -auto-orient -geometry 1920x1920 "$base.jpg"
    	convert "$f" -auto-orient -geometry 640x640 "$base-640px.jpg"
    	# ^ duplicating the dimension applies it nicely to whichever is greater
    done
    

    And on the second:

    Come to think of it, the first three substitutions there also work for any sort of code import that might contain those pesky &<> characters

    I improved on this concept with some cleverness picked up along the way (mostly from MP, if I recall): the greater-than doesn't technically need quoting, while the double-left-paren and proposed square bracket notation for footnote and code snippet preprocessing can be quoted in the same manner. raw2wp:

    #!/bin/sh
    sed 's/&/\&amp;/g; s/</\&lt;/g; s/((/(\&lpar;/g; s/\[\([0-9]*\)\[/[\1\&lsqb;/g'
    
    
    

    Comment by Jacob Welsh — 2020-10-01 @ 19:27

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by MP-WP. Copyright Jacob Welsh.