# -*- coding: utf-8 mode: python -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
import ulib.base.i_need_py23

__all__ = ('CssTempl',
           'JsTempl',
           'HtmlTempl', 'OverviewHtmlTempl', 'PackageHtmlTempl',
           'PHPTempl',
           )

from ulib.base.uio import _u
from ulib.base.lines import Lines
from base import FileTempl, register_templ

class CssTempl(FileTempl):
    NAME = 'css'
    EXT = '.css'

    MODELINE_PREFIX = u"/* "
    MODELINE_SUFFIX = u"\n*/"

    def get_lines(self):
        lines = Lines()
        self.appendtext(lines, self.modeline)
        self.appendtext(lines, u"""@CHARSET "%(encoding)s";""")
        return lines

class JsTempl(FileTempl):
    NAME = ('javascript', 'js')
    EXT = '.js'

    MODELINE_PREFIX = u"/* "
    MODELINE_SUFFIX = u"\n*/"

    def get_lines(self):
        return self.appendtext(None, self.modeline)

class HtmlTempl(FileTempl):
    NAME = ('html4', 'html')
    EXT = ('.html', '.htm')

    titlehl = 'h1'
    head_title = None
    title = None

    baseurl = ''
    bp = True
    bpurl = '/blueprint'

    PROPERTIES, ALIASES, OPTIONS = \
        FileTempl.DECL_P(FileTempl, 'titlehl=', 'head_title=', 'title=', 'baseurl=', 'bp', 'nobp', 'bpurl=',
                         titlehl=dict(initial=titlehl),
                         head_title=dict(initial=head_title),
                         title=dict(initial=title, aliases='T'),
                         baseurl=dict(initial=baseurl),
                         bp=dict(initial=bp, value=True),
                         nobp=dict(varname='bp', value=False),
                         bpurl=dict(initial=bpurl),
                         )

    def fix_properties(self):
        if self.head_title is None:
            self.head_title = self.title
        FileTempl.fix_properties(self)

    MODE = u"html"
    MODELINE_PREFIX = u"<!-- "
    MODELINE_SUFFIX = u"\n-->"

    DOCTYPE = u"""<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">"""
    PROLOG0 = u"""\
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=%(encoding)s" />
<link rel="shortcut icon" href="%(baseurl)s/favicon.ico" />"""
    BLUEPRINT = u"""\
<link rel="stylesheet" href="%(bpurl)s/screen.css" type="text/css" media="screen, projection" />
<link rel="stylesheet" href="%(bpurl)s/print.css" type="text/css" media="print" />
<!--[if lt IE 8]>
<link rel="stylesheet" href="%(bpurl)s/ie.css" type="text/css" media="screen, projection" />
<![endif]-->"""
    PROLOG1 = u"""\
<title>%(head_title)s</title>
</head>
<body>"""
    BP_PROLOG = u"""<div class="container">"""
    BP_EPILOG = u"""</div>"""
    EPILOG = u"""</body>
</html>"""

    def get_lines(self):
        lines = Lines()
        self.appendtext(lines, self.DOCTYPE)
        self.appendtext(lines, self.get_modeline(nl=False))
        self.appendtext(lines, self.PROLOG0)
        if self.bp: self.appendtext(lines, self.BLUEPRINT)
        self.appendtext(lines, self.PROLOG1)
        if self.bp: self.appendtext(lines, self.BP_PROLOG)
        if self.title is not None:
            self.appendtext(lines, u"<%(titlehl)s>%(title)s</%(titlehl)s>")
        if self.bp: self.appendtext(lines, self.BP_EPILOG)
        self.appendtext(lines, self.EPILOG)
        return lines

class _JavadocHtmlTempl(HtmlTempl):
    bp = False

    PROPERTIES, ALIASES, OPTIONS = \
        HtmlTempl.DECL_P(HtmlTempl, 'bp', 'nobp',
                         bp=dict(initial=bp, value=True),
                         nobp=dict(varname='bp', value=False),
                         )

    def fix_properties(self):
        if self.title is not None and not self.title.endswith('.'):
            # S'assurer que le titre se termine par un point
            self.title += u'.'
        HtmlTempl.fix_properties(self)

class OverviewHtmlTempl(_JavadocHtmlTempl):
    NAME = 'overview.html'
    FILENAME = 'overview.html'

    titlehl = 'h2'
    project = None

    PROPERTIES, ALIASES, OPTIONS = \
        _JavadocHtmlTempl.DECL_P(_JavadocHtmlTempl, 'project=',
                                 project=dict(initial=project, aliases='P'),
                                 )

    def fix_properties(self):
        if self.title is None and self.project is not None:
            self.title = u"Projet: %s" % _u(self.project)
        _JavadocHtmlTempl.fix_properties(self)

class PackageHtmlTempl(_JavadocHtmlTempl):
    NAME = 'package.html'
    FILENAME = 'package.html'

    titlehl = 'h2'
    package = None

    PROPERTIES, ALIASES, OPTIONS = \
        _JavadocHtmlTempl.DECL_P(_JavadocHtmlTempl, 'package=',
                                 package=dict(initial=package, aliases='P'),
                                 )

    def fix_properties(self):
        if self.title is None and self.package is not None:
            self.title = u"Package: %s" % _u(self.package)
        _JavadocHtmlTempl.fix_properties(self)

class PHPTempl(HtmlTempl):
    NAME = 'php'
    EXT = ('.php', '.phps')

    MODE = 'php'
    MODELINE = u"-*- coding: %(encoding)s%(mode)s -*- vim:sw=2:sts=2:et:ai:si:sta:fenc=%(encoding)s"
    MODELINE_PREFIX = u"<?php #"
    MODELINE_SUFFIX = u"\n?>"

    def get_lines(self):
        lines = Lines()
        self.appendtext(lines, self.get_modeline(nl=False) + self.DOCTYPE)
        self.appendtext(lines, self.PROLOG0)
        if self.bp: self.appendtext(lines, self.BLUEPRINT)
        self.appendtext(lines, self.PROLOG1)
        if self.bp: self.appendtext(lines, self.BP_PROLOG)
        if self.title is not None:
            self.appendtext(lines, u"<%(titlehl)s>%(title)s</%(titlehl)s>")
        if self.bp: self.appendtext(lines, self.BP_EPILOG)
        self.appendtext(lines, self.EPILOG)
        return lines

class CakePHPCtpTempl(FileTempl):
    NAME = ('CakePHP ctp', 'cakephpctp', 'cakectp', 'ctp')
    EXT = ('.ctp', )

    MODE = u"php"
    MODELINE = u"-*- coding: %(encoding)s%(mode)s -*- vim:sw=2:sts=2:et:ai:si:sta:fenc=%(encoding)s"
    MODELINE_PREFIX = u"<?php #"
    MODELINE_SUFFIX = u"\n?>"

    def get_lines(self):
        lines = Lines()
        self.appendtext(lines, self.get_modeline(nl=False))
        return lines

register_templ(
    CssTempl, JsTempl,
    HtmlTempl, OverviewHtmlTempl, PackageHtmlTempl,
    PHPTempl,
    CakePHPCtpTempl,
    )
