Blame Scripts/CentOS-Web/Xhtml/output.py

cc10e6
#!/usr/bin/python
cc10e6
#
cc10e6
# Xhtml -- This module encapsulates XHTML construction needed by The
cc10e6
# CentOS Web application.
cc10e6
#
cc10e6
# Copyright (C) 2009, 2010, 2011 The CentOS Project
cc10e6
#
cc10e6
# This program is free software; you can redistribute it and/or modify
cc10e6
# it under the terms of the GNU General Public License as published by
cc10e6
# the Free Software Foundation; either version 2 of the License, or (at
cc10e6
# your option) any later version.
cc10e6
#
cc10e6
# This program is distributed in the hope that it will be useful, but
cc10e6
# WITHOUT ANY WARRANTY; without even the implied warranty of
cc10e6
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
cc10e6
# General Public License for more details.
cc10e6
#
cc10e6
# You should have received a copy of the GNU General Public License
cc10e6
# along with this program; if not, write to the Free Software
cc10e6
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
cc10e6
#
cc10e6
# ----------------------------------------------------------------------
cc10e6
# $Id$
cc10e6
# ----------------------------------------------------------------------
cc10e6
cc10e6
import os
cc10e6
import cgi
cc10e6
import cgitb; cgitb.enable()
cc10e6
cc10e6
qs = cgi.parse_qs(os.environ['QUERY_STRING'])
cc10e6
copyright = '2009-2011 The CentOS Project. All rights reserved.'
cc10e6
language = 'en'
cc10e6
cc10e6
def tag(name, attributes, indentation=[8,1], content="", has_child=0):
cc10e6
    """Returns XHTML tag definition.
cc10e6
cc10e6
    Arguments:
cc10e6
cc10e6
    name: The XHTML tag's name. Notice that this function doesn't
cc10e6
        verify nor validate the XHTML tags you provide. It is up to
cc10e6
        you write them correctly considering the XHTML standard
cc10e6
        definition.
cc10e6
cc10e6
    attributes: The XHTML tag's attribute. Notice that this function
cc10e6
        doesn't verify the attributes assignation to tags. You need to
cc10e6
        know what attributes are considered valid to the tag you are
cc10e6
        creating in order to build a well-formed XHTML document. Such
cc10e6
        verification can be achived inside firefox browser through the
cc10e6
        `firebug' plugin.
cc10e6
cc10e6
    indentation: The XHTML tag's indentation (Optional). This argument
cc10e6
        is a list of two numerical values. The first value in the list
cc10e6
        represents the amount of horizontal spaces between the
cc10e6
        beginning of line and the opening tag.  The second value in
cc10e6
        the list represents the amount of vertical spaces (new lines)
cc10e6
        between tags.
cc10e6
cc10e6
    content: The XHTML tag's content (Optional). This argument
cc10e6
        provides the information the tag encloses. When this argument
cc10e6
        is empty, tag is rendered without content.
cc10e6
cc10e6
    has_child: The XHTML tag has a child? (Optional). This argument is
cc10e6
        specifies whether a tag has another tag inside (1) or not (0).
cc10e6
        When a tag has not a child tag, indentation is applied between
cc10e6
        the tag content and the closing tag provoking an unecessary
cc10e6
        spaces to be shown. Such kind of problems are prevented by
cc10e6
        setting this option to `0'. On the other hand, when a tag has
cc10e6
        a child tag inside, using the value `1' will keep the closing
cc10e6
        tag indentation aligned with the opening one.
cc10e6
cc10e6
    This function encapsulates the construction of XHTML tags.  Use
cc10e6
    this function wherever you need to create XHTML tags. It helps to
cc10e6
    standardize tag constructions and their final output and, this
cc10e6
    way, produce consistent XHTML documents.
cc10e6
cc10e6
    """
cc10e6
    if indentation[0] > 0:
cc10e6
        h_indent = ' '*indentation[0]
cc10e6
    else:
cc10e6
        h_indent = ''
cc10e6
cc10e6
    if indentation[1] > 0: 
cc10e6
        v_indent = "\n"*indentation[1]
cc10e6
    else:
cc10e6
        v_indent = ''
cc10e6
    
cc10e6
    output = v_indent + h_indent + '<' + str(name)
cc10e6
    if len(attributes) > 0:
cc10e6
        for k, v in attributes.iteritems():
d1cf1c
            output += ' ' + str(k) + '="' + str(v) + '"'
cc10e6
    if content == '':
d1cf1c
        output += ' />'
cc10e6
    else:
d1cf1c
        output += '>'
d1cf1c
        output += str(content)
cc10e6
        if has_child == 1:
d1cf1c
            output += h_indent + '</' + str(name) + '>'
cc10e6
        else:
d1cf1c
            output += '</' + str(name) + '>'
d1cf1c
    output += v_indent
cc10e6
cc10e6
    return output
cc10e6
cc10e6
cc10e6
def page_preamble():
cc10e6
    """Define XHTML preamble.
cc10e6
cc10e6
    Use this section to set the content-type required by CGI
cc10e6
    definition, the document type and the head section required by
cc10e6
    XHTML standard.
cc10e6
cc10e6
    """
cc10e6
    output = '' + "\n"
d1cf1c
    output += '
d1cf1c
    output += ' '*4 + 'PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"' + "\n"
d1cf1c
    output += ' '*4 + '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' + "\n"
cc10e6
cc10e6
    return output
cc10e6
cc10e6
cc10e6
def page_logo():
cc10e6
    """Print XHTML code of page logo.
cc10e6
cc10e6
    The page logo is displayed on the top-left corner of the page. We
cc10e6
    use this area to show The CentOS Logo, the main visual
cc10e6
    representation of The CentOS Project. In order to print the page
cc10e6
    logo correctly, the image related must be 78 pixels of height.
cc10e6
    """
cc10e6
    attrs = []
cc10e6
    attrs.append({'id': 'logo'})
cc10e6
    attrs.append({'title': 'Community Enterprise Operating System', 'href': '/centos-web/'})
cc10e6
    attrs.append({'src': '/centos-web-pub/Images/centos-logo.png', 'alt': 'CentOS'})
cc10e6
cc10e6
    return tag('div', attrs[0], [8,1], tag('a', attrs[1], [12,1], tag('img', attrs[2], [0,0], '', 0), 0), 1)
cc10e6
cc10e6
cc10e6
def page_ads_google():
cc10e6
    """Returns Google advertisement (468x60 pixels).
cc10e6
    
cc10e6
    """
cc10e6
    output = """
cc10e6
        
cc10e6
            Google Advertisement
cc10e6
            <script type="text/javascript">
cc10e6
                google_ad_client = "pub-6973128787810819";
cc10e6
                google_ad_width = 468;
cc10e6
                google_ad_height = 60;
cc10e6
                google_ad_format = "468x60_as";
cc10e6
                google_ad_type = "text_image";
cc10e6
                google_ad_channel = "";
cc10e6
                google_color_border = "204c8d";
cc10e6
                google_color_bg = "345c97";
cc10e6
                google_color_link = "0000FF";
cc10e6
                google_color_text = "FFFFFF";
cc10e6
                google_color_url = "008000";
cc10e6
                //-->
cc10e6
            </script>
cc10e6
            
cc10e6
                src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
cc10e6
            </script>
cc10e6
        
cc10e6
cc10e6
        

cc10e6
    """
cc10e6
cc10e6
    return output
cc10e6
cc10e6
cc10e6
def page_navibar_top():
cc10e6
    """Return The CentOS Project top-level navigation links. 
cc10e6
    
cc10e6
    Use this section to list the web applications you want to be
cc10e6
    always visible no matter where you are inside the CentOS web
cc10e6
    henvironment.  Example of web applications are `Home', `Wiki',
cc10e6
    `Lists', `Forums', `Planet', etc.
cc10e6
cc10e6
    """
cc10e6
    names = []
cc10e6
    attrs = []
cc10e6
cc10e6
    names.append('Home')
cc10e6
    attrs.append({'accesskey': '1', 'title': 'The CentOS Project (Alt+Shift+1)', 'href': '/centos-web/'})
cc10e6
    names.append('Wiki')
cc10e6
    attrs.append({'accesskey': '2', 'title': 'The CentOS Wiki (Alt+Shift+2)', 'href': '/centos-web/?app=wiki'})
cc10e6
    names.append('Lists')
cc10e6
    attrs.append({'accesskey': '3', 'title': 'The CentOS Lists (Alt+Shift+3)', 'href': '/centos-web/?app=lists'})
cc10e6
    names.append('Forums')
cc10e6
    attrs.append({'accesskey': '4', 'title': 'The CentOS Forums (Alt+Shift+4)', 'href': '/centos-web/?app=forums'})
cc10e6
    names.append('Projects')
cc10e6
    attrs.append({'accesskey': '5', 'title': 'The CentOS Projects (Alt+Shift+5)', 'href': '/centos-web/?app=projects'})
cc10e6
    names.append('Bugs')
cc10e6
    attrs.append({'accesskey': '6', 'title': 'The CentOS Bugs (Alt+Shift+6)', 'href': '/centos-web/?app=bugs'})
de0f25
    names.append('Docs')
de0f25
    attrs.append({'accesskey': '7', 'title': 'The CentOS Documentation (Alt+Shift+7)', 'href': '/centos-web/?app=docs'})
cc10e6
    names.append('Downloads')
cc10e6
    attrs.append({'accesskey': '8', 'title': 'The CentOS Downloads (Alt+Shift+8)', 'href': '/centos-web/?app=downloads'})
cc10e6
    names.append('Sponsors')
cc10e6
    attrs.append({'accesskey': '9', 'title': 'The CentOS Downloads (Alt+Shift+9)', 'href': '/centos-web/?app=sponsors'})
cc10e6
cc10e6
    if 'app' in qs:
cc10e6
        focus = qs['app'][0]
cc10e6
    else:
cc10e6
        focus = names[0]
cc10e6
cc10e6
    output = page_navibar_tabs(names, attrs, focus)
d1cf1c
    output += tag('div', {'class': 'page-line white'}, [8,1], tag('hr', {'style': 'display:none;'}, [0,0], '', 0), 0)
cc10e6
cc10e6
    return output
cc10e6
cc10e6
cc10e6
def page_navibar_tabs(names, attrs, focus="Home"):
cc10e6
    """Returns the XHTML code of navigation tabs.
cc10e6
cc10e6
    Argumuents:
cc10e6
cc10e6
    names: List of tab names.
cc10e6
cc10e6
    attrs: List of dictionaries for each tab name inside the `names'
cc10e6
        list. Dictionaries inside attrs argument contain the XHTML
cc10e6
        link attributes (e.g., accesskey, title, and href) used by tab
cc10e6
        names so they can be linkable once rendered.
cc10e6
cc10e6
    focus: Name of the tab marked as current. When no value is passed
cc10e6
        to this argument the `Home' value is used as default value.
cc10e6
cc10e6
    """
cc10e6
    links = ''
cc10e6
cc10e6
    for i in names:
cc10e6
        content = tag('span', '', [0,0], str(i))
cc10e6
        content = tag('a', attrs[names.index(i)], [16,1], content)
cc10e6
        if str(i).lower() == focus.lower():
cc10e6
            content = tag('span', {'class': 'current'}, [12,1], content, 1)
cc10e6
        else:
cc10e6
            content = tag('span', '', [12,1], content, 1)
cc10e6
        links = links + content
cc10e6
cc10e6
    output = tag('div', {'class': 'tabs1'}, [8,1], links, 1)
cc10e6
cc10e6
    return output
cc10e6
cc10e6
cc10e6
def page_ads_release():
cc10e6
    """Return XHTML code of last-release advertisement.
cc10e6
    
cc10e6
    The release advertisment is a 728x90 pixels image graphically
cc10e6
    designed to promote the last releases of The CentOS Distribution.
cc10e6
    This image is located on the header space, between the top-level
cc10e6
    links and application specific links. This image changes each time
cc10e6
    a new release is published from The CentOS Project.  
cc10e6
    
cc10e6
    The place where the release advertisement is displayed on the web
cc10e6
    is an area of high visual impact, so images appearing therein
cc10e6
    should be carefully designed in consequence with it.  Likewise,
cc10e6
    the frequency and priority of images in the rotation must be
cc10e6
    controlled and somehow syncronized with the releasing process of
cc10e6
    The CentOS Distribution.
cc10e6
cc10e6
    Previous to consider the release advertisement as such, it was
cc10e6
    reserved for sponsor advertisements. Nevertheless, sponsor
cc10e6
    advertisements were moved to a page for its own through a link in
cc10e6
    the top-level navegation bar.
cc10e6
    
cc10e6
    """
cc10e6
    attrs = []
cc10e6
    attrs.append({'class': 'ads-release'})
cc10e6
    attrs.append({'title': 'Release Advertisement', 'href': ''})
cc10e6
    attrs.append({'src': '/centos-web-pub/Images/ads-sample-728x90.png', 'alt': 'Release Advertisement'})
cc10e6
    output = tag('div', attrs[0], [8,1], tag('a', attrs[1], [12,1], tag('img', attrs[2], [0,0], '', 0), 0), 1)
cc10e6
cc10e6
    return output
cc10e6
cc10e6
cc10e6
def page_userlinks():
cc10e6
    """Return XHTML code of user's links.
cc10e6
cc10e6
    """
cc10e6
    links = ''
cc10e6
    names = []
cc10e6
    attrs = []
cc10e6
cc10e6
    names.append('Lost your password?')
cc10e6
    attrs.append({'href': '/centos-web/?p=lostpwd'})
cc10e6
    names.append('Register')
cc10e6
    attrs.append({'href': '/centos-web/?p=register'})
cc10e6
    names.append('Login')
cc10e6
    attrs.append({'href': '/centos-web/?p=login'})
cc10e6
cc10e6
    for i in names:
cc10e6
        content = tag('a', attrs[names.index(i)], [20,1], str(i), 0)
cc10e6
        if str(i) == names[len(names) -1]:
cc10e6
            content = tag('span', {'class': 'last'}, [16,1], content, 1)
cc10e6
        else:
cc10e6
            content = tag('span', '', [16,1], content, 1)
cc10e6
        links = links + content
cc10e6
cc10e6
    content = tag('div', {'class': 'user'}, [12,1], links, 1)
cc10e6
    output = tag('div', {'class': 'links'}, [8,1], content, 1)
cc10e6
cc10e6
    return output
cc10e6
cc10e6
cc10e6
def page_navibar_app():
cc10e6
    """Returns navigation bar with application links.
cc10e6
cc10e6
    """
cc10e6
    names = []
cc10e6
    attrs = [] 
cc10e6
cc10e6
    names.append('Erratas')
cc10e6
    attrs.append({'accesskey': 'e', 'title': 'The CentOS News (Alt+Shift+E)', 'href': '/centos-web/?p=erratas'})
cc10e6
    names.append('Articles')
cc10e6
    attrs.append({'accesskey': 'a', 'title': 'The CentOS Articles (Alt+Shift+A)', 'href': '/centos-web/?p=articles'})
cc10e6
    names.append('Events')
cc10e6
    attrs.append({'accesskey': 'v', 'title': 'The CentOS Events (Alt+Shift+V)', 'href': '/centos-web/?p=events'})
cc10e6
    
cc10e6
    if 'p' in qs:
cc10e6
        focus = qs['p'][0]
cc10e6
    else:
cc10e6
        focus = names[0]
cc10e6
cc10e6
    output = page_userlinks()
d1cf1c
    output += page_navibar_tabs(names, attrs, focus)
d1cf1c
    output += tag('div', {'class': 'page-line white'}, [4,1], tag('hr', {'style': 'display:none;'}, [0,0], '', 0), 0)
cc10e6
cc10e6
    return output
cc10e6
cc10e6
cc10e6
def page_header():
cc10e6
    """Returns XHTML code of page header."""
cc10e6
cc10e6
    content = page_logo()
d1cf1c
    content += page_ads_google()
d1cf1c
    content += page_navibar_top()
cc10e6
    if not 'app' in qs:
d1cf1c
        content += page_ads_release()
d1cf1c
        content += page_navibar_app()
cc10e6
cc10e6
    return tag('div', {'id': 'page-header'}, [4,1], content, 1)
cc10e6
    
cc10e6
cc10e6
def page_body_breadcrumbs():
cc10e6
    """Application specific links for breadcrumbs sutff. 
cc10e6
    
cc10e6
    Use this section if your application supports breadcrumbs.
cc10e6
cc10e6
    """
cc10e6
    links = ''
cc10e6
    names = []
cc10e6
    attrs = []
cc10e6
cc10e6
    names.append('Pagination')
cc10e6
    attrs.append({'href': ''})
cc10e6
    names.append('Lists')
cc10e6
    attrs.append({'href': ''})
cc10e6
    names.append('Headings')
cc10e6
    attrs.append({'href': ''})
cc10e6
    names.append('Links')
cc10e6
    attrs.append({'href': ''})
cc10e6
cc10e6
    for i in names:
cc10e6
        if names.index(i) == len(names) - 1:
cc10e6
            content = tag('span', {'class':'last'}, [16,1], tag('a', attrs[names.index(i)], [20, 1], i), 1)
cc10e6
        else:
cc10e6
            content = tag('span', '', [16,1], tag('a', attrs[names.index(i)], [20, 1], i, 0), 1)
cc10e6
        links = links + content
cc10e6
cc10e6
    return tag('div', {'class': 'trail'}, [12,1], links, 1)
cc10e6
cc10e6
cc10e6
def page_body():
cc10e6
    """Start page body definitions.
cc10e6
    """
cc10e6
    content = page_body_breadcrumbs()
cc10e6
d1cf1c
    content += tag('h1', {'class': 'title'}, [12,1], 'My first CGI script')
d1cf1c
    content += tag('p', '', [12,1], 'This is the first paragraph.')
cc10e6
d1cf1c
    content += tag('div', {'class': 'page-line'}, [8,1], tag('hr', {'style': 'display:none;'}, [0,0]))
cc10e6
    content = tag('div', {'id':'content'}, [8,1], content, 1)
cc10e6
cc10e6
    output = tag('div', {'id':'page-body'}, [4,1], content, 1)
cc10e6
cc10e6
    return output
cc10e6
cc10e6
cc10e6
def page_footer():
cc10e6
    """Returns XHTML code of page's footer.
cc10e6
    """
cc10e6
    attrs = []
cc10e6
    attrs.append({'title': 'Top', 'href': '#top'})
cc10e6
    attrs.append({'src': '/centos-web-pub/Images/top.png', 'alt': 'Top'})
cc10e6
    attrs.append({'href': 'http://creativecommons.org/licenses/by-sa/3.0/'})
cc10e6
cc10e6
    license = tag('a', attrs[2], [0,0], 'Creative Commons Attribution-Share Alike 3.0 Unported License.')
cc10e6
cc10e6
    credits = tag('div', {'class': 'copyright'}, [12,1], 'Copyright © ' + str(copyright))
cc10e6
    credits = credits + tag('div', {'class': 'license'}, [12,1], 'This website is licensed under a ' + str(license))
cc10e6
cc10e6
    content = tag('img', attrs[1], [0,0])
cc10e6
    content = tag('a', attrs[0], [12,1], content)
cc10e6
    content = tag('div', {'class': 'top'}, [8,1], content, 1)
d1cf1c
    content += tag('div', {'class': 'credits'}, [8,1], credits, 1)
cc10e6
cc10e6
    output = tag('div', {'class': 'page-line'}, [4,1], tag('hr', {'style': 'display:none;'}, [0,0]))
d1cf1c
    output += tag('div', {'id': 'page-footer'}, [4,1], content, 1)
cc10e6
cc10e6
    return output
cc10e6
cc10e6
cc10e6
def page_wrap(title, keywords, description):
cc10e6
    """Returns the XHTML code that wraps the whole page.
cc10e6
    """
cc10e6
    attrs = []
cc10e6
cc10e6
    attrs.append({'xmlns': 'http://www.w3.org/1999/xhtml', 'dir': 'ltr', 'lang': str(language), 'xml:lang': str(language)})
cc10e6
    
cc10e6
    content = tag('meta http-equiv="content-type" content="text/html; charset=UTF-8"', '', [4,1])
d1cf1c
    content += tag('meta http-equiv="content-style-type" content="text/css"', '', [4,0])
d1cf1c
    content += tag('meta http-equiv="content-language" content="' + str(language) + '"', '', [4,1])
d1cf1c
    content += tag('meta name="keywords" content="' + str(keywords) + '"', '', [4,0])
d1cf1c
    content += tag('meta name="description" content="' + str(description) + '"', '', [4,1])
d1cf1c
    content += tag('meta name="copyright" content="Copyright © ' + str(copyright) + '"', '', [4,0])
d1cf1c
    content += tag('title', '', [4,1], str(title))
d1cf1c
    content += tag('link href="/centos-web-pub/stylesheet.css" rel="stylesheet" type="text/css" media="screen projection"', '', [4,1])
cc10e6
    head = tag('head', '', [0,1], content)
cc10e6
cc10e6
    top = tag('a', {'name':'top'}, [0,1], '')
cc10e6
cc10e6
    content = page_header()
d1cf1c
    content += page_body()
d1cf1c
    content += page_footer()
cc10e6
    wrap = tag('div', {'id': 'wrap'}, [0,1], content)
cc10e6
cc10e6
    body = tag('body', '', [0,1], top + wrap)
cc10e6
cc10e6
    output = page_preamble()
d1cf1c
    output += tag('html', attrs[0], [0,1], head + body)
cc10e6
cc10e6
    return output
cc10e6
cc10e6
cc10e6
def page():
cc10e6
    """The Xhtml code of a complete page."""
cc10e6
cc10e6
    title       = 'The CentOS Project'
cc10e6
    keywords    = 'centos, project, community, enterprise, operating system'
cc10e6
    if 'app' in qs:
cc10e6
        title = title + ' :: ' + str(qs['app'][0]).capitalize()
cc10e6
    description = title
cc10e6
cc10e6
    print 'Content-type: text/html' + "\n"
cc10e6
    print page_wrap(title, keywords, description)