# HG changeset patch # User dukebody # Date 1255975275 -7200 # Node ID 86073756fe77b8191a6b460f062ca4c55279d16c # Parent 2aebffe8609dd1dac110b26e544d0b29f7d11294 Register script to insert ids and generate the associated elements in the database. diff -r 2aebffe8609d -r 86073756fe77 web/hgbook/converter.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/hgbook/converter.py Mon Oct 19 20:01:15 2009 +0200 @@ -0,0 +1,45 @@ +from lxml import etree +from lxml import html +from lxml.cssselect import CSSSelector +import md5 +import sys + + +args = sys.argv[1:] + +# django stuff +from django.core.management import setup_environ +import settings # Assumed to be in the same directory. +setup_environ(settings) # ugly django collateral effects :( +from comments.models import Element + +doc_id = 'MMSC' +sel = CSSSelector('p, pre, h1, table.equation') +body = CSSSelector('body') + +filename = args[0] +tree = etree.parse(filename, html.HTMLParser()) +root = tree.getroot() + + +body(root)[0].set('id', doc_id) + +for element in sel(root): + hsh_source = element.text or element.get('alt') or etree.tostring(element) + + if hsh_source: + hsh_source_encoded = hsh_source.encode('utf8') + hsh = md5.new(hsh_source_encoded).hexdigest() + element.set('id', '%s-%s' % (doc_id, hsh)) + + # create the commentable element in the DB + e = Element() + e.id = '%s-%s' % (doc_id, hsh) + e.chapter = doc_id + e.title = hsh + e.save() + + + +print etree.tostring(root) # pipe to a file if you wish + diff -r 2aebffe8609d -r 86073756fe77 web/hgbook/urls.py --- a/web/hgbook/urls.py Mon Oct 19 20:00:01 2009 +0200 +++ b/web/hgbook/urls.py Mon Oct 19 20:01:15 2009 +0200 @@ -1,4 +1,4 @@ -import os +import os, sys from django.conf.urls.defaults import * import hgbook.comments.feeds as feeds from django.contrib import admin @@ -16,9 +16,12 @@ {'feed_dict': feeds}), # Only uncomment this for local testing without Apache. - # (r'^html/(?P.*)$', 'django.views.static.serve', - # {'document_root': os.path.realpath(os.path.dirname( - # sys.modules[__name__].__file__) + '/../../en/html')}), + (r'^html/(?P.*)$', 'django.views.static.serve', + {'document_root': os.path.realpath(os.path.dirname( + sys.modules[__name__].__file__) + '/../html')}), + (r'^support/(?P.*)$', 'django.views.static.serve', + {'document_root': os.path.realpath(os.path.dirname( + sys.modules[__name__].__file__) + '/../javascript')}), # Uncomment this for admin: (r'^admin/(.*)', admin.site.root),