# -*- coding: utf-8 -*-
#
# Copyright 2013 Mark Lee
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from ._compat import _iterify
from itertools import chain
import re

OPTIONAL_SUBS = {
    # Google Analytics ID in config
    'google_analytics': [(re.compile(u"'UA-XXXXX-X'"),
                          u'{{ config.GOOGLE_ANALYTICS_ID }}')],
    # Remove charset meta tag (use charset in Content-Type header instead)
    'no_charset_tag': [(re.compile(u' +.*charset="utf-8".*>\n'), u'')],
    # Remove IE compat tag (use header instead)
    'no_compat_tag': [(re.compile(u' +.*X-UA-Compatible.*>\n'), u'')],
    # Make it obvious to Vim that it's a Jinja file
    'vim': [(re.compile(u'(<!DOCTYPE html>)$'),
             u'\\1 {#- vim: set ft=jinja: #}')],
    # webassets support
    'webassets': [
        # CSS
        (re.compile(u'''\
(?:(\\s+)(<link rel="stylesheet" href=")[^{].*?(">\n))+'''),
         u'''
\\1{% assets 'css_all' -%}
\\1\\2{{ ASSET_URL }}\\3\\1{% endassets -%}
'''),
        # JS
        (re.compile(u'''\
(<body>.*?)(?:(\s+)(<script src=")[^{/].*?("></script>)\n)+''',
                    re.DOTALL),
         u'''\\1
\\2{% assets 'js_all' -%}
\\2\\3{{ ASSET_URL }}\\4
\\2{% endassets -%}
'''),
    ],
}

SUBS = [
    # title
    (re.compile(u'(<(title>))(</\\2)', re.MULTILINE),
     u'\\1{% block title %}{% endblock %}\\3'),
    # HTML comments -> Jinja comments
    (re.compile(u'<!-- (.*?) -->', re.MULTILINE), u'{# \\1 #}'),
    # meta description
    (re.compile(u'(<meta name="description" content=")(">)'),
     u'{% if meta_description %}\\1{{ meta_description }}\\2{% endif %}'),
    # content
    (re.compile(u'<p>.*?</p>'), u'{% block content %}{% endblock %}'),
]


def parse_template(tpl_obj, **kwargs):
    tpl = tpl_obj.read()
    subs = SUBS + list(chain(*[s for k, s in _iterify(OPTIONAL_SUBS)
                               if kwargs.get(k)]))
    for regex, repl in subs:
        tpl = regex.sub(repl, tpl)
    return tpl

@@ -35,16 +35,22 @@
 ght 2013
+, 2014
  Mark Le
@@ -598,16 +598,57 @@
 cense.%0A%0A
+from __future__ import unicode_literals%0A%0A
 from ._c
@@ -799,17 +799,16 @@
 compile(
-u
 %22'UA-XXX
@@ -842,17 +842,16 @@
         
-u
 '%7B%7B conf
@@ -986,25 +986,24 @@
 (re.compile(
-u
 ' +.*charset
@@ -1011,33 +1011,32 @@
 %22utf-8%22.*%3E%5Cn'), 
-u
 '')%5D,%0A    # Remo
@@ -1107,17 +1107,16 @@
 compile(
-u
 ' +.*X-U
@@ -1136,17 +1136,16 @@
 *%3E%5Cn'), 
-u
 '')%5D,%0A  
@@ -1218,17 +1218,16 @@
 compile(
-u
 '(%3C!DOCT
@@ -1250,25 +1250,24 @@
             
-u
 '%5C%5C1 %7B#- vim
@@ -1362,25 +1362,24 @@
 (re.compile(
-u
 '''%5C%0A(?:(%5C%5Cs
@@ -1441,17 +1441,16 @@
         
-u
 '''%0A%5C%5C1%7B
@@ -1555,17 +1555,16 @@
 compile(
-u
 '''%5C%0A(%3Cb
@@ -1665,17 +1665,16 @@
         
-u
 '''%5C%5C1%0A%5C
@@ -1796,17 +1796,16 @@
 compile(
-u
 '(%3C(titl
@@ -1838,17 +1838,16 @@
 ),%0A     
-u
 '%5C%5C1%7B%25 b
@@ -1934,17 +1934,16 @@
 compile(
-u
 '%3C!-- (.
@@ -1967,17 +1967,16 @@
 ILINE), 
-u
 '%7B# %5C%5C1 
@@ -2020,17 +2020,16 @@
 compile(
-u
 '(%3Cmeta 
@@ -2070,17 +2070,16 @@
 ),%0A     
-u
 '%7B%25 if m
@@ -2169,17 +2169,16 @@
 compile(
-u
 '%3Cp%3E.*?%3C
@@ -2184,17 +2184,16 @@
 %3C/p%3E'), 
-u
 '%7B%25 bloc

Use unicode_literals