2009-06-06 15:19:40 +05:30
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
|
|
|
# Copyright (C) 2009 Benny Malengier
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public 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
|
|
|
|
#
|
|
|
|
|
2009-12-21 19:13:50 +05:30
|
|
|
# $Id$
|
2009-06-06 15:19:40 +05:30
|
|
|
|
|
|
|
"""Html and Html format management for the different reports
|
|
|
|
"""
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Python modules
|
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
from xml.sax.saxutils import escape
|
2009-06-09 04:49:37 +05:30
|
|
|
import os.path
|
2009-06-06 15:19:40 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# GTK modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Gramps modules
|
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
from gen.plug.docbackend import DocBackend
|
|
|
|
from libhtml import Html
|
2009-06-09 04:49:37 +05:30
|
|
|
from Utils import xml_lang
|
2009-06-06 15:19:40 +05:30
|
|
|
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Document Backend class for html pages
|
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class HtmlBackend(DocBackend):
|
|
|
|
"""
|
|
|
|
Implementation for html pages
|
|
|
|
Contrary to other backends, we do not write to file but to a Html object
|
|
|
|
instead, writing out the file on close.
|
|
|
|
"""
|
|
|
|
|
|
|
|
STYLETAG_TO_PROPERTY = {
|
2009-06-11 18:31:27 +05:30
|
|
|
DocBackend.FONTCOLOR : 'color:%s;',
|
2009-06-06 15:19:40 +05:30
|
|
|
DocBackend.HIGHLIGHT : 'background-color:%s;',
|
|
|
|
DocBackend.FONTFACE : "font-family:'%s';",
|
|
|
|
DocBackend.FONTSIZE : 'font-size:%spx;',
|
|
|
|
}
|
|
|
|
|
|
|
|
# overwrite base class attributes, they become static var of CairoDoc
|
|
|
|
SUPPORTED_MARKUP = [
|
|
|
|
DocBackend.BOLD,
|
|
|
|
DocBackend.ITALIC,
|
|
|
|
DocBackend.UNDERLINE,
|
|
|
|
DocBackend.FONTFACE,
|
|
|
|
DocBackend.FONTSIZE,
|
|
|
|
DocBackend.FONTCOLOR,
|
|
|
|
DocBackend.HIGHLIGHT,
|
2010-05-06 21:24:33 +05:30
|
|
|
DocBackend.SUPERSCRIPT,
|
|
|
|
DocBackend.LINK,
|
|
|
|
]
|
2009-06-06 15:19:40 +05:30
|
|
|
|
|
|
|
STYLETAG_MARKUP = {
|
|
|
|
DocBackend.BOLD : ("<strong>", "</strong>"),
|
|
|
|
DocBackend.ITALIC : ("<em>", "</em>"),
|
|
|
|
DocBackend.UNDERLINE : ('<span style="text-decoration:underline;">',
|
|
|
|
"</span>"),
|
|
|
|
DocBackend.SUPERSCRIPT : ("<sup>", "</sup>"),
|
|
|
|
}
|
|
|
|
|
2010-05-06 21:24:33 +05:30
|
|
|
ESCAPE_FUNC = lambda self: escape
|
2009-06-06 15:19:40 +05:30
|
|
|
|
|
|
|
def __init__(self, filename=None):
|
|
|
|
"""
|
|
|
|
@param filename: path name of the file the backend works on
|
|
|
|
"""
|
|
|
|
DocBackend.__init__(self, filename)
|
|
|
|
self.html_page = None
|
|
|
|
self.html_header = None
|
|
|
|
self.html_body = None
|
2009-06-09 04:49:37 +05:30
|
|
|
self._subdir = None
|
2009-06-11 18:31:27 +05:30
|
|
|
self.title = None
|
2010-05-06 21:24:33 +05:30
|
|
|
self.build_link = None
|
2009-06-06 15:19:40 +05:30
|
|
|
|
2009-06-09 04:49:37 +05:30
|
|
|
def _create_xmltag(self, tagtype, value):
|
2009-06-06 15:19:40 +05:30
|
|
|
"""
|
|
|
|
overwrites the method in DocBackend
|
|
|
|
creates the pango xml tags needed for non bool style types
|
|
|
|
"""
|
2009-06-09 04:49:37 +05:30
|
|
|
if tagtype not in self.SUPPORTED_MARKUP:
|
2009-06-06 15:19:40 +05:30
|
|
|
return None
|
2009-06-09 04:49:37 +05:30
|
|
|
if tagtype == DocBackend.FONTSIZE:
|
2009-06-06 15:19:40 +05:30
|
|
|
#size is in points
|
|
|
|
value = str(value)
|
2009-06-11 18:31:27 +05:30
|
|
|
elif tagtype == DocBackend.FONTFACE:
|
|
|
|
#fonts can have strange symbols in them, ' needs to be escaped
|
|
|
|
value = value.replace("'", "\\'")
|
2009-06-09 04:49:37 +05:30
|
|
|
return ('<span style="%s">' % (self.STYLETAG_TO_PROPERTY[tagtype] %
|
2009-06-06 15:19:40 +05:30
|
|
|
(value)),
|
|
|
|
'</span>')
|
|
|
|
|
2009-06-09 04:49:37 +05:30
|
|
|
def _checkfilename(self):
|
|
|
|
"""
|
|
|
|
Check to make sure filename satisfies the standards for this filetype
|
|
|
|
"""
|
|
|
|
fparts = os.path.basename(self._filename).split('.')
|
|
|
|
if not len(fparts) >= 2 and not (fparts[-1] == 'html' or
|
|
|
|
fparts[-1] == 'htm' or fparts[-1] == 'php'):
|
|
|
|
self._filename = self._filename + ".htm"
|
2009-06-11 18:31:27 +05:30
|
|
|
fparts = os.path.basename(self._filename).split('.')
|
2009-06-09 04:49:37 +05:30
|
|
|
self._subdir = '.'.join(fparts[:-1])
|
|
|
|
|
|
|
|
def set_title(self, title):
|
|
|
|
"""
|
|
|
|
Set the title to use for the html page
|
|
|
|
"""
|
|
|
|
self.title = title
|
|
|
|
|
2009-06-06 15:19:40 +05:30
|
|
|
def open(self):
|
|
|
|
"""
|
|
|
|
overwrite method, htmlbackend creates a html object that is written on
|
|
|
|
close
|
|
|
|
"""
|
2009-06-09 04:49:37 +05:30
|
|
|
DocBackend.open(self)
|
2009-06-11 18:31:27 +05:30
|
|
|
if not os.path.isdir(self.datadirfull()):
|
|
|
|
os.mkdir(self.datadirfull())
|
2009-06-09 04:49:37 +05:30
|
|
|
self.html_page, self.html_header, self.html_body = Html.page(
|
|
|
|
lang=xml_lang(), title=self.title)
|
2009-06-06 15:19:40 +05:30
|
|
|
|
2009-06-09 04:49:37 +05:30
|
|
|
def __write(self, string):
|
2009-06-06 15:19:40 +05:30
|
|
|
""" a write to the file
|
|
|
|
"""
|
2009-06-11 18:31:27 +05:30
|
|
|
DocBackend.write(self, string + '\n')
|
2009-06-06 15:19:40 +05:30
|
|
|
|
2009-06-09 04:49:37 +05:30
|
|
|
def write(self, obj):
|
2009-06-06 15:19:40 +05:30
|
|
|
""" write to the html page. One can pass a html object, or a string
|
|
|
|
"""
|
|
|
|
self.html_body += obj
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
"""
|
|
|
|
write out the html to the page
|
|
|
|
"""
|
2009-06-11 18:31:27 +05:30
|
|
|
self.html_page.write(self.__write, indent=' ')
|
2009-06-06 15:19:40 +05:30
|
|
|
DocBackend.close(self)
|
2009-06-09 04:49:37 +05:30
|
|
|
|
|
|
|
def datadir(self):
|
|
|
|
"""
|
|
|
|
the directory where to save extra files
|
|
|
|
"""
|
|
|
|
return self._subdir
|
2009-06-11 18:31:27 +05:30
|
|
|
|
|
|
|
def datadirfull(self):
|
|
|
|
"""
|
|
|
|
full path of the datadir directory
|
|
|
|
"""
|
|
|
|
return os.path.join(os.path.dirname(self.getf()), self.datadir())
|
2010-05-06 21:24:33 +05:30
|
|
|
|
|
|
|
def format_link(self, value):
|
|
|
|
"""
|
|
|
|
Override of base method.
|
|
|
|
"""
|
|
|
|
if value.startswith("gramps://"):
|
|
|
|
if self.build_link:
|
|
|
|
obj_class, prop, handle = value[9:].split("/", 3)
|
|
|
|
if prop in ["handle", "gramps_id"]:
|
|
|
|
value = self.build_link(prop, handle, obj_class, up=True)
|
|
|
|
if not value:
|
|
|
|
return self.STYLETAG_MARKUP[DocBackend.UNDERLINE]
|
|
|
|
else:
|
|
|
|
return self.STYLETAG_MARKUP[DocBackend.UNDERLINE]
|
|
|
|
else:
|
|
|
|
return self.STYLETAG_MARKUP[DocBackend.UNDERLINE]
|
|
|
|
return ('<a href="%s">' % self.ESCAPE_FUNC()(value),
|
|
|
|
'</a>')
|
|
|
|
|