]> oss.titaniummirror.com Git - webber.git/commitdiff
new plugin: google_sitemap.py, which auto-generates a sitemap.xml
authorHolger Schurig <holgerschurig@gmx.de>
Fri, 26 Jun 2009 08:20:10 +0000 (10:20 +0200)
committerHolger Schurig <holgerschurig@gmx.de>
Fri, 26 Jun 2009 08:20:10 +0000 (10:20 +0200)
in/plugins/google_sitemap.md [new file with mode: 0644]
in/plugins/index.md
in/webber.conf
plugins/google_sitemap.py [new file with mode: 0644]

diff --git a/in/plugins/google_sitemap.md b/in/plugins/google_sitemap.md
new file mode 100644 (file)
index 0000000..913ec2e
--- /dev/null
@@ -0,0 +1,38 @@
+title: Google Sitemap generation
+linktitle: google_sitemap.py
+parent: Plugins
+keywords: Google, XML, Sitemap generator
+ctime: 2009-06-26
+
+This plugins write an XML "`sitemap.xml`" file into the out-directory. The
+format is documented at [[http://www.sitemaps.org|http://www.sitemaps.org]].
+
+
+= Configuration =
+
+The sitemap generator needs three [[Configuration]] items:
+
+       main_url: "www.holgerschurig.de"
+       sitemap_changefreq: "monthly"
+       sitemap_priority: 0.5
+
+
+== main_url ==
+
+This is the main URL of your website, without the "`http://`" stuff.
+
+
+== sitemap_changefreq ==
+
+You can define an estimated change frequency on each page by specifying
+this keyword at the header of each [[page|Page format]]. However, the
+"`sitemap_changefreq`" from the configuration file will be used as a
+default.
+
+
+== sitemap_priority ==
+
+You can define an relative page importance on each page by specifying
+this keyword at the header of each [[page|Page format]]. However, the
+"`sitemap_priority`" from the configuration file will be used as a
+default.
index 588e65392a48c906c0e442193c6a6a622573e44e..176cde1faea168f000ccaf0556f6d677a74d287c 100644 (file)
@@ -12,8 +12,9 @@ to plugins. Those plugins do:
 * Update internal state or modify HTML snippets
   ([[hierarchy.py|hierarchy]], [[link.py|link]])
 * Create HTML pages ([[template_mako.py|template_mako]])
+* Create additional output files ([[google_sitemap.py||Google Sitemap generation]])
 
-There's another plugin there ([[skeleton.py|skeleton]], which is
+There's another plugin there ([[skeleton.py|skeleton]]), which is
 is just a demo for plugin-programmers.
 
 Plugins simply reside in the "`plugins/`" directory. However, webber
index 225a2a3d471d9ae3077623c3dc2916f501408fd0..2a7866b4db22c0ff300399abaf87116bc86d3b1a 100644 (file)
@@ -1,5 +1,6 @@
 template: "history"
 subtitle: "Webber"
+main_url: "www.holgerschurig.de/projects/webber"
 date_format: "%Y-%m.%d"
 input_encoding: "iso-8859-1"
 output_encoding: "iso-8859-1"
@@ -12,6 +13,7 @@ plugins: [
        "read_copyonly",
        "read_markdown",
        "template_mako",
+       "google_sitemap",
        ]
 exclude_dir: [
        ]
@@ -27,3 +29,5 @@ copy_files: [
        "*.css",
        "robots.txt",
 ]
+sitemap_changefreq: "monthly"
+sitemap_priority: 0.5
diff --git a/plugins/google_sitemap.py b/plugins/google_sitemap.py
new file mode 100644 (file)
index 0000000..0db3df5
--- /dev/null
@@ -0,0 +1,71 @@
+# -*- coding: iso-8859-1 -*-
+from webber import *
+import os, sys, time
+
+f = None
+
+@set_hook("checkconfig")
+def checkconfig(params):
+       if not cfg.has_key("main_url"):
+               error('no "main_url:" configured:')
+               error('  this should be something like "www.yourpage.org"')
+               sys.exit(1)
+       if not cfg.has_key("sitemap_changefreq"):
+               warning('no default "sitemap_changefreq:" configured, using default "monthly"')
+               cfg.sitemap_changefreq = "monthly"
+       if not cfg.has_key("sitemap_priority"):
+               warning('no default "sitemap_priority:" configured, using default "0.5"')
+               cfg.sitemap_priority = "0.5"
+
+
+def write_initial(params):
+       global f
+       try:
+               os.makedirs(params.file.out_dir)
+       except:
+               pass
+       f = open(os.path.join(params.file.out_dir, "sitemap.xml"), "w")
+       print >>f, '<?xml version="1.0" encoding="UTF-8"?>'
+       print >>f, '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"'
+       print >>f, '        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'
+       print >>f, '        xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9'
+       print >>f, '                            http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">'
+
+
+@set_hook("scan")
+def sitemap_scan(params):
+       global f
+       file = params.file
+       if not file.has_key("linktitle"):
+               return
+       if f is None:
+               write_initial(params)
+
+       # Sanity checks
+       ok = True
+       try:
+               prio = float(file.sitemap_priority)
+       except:
+               ok = False
+       if not ok or (prio < 0.0 or prio > 1.0):
+               error("%s: sitemap_priority '%s' is invalid" % (file.rel_path, file.sitemap_priority))
+               return
+       if file.sitemap_changefreq not in ("always", "hourly", "daily", "weekly", "monthly", "yearly", "never"):
+               error("%s: sitemap_changefreq '%s' is invalid" % (file.rel_path, file.sitemap_changefreq))
+               return
+
+       #print file.sitemap_priority, file.sitemap_changefreq, file.rel_path
+       f.write('<url>\n')
+       f.write('   <loc>http://%s/%s</loc>\n' % (file.main_url, file.rel_path))
+       f.write('   <lastmod>%s</lastmod>\n' % time.strftime( "%Y-%m-%d", time.localtime(file.mtime)) )
+       f.write('   <changefreq>%s</changefreq>\n' % file.sitemap_changefreq)
+       f.write('   <priority>%s</changefreq>\n' % file.sitemap_priority)
+       f.write('</url>\n')
+
+
+@set_hook("finish")
+def finish(params):
+       global f
+       if f:
+               print >>f, '</urlset>'
+               f.close()