This page shows how to use elisp to create a sitemap. If you don't know elisp, first take a look at Emacs Lisp Basics.
I want to write a elisp script to generate a sitemap. That is: create a file of sitemap format (XML) that lists all files in a directory.
A sitemap is a XML file that lists URLs of all files in a website for web crawlers to crawl. (See: Sitemaps ◇ www.sitemaps.org. )
A sitemap file looks like this:
<?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <loc>http://www.example.com/</loc> <lastmod>2005-01-01</lastmod> <changefreq>monthly</changefreq> <priority>0.8</priority> </url> … </urlset>
<url>container represent a file and other info.
<loc>is a URL of the file.
The purpose of sitemap file is for web crawlers to easily know all files that exist on your site, without it having to find out by the haphazard process of extracting links from pages it happens to know. This helps web crawling efficiency. Once a crawler knows all your files, it can then decide which page it actually wants to crawl for content. (modern search engines do not crawl all the pages it knows, because there are too many.)
My website xahlee.org has ≈5000 HTML files. I want to use elisp to generate a sitemap file. Once i have the script, i can set it to run automatically (⁖ by unix
cron) daily or weekly.
Some of the files under my website document dir are temp files. These files or dir's names start with “xx”. I don't want these included in the sitemap. Also, some files whose content contains a particular string shouldn't be in the sitemap neither. So, my elisp program will need to be able to check on the file's name, and the file's content too.
The general plan is very simple:
First, i define some parameters for the program.
;; full path to web's doc root. Must end in a slash. (setq webroot "/Users/xah/web/") ;; file name of sitemap file, relative to webroot, without “.xml” suffix. (setq sitemapFileName "sitemap") ;; gzip it or not. t for true, nil for false. (setq gzip-it-p t)
I plan to generate a fresh sitemap regularly since my website has new files each week. So, if a sitemap file already exists, i want to back it up and generate a new one. Here's the code:
; rename file to backup~ if already exists (let (f1 f2) (setq f1 (concat webroot sitemapFileName ".xml")) (setq f2 (concat f1 ".gz")) (when (file-exists-p f1) (rename-file f1 (concat f1 "~") t) ) (when (file-exists-p f2) (rename-file f2 (concat f2 "~") t) ) )
Note that the
rename-file function takes a 3rd argument. If true, it means just override existing file at the new name.
The next step, is to open a buffer sitemapBuf, insert the sitemap header tags, then, for each file in my web dir, insert its URL into the sitemapBuf, then add the ending tags, save, then done. Here's the code:
;; filePath is the full path to the sitemap file ;; sitemapBuf is the buffer of the sitemap file (let (filePath sitemapBuf) (setq filePath (concat webroot sitemapFileName ".xml")) ;; open file and save a handle to the buffer (setq sitemapBuf (find-file filePath)) ;; insert header tags (insert "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"> ") ;; for each file in my site, insert its url (require 'find-lisp) (mapc (lambda (x) (my-process-file x sitemapBuf)) (find-lisp-find-files webroot "\\.html$")) ;; insert ending tag (insert "</urlset>") ;; some post processing to add some optional tags (goto-char 1) (search-forward "http://xahlee.org/Periodic_dosage_dir/pd.html</loc>") (insert "<changefreq>daily</changefreq>") (save-buffer) ;; gzip it (when gzip-it-p (shell-command (concat "gzip " filePath)) ) )
In the above, first we generate the full path to the sitemap file to be created. The full path is saved as string in “filePath”. Then we open the file, effective creating a new buffer. The buffer instance is saved as the variable sitemapBuf. (note: “buffer” is a elisp data type, or a instance of the data type. Normally, it also mean the buffer's content or a file. (info "(elisp) Buffers") )
The interesting part in the above code is the traverse directory
section. The “find-lisp-find-files” line returns a list of full paths
of all HTML files. The
mapc maps a function to each element of the
list. The lambda line is the function that will be applied to each
So, for example, if a element is 〔~/web/emacs/emacs.html〕, then the lambda function will get that as argument, and execute
(my-process-file "~/web/emacs/emacs.html" sitemapBuf).
The “my-process-file” is a function that takes a file full path and a buffer. So that, it can open the file and see whether the file should be added to the sitemap file. If so, it will add to the sitemapBuf buffer.
“my-process-file” is defined this way:
(defun my-process-file (fPath destBuff) "Process the file at fullpath FPATH. Write result to buffer DESTBUFF." (when (not (string-match "/xx" fPath)) ; dir/file starting with xx are temp files (with-temp-buffer (insert-file-contents fPath nil nil nil t) (goto-char 1) (when (not (search-forward "<meta http-equiv=\"refresh\"" nil "noerror")) (with-current-buffer destBuff (insert "<url><loc>") (insert (concat "http://" domainName "/" (substring fPath (length webroot)))) (insert "</loc></url>\n") )) ) ) )
It takes 2 arguments. The
fPath is the path to a HTML file, and
destBuff is the buffer holding the sitemap file.
First it checks if the file path contains any “/xx”. On my website, file names starting with “xx” is meant to be temp files. So, if a file or dir starts with “/xx”, then skip it.
Otherwise, open the file and check if the file contains a HTML meta redirect tag. Google's webmaster guide says Google doesn't like URL in sitemap that points to a file that redirects with a HTML meta tag. So, if the HTML file is a redirect, then don't generate a sitemap URL for it.
Finally, the code calls
(with-current-buffer destBuff …)
to insert the proper URL tag into the sitemap buffer.
(with-current-buffer ‹a buffer› ‹code›) will temporarily make
‹a buffer› the current buffer and execute
‹code›. When the execution is done, the current buffer returns to whatever it was.
You can download the whole complete code here: sitemap_generator.el.gz
You can either run it in a buffer by calling
eval-buffer or in shell by
emacs --script sitemap_generator.el.
For the sitemap at ergoemacs.org generated by this script, see: sitemap.xml.gz.
Emacs is super!Disqus