|
This is a description of one way (certainly not the best one) to automatically generate photo galleries using muse. In fact, it was the way i hacked muse in order to create the photo galleries for this very website. What we wantWe have a directory populated with jpeg (or any other image compressed format) files. The files are numerated in the form: 01.jpg 02.jpg ... 23.jpg ...and so on. Using convert (from imagemagick productions) we resized the images to some size (say 640x480 1) and created some nice thumbnails: # Resize to 640x480 (dont worry about the aspect ratio: convert cares about it) for f in `ls *.jpg`; do convert -geometry 640x480 $f $f ; done # Create thumbnails for f in `ls *.jpg`; do convert -thumbnail 25%x25% $f thumb-$f ; done Note that thumbnails are also numerated: thumb-01.jpg thumb-02.jpg ... thumb-23.jpg Now, we would want to be able to write something like this to create a muse page with a photo gallery: #title My Cool Gallery!! This party was quite funny! The police arrested me due to illegal nudity in public places! <gallery row="3" dir="/path/to/local/images" pdir="../path/to/published/images"/> And voila! Muse would publish an ordered table of thumbnails with rows of size row. The purpose of the dir attribute is to let Muse to find the images stored locally in order to generate the HTML markup. Then, the pdir attribute is used to reference both images and thumbnails in the server. Lets see how to achieve that. It is quite easy! Adding the <gallery> tag to MuseThe first thing to do is to register a new custom tag into the muse html backend: <gallery>. Muse stores custom tag specifications (or user-defined tags) into the muse-html-markup-tags alist. The information stored for each tag is:
(TAG-NAME EXPECT-CLOSING-TAG-P PARSE-ATTRIBUTES-P NESTABLE-P PUBLISH-FUNCTION)
Our <gallery> tag will expect a closing tag 2, has attributes, and definitely it should not be nestable. The publish function will be called jem-muse-publish-gallery-tag. Then, we can add the gallery tag specification to the alist:
(add-to-list 'muse-html-markup-tags
'("gallery" t t nil jem-muse-publish-gallery-tag))
Writing the tag publishing functionNow, Muse will call jem-muse-publish-gallery-tag in order to publish any occurrence of a <gallery> tag. Muse erases the markup of the tag and then passes three arguments to the publish functions:
In this case we wont need the contents of the tag: the <gallery> tag doesnt has contents. But we will need the region delimiters. This could be the publish function for <gallery>:
(defun jem-muse-publish-gallery-tag (beg end attrs)
(let ((dir (cdr (assoc "dir" attrs)))
(pdir (cdr (assoc "pdir" attrs)))
(row (string-to-number (cdr (assoc "row" attrs)))))
(insert (jem-muse-make-gallery dir pdir row))
(muse-publish-mark-read-only beg (point))))
Note how we extract the values of the attributes from the attrs alist. Then, we call jem-muse-make-gallery to get the actual html for the gallery, insert it, and finally we call to muse-publish-mark-read-only. The later needs an explanation. Muse calls tag publish functions twice: before the markup process and after the markup process, in order to manage any tag generated by the markup process. The consequence is that the html code we generated with jem-muse-make-gallery would become marked up! Clearly, that is not what we want. The trick here is to mark the html code we generated as read-only. Yes, this is a fucking trick... but it works. Generating the HTML for the GalleryThis is the easy part of the hack. Again, this is my own implementation. Your own one will be better, full fledged, using CSS, flutes and whistles. (defun jem-muse-make-gallery (dir pdir row) "Generate a photo gallery from a directory" (let ((files (directory-files dir nil "^[0-9][0-9]\\.jpg$")) (replace "") (i 0)) (setq replace (concat replace "<table>\n")) (while files (let ((filename (car files))) (setq replace (concat replace (if (equal (mod i row) 0) (concat (if (not (equal i 0)) "</tr>\n" "") "<tr>\n") "") "<td>\n" "<a href=\"" pdir "/" filename "\">" "<img src=\"" pdir "/" "thumb-" filename "\"/></a>\n" "</td>\n"))) (setq i (+ i 1)) (setq files (cdr files))) (setq replace (concat replace "</tr>\n" "</table>\n")) replace)) That is all. Enjoy! 1. A reasonable size. Internet + 100000 megapixels == troubles 2. It doesnt has any content beside attributes, but i am not a piggy hacker and i will close any XML-like structure |