#! /usr/bin/python
import sys

out_prefix = 'generated/'
out_rel_prefix = '../'   # For links from a generated file to a static file.

# I don't know how many of these prefixes are useful.
#
# Note that the script currently special-cases the -webkit- prefix,
# generating {before,after,start,end} instead of {block,inline}-{start,end},
# and ...logical-{height,width} instead of ...{block,inline}-size.
# I haven't checked whether any other implementation wants such names.
ua_prefixes = (
    '-ah-',
    '-hp-',
    '-ms-',
    'prince-',
    '-rim-',
    '-ro-',
    '-tc-',
    '-webkit-',
    '',
    )

program_name = sys.argv[0]
wm2css = {
    'htb': 'horizontal-tb',
    'vrl': 'vertical-rl',
}
side2css = {
    'b': 'block-start',
    'e': 'inline-end',
    'a': 'block-end',
    's': 'inline-start',
}
side2old_css = {
    'b': 'before',
    'e': 'end',
    'a': 'after',
    's': 'start',
}
(t,r,b,l) = ('top','right','bottom','left')
trbl_wanted = {
    'htb,ltr,b': t,
    'htb,ltr,e': r,
    'htb,ltr,a': b,
    'htb,ltr,s': l,

    'htb,rtl,b': t,
    'htb,rtl,e': l,
    'htb,rtl,a': b,
    'htb,rtl,s': r,

    'vrl,ltr,b': r,
    'vrl,ltr,e': b,
    'vrl,ltr,a': l,
    'vrl,ltr,s': t,

    'vrl,rtl,b': r,
    'vrl,rtl,e': t,
    'vrl,rtl,a': l,
    'vrl,rtl,s': b,
}
border_attr2val = {
    '': '32px solid green',
    '-color': 'green',
}
for attr in border_attr2val.keys():
    for wm in wm2css.keys():
        for dir in ('ltr', 'rtl'):
            for side in side2css.keys():
                filename = out_prefix + ('bord-L%s-%s-%s-%s.html' % (attr, wm, dir, side))
                f = open(filename, 'w')
                f.write("""<!DOCTYPE html>
<html lang="en" xml:lang="en">
<!-- Generated automatically by %s; do not edit. -->
<head>
<title></title>
<link rel="stylesheet" type="text/css" media="all" href="%sbord-L-color.css" />
<meta name="assert" content="The mapping depends on the [target] element's [writing mode].">
<link rel="author" title="Peter Moulder" href="mailto:pjrm@internode.on.net">
<link rel="help" href="https://drafts.csswg.org/css-logical-props/#border-padding">
</head>
<body>
<p>Test passes if there is a green square below.</p>
""" % (program_name, out_rel_prefix))
                div = ('<div style="writing-mode:%s; direction:%s; border-%s:32px solid red;'
                       % (wm2css[wm],
                          dir,
                          trbl_wanted[','.join((wm,dir,side))]))
                for ua_prefix in ua_prefixes:
                    if ua_prefix == '-webkit-':
                        side_css = side2old_css[side]
                    else:
                        side_css = side2css[side]
                    div += ' %sborder-%s%s:%s;' % (ua_prefix, side_css, attr, border_attr2val[attr])
                div += '"></div>'
                f.write(div + "\n")
                f.write("</body>\n</html>\n")
                f.close()

dim2size_css = {
    'b': 'block-size',
    'i': 'inline-size',
}
dim2webkit_size_css = {
    'b': 'logical-height',
    'i': 'logical-width',
}
(h,w) = ('left', 'top')
hw_wanted = {
    'htb,b': h,
    'htb,i': w,

    'vrl,b': w,
    'vrl,i': h,
}
for size_prefix in ('', 'min-', 'max-'):
    for wm in wm2css.keys():
        for dir in ('ltr', 'rtl'):
            for dim in dim2size_css.keys():
                filename = out_prefix + ('%sL-%s-%s-%s.html' % (size_prefix, wm, dir, dim))
                f = open(filename, 'w')
                f.write("""<!DOCTYPE html>
<html lang="en" xml:lang="en">
<!-- Generated automatically by %s; do not edit. -->
<head>
<title></title>
<link rel="author" title="Peter Moulder" href="mailto:pjrm@internode.on.net">
<meta name="assert" content="The mapping depends on the [target] element's writing-mode.">
<link rel="help" href="https://drafts.csswg.org/css-logical-props/#logical-dimension-properties">
<style type="text/css" media="all">html { font: 16px/1.1875 FreeSerif, serif }</style>
</head>
<body>
<p>Test passes if there is a green square below.</p>
<div style="border-left:32px solid red; height:32px; margin-bottom:-32px;"></div>
""" % program_name)
                div = ('<div style="writing-mode:%s; direction:%s; border-%s:32px solid green;'
                       % (wm2css[wm],
                          dir,
                          hw_wanted[','.join((wm,dim))]))
                if size_prefix == 'max-':
                    div += ' width:64px; max-width:0; height:64px; max-height:0;'
                else:
                    div += ' width:0; height:0;'
                for ua_prefix in ua_prefixes:
                    if ua_prefix == '-webkit-':
                        dim_size_css = dim2webkit_size_css[dim]
                    else:
                        dim_size_css = dim2size_css[dim]
                    prop = ua_prefix + size_prefix + dim_size_css
                    div += ' %s:32px;' % prop
                div += '"></div>'
                f.write(div + "\n")
                f.write("</body>\n</html>\n")
                f.close()


# vi: set autoindent shiftwidth=4 tabstop=8 expandtab softtabstop=4 filetype=python :
