#
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2000-2007 Donald N. Allingham
#
# 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
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# $Id$
"""
Narrative Web Page generator.
"""
#------------------------------------------------------------------------
#
# python modules
#
#------------------------------------------------------------------------
import os
import md5
import time
import locale
import shutil
import codecs
import tarfile
from gettext import gettext as _
from cStringIO import StringIO
from textwrap import TextWrapper
#------------------------------------------------------------------------
#
# Set up logging
#
#------------------------------------------------------------------------
import logging
log = logging.getLogger(".WebPage")
#------------------------------------------------------------------------
#
# GNOME/gtk
#
#------------------------------------------------------------------------
import gtk
#------------------------------------------------------------------------
#
# GRAMPS module
#
#------------------------------------------------------------------------
import RelLib
import const
from GrampsCfg import get_researcher
from Filters import GenericFilter, Rules
import Sort
from PluginUtils import register_report
from ReportBase import Report, ReportUtils, ReportOptions, \
CATEGORY_WEB, MODE_GUI, MODE_CLI
from ReportBase._ReportDialog import ReportDialog
from ReportBase._CommandLineReport import CommandLineReport
import Errors
import Utils
import ImgManip
import GrampsLocale
from QuestionDialog import ErrorDialog, WarningDialog
from BasicUtils import name_displayer as _nd
from DateHandler import displayer as _dd
#------------------------------------------------------------------------
#
# constants
#
#------------------------------------------------------------------------
_NARRATIVE = "narrative.css"
_NAME_COL = 3
WIDTH=160
HEIGHT=50
VGAP=10
HGAP=30
SHADOW=5
XOFFSET=5
_css_files = [
[_("Modern"), 'main1.css'],
[_("Business"), 'main2.css'],
[_("Certificate"), 'main3.css'],
[_("Antique"), 'main4.css'],
[_("Tranquil"), 'main5.css'],
[_("Sharp"), 'main6.css'],
[_("No style sheet"), ''],
]
_character_sets = [
[_('Unicode (recommended)'), 'utf-8'],
['ISO-8859-1', 'iso-8859-1' ],
['ISO-8859-2', 'iso-8859-2' ],
['ISO-8859-3', 'iso-8859-3' ],
['ISO-8859-4', 'iso-8859-4' ],
['ISO-8859-5', 'iso-8859-5' ],
['ISO-8859-6', 'iso-8859-6' ],
['ISO-8859-7', 'iso-8859-7' ],
['ISO-8859-8', 'iso-8859-8' ],
['ISO-8859-9', 'iso-8859-9' ],
['ISO-8859-10', 'iso-8859-10' ],
['ISO-8859-13', 'iso-8859-13' ],
['ISO-8859-14', 'iso-8859-14' ],
['ISO-8859-15', 'iso-8859-15' ],
['koi8_r', 'koi8_r', ],
]
_cc = [
'',
'
',
'
',
'
',
'
',
'
',
]
wrapper = TextWrapper()
wrapper.break_log_words = True
wrapper.width = 20
class BasePage:
def __init__(self, title, options, archive, photo_list, gid):
self.title_str = title
self.gid = gid
self.inc_download = options.handler.options_dict['NWEBdownload']
self.html_dir = options.handler.options_dict['NWEBod']
self.copyright = options.handler.options_dict['NWEBcopyright']
self.options = options
self.archive = archive
self.ext = options.handler.options_dict['NWEBext']
self.encoding = options.handler.options_dict['NWEBencoding']
self.css = options.handler.options_dict['NWEBcss']
self.noid = options.handler.options_dict['NWEBnoid']
self.use_intro = options.handler.options_dict['NWEBintronote'] != u""
self.use_contact = options.handler.options_dict['NWEBcontact'] != u""
self.use_gallery = options.handler.options_dict['NWEBgallery']
self.header = options.handler.options_dict['NWEBheader']
self.footer = options.handler.options_dict['NWEBfooter']
self.photo_list = photo_list
self.exclude_private = not options.handler.options_dict['NWEBincpriv']
self.usegraph = options.handler.options_dict['NWEBgraph']
self.graphgens = options.handler.options_dict['NWEBgraphgens']
self.use_home = self.options.handler.options_dict['NWEBhomenote'] != ""
self.page_title = ""
self.warn_dir = True
def store_file(self,archive,html_dir,from_path,to_path):
if archive:
archive.add(str(from_path),str(to_path))
else:
dest = os.path.join(html_dir,to_path)
dirname = os.path.dirname(dest)
if not os.path.isdir(dirname):
os.makedirs(dirname)
if from_path != dest:
shutil.copyfile(from_path,dest)
elif self.warn_dir:
WarningDialog(
_("Possible destination error") + "\n" +
_("You appear to have set your target directory "
"to a directory used for data storage. This "
"could create problems with file management. "
"It is recommended that you consider using "
"a different directory to store your generated "
"web pages."))
self.warn_dir = False
def copy_media(self,photo,store_ref=True):
handle = photo.get_handle()
if store_ref:
lnk = (self.cur_name,self.page_title,self.gid)
if self.photo_list.has_key(handle):
if lnk not in self.photo_list[handle]:
self.photo_list[handle].append(lnk)
else:
self.photo_list[handle] = [lnk]
ext = os.path.splitext(photo.get_path())[1]
real_path = "%s/%s" % (self.build_path(handle,'images'),handle+ext)
thumb_path = "%s/%s.png" % (self.build_path(handle,'thumb'),handle)
return (real_path,thumb_path)
def create_file(self,name):
self.cur_name = self.build_name("",name)
if self.archive:
self.string_io = StringIO()
of = codecs.EncodedFile(self.string_io,'utf-8',self.encoding,
'xmlcharrefreplace')
else:
page_name = os.path.join(self.html_dir,self.cur_name)
of = codecs.EncodedFile(open(page_name, "w"),'utf-8',
self.encoding,'xmlcharrefreplace')
return of
def link_path(self,name,path):
base = self.build_name("",name)
path = "%s/%s/%s/%s" % (path,name[0],name[1],base)
if os.sys.platform == "win32":
path = path.lower()
return path
def create_link_file(self,name,path):
self.cur_name = self.link_path(name,path)
if self.archive:
self.string_io = StringIO()
of = codecs.EncodedFile(self.string_io,'utf-8',
self.encoding,'xmlcharrefreplace')
else:
dirname = os.path.join(self.html_dir,path,name[0],name[1])
if os.sys.platform == "win32":
dirname = dirname.lower()
if not os.path.isdir(dirname):
os.makedirs(dirname)
page_name = self.build_name(dirname,name)
of = codecs.EncodedFile(open(page_name, "w"),'utf-8',
self.encoding,'xmlcharrefreplace')
return of
def close_file(self,of):
if self.archive:
tarinfo = tarfile.TarInfo(self.cur_name)
tarinfo.size = len(self.string_io.getvalue())
tarinfo.mtime = time.time()
if os.sys.platform != "win32":
tarinfo.uid = os.getuid()
tarinfo.gid = os.getgid()
self.string_io.seek(0)
self.archive.addfile(tarinfo,self.string_io)
of.close()
else:
of.close()
def lnkfmt(self,text):
return md5.new(text).hexdigest()
def display_footer(self,of,db):
of.write('\n')
of.write('