# -*- coding: utf-8 -*- 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 ulib.base.input import is_yes
from base import FileTempl, register_templates

class CssTempl(FileTempl):
    TEMPLATE = ('css file', 'css')
    EXT = '.css'

    #MODE = u"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):
    TEMPLATE = ('javascript file', 'js file', 'js')
    EXT = '.js'

    #MODE = u"javascript"
    MODELINE_PREFIX = u"/* "
    MODELINE_SUFFIX = u"\n*/"

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

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

    VPS, ACPS, NCPS, ICPS = FileTempl.PS('titlehl', 'head_title', 'base', 'bpurl',
                                         ncps=dict(title=None),
                                         icps=dict(bp=is_yes))

    titlehl = 'h1'
    head_title = None
    title = None

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

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

    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):
    def fix_properties(self):
        HtmlTempl.fix_properties(self)
        if self.title and not self.title.endswith('.'):
            # S'assurer que le titre se termine par un point
            self.title += u'.'

class OverviewHtmlTempl(JavadocHtmlTempl):
    TEMPLATE = ('javadoc overview file', 'overview.html')
    FILENAME = 'overview.html'

    VPS, ACPS, NCPS, ICPS = JavadocHtmlTempl.PS(ncps=dict(project=None))

    titlehl = 'h2'
    project = None

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

class PackageHtmlTempl(JavadocHtmlTempl):
    TEMPLATE = ('javadoc package file', 'package.html')
    FILENAME = 'package.html'

    VPS, ACPS, NCPS, ICPS = JavadocHtmlTempl.PS(ncps=dict(package=None))

    titlehl = 'h2'
    package = None

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

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

    MODE = 'php'
    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

register_templates(CssTempl, JsTempl, HtmlTempl, OverviewHtmlTempl, PackageHtmlTempl, PHPTempl)
