Commits

Israel Saeta Pérez  committed 8607375

Register script to insert ids and generate the associated elements in the database.

  • Participants
  • Parent commits 2aebffe

Comments (0)

Files changed (2)

File web/hgbook/converter.py

+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
+

File web/hgbook/urls.py

-import os
+import os, sys
 from django.conf.urls.defaults import *
 import hgbook.comments.feeds as feeds
 from django.contrib import admin
      {'feed_dict': feeds}),          
 
     # Only uncomment this for local testing without Apache.
-    # (r'^html/(?P<path>.*)$', 'django.views.static.serve',
-    # {'document_root': os.path.realpath(os.path.dirname(
-    #    sys.modules[__name__].__file__) + '/../../en/html')}),
+     (r'^html/(?P<path>.*)$', 'django.views.static.serve',
+     {'document_root': os.path.realpath(os.path.dirname(
+        sys.modules[__name__].__file__) + '/../html')}),
+     (r'^support/(?P<path>.*)$', '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),