"""
Inserts appropriate link tags into the HTML header, including handling
home, up, first, last, next, previous, author, contents, index, glossary,
appendices, chapters, subchapters, bookmarks, icons, and style sheets.
"""
# Copyright © 2004 Saugus.net, Inc.
# All Rights Reserved.
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
request=container.REQUEST
response=request.RESPONSE
linkTag=None
def getFullLinkTag(obj,relName,url,titleAtt,typeAtt,mediaAtt,langAtt):
"""
Assemble the link tag based upon its component parts.
"""
if url:
# only bother if there's a url to work with...
if titleAtt:
if titleAtt==1:
if obj:
# title wanted, get it from existing object
if obj.title_or_id()!=obj.getId():
titleAttText=' title="'+obj.title_or_id()+'"'
else:
# this odd case handles persistent (nameless) style sheets
titleAttText=""
else:
# default; pretty lame
titleAttText=' title="'+relName+'"'
else:
# title wanted and provided; use it
titleAttText=' title="'+titleAtt+'"'
else:
# title not wanted
titleAttText=""
if typeAtt:
typeAttText=""
if typeAtt==1:
if obj:
# type wanted, get it from existing object
typeAttText=' type="'+obj.getContentType()+'"'
else:
# default
typeAttText=' type="image/png"'
else:
# type wanted and provided; use it
typeAttText=' type="'+typeAtt+'"'
else:
# type not wanted
typeAttText=""
if mediaAtt:
if mediaAtt==1:
if obj:
# media type wanted, get it from existing object
mediaAttText=' media="'+obj.getitem('target_media')+'"'
else:
# default
mediaAttText=' media="all"'
else:
# media wanted and provided; use it
mediaAttText=' media="'+mediaAtt+'"'
else:
# media not wanted
mediaAttText=""
if langAtt:
if langAtt==1:
if obj:
# lang type wanted, get it from existing object
langAttText=' lang="'+obj.getitem('target_language')+'"'
else:
# default
langAttText=' lang="en"'
else:
# language wanted and provided; use it
langAttText=' lang="'+langAtt+'"'
else:
# language not wanted
langAttText=""
return ''
else:
return ''
def getLinkTags(attrPair,**kw):
"""
"""
titleAtt=None
typeAtt=None
mediaAtt=None
langAtt=None
for eachKW in kw.keys():
if eachKW=='titleAtt':
titleAtt=kw[eachKW]
elif eachKW=='typeAtt':
typeAtt=kw[eachKW]
elif eachKW=='mediaAtt':
mediaAtt=kw[eachKW]
elif eachKW=='langAtt':
langAtt=kw[eachKW]
linkTag=""
obj=None
if _.has_key(attrPair[0]):
obj=_.getitem(attrPair[0])
if obj:
if same_type(obj,[]) or same_type(obj,()):
for eachObj in obj:
if _.has_key(eachObj):
eachObj=_.getitem(eachObj)
url=eachObj.absolute_url()
if url!=request.URL1:
if len(linkTag)>0:
linkTag+='\n'
linkTag+=getFullLinkTag(eachObj,attrPair[1],url,titleAtt,typeAtt,mediaAtt,langAtt)
else:
url=eachObj
eachObj=None
if len(linkTag)>0:
linkTag+='\n'
linkTag+=getFullLinkTag(eachObj,attrPair[1],url,titleAtt,typeAtt,mediaAtt,langAtt)
elif _.has_key(obj):
obj=_.getitem(obj)
url=obj.absolute_url()
if url!=request.URL1:
linkTag=getFullLinkTag(obj,attrPair[1],url,titleAtt,typeAtt,mediaAtt,langAtt)
else:
url=obj
obj=None
linkTag=getFullLinkTag(obj,attrPair[1],url,titleAtt,typeAtt,mediaAtt,langAtt)
else:
url=obj
obj=None
linkTag=getFullLinkTag(obj,attrPair[1],url,titleAtt,typeAtt,mediaAtt,langAtt)
return linkTag
# Author/Made links have fixed title
for attrPair in [('link_contact','Author'),
('link_contact','Made')]:
if _.has_key(attrPair[0]):
address=_.getitem(attrPair[0])
linkTag=context.eMailTag(address,attrPair[1],'E-mail webmaster','HTML link')
if linkTag:
print linkTag
# if there's a level above, it's always okay to include Home & Up
if context.notAtRoot():
print ''
print ''
# handle link_first, link_last, link_next, and link_prev specially as they require scripts
for attrPair in [('link_first','First'),
('link_first','Start'),
('link_next','Next'),
('link_prev','Previous'),
('link_prev','Prev'),
('link_last','Last'),
('link_last','End')]:
if _.has_key(attrPair[0]):
url=_.getitem(attrPair[0],1)
obj=None
if url!=request.URL1:
linkTag=getFullLinkTag(obj,attrPair[1],url,1,0,0,0)
if linkTag:
print linkTag
# position and context rel links want a title but not really anything else
for attrPair in [('link_copyright','Copyright'),
('link_bookmarks','Bookmarks'),
('link_search','Search'),
('link_search','Find'),
('link_help','Help'),
('link_toc','Contents'),
('link_toc','ToC'),
('link_chapters','Chapter'),
('link_sections','Section'),
('link_subsections','Subsection'),
('link_glossary','Glossary'),
('link_appendices','Appendix'),
('link_index','Index'),
('link_subdocuments','Subdocument'),
('link_prefetch','prefetch')]:
linkTag=getLinkTags(attrPair,titleAtt=1)
if linkTag:
print linkTag
#icons (proper and MS-specific) want a MIME type
linkTag=getLinkTags(('link_favicon','Shortcut Icon'),typeAtt="application/octet-stream")
if linkTag:
print linkTag
else:
print ''
linkTag=getLinkTags(('link_icon','Icon'),typeAtt=1)
if linkTag:
print linkTag
#MSIE compliance
print """"""
#RDF
print """"""
#stylesheets; primary and alternates -- each can have multiples; type is important
for attrPair in [('link_stylesheets','Stylesheet'),
('link_alternate_stylesheets','Alternate Stylesheet')]:
linkTag=getLinkTags(attrPair,typeAtt="text/css",titleAtt=1)
if linkTag:
print linkTag
#alternates; often multiple
for attrPair in [('link_alternates','Alternate'),
('link_alternate_copyright','Alternate Copyright'),
('link_alternate_search','Alternate Search'),
('link_alternate_search','Alternate Find'),
('link_alternate_help','Alternate Help'),
('link_alternate_toc','Alternate Contents'),
('link_alternate_chapters','Alternate Chapter'),
('link_alternate_sections','Alternate Section'),
('link_alternate_subsections','Alternate Subsection'),
('link_alternate_glossary','Alternate Glossary'),
('link_alternate_appendices','Alternate Appendix'),
('link_alternate_index','Alternate Index'),
('link_alternate_next','Alternate Next'),
('link_alternate_prev','Alternate Prev'),
('link_alternate_first','Alternate First'),
('link_alternate_last','Alternate Last'),
('link_alternate_first','Alternate Start'),
('link_alternate_last','Alternate End')]:
linkTag=getLinkTags(attrPair,titleAtt=1,mediaAtt=1,langAtt=1)
if linkTag:
print linkTag
#anything raw to be inserted
if _.has_key('raw_html_headers'):
rawHeaders=_.getitem('raw_html_headers')
for eachHeader in rawHeaders:
print eachHeader
latestTime=DateTime().latestTime().timeTime()
expirationTime=DateTime().timeTime()+_.getitem('cacheDefaultExpire')
if latestTime