From 6c2d3df3ff956481cacfee43276fd23a5fb872aa Mon Sep 17 00:00:00 2001 From: Holger Schurig Date: Fri, 26 Jun 2009 10:20:10 +0200 Subject: [PATCH] new plugin: google_sitemap.py, which auto-generates a sitemap.xml --- in/plugins/google_sitemap.md | 38 +++++++++++++++++++ in/plugins/index.md | 3 +- in/webber.conf | 4 ++ plugins/google_sitemap.py | 71 ++++++++++++++++++++++++++++++++++++ 4 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 in/plugins/google_sitemap.md create mode 100644 plugins/google_sitemap.py diff --git a/in/plugins/google_sitemap.md b/in/plugins/google_sitemap.md new file mode 100644 index 0000000..913ec2e --- /dev/null +++ b/in/plugins/google_sitemap.md @@ -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. diff --git a/in/plugins/index.md b/in/plugins/index.md index 588e653..176cde1 100644 --- a/in/plugins/index.md +++ b/in/plugins/index.md @@ -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 diff --git a/in/webber.conf b/in/webber.conf index 225a2a3..2a7866b 100644 --- a/in/webber.conf +++ b/in/webber.conf @@ -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 index 0000000..0db3df5 --- /dev/null +++ b/plugins/google_sitemap.py @@ -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, '' + print >>f, '>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('\n') + f.write(' http://%s/%s\n' % (file.main_url, file.rel_path)) + f.write(' %s\n' % time.strftime( "%Y-%m-%d", time.localtime(file.mtime)) ) + f.write(' %s\n' % file.sitemap_changefreq) + f.write(' %s\n' % file.sitemap_priority) + f.write('\n') + + +@set_hook("finish") +def finish(params): + global f + if f: + print >>f, '' + f.close() -- 2.39.2