diff --git a/src/plugins/webreport/WebCal.py b/src/plugins/webreport/WebCal.py index ba025d892..1af4a1ade 100644 --- a/src/plugins/webreport/WebCal.py +++ b/src/plugins/webreport/WebCal.py @@ -1,10 +1,10 @@ -# +# # Gramps - a GTK+/GNOME based genealogy program # -# Copyright (C) 2007 Thom Sturgill +# Copyright (C) 2007 Thom Sturgill # Copyright (C) 2007-2009 Brian G. Matherly # Copyright (C) 2008-2009 Rob G. Healey -# Copyright (C) 2008 Jason Simanek +# Copyright (C) 2008 Jason Simanek # # 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 @@ -21,7 +21,7 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # -# $Id$ +# $Id: WebCal.py 12238 2009-03-07 09:51:27Z s_charette $ """ Web Calendar generator. @@ -68,12 +68,16 @@ from DateHandler import displayer as _dd from DateHandler import parser as _dp import libholiday +from html import Html #------------------------------------------------------------------------ # # constants # #------------------------------------------------------------------------ +# Web page filename extensions +_WEB_EXT = ['.html', '.htm', '.shtml', '.php', '.php3', '.cgi'] + # Calendar stylesheet names _CALENDARSCREEN = 'calendar-screen.css' _CALENDARPRINT = 'calendar-print.css' @@ -103,20 +107,21 @@ _CSS_FILES = [ _CHARACTER_SETS = [ # First is used as default selection. - [_('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' ], + # As seen on the internet, ISO-xxx are listed as capital letters + [_('Unicode UTF-8 (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', ], ] @@ -190,7 +195,6 @@ class WebCalReport(Report): self.filter = filter_option.get_filter() self.ext = mgobn('ext') self.copy = mgobn('cright') - self.encoding = mgobn('encoding') self.css = mgobn('css') self.country = mgobn('country') @@ -201,8 +205,6 @@ class WebCalReport(Report): self.start_year = mgobn('start_year') self.end_year = mgobn('end_year') - self.fullyear = mgobn('fullyear') - self.maiden_name = mgobn('maiden_name') self.alive = mgobn('alive') @@ -214,6 +216,10 @@ class WebCalReport(Report): for month in ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']] + self.encoding = mgobn('encoding') + self.fullyear = mgobn('fullyear') + self.makeoneday = mgobn('makeoneday') + # identify researcher name and e-mail address # as NarrativeWeb already does researcher = get_researcher() @@ -222,8 +228,9 @@ class WebCalReport(Report): self.author = self.author.replace(',,,', '') self.email = researcher.email - today = time.localtime() # set to today's date - self.today = datetime.date(today[0], today[1], today[2]) + # set to today's date + today = time.localtime() + self.today = gen.lib.Date(today[0], today[1], today[2]) self.warn_dir = True # Only give warning once. @@ -235,6 +242,12 @@ class WebCalReport(Report): calendar.setfirstweekday(dow_gramps2iso[self.start_dow]) +# --------------------------------------------------------------------------------------- +# +# Copy files to their destination +# +# --------------------------------------------------------------------------------------- + def copy_file(self, from_fname, to_fname, to_dir=''): """ Copy a file from a source to a (report) destination. @@ -265,6 +278,12 @@ class WebCalReport(Report): "web pages.")) self.warn_dir = False +# --------------------------------------------------------------------------------------- +# +# Adds Birthdays and Anniversaries +# +# --------------------------------------------------------------------------------------- + def add_day_item(self, text, year, month, day, event): if day == 0: # This may happen for certain "about" dates. @@ -285,7 +304,16 @@ class WebCalReport(Report): month_dict[day] = day_list self.calendar[month] = month_dict +# --------------------------------------------------------------------------------------- +# +# Retrieves Holidays from the Holiday file, src/plugins/lib/holidays.xml +# +# --------------------------------------------------------------------------------------- + def __get_holidays(self, year): + + self.progress.set_pass(_('Calculating Holidays for year %d' % year), 365) + """ Get the holidays for the specified country and year """ holiday_table = libholiday.HolidayTable() country = holiday_table.get_countries()[self.country] @@ -296,6 +324,15 @@ class WebCalReport(Report): for holiday_name in holiday_names: self.add_holiday_item(holiday_name, month, day) + # increment progress bar + self.progress.step() + +# --------------------------------------------------------------------------------------- +# +# Adds Holiday events +# +# --------------------------------------------------------------------------------------- + def add_holiday_item(self, text, month, day): """ add holiday to its dictionary @@ -315,6 +352,12 @@ class WebCalReport(Report): month_dict[day] = day_list self.holidays[month] = month_dict +# --------------------------------------------------------------------------------------- +# +# Copies all of the calendar files for all calendars +# +# --------------------------------------------------------------------------------------- + def copy_calendar_files(self): """ Copies all the necessary stylesheets and images for these calendars @@ -346,7 +389,189 @@ class WebCalReport(Report): from_path = os.path.join(const.IMAGE_DIR, fname) self.copy_file(from_path, fname, "images") - def display_month_navs(self, of, nr_up, year, currentsection, use_home): +# --------------------------------------------------------------------------------------- +# +# Creates file name passed to it +# +# --------------------------------------------------------------------------------------- + + def create_file(self, fname, subdir): + """ + Create a file in the html_dir tree. + If the directory does not exist, create it. + + fname -- filename to be created + subdir -- any subdirs to be added + """ + + fname = os.path.join(self.html_dir, subdir, fname) + + if not _has_webpage_extension(fname): + fname += self.ext + + destdir = os.path.dirname(fname) + + if not os.path.isdir(destdir): + os.makedirs(destdir) + + of = codecs.EncodedFile(open(fname, "w"), 'utf-8', self.encoding, 'xmlcharrefreplace') + return of + +# --------------------------------------------------------------------------------------- +# +# Closes all file name passed to it +# +# --------------------------------------------------------------------------------------- + + def close_file(self, of): + """ will close whatever filename is passed to it """ + of.close() + +# --------------------------------------------------------------------------------------- +# +# Beginning of Calendar Creation +# +# --------------------------------------------------------------------------------------- + + def write_header(self, nr_up, title, add_print=True): + """ + This creates the header for the Calendars + 'nr_up' - number of directory levels up, started from current page, to the + root of the directory tree (i.e. to self.html_dir). + title -- to be inserted into page header section + add_print -- whether to add printer stylesheet or not + * only webcalendar() and one_day() only! + """ + + # number of subdirectories up to reach root + subdirs = '../'*nr_up + + # Header contants + xmllang = Utils.xml_lang() + _XMLNS = 'http://www.w3.org/1999/xhtml' + _META1 = 'http-equiv="content-type" content="text/html;charset=%s"' % self.encoding + _META2 = 'http-equiv="Content-Style-Type" content="text/css"' + _META3 = 'name="generator" content="%s %s %s"' % (const.PROGRAM_NAME, const.VERSION, + const.URL_HOMEPAGE) + _META4 = 'name="author" content="%s"' % self.author + + # GRAMPS favicon + fname1 = '/'.join([subdirs] + ['images'] + ['favicon.ico']) + + # _CALENDARSCREEN stylesheet + fname2 = '/'.join([subdirs] + ['styles'] + [_CALENDARSCREEN]) + + header = Html(xmlns=_XMLNS, attr='xml:lang="%s" lang="%s"' % ((xmllang,)*2)) + header.addXML() + header.addDOCTYPE() + head = Html('head') + ( + + # header title + Html('title', title, indent=True, inline=True), + + # meta tags for head section + Html('meta', attr = _META1, indent = True, inline = True), + Html('meta', attr = _META2, indent = True, inline = True), + Html('meta', attr = _META3, indent = True, inline = True), + Html('meta', attr = _META4, indent = True, inline = True), + + # links for GRAMPS favicon and stylesheets + Html('link',rel='shortcut icon', href=fname1,type='image/x-icon',indent=True), + Html('link',rel='stylesheet', href=fname2,type='text/css',media='screen',indent=True) + ) + + # add printer stylesheet to webcalendar() and one_day() only + if add_print: + fname = '/'.join([subdirs] + ['styles'] + [_CALENDARPRINT]) + head += Html('link',rel='stylesheet', href=fname,type='text/css',media='print',indent=True) + + # add head section to page header + header += head + + # return header section to its caller + # either webcalendar(), year_glance(), or one_day() + return header + +# --------------------------------------------------------------------------------------- +# +# Creates year navigation, if multiyear +# +# --------------------------------------------------------------------------------------- + + def display_year_navs(self, nr_up, currentsection): + """ + This will create the year navigation menu bar + + nr_up = number of directories up to reach root directory + currentsection = proper styling of this navigation bar + """ + + # creating more than one year + if not self.multiyear: + return + + num_years = (self.end_year - self.start_year) + cal_year = self.start_year + + # stylesheets other than "Web_Visually.css" will hold 22 years in a row + # otherwise, 18 years in a row + years_in_row = 22 if self.css is not 'Web_Visually.css' else 18 + + # figure out number of rows + nrows = get_num_of_rows(num_years, years_in_row) + + for rows in range(0, nrows): + yearnav = Html('div', id="navigation", indent=True) + + ul = Html('ul', indent=True) + cols = 1 + while (cols <= years_in_row and cal_year <= self.end_year): + url = '' + + # begin subdir level + subdirs = ['..'] * nr_up + subdirs.append(str(cal_year)) + + # each year will link to current month. + # this will always need an extension added + full_month_name = get_full_month_name(self.today.get_month()) + + # Note. We use '/' here because it is a URL, not a OS dependent + # pathname. + url = '/'.join(subdirs + [full_month_name]) + self.ext + + # Figure out if we need
  • or just plain
  • + cs = str(cal_year) == currentsection and 'class="CurrentSection"' or '' + li = Html('li',attr=cs , indent=True, inline=True) + + # create hyperlink + ahref = Html('a', str(cal_year), href = url, inline=True) + + # add hyperlink to
  • cell + li += ahref + + # add cell to unordered list + ul += li + + # increase columns + cols += 1 + + # increase calendar year + cal_year += 1 + + # add ul to yearnav + yearnav += ul + + # return yearnav to its caller + return yearnav + +# --------------------------------------------------------------------------------------- +# +# Creates month navigation for all Calendars +# +# --------------------------------------------------------------------------------------- + + def display_month_navs(self, nr_up, year, currentsection, add_home): """ Will create and display the navigation menu bar @@ -361,16 +586,16 @@ class WebCalReport(Report): navs = [] # An optional link to a home page - navs.append((self.home_link, _('Home'), use_home)) + navs.append((self.home_link, _('Home'), add_home)) for month in range(1, 13): navs.append((month, month, True)) # Add a link for year_glance() if requested - navs.append(('fullyear', _('Year Glance'), self.fullyear)) + navs.append(('fullyearlinked', _('Year Glance'), self.fullyear)) - of.write('\n\n') + # create hyperlink + ahref = Html('a', nav_text, href = url) - def display_year_navs(self, of, nr_up, currentsection): - """ - This will create the year navigation menu bar + # add hyperlink to
  • cell + li += ahref - of = current calendar filename being created - nr_up = number of directories up to reach root directory - currentsection = proper styling of this navigation bar - """ + # add
  • to