[Commits] python.it commit r149 -
www/branches/python/scripts/ht2html
commit a svn.python.it
commit a svn.python.it
Gio 18 Maggio 2006 20:23:33 CEST
Author: manlio
Date: Thu May 18 20:23:32 2006
New Revision: 149
Added:
www/branches/python/scripts/ht2html/PDONewGenerator.py
Log:
inserito nuovo generatore per ht2hml, di Stefano Giraldi
Added: www/branches/python/scripts/ht2html/PDONewGenerator.py
==============================================================================
--- (empty file)
+++ www/branches/python/scripts/ht2html/PDONewGenerator.py Thu May 18 20:23:32 2006
@@ -0,0 +1,554 @@
+"""Generates the www.python.it website style
+ Realizzata da Stefano Giraldi <e.tino a email.it>
+ sulla base di PDOGenerator.py per ht2html
+"""
+
+import os
+import posixpath
+#~ import whrandom
+
+from Skeleton import Skeleton
+from Sidebar import Sidebar, BLANKCELL
+from Banner import Banner
+from HTParser import HTParser
+from LinkFixer import LinkFixer
+#~ per SkeletonCSS
+from ht2html import __version__
+
+from cStringIO import StringIO
+import sys
+import time
+
+sitelinks = [
+ ('%(rootdir)s', 'Home'),
+ ('%(rootdir)sdownload/', 'Download'),
+ ('%(rootdir)sdoc/', 'Documentazione'),
+ ('%(rootdir)sfaq/', 'FAQ'),
+ ('%(rootdir)shelp/', 'Aiutaci!!!'),
+ ]
+
+
+class SkeletonCSS(Skeleton):
+
+ def get_banner(self):
+ """Returns HTML for the top banner, or None if no banner.
+ """
+ return None
+
+ def get_left_sidebar(self):
+ """Returns HTML for the left sidebar or None.
+ """
+ return None
+ # for backwards compatibility
+ get_sidebar = get_left_sidebar
+
+ def get_right_sidebar(self):
+ """Returns HTML for the right sidebar or None.
+ """
+ return None
+
+ def get_banner_width(self):
+ """HTML `width' of banner column as a percentage.
+
+ Should be a string that does not include the percent sign (e.g. "90").
+ This affects the column containing both the banner and body text (if
+ they exist).
+ """
+ return '90'
+
+ def get_corner(self):
+ """Returns HTML for the upper-left corner or None.
+
+ Note that if get_banner() and get_sidebar() both return None, then
+ get_corner() is ignored. Also if both get_banner() and get_sidebar()
+ return a string, but get_corner() returns None, the smallest blank
+ corner possible is emitted.
+ """
+ return None
+
+ def get_body(self):
+ """Returns HTML for the internal document body.
+
+ Note that using this method causes the get_sidebar() -- if there is
+ one -- to run the full height of the page. If you don't want this,
+ then make sure get_cont() returns a string.
+ """
+ return '<b>Intentionally left blank</b>'
+
+ def get_cont(self):
+ """Returns HTML for the continuation of the body.
+
+ When this method returns a string, and there is a get_sidebar(), the
+ continuation text appears below the get_sidebar() and get_body() at
+ the full width of the screen. If there is no get_sidebar(), then
+ having a get_cont() is pointless.
+ """
+ return None
+
+ def get_title(self):
+ """Return the title of the page. Required."""
+ return 'Intentionally left blank'
+
+ def get_meta(self):
+ """Return extra meta-data. Must be a string."""
+ import __main__
+ return '<meta name="generator" content="HT2HTML/%s">' \
+ % __version__
+
+ def get_headers(self):
+ """Return extra header information. Must be a string."""
+ return ''
+
+ def get_bgcolor(self):
+ """Return the background color"""
+ return '#ffffff'
+
+ def get_fgcolor(self):
+ """Return foreground color"""
+ return '#000000'
+
+ def get_linkcolor(self):
+ """Return link color"""
+ return '#0000bb'
+
+ def get_vlinkcolor(self):
+ """Return vlink color"""
+ return '#551a8b'
+
+ def get_alinkcolor(self):
+ """Return alink color"""
+ return '#ff0000'
+
+ def get_corner_bgcolor(self):
+ """Return the background color for the corner"""
+ return self.get_lightshade()
+
+ # Barry's prefs
+ def get_lightshade(self):
+ """Return lightest of 3 color scheme shade."""
+ return '#cdba96'
+
+ def get_mediumshade(self):
+ """Return middle of 3 color scheme shade."""
+ return '#cc9966'
+
+ def get_darkshade(self):
+ """Return darkest of 3 color scheme shade."""
+ return '#b78900'
+
+ def get_body_attributes(self):
+ """Return extra attributes for the body start tag."""
+ # These are not supported in HTML, but are needed for
+ # Netscape 4
+ return 'marginwidth="0" marginheight="0"'
+
+ def get_banner_attributes(self):
+ """Return extra attributes for the TABLE in the banner."""
+ return 'cellspacing="0" cellpadding="2"'
+
+ def get_charset(self):
+ """Return charset of pages"""
+ return 'us-ascii'
+
+ # Style sheets
+ def get_stylesheet(self):
+ """Return filename of CSS stylesheet."""
+ return ''
+
+
+ def get_stylesheet_pi(self):
+ s = self.get_stylesheet()
+ if s:
+ return '<?xml-stylesheet href="%s" type="%s"?>\n' \
+ % (s, self.get_stylesheet_type(s))
+ else:
+ return ''
+
+ def get_stylesheet_type(self, filename):
+ ext = os.path.splitext(filename)[1]
+ if ext == ".css":
+ return "text/css"
+ elif ext in (".xsl", ".xslt"):
+ return "text/xslt"
+ else:
+ raise ValueError("unknown stylesheet language")
+
+ def get_style(self):
+ """Return the style sheet for this document"""
+ s = self.body_style()
+ if s:
+ return 'body { %s }' % self.body_style()
+ else:
+ return ''
+
+ def body_style(self):
+ if self.get_stylesheet():
+ # If there's an external stylesheet, rely on that for the body.
+ return ''
+ else:
+ return 'margin: 0px;'
+
+ # Call this method
+ def makepage(self):
+ banner = self.get_banner()
+ sidebar = self.get_sidebar()
+ corner = self.get_corner()
+ body = self.get_body()
+ cont = self.get_cont()
+ html = StringIO()
+ stdout = sys.stdout
+ closed = 0
+ try:
+ sys.stdout = html
+ self.__do_head()
+ self.__start_body()
+ print '<!-- start of page -->'
+ print '<div id="page">'
+
+ if banner is not None:
+ print '<!-- start of banner -->'
+ print '<div id="banner">'
+ if corner is not None:
+ self.__do_corner(corner)
+ print banner
+ print '</div><!-- end of banner -->'
+ # if there is a body but no sidebar, then we'll just close the
+ # table right here and put the body (and any cont) in the full
+ # page. if there is a sidebar but no body, then we still create
+ # the new row and just populate the body cell with a non-breaking
+ # space. Watch out though because we don't want to close the
+ # table twice
+ if sidebar is None:
+#ste print '</table><!-- end of page table -->'
+ closed = 1
+ else:
+#ste print '<tr><!-- start of sidebar/body row -->'
+ print '<div id="sidebar"><!-- start of sidebar -->'
+ self.__do_sidebar(sidebar)
+ print '</div><!-- end sidebar -->'
+ if body is not None:
+ print '<div id="body"><!-- start of body -->'
+ if closed:
+ print body
+ else:
+ self.__do_body(body)
+ print '</div><!-- end of body -->'
+# if not closed:
+# ste print '</tr><!-- end of sidebar/body row -->'
+# ste print '</table><!-- end of page table -->'
+# print '</div><!-- end of page-->'
+ if cont is not None:
+ print '<div id="cont"><!-- start of cont-->'
+ self.__do_cont(cont)
+ print '</div><!-- start of cont-->'
+ self.__finish_all()
+ finally:
+ sys.stdout = stdout
+ return html.getvalue()
+
+ def __do_corner(self, corner):
+#ste print '<!-- start of corner cells -->'
+#ste print '<td width="150" valign="middle" bgcolor="%s" class="corner">' \
+#ste % self.get_corner_bgcolor()
+ # it is important not to have a newline between the corner text and
+ # the table close tag, otherwise layout is messed up
+ if corner is None:
+ print ' ',
+ else:
+ print corner,
+#ste print '</td>'
+#ste print '<td width="15" bgcolor="%s"> </td><!--spacer-->' % (
+#ste self.get_lightshade())
+#ste print '<!-- end of corner cells -->'
+
+ def __do_sidebar(self, sidebar):
+#ste print '<!-- start of sidebar cells -->'
+#ste print '<td width="150" valign="top" bgcolor="%s" class="sidebar">' % (
+#ste self.get_lightshade())
+ print sidebar
+#ste print '</td>'
+#ste print '<td width="15"> </td><!--spacer-->'
+#ste print '<!-- end of sidebar cell -->'
+
+ def __do_body(self, body):
+#ste print '<!-- start of body cell -->'
+#ste print '<td valign="top" width="%s%%" class="body"><br>' % (
+#ste self.get_banner_width())
+#ste print '<!-- start of side/content -->'
+#ste print '<div id="sidecontent">'
+
+ print body
+#ste print '</td><!-- end of body cell -->'
+#ste print '</div><!-- end of side/content -->'
+
+ def __do_cont(self, cont):
+#ste print '<div class="body">'
+#ste print '<div class="continuation">'
+#ste print '<!-- start of continued wide-body text -->'
+#ste print '<div id="body">'
+#ste print '<!-- start of continued wide-body text -->'
+ print cont
+#ste print '<!-- end of continued wide-body text -->'
+#ste print '</div>'
+#ste print '</div>'
+
+ def __do_head(self):
+ """Return the HTML <head> stuff."""
+ print '''\
+<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
+%(xmlstyle)s<html>
+<!-- THIS PAGE IS AUTOMATICALLY GENERATED. DO NOT EDIT. -->
+<!-- %(time)s -->
+<!-- USING HT2HTML %(version)s -->
+<!-- SEE http://ht2html.sf.net -->
+<!-- User-specified headers:
+Title: %(title)s
+%(headers)s
+-->
+
+<head>
+<title>%(title)s</title>
+<meta http-equiv="Content-Type" content="text/html; charset=%(charset)s">
+%(meta)s
+%(style)s
+</head>''' % {'title' : self.get_title(),
+ 'headers' : self.get_headers(),
+ 'meta' : self.get_meta(),
+ 'time' : time.ctime(time.time()),
+ 'version' : __version__,
+ 'charset' : self.get_charset(),
+ 'style' : self.__do_styles(),
+ 'xmlstyle': self.get_stylesheet_pi(),
+ }
+
+ def __do_styles(self):
+ # assemble all the style information we have to produce the
+ # appropriate LINK and STYLE elements
+ stylesheet = self.get_stylesheet()
+ localstyle = self.get_style()
+ s = ''
+ if stylesheet and stylesheet.strip():
+ stylesheet = stylesheet.strip()
+ type = self.get_stylesheet_type(stylesheet)
+ s = '<link rel="stylesheet" href="%s" type="%s">' \
+ % (stylesheet, type)
+ if localstyle and localstyle.strip():
+ localstyle = '<style type="text/css">\n%s\n</style>' \
+ % localstyle.strip()
+ if stylesheet:
+ s = s + "\n" + localstyle
+ else:
+ s = localstyle
+ return s
+
+ def __start_body(self):
+ #~ print '''\
+#~ <body bgcolor="%(bgcolor)s" text="%(fgcolor)s"
+ #~ %(extraattrs)s
+ #~ link="%(linkcolor)s" vlink="%(vlinkcolor)s"
+ #~ alink="%(alinkcolor)s">''' % {
+ #~ 'bgcolor' : self.get_bgcolor(),
+ #~ 'fgcolor' : self.get_fgcolor(),
+ #~ 'linkcolor' : self.get_linkcolor(),
+ #~ 'vlinkcolor': self.get_vlinkcolor(),
+ #~ 'alinkcolor': self.get_alinkcolor(),
+ #~ 'extraattrs': self.get_body_attributes(),
+ #~ }
+ print "<body>"
+
+ def __finish_all(self):
+#ste print '</body></html>'
+ print '</div></body></html>'
+
+
+
+
+class BannerCSS(Banner):
+ def __init__(self, links, cols=4):
+ """Initialize the Banner instance.
+
+ This class is intended to be a mixin-class with Skeleton.
+
+ links must be a list of 2-tuples of the form: (URL, text). If URL is
+ None, then the text is not hyperlinked. These are arranged in a table
+ in order, evenly in the specified number of columns.
+
+ """
+ self.__links = links
+ self.__cols = cols
+ rows, leftover = divmod(len(links), self.__cols)
+ if leftover:
+ rows = rows + 1
+ self.__rows = rows
+
+ def get_banner(self):
+ stdout = sys.stdout
+ html = StringIO()
+ try:
+ sys.stdout = html
+ self.__start_table()
+ self.__do_table()
+ self.__end_table()
+ finally:
+ sys.stdout = stdout
+ return html.getvalue()
+
+ def __start_table(self):
+ #~ Modificato
+ print '<ul>'
+
+ def __end_table(self):
+ #~ Modificato
+ print '</ul>'
+
+ def __do_table(self):
+ #~ Modificato
+ col = 0
+ for item in self.__links:
+ if len(item) == 3:
+ url, text, extra = item
+ else:
+ url, text = item
+ extra = ''
+ if not url:
+ s = text + extra
+ else:
+ s = '<a href="%s">%s</a>%s' % (url, text, extra)
+ print '<li>'
+ print s
+ print '</li>'
+
+class PDONewGenerator(SkeletonCSS, Sidebar, BannerCSS):
+ AUTHOR = 'webmaster a python.it'
+
+ def __init__(self, file, rootdir, relthis):
+ root, ext = os.path.splitext(file)
+ html = root + '.html'
+ p = self.__parser = HTParser(file, self.AUTHOR)
+ f = self.__linkfixer = LinkFixer(html, rootdir, relthis)
+ self.__body = None
+ self.__cont = None
+ # calculate the sidebar links, adding a few of our own
+ self.__d = {'rootdir': rootdir}
+ p.process_sidebar()
+ p.sidebar.append(BLANKCELL)
+ # it is important not to have newlines between the img tag and the end
+ # end center tags, otherwise layout gets messed up
+ p.sidebar.append(('%(rootdir)s', '''
+ <img alt="" border="0"
+ src="%(rootdir)s/pics/PythonPoweredSmall.gif">
+''' % self.__d))
+ self.__linkfixer.massage(p.sidebar, self.__d)
+ Sidebar.__init__(self, p.sidebar)
+ #
+ # fix up our site links, no relthis because the site links are
+ # relative to the root of our web pages
+ #
+ sitelink_fixer = LinkFixer(f.myurl(), rootdir)
+ sitelink_fixer.massage(sitelinks, self.__d, aboves=1)
+ Banner.__init__(self, sitelinks)
+ # calculate the random corner
+ # XXX Should really do a list of the pics directory...
+ i = 0
+ #~ s = self.__d['rootdir']+"python-logo.gif"
+ s = posixpath.join(self.__d['rootdir'], 'styles/images/python-logo.gif')
+ self.__d['banner'] = s
+ self.__whichbanner = i
+
+ def get_meta(self):
+ s1 = Skeleton.get_meta(self)
+ s2 = self.__parser.get('meta', '')
+ if s1 and s2:
+ return s1 + "\n" + s2
+ else:
+ return s1 or s2
+
+ def get_style(self):
+ s1 = Skeleton.get_style(self)
+ s2 = self.__parser.get('local-css')
+ if s1:
+ if s2:
+ return s1 + "\n" + s2
+ else:
+ return s1
+ else:
+ return s2
+
+ def get_stylesheet(self):
+ #~ return posixpath.join('/style.css')
+ return posixpath.join(self.__d['rootdir'], 'styles/style.css')
+
+ def get_title(self):
+ return self.__parser.get('title')
+
+ def get_sidebar(self):
+ if self.__parser.get('wide-page', 'no').lower() == 'yes':
+ return None
+ return Sidebar.get_sidebar(self)
+
+ def get_banner(self):
+ return Banner.get_banner(self)
+
+ def get_banner_attributes(self):
+ return 'cellspacing="0" cellpadding="0"'
+
+ def get_corner(self):
+ # it is important not to have newlines between the img tag and the end
+ # anchor and end center tags, otherwise layout gets messed up
+ return '''
+ <a href="%(rootdir)s/" id="logolink">
+ <img alt="" border="0"
+ src="%(rootdir)s/%(banner)s"></a>''' % \
+ self.__d
+
+ def get_corner_bgcolor(self):
+ # this may not be 100% correct. it uses PIL to get the RGB values at
+ # the corners of the image and then takes a vote as to the most likely
+ # value. Some images may be `bizarre'. See .../pics/backgrounds.py
+ return [
+ '#3399ff', '#6699cc', '#3399ff', '#0066cc', '#3399ff',
+ '#0066cc', '#0066cc', '#3399ff', '#3399ff', '#3399ff',
+ '#3399ff', '#6699cc', '#3399ff', '#3399ff', '#ffffff',
+ '#6699cc', '#0066cc', '#3399ff', '#0066cc', '#3399ff',
+ '#6699cc', '#0066cc', '#6699cc', '#3399ff', '#3399ff',
+ '#6699cc', '#3399ff', '#3399ff', '#6699cc', '#6699cc',
+ '#0066cc', '#6699cc', '#0066cc', '#6699cc', '#0066cc',
+ '#0066cc', '#6699cc', '#3399ff', '#0066cc', '#bbd6f1',
+ '#0066cc', '#6699cc', '#3399ff', '#3399ff', '#0066cc',
+ '#0066cc', '#0066cc', '#6699cc', '#6699cc', '#3399ff',
+ '#3399ff', '#6699cc', '#0066cc', '#0066cc', '#6699cc',
+ '#0066cc', '#6699cc', '#3399ff', '#6699cc', '#3399ff',
+ '#d6ebff', '#6699cc', '#3399ff', '#0066cc',
+ ][self.__whichbanner]
+
+ def get_body(self):
+ self.__grokbody()
+ return self.__body
+
+ def get_cont(self):
+ self.__grokbody()
+ return self.__cont
+
+ def __grokbody(self):
+ if self.__body is None:
+ text = self.__parser.fp.read()
+ i = text.find('<!--table-stop-->')
+ if i >= 0:
+ self.__body = text[:i]
+ self.__cont = text[i+17:]
+ else:
+ # there is no wide body
+ self.__body = text
+
+ # python.org color scheme overrides
+ def get_lightshade(self):
+ return '#99ccff'
+
+ def get_mediumshade(self):
+ return '#3399ff'
+
+ def get_darkshade(self):
+ return '#003366'
+
+ def get_charset(self):
+ return 'iso-8859-1'
More information about the Commits
mailing list