#!/usr/bin/python3

# Copyright (c) 2011, Caio Filippo Corro
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the <organization> nor the
#       names of its contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import os, sys
import xml.etree.ElementTree as etree

# create and return node :
# <IncludeRules context="FindPHP" />
def create_node_include_twig_rules():
    node = etree.Element("IncludeRules")
    node.attrib['context'] = 'FindTwig'
    return node


# create and return node :
# <Detect2Chars char="?" char1="?" context="?" lookAhead="true" />
def create_node_detect2chars_rule(char1, char2, context, lookAhead):
    node = etree.Element("Detect2Chars")
    node.attrib['char'] = char1;
    node.attrib['char1'] = char2;
    node.attrib['context'] = context;
    if (lookAhead):
        node.attrib['lookAhead'] = "true"
    return node


# create and return node :
#<context name="FindTwig">
#    <Detect2Chars char="{" char1="#" context="##Twig" lookAhead="true" />
#    <Detect2Chars char="{" char1="%" context="##Twig" lookAhead="true" />
#    <Detect2Chars char="{" char1="{" context="##Twig" lookAhead="true" />
#</context>
def create_node_find_twig_rules():
    node = etree.Element("context")
    node.attrib['name'] = "FindTwig"
    node.append(create_node_detect2chars_rule("{", "#", "##Twig", True))
    node.append(create_node_detect2chars_rule("{", "%", "##Twig", True))
    node.append(create_node_detect2chars_rule("{", "{", "##Twig", True))
    return node


# display help and quit
def display_help():
    print()
    print('Twig (HTML) syntaxe hightlight for Katepart generator')
    print()
    print('Usage :')
    print(sys.argv[0] + ' [html.xml] [output.xml]')
    print(' - html.xml : the html hightlight syntax')
    print(' - output.xml : path where the html + twig hightlight syntax must be saved')
    print()
    exit()
    
    
if __name__ == '__main__':
    if len(sys.argv) != 3:
        display_help()
    
    path_input = sys.argv[1]
    path_output = sys.argv[2]
    
    tree = etree.parse(path_input)
    root = tree.getroot()

    root.attrib['extensions'] = "*.twig;*.html.twig"
    root.attrib['name'] = "Twig (HTML)"

    for context in root.findall('.//context'):
        context.insert(0, create_node_include_twig_rules())

    root.find('.//contexts').insert(1, create_node_find_twig_rules())


    out = open('path_output', mode='w', encoding='utf-8')

    out.write('<?xml version="1.0" encoding="UTF-8"?>\n')
    out.write('<!DOCTYPE language SYSTEM "language.dtd">\n')
    out.write('\n')
    out.write('<!-- This file has been generated by a script - do not edit -->\n')
    out.write('\n')

    out.write(etree.tostring(root).decode("utf-8"))

    out.close()