This is something I run into pretty often, so I thought I'd share how I handle it, in case it's helpful.
Sometimes you have a list of image files, and you need to create the html formatting to make them into a gallery of some sort. Imagine each file has two versions, a full size and a thumbnail image. Pretty common scenario. The URL for the files will look like /images/gallery/full/[filename] and /images/gallery/thumb/[filename]
There's lots of ways to do this, here's what works well for me. First I grab a list of the files from the command line, ending up with something like this
ls -l
-rwxrwxrwx 1 user group 388044 May 25 11:16 IMG_20161205_120541.jpg
-rwxrwxrwx 1 user group 451788 May 25 11:16 IMG_20161215_114201.jpg
-rwxrwxrwx 1 user group 298125 May 25 11:16 IMG_20161216_121618.jpg
-rwxrwxrwx 1 user group 369981 May 25 11:16 IMG_20161216_125940.jpg
-rwxrwxrwx 1 user group 479464 May 25 11:16 IMG_20161216_131010.jpg
-rwxrwxrwx 1 user group 664528 May 25 11:16 IMG_20171206_142202.jpg
-rwxrwxrwx 1 user group 799900 May 25 11:16 IMG_20171213_122802.jpg
-rwxrwxrwx 1 user group 474816 May 25 11:16 IMG_20171213_141802.jpg
-rwxrwxrwx 1 user group 825686 May 25 11:16 IMG_20171213_145425.jpg
-rwxrwxrwx 1 user group 452369 May 25 11:16 IMG_20180121_112151.jpg
-rwxrwxrwx 1 user group 555198 May 25 11:16 IMG_20180121_130043.jpg
-rwxrwxrwx 1 user group 579954 May 25 11:16 IMG_20180121_142140.jpg
-rwxrwxrwx 1 user group 517832 May 25 11:16 IMG_20180606_151321.jpg
-rwxrwxrwx 1 user group 547884 May 25 11:16 IMG_20180609_155512.jpg
-rwxrwxrwx 1 user group 387731 May 25 11:16 IMG_20180616_132019.jpg
-rwxrwxrwx 1 user group 402792 May 25 11:16 IMG_20181013_180230.jpg
-rwxrwxrwx 1 user group 653990 May 25 11:16 IMG_20190930_144423.jpg
-rwxrwxrwx 1 user group 451788 May 25 11:16 IMG_20161215_114201.jpg
-rwxrwxrwx 1 user group 298125 May 25 11:16 IMG_20161216_121618.jpg
-rwxrwxrwx 1 user group 369981 May 25 11:16 IMG_20161216_125940.jpg
-rwxrwxrwx 1 user group 479464 May 25 11:16 IMG_20161216_131010.jpg
-rwxrwxrwx 1 user group 664528 May 25 11:16 IMG_20171206_142202.jpg
-rwxrwxrwx 1 user group 799900 May 25 11:16 IMG_20171213_122802.jpg
-rwxrwxrwx 1 user group 474816 May 25 11:16 IMG_20171213_141802.jpg
-rwxrwxrwx 1 user group 825686 May 25 11:16 IMG_20171213_145425.jpg
-rwxrwxrwx 1 user group 452369 May 25 11:16 IMG_20180121_112151.jpg
-rwxrwxrwx 1 user group 555198 May 25 11:16 IMG_20180121_130043.jpg
-rwxrwxrwx 1 user group 579954 May 25 11:16 IMG_20180121_142140.jpg
-rwxrwxrwx 1 user group 517832 May 25 11:16 IMG_20180606_151321.jpg
-rwxrwxrwx 1 user group 547884 May 25 11:16 IMG_20180609_155512.jpg
-rwxrwxrwx 1 user group 387731 May 25 11:16 IMG_20180616_132019.jpg
-rwxrwxrwx 1 user group 402792 May 25 11:16 IMG_20181013_180230.jpg
-rwxrwxrwx 1 user group 653990 May 25 11:16 IMG_20190930_144423.jpg
In vim, there's a handle block visual mode which allows you to select blocks of text, rather than whole rows. To use it, hit ctrl+v, then select all the columns up to the actual filenames:
And delete that stuff, so you end up with just a list of filenames, one per line. Yes, I know there's other ways to end up with this list, but I can never remember them - and this is faster for me :)
Now we're going to use a fun little bit of regex to drop in the formatting. Of course you can edit this to your own needs, but the idea is that we're going to select everything on each line of the image list, and reformat that line with all the necessary html around it. The pattern looks like this:
s/\(.*\)/<div class="gallery-image"><a href="_FULL_\/\1"><img src="_THUMB_\/\1" alt=""><\/a>\/div>/g
The first bit ("/\(.*\)/") choose every character on that line and stories the match - the filename. The filename is then inserted in the 2nd part of the regular expression (the replacement part) via the "\1". If we'd had two matching sections in the first part, then the 2nd match would be referenced by "\2".
I use two placeholders to keep things a little simpler - _FULL_ and _THUMB_ will be replaced next by the correct path to use for either the URL of the full size image or the URL of the thumbnail image. Those are simple replace regular expressions that look like this:
s/(_FULL_)/\/images\/gallery\/full\//g
Really only trick thing is not missing the "/" characters that you need to escape (red above).
Once this is all done you should have a nicely formatted chunk of html ready to go! I use this often enough that's it's generally in my buffer of recently used commands - but it'd be easy enough to set up a key mapping or something.
Comments