#!/usr/local/bin/python
# $Id: html2lout.py,v 1.1 1997/05/22 19:40:06 connolly Exp $
# http://www.w3.org/pub/WWW/COPYRIGHT

import xml2lout
from xml2lout import ON, OFF, CASE, ATTR, RESET, INC, VALUE, CHAR
from loutwr import BOL, INDENT, LB, RB

Rules = (
    ##########
    # HTML Document

    (('html',),
     (BOL, '@SysInclude', LB, 'doc', RB, # this goes with <!doctype
      BOL, '@Document', BOL,),
     ()),
    
    (('head',),
     (OFF,),
     (ON, BOL, '//')),

#@ Hmmm... would be nice to put title in head/foot
    (('title',), (), ()),

    #@@ PRE -> @Display @Font, clines
    
    (('body',),
     (BOL, '@Text', '@Begin'),
     (BOL, '@End', '@Text')),

    (('h1',), (BOL, '@CD @Heading', LB), (RB,)),
    (('h2',), (BOL, '@ID @Heading', LB), (RB,)),
    (('h3',), (BOL, '@Display', '@Heading', LB), (RB,)),
    (('h4',), (BOL, '@Display', '@Heading', LB), (RB,)),
    (('h5',), (BOL, '@Display', '@Heading', LB), (RB,)),
    (('h6',), (BOL, '@Display', '@Heading', LB), (RB,)),
    
    (('blockquote',),
     (BOL, '@QD', LB),
     (RB,)),
    
    (('blockquote', 'p',),
     (BOL, '@LP', BOL),
     ()),

    (('hr',),
     ('@HLine',),
     ()),
    
    (('p',),
     (BOL, '@PP', BOL),
     ()),
    
    (('address',),
     (BOL, 'Slope', '@Font', '@RD', BOL),
     ()),
    
    ############
    # Table
    # @# propagate TABLE BORDER markup using CASE and ATTR?
    
    (('table',), #@# only for real tables, not for layout
     (BOL, '@CD', (RESET, 'row', 'A'), LB), #@@ @Table drops the whole thing!
     (BOL, RB, RB)),
    
    (('table', 'caption'),
     (BOL, INDENT, '@Heading', LB),
     (RB,)),
    
    (('table', 'thead'),
     ((CASE, 'row',
       ('A',
	(BOL, '@Tab',
	 BOL, INDENT, 'above', LB, 'single', RB,
	 BOL, INDENT, 'below', LB, 'single', RB,
	 BOL, '@Fmta', LB,
	 '@Col', 'A', '!!', #@@ "enough" columns
	 '@Col', 'B', '!!',
	 '@Col', 'C', '!!',
	 '@Col', 'D', '!!',
	 '@Col', 'E', '!!',
	 '@Col', 'F', '!!',
	 '@Col', 'G',
	 RB, BOL, LB) )),),
     ()),

    #@@ tbody

    (('table', 'tr'),
     ((INC, 'row'),
      BOL, INDENT, '@Rowa', (RESET, 'col', 'A')),
     ()),
    
    (('table', 'tr', 'th'),
     ((VALUE, 'col'), (INC, 'col'), LB, '@B', '@CC', LB),
     (RB,RB)),
    
    (('table', 'tr', 'td'),
     ((VALUE, 'col'), (INC, 'col'), LB,),
     (RB,)),
    
    ############
    # List rules
    # @@raw lists for UL inside UL
    (('ul',),
     (BOL, '@List'),
     (BOL, '@EndList')),
    
    (('ol',),
     (BOL, '@NumberedList'),
     (BOL, '@EndList')),
    
    (('li',),
     (BOL, '@ListItem', LB),
     (RB,)),
    
    (('dl',),
     (BOL, '@TaggedList'),
     (BOL, '@EndList')),
    
    
    (('dl', 'dt'),
     (BOL, '@DropTagItem', LB), #@# compact -> @TagItem, WideTagList, ...
     (BOL, RB)),
    
    (('dl', 'dt', 'dd'),
     (RB, LB),
     ()),
    
    ############
    # Text rules
    
    #@@ should absolutize hrefs
    #@@ shouldn't print a footnote when there is not href
    (('a',),
     (),
     ('@FootNote', LB, (ATTR, 'href'), RB,)),
    
    (('tt',),
     ('@F', LB),
     (RB,)),
    
    (('var',),
     ('@I', LB), #@# should define lout symbols @Var, @Dfn
     (RB,)),
    
    (('dfn',),
     ('@I', LB),
     (RB,)),
    
    (('emdash',),
     ((CHAR, '---'), OFF),
     (ON,)),
    
    (('q',),
     ((CHAR, '``'),),
     ((CHAR, "''"),)),
    
    (('q', 'c'),
     (OFF,),
     (ON,)),
    
    )

class T(xml2lout.T):
    def __init__(self, wr):
	xml2lout.T.__init__(self, wr)
	self.html32()
	self.rules = Rules


if __name__ == '__main__':
    import sys
    x = T(sys.stdout)
    x.convert(sys.stdin)
