# -*- coding: iso-8859-1 -*-
# Copyright (C) 2000-2005 Bastian Kleineidam
#
# 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., 675 Mass Ave, Cambridge, MA 02139, USA.
"""
A HTML logger.
"""
import time
import cgi
import os
import linkcheck.logger
import linkcheck.strformat
import linkcheck.configuration
# ss=1 enables show source
validate_html = "http://validator.w3.org/check?ss=1&uri=%(uri)s"
# options are the default
validate_css = "http://jigsaw.w3.org/css-validator/validator?" \
"uri=%(uri)s&warning=1&profile=css2&usermedium=all"
HTML_HEADER = """
"+
linkcheck.configuration.Freeware+"
"+
(_("Start checking at %s") % \
linkcheck.strformat.strtime(self.starttime))+
os.linesep+"
")
self.check_date()
self.flush()
def log_url (self, url_data):
"""
Write url checking info as HTML.
"""
if self.fd is None:
return
self.write_table_start()
if self.has_part("url"):
self.write_url(url_data)
if url_data.name and self.has_part("name"):
self.write_name(url_data)
if url_data.parent_url and self.has_part("parenturl"):
self.write_parent(url_data)
if url_data.base_ref and self.has_part("base"):
self.write_base(url_data)
if url_data.url and self.has_part("realurl"):
self.write_real(url_data)
if url_data.dltime >= 0 and self.has_part("dltime"):
self.write_dltime(url_data)
if url_data.dlsize >= 0 and self.has_part("dlsize"):
self.write_dlsize(url_data)
if url_data.checktime and self.has_part("checktime"):
self.write_checktime(url_data)
if url_data.info and self.has_part("info"):
self.write_info(url_data)
if url_data.warnings and self.has_part("warning"):
self.write_warning(url_data)
if self.has_part("result"):
self.write_result(url_data)
self.write_table_end()
self.flush()
def write_table_start (self):
"""
Start html table.
"""
self.writeln(u"
")
self.writeln(u"")
self.writeln(u"")
self.writeln(u"")
self.writeln(u"")
def write_table_end (self):
"""
End html table.
"""
self.write(u" |
")
def write_url (self, url_data):
"""
Write url_data.base_url.
"""
self.writeln(u"")
self.writeln(u"| "+
self.part("url")+u" | ")
self.write(u""+
cgi.escape(repr(url_data.base_url or u"")[1:]))
if url_data.cached:
self.write(_(" (cached)"))
self.writeln(u" |
")
def write_name (self, url_data):
"""
Write url_data.name.
"""
self.writeln(u"| "+self.part("name")+u" | "+
cgi.escape(repr(url_data.name)[1:])+u" |
")
def write_parent (self, url_data):
"""
Write url_data.parent_url.
"""
self.write(u"| "+self.part("parenturl")+
u' | '+
cgi.escape(url_data.parent_url)+u"")
self.write(_(", line %d") % url_data.line)
self.write(_(", col %d") % url_data.column)
if not url_data.valid:
# on errors show HTML and CSS validation for parent url
vhtml = validate_html % {'uri': url_data.parent_url}
vcss = validate_css % {'uri': url_data.parent_url}
self.writeln()
self.writeln(u'(HTML)')
self.write(u'(CSS)')
self.writeln(u" |
")
def write_base (self, url_data):
"""
Write url_data.base_ref.
"""
self.writeln(u"| "+self.part("base")+u" | "+
cgi.escape(url_data.base_ref)+u" |
")
def write_real (self, url_data):
"""
Write url_data.url.
"""
self.writeln("| "+self.part("realurl")+u" | "+
u''+cgi.escape(url_data.url)+u" |
")
def write_dltime (self, url_data):
"""
Write url_data.dltime.
"""
self.writeln(u"| "+self.part("dltime")+u" | "+
(_("%.3f seconds") % url_data.dltime)+
u" |
")
def write_dlsize (self, url_data):
"""
Write url_data.dlsize.
"""
self.writeln(u"| "+self.part("dlsize")+u" | "+
linkcheck.strformat.strsize(url_data.dlsize)+
u" |
")
def write_checktime (self, url_data):
"""
Write url_data.checktime.
"""
self.writeln(u"| "+self.part("checktime")+u" | "+
(_("%.3f seconds") % url_data.checktime)+u" |
")
def write_info (self, url_data):
"""
Write url_data.info.
"""
text = os.linesep.join(url_data.info)
self.writeln(u"| "+self.part("info")+
u" | "+cgi.escape(text).replace(os.linesep, " ")+
u" |
")
def write_warning (self, url_data):
"""
Write url_data.warnings.
"""
sep = u"
"+os.linesep
text = sep.join([cgi.escape(x[1]) for x in url_data.warnings])
self.writeln(u"| "+self.part("warning")+
u" | "+
text+u" |
")
def write_result (self, url_data):
"""
Write url_data.result.
"""
if url_data.valid:
self.write(u"| "+
self.part("result")+u" | ")
self.write(_("Valid"))
else:
self.write(u" |
| "+
self.part("result")+u" | ")
self.write(_("Error"))
if url_data.result:
self.write(u": "+cgi.escape(url_data.result))
self.writeln(u" |
")
def end_output (self):
"""
Write end of checking info as HTML.
"""
if self.fd is None:
return
if self.has_part("outro"):
self.writeln()
self.write(_("That's it.")+" ")
if self.number >= 0:
self.write(_n("%d link checked.", "%d links checked.",
self.number) % self.number)
self.write(u" ")
self.write(_n("%d warning found.", "%d warnings found.",
self.warnings) % self.warnings)
self.write(u" ")
self.writeln(_n("%d error found.", "%d errors found.",
self.errors) % self.errors)
self.writeln(u"
")
self.stoptime = time.time()
duration = self.stoptime - self.starttime
self.writeln(_("Stopped checking at %s (%s)") % \
(linkcheck.strformat.strtime(self.stoptime),
linkcheck.strformat.strduration(duration)))
self.writeln(u"