Added space after commas to improve readability. This makes pylint

happier too. Started to add more comments here and there.
        * src/plugins/NarrativeWeb.py


svn: r10243
This commit is contained in:
Kees Bakker 2008-03-09 20:12:56 +00:00
parent 9868432522
commit 7ea0f31341

View File

@ -6,7 +6,7 @@
# Copyright (C) 2007 Gary Burton <gary.burton@zen.co.uk> # Copyright (C) 2007 Gary Burton <gary.burton@zen.co.uk>
# #
# This program is free software; you can redistribute it and/or modify # This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Pubilc License as published by # it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or # the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version. # (at your option) any later version.
# #
@ -31,7 +31,6 @@ Narrative Web Page generator.
# python modules # python modules
# #
#------------------------------------------------------------------------ #------------------------------------------------------------------------
import cgi
import os import os
import md5 import md5
import time import time
@ -196,22 +195,38 @@ html_escape_table = {
# This command then defines the 'html_escape' option for escaping # This command then defines the 'html_escape' option for escaping
# special characters for presentation in HTML based on the above list. # special characters for presentation in HTML based on the above list.
def html_escape(text): def html_escape(text):
"""Produce entities within text.""" """Convert the text and replace some characters with a &# variant."""
L=[] return ''.join([html_escape_table.get(c, c) for c in text])
for c in text:
L.append(html_escape_table.get(c,c))
return "".join(L)
class BasePage: class BasePage:
"""
This the base class to write certain HTML pages.
"""
def __init__(self, title, options, archive, photo_list, gid): def __init__(self, title, options, archive, photo_list, gid):
"""
title - text for the <title> tag
options - dictionary with the options
archive - TODO describe
photo_list - TODO describe
gid - Gramps ID
"""
self.title_str = title self.title_str = title
self.gid = gid self.gid = gid
self.options = options
self.archive = archive
self.page_title = ""
self.warn_dir = True
self.cur_name = None # Internal use. The name of the output file.
# TODO. All of these attributes are not necessary, because we have
# als the options in self.options. Besides, we need to check which
# are still required.
self.inc_download = options['incdownload'] self.inc_download = options['incdownload']
self.html_dir = options['target'] self.html_dir = options['target']
self.copyright = options['cright'] self.copyright = options['cright']
self.options = options
self.archive = archive
self.ext = options['ext'] self.ext = options['ext']
self.encoding = options['encoding'] self.encoding = options['encoding']
self.css = options['css'] self.css = options['css']
@ -234,8 +249,6 @@ class BasePage:
self.graphgens = options['graphgens'] self.graphgens = options['graphgens']
self.use_home = self.options['homenote'] != "" or \ self.use_home = self.options['homenote'] != "" or \
self.options['homeimg'] != "" self.options['homeimg'] != ""
self.page_title = ""
self.warn_dir = True
def store_file(self, archive, html_dir, from_path, to_path): def store_file(self, archive, html_dir, from_path, to_path):
if archive: if archive:
@ -259,11 +272,10 @@ class BasePage:
self.warn_dir = False self.warn_dir = False
def copy_media(self, photo, store_ref=True): def copy_media(self, photo, store_ref=True):
handle = photo.get_handle() handle = photo.get_handle()
if store_ref: if store_ref:
lnk = (self.cur_name, self.page_title, self.gid) lnk = (self.cur_name, self.page_title, self.gid)
if self.photo_list.has_key(handle): if handle in self.photo_list:
if lnk not in self.photo_list[handle]: if lnk not in self.photo_list[handle]:
self.photo_list[handle].append(lnk) self.photo_list[handle].append(lnk)
else: else:
@ -321,6 +333,7 @@ class BasePage:
of.close() of.close()
else: else:
of.close() of.close()
self.cur_name = None
def lnkfmt(self, text): def lnkfmt(self, text):
return md5.new(text).hexdigest() return md5.new(text).hexdigest()
@ -503,6 +516,7 @@ class BasePage:
divid = "Contact" divid = "Contact"
else: else:
divid = '' divid = ''
if divid: if divid:
divid = ' id="%s"' % divid divid = ' id="%s"' % divid
of.write('<div%s class="content">\n' % divid) of.write('<div%s class="content">\n' % divid)
@ -563,15 +577,15 @@ class BasePage:
descr = " ".join(wrapper.wrap(photo.get_description())) descr = " ".join(wrapper.wrap(photo.get_description()))
self.doc_link(of, photo_handle, descr, up=True) self.doc_link(of, photo_handle, descr, up=True)
of.write('\t</div>\n\n') of.write('\t</div>\n\n')
lnk = (self.cur_name, self.page_title, self.gid) lnk = (self.cur_name, self.page_title, self.gid)
if self.photo_list.has_key(photo_handle): if photo_handle in self.photo_list:
if lnk not in self.photo_list[photo_handle]: if lnk not in self.photo_list[photo_handle]:
self.photo_list[photo_handle].append(lnk) self.photo_list[photo_handle].append(lnk)
else: else:
self.photo_list[photo_handle] = [lnk] self.photo_list[photo_handle] = [lnk]
def display_additional_images_as_gallery( self, of, db, photolist=None): def display_additional_images_as_gallery( self, of, db, photolist=None):
if not photolist or not self.use_gallery: if not photolist or not self.use_gallery:
return return
@ -595,8 +609,9 @@ class BasePage:
try: try:
descr = " ".join(wrapper.wrap(title)) descr = " ".join(wrapper.wrap(title))
self.doc_link(of, photo_handle, descr, up=True) self.doc_link(of, photo_handle, descr, up=True)
lnk = (self.cur_name, self.page_title, self.gid) lnk = (self.cur_name, self.page_title, self.gid)
if self.photo_list.has_key(photo_handle): if photo_handle in self.photo_list:
if lnk not in self.photo_list[photo_handle]: if lnk not in self.photo_list[photo_handle]:
self.photo_list[photo_handle].append(lnk) self.photo_list[photo_handle].append(lnk)
else: else:
@ -1285,7 +1300,7 @@ class MediaPage(BasePage):
height = int(height * scale) height = int(height * scale)
of.write('<a href="../../../%s">' % newpath) of.write('<a href="../../../%s">' % newpath)
of.write('<img width="%d" height="%d" src="../../../%s" alt="%s" />' % (width, height, newpath, html_escape(self.page_title))) of.write('<img width="%d" height="%d" src="../../../%s" alt="%s" />' % (width, height, newpath, html_escape(self.page_title)))
if scale <> 1.0: if scale != 1.0:
of.write('</a>'); of.write('</a>');
of.write('\n') of.write('\n')
@ -1876,6 +1891,9 @@ class ContactPage(BasePage):
# #
#------------------------------------------------------------------------ #------------------------------------------------------------------------
class IndividualPage(BasePage): class IndividualPage(BasePage):
"""
This class is used to write HTML for an individual.
"""
gender_map = { gender_map = {
gen.lib.Person.MALE : _('male'), gen.lib.Person.MALE : _('male'),
@ -1955,8 +1973,7 @@ class IndividualPage(BasePage):
of.write('\t\t\t<div class="boxbg" style="top: %dpx; left: %dpx;">\n' % (top, xoff+1)) of.write('\t\t\t<div class="boxbg" style="top: %dpx; left: %dpx;">\n' % (top, xoff+1))
of.write('\t\t\t\t<div class="box">') of.write('\t\t\t\t<div class="box">')
person_link = person.handle in self.ind_list if person.handle in self.ind_list:
if person_link:
person_name = _nd.display(person) person_name = _nd.display(person)
path = self.build_path(person.handle, "ppl", False) path = self.build_path(person.handle, "ppl", False)
fname = self.build_name(path, person.handle) fname = self.build_name(path, person.handle)
@ -2075,19 +2092,23 @@ class IndividualPage(BasePage):
of.write('\t\t\t') of.write('\t\t\t')
if father and mother: if father and mother:
of.write('<li>')
self.pedigree_person(of, father) self.pedigree_person(of, father)
of.write('\n') of.write('\n')
of.write('\t\t\t\t<ol>\n') of.write('\t\t\t\t<ol>\n')
of.write('\t\t\t\t\t') of.write('\t\t\t\t\t')
self.pedigree_person(of,mother,True) of.write('<li class="spouse">')
self.pedigree_person(of, mother)
of.write('\n') of.write('\n')
of.write('\t\t\t\t\t\t<ol>\n') of.write('\t\t\t\t\t\t<ol>\n')
elif father: elif father:
of.write('<li>')
self.pedigree_person(of, father) self.pedigree_person(of, father)
of.write('\n') of.write('\n')
of.write('\t\t\t\t<ol>\n') of.write('\t\t\t\t<ol>\n')
elif mother: elif mother:
self.pedigree_person(of,mother,True) of.write('<li class="spouse">')
self.pedigree_person(of, mother)
of.write('\n') of.write('\n')
of.write('\t\t\t\t<ol>\n') of.write('\t\t\t\t<ol>\n')
@ -2104,6 +2125,7 @@ class IndividualPage(BasePage):
else: else:
of.write('\t\t\t\t\t\t\t') of.write('\t\t\t\t\t\t\t')
child = self.db.get_person_from_handle(child_handle) child = self.db.get_person_from_handle(child_handle)
of.write('<li>')
self.pedigree_person(of, child) self.pedigree_person(of, child)
of.write('</li>\n') of.write('</li>\n')
of.write('\t\t\t\t\t\t</ol>\n') of.write('\t\t\t\t\t\t</ol>\n')
@ -2136,7 +2158,7 @@ class IndividualPage(BasePage):
of.write('\t\t\t</tr>\n') of.write('\t\t\t</tr>\n')
# Names [and their sources] # Names [and their sources]
for name in [self.person.get_primary_name(),]+self.person.get_alternate_names(): for name in [self.person.get_primary_name()] + self.person.get_alternate_names():
pname = _nd.display_name(name) pname = _nd.display_name(name)
pname += self.get_citation_links( name.get_source_references() ) pname += self.get_citation_links( name.get_source_references() )
type = str( name.get_type() ) type = str( name.get_type() )
@ -2196,7 +2218,7 @@ class IndividualPage(BasePage):
def display_addresses(self, of): def display_addresses(self, of):
alist = self.person.get_address_list() alist = self.person.get_address_list()
if len(alist) == 0: if not alist:
return return
of.write('\t<div id="addresses" class="subsection">\n') of.write('\t<div id="addresses" class="subsection">\n')
@ -2217,10 +2239,9 @@ class IndividualPage(BasePage):
of.write('\t</div>\n\n') of.write('\t</div>\n\n')
def display_child_link(self, of, child_handle): def display_child_link(self, of, child_handle):
use_link = child_handle in self.ind_list
child = self.db.get_person_from_handle(child_handle) child = self.db.get_person_from_handle(child_handle)
gid = child.get_gramps_id() gid = child.get_gramps_id()
if use_link: if child_handle in self.ind_list:
of.write("\t\t\t\t\t\t<li>") of.write("\t\t\t\t\t\t<li>")
child_name = _nd.display(child) child_name = _nd.display(child)
path = self.build_path(child_handle, "ppl", False) path = self.build_path(child_handle, "ppl", False)
@ -2232,12 +2253,11 @@ class IndividualPage(BasePage):
of.write(u"</li>\n") of.write(u"</li>\n")
def display_parent(self, of, handle, title, rel): def display_parent(self, of, handle, title, rel):
use_link = handle in self.ind_list
person = self.db.get_person_from_handle(handle) person = self.db.get_person_from_handle(handle)
of.write('\t\t\t\t<td class="ColumnAttribute">%s</td>\n' % title) of.write('\t\t\t\t<td class="ColumnAttribute">%s</td>\n' % title)
of.write('\t\t\t\t<td class="ColumnValue">') of.write('\t\t\t\t<td class="ColumnValue">')
val = person.gramps_id val = person.gramps_id
if use_link: if handle in self.ind_list:
path = self.build_path(handle, "ppl", False) path = self.build_path(handle, "ppl", False)
fname = self.build_name(path, handle) fname = self.build_name(path, handle)
self.person_link(of, fname, _nd.display(person), self.person_link(of, fname, _nd.display(person),
@ -2407,9 +2427,8 @@ class IndividualPage(BasePage):
of.write('\t\t\t\t<td class="ColumnAttribute">%s</td>\n' % relstr) of.write('\t\t\t\t<td class="ColumnAttribute">%s</td>\n' % relstr)
of.write('\t\t\t\t<td class="ColumnValue">') of.write('\t\t\t\t<td class="ColumnValue">')
if spouse_id: if spouse_id:
use_link = spouse_id in self.ind_list
gid = spouse.get_gramps_id() gid = spouse.get_gramps_id()
if use_link: if spouse_id in self.ind_list:
spouse_name = _nd.display(spouse) spouse_name = _nd.display(spouse)
path = self.build_path(spouse.handle, "ppl", False) path = self.build_path(spouse.handle, "ppl", False)
fname = self.build_name(path, spouse.handle) fname = self.build_name(path, spouse.handle)
@ -2456,11 +2475,7 @@ class IndividualPage(BasePage):
of.write('\t\t\t\t</td>\n') of.write('\t\t\t\t</td>\n')
of.write('\t\t\t</tr>\n') of.write('\t\t\t</tr>\n')
def pedigree_person(self, of, person, is_spouse=False): def pedigree_person(self, of, person):
if is_spouse:
of.write('<li class="spouse">')
else:
of.write('<li>')
person_link = person.handle in self.ind_list person_link = person.handle in self.ind_list
person_name = _nd.display(person) person_name = _nd.display(person)
if person_link: if person_link:
@ -2476,7 +2491,8 @@ class IndividualPage(BasePage):
spouse_handle = ReportUtils.find_spouse(self.person, rel_family) spouse_handle = ReportUtils.find_spouse(self.person, rel_family)
if spouse_handle: if spouse_handle:
spouse = self.db.get_person_from_handle(spouse_handle) spouse = self.db.get_person_from_handle(spouse_handle)
self.pedigree_person(of,spouse,True) of.write('<li class="spouse">')
self.pedigree_person(of, spouse)
childlist = rel_family.get_child_ref_list() childlist = rel_family.get_child_ref_list()
if childlist: if childlist:
of.write('\n') of.write('\n')
@ -2484,6 +2500,7 @@ class IndividualPage(BasePage):
for child_ref in childlist: for child_ref in childlist:
of.write('\t\t\t\t\t\t\t\t\t\t\t') of.write('\t\t\t\t\t\t\t\t\t\t\t')
child = self.db.get_person_from_handle(child_ref.ref) child = self.db.get_person_from_handle(child_ref.ref)
of.write('<li>')
self.pedigree_person(of, child) self.pedigree_person(of, child)
of.write('</li>\n') of.write('</li>\n')
of.write('\t\t\t\t\t\t\t\t\t\t</ol>\n\t\t\t\t\t\t\t\t\t</li>\n') of.write('\t\t\t\t\t\t\t\t\t\t</ol>\n\t\t\t\t\t\t\t\t\t</li>\n')
@ -2629,16 +2646,16 @@ class NavWebReport(Report):
self.target_path = self.opts['target'] self.target_path = self.opts['target']
self.copyright = self.opts['cright'] self.copyright = self.opts['cright']
self.ext = self.opts['ext'] # self.ext = self.opts['ext']
self.encoding = self.opts['encoding'] # self.encoding = self.opts['encoding']
self.css = self.opts['css'] self.css = self.opts['css']
self.noid = self.opts['nogid'] # self.noid = self.opts['nogid']
self.linkhome = self.opts['linkhome'] # self.linkhome = self.opts['linkhome']
self.showbirth = self.opts['showbirth'] # self.showbirth = self.opts['showbirth']
self.showdeath = self.opts['showdeath'] # self.showdeath = self.opts['showdeath']
self.showspouse = self.opts['showspouse'] # self.showspouse = self.opts['showspouse']
self.showparents = self.opts['showparents'] # self.showparents = self.opts['showparents']
self.showhalfsiblings = self.opts['showhalfsiblings'] # self.showhalfsiblings = self.opts['showhalfsiblings']
self.title = self.opts['title'] self.title = self.opts['title']
self.sort = Sort.Sort(self.database) self.sort = Sort.Sort(self.database)
self.inc_gallery = self.opts['gallery'] self.inc_gallery = self.opts['gallery']