Show
Ignore:
Timestamp:
09/26/09 10:45:22 (3 years ago)
Author:
todd
Message:

fixed: htmlpage doctypes now a more comprehesive data structure;
updated: we now default to the HTML5 doctype;
updated: we now send charset in our headers and embed content-type in a meta-tag on the page;
remove: obsolete Controls directory

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • Trunk/TurtolCMS/Types/HtmlPage.py

    r1369 r1373  
    6262    req.write ('\n') 
    6363 
     64class ContentTypeWidget (Asset): 
     65  def __init__ (self, charset, ctx): 
     66    Asset.__init__ (self, '', ctx) 
     67    self._charset = charset 
     68    self['TurtolCMS.Types.Widget.position'] = 2 
     69 
     70  def Render (self, ctx, req): 
     71    req.write ('''<meta http-equiv="Content-type" content="text/html;charset=%s" />\n''' % self._charset) 
     72 
    6473class GeneratorWidget (Asset): 
    6574  def __init__ (self, close, ctx): 
     
    9099 
    91100  def Render (self, ctx, req): 
    92     # FIXME: need baselocation working 
    93     req.write ('''<div id="tcms_logo"><img src="/tcms/logo.png"/></div>\n''') 
     101    req.write ('''<div id="tcms_logo"><img src="%s/tcms/logo.png"/></div>\n''' % ctx.BaseLocation()) 
    94102 
    95103class HtmlPage (AssetType): 
     
    109117    asset['$.layout'] = asset['parent'] 
    110118 
     119    # Set the asset's doctype 
     120    asset['$.dtd'] = HtmlPage._DocTypes.get (asset['$.doctype'], None) 
     121    if asset['$.dtd'] is None: 
     122      asset['$.dtd'] = HtmlPage._DocTypes.get ('html') 
     123 
    111124    asset['$.layercount'] = 1 
    112125    while (asset['$.layout.parent']): 
     
    119132 
    120133    # Calculate our document type 
    121     asset['$.doctype'] = asset['$.layout'].get ('TurtolCMS.Types.HtmlLayout.docType', 'xml10trans')  
     134    asset['$.doctype'] = asset['$.layout'].get ('TurtolCMS.Types.HtmlLayout.docType', 'html')  
    122135       
    123136    # Construct an list of widgets broken down by div 
     
    151164        pass  # TODO: this should be an error, but how to handle? 
    152165 
     166    # Add the content-type widget 
     167    divs['head'].addWidget (ContentTypeWidget ('UTF-8', ctx)) 
    153168    # Add the generator meta widget 
    154     divs['head'].addWidget (GeneratorWidget (asset['$.doctype'][:3] == 'xml', ctx)) 
    155  
     169    divs['head'].addWidget (GeneratorWidget (asset['$.dtd'][2], ctx)) 
    156170    # Add the title widget 
    157171    divs['head'].addWidget (TitleWidget (asset['$.title'], ctx)) 
    158172 
    159173    # gather all our widgets and put them into the divs structure 
    160     # TODO: collect prereqs from our widgets, reduce and insert into head? 
    161174    iLayer = asset['$.layercount'] 
    162175    layer = asset 
     
    179192  @staticmethod 
    180193  def Head (ctx, req, asset): 
    181     req.SetResponseHeader ('Content-Type', 'text/html') 
     194    req.SetResponseHeader ('Content-Type', 'text/html; charset=UTF-8') 
    182195 
    183196  @staticmethod 
     
    185198    layout = asset['$.layout'] 
    186199 
    187     # Set our doctype 
    188     dtd = HtmlPage._DocTypes.get (asset['$.doctype'], None) 
    189     if dtd is None: 
    190       dtd = HtmlPage._DocTypes.get ('xml10trans') 
    191     req.write (dtd) 
     200    # write our dtd 
     201    req.write (asset['$.dtd'][0]) 
    192202     
    193203    # Open the html 
    194     if 'xhtml' in dtd: 
    195       req.write ('''<html xmlns="http://www.w3.org/1999/xhtml">\n''') 
    196     else: 
    197       req.write ('''<html>\n''') 
     204    req.write (asset['$.dtd'][1]) 
    198205 
    199206    # render each tagged container 
     
    211218  # Raw data begins here 
    212219  #========================================================================== 
    213   _DocTypes = { 'xml10trans'  : '''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    214                   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>\n''' 
    215               , 'xml10strict' : '''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  
    216                   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n''' 
    217               , 'html40strict'  : '''<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 
    218                   "http://www.w3.org/TR/html4/strict.dtd">\n''' 
    219               , 'html40trans' : '''<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    220                   "http://www.w3.org/TR/html4/loose.dtd">\n''' 
     220  # _DocTypes lists the "types" of documents we can output 
     221  #  each entry in the dictionary is a tuple of: 
     222  #   (doctype declaration, html open tag, bool: use xml-style tag closes) 
     223  _DocTypes = { 'html' : ( '''<!DOCTYPE html>\n''' 
     224                         , '''<html>\n''' 
     225                         , False 
     226                         ) 
     227              , 'xml10trans'  : ( '''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
     228                                  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>\n''' 
     229                                , '''<html xmlns="http://www.w3.org/1999/xhtml\n''' 
     230                                , True 
     231                                ) 
     232              , 'xml10strict' : ( '''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  
     233                                  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n''' 
     234                                , '''<html xmlns="http://www.w3.org/1999/xhtml\n''' 
     235                                , True 
     236                                ) 
     237              , 'html40strict'  : ( '''<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 
     238                                    "http://www.w3.org/TR/html4/strict.dtd">\n''' 
     239                                , '''<html>\n''' 
     240                                , False 
     241                                ) 
     242              , 'html40trans' : ( '''<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
     243                                  "http://www.w3.org/TR/html4/loose.dtd">\n''' 
     244                                , '''<html>\n''' 
     245                                , False 
     246                                ) 
    221247              } 
    222248