from django import template

from ..conf import conf
from ..loading import find, ICanHazTemplateNotFound



register = template.Library()



class ICanHazNode(template.Node):
    def __init__(self, name, classes="''"):
        self.name = template.Variable(name)
        self.classes = template.Variable(classes)


    def render(self, context):
        name = self.name.resolve(context)
        classes = self.classes.resolve(context)

        try:
            filepath = find(name)
            fp = open(filepath, "r")
            output = fp.read()
            fp.close()
            output = ('<script id="%s" class="%s" type="text/html">\n'
                      % (name, classes)) + output + "\n</script>\n"
        except (IOError, ICanHazTemplateNotFound):
            output = ""
            if conf.DEBUG:
                raise

        return output



@register.tag
def icanhaz(parser, token):
    """
    Finds the ICanHaz template for the given name and renders it surrounded by
    the requisite ICanHaz <script> tags.

    """
    bits = token.contents.split()
    if len(bits) not in [2, 3]:
        raise template.TemplateSyntaxError(
            "'icanhaz' tag takes one argument: the name/id of the template")
    return ICanHazNode(bits[1])

@register.tag
def icanhaz_partial(parser, token):
    """
    Finds the ICanHaz template partial for the given name and renders it
    surrounded by the requisite ICanHaz <script> tags.

    """
    bits = token.contents.split()
    if len(bits) not in [2, 3]:
        raise template.TemplateSyntaxError(
            "'icanhaz_partial' tag takes one argument: the name/id of the template")
    return ICanHazNode(bits[1], "'partial'")

@@ -199,25 +199,11 @@
 name
-, classes=%22''%22
 ):%0A
+
     
@@ -246,58 +246,8 @@
 me)%0A
-        self.classes = template.Variable(classes)%0A
 %0A%0A  
@@ -320,56 +320,8 @@
 ext)
-%0A        classes = self.classes.resolve(context)
 %0A%0A  
@@ -498,19 +498,8 @@
 %22%25s%22
- class=%22%25s%22
  typ
@@ -544,23 +544,12 @@
   %25 
-(
 name
-, classes)
 ) + 
@@ -1081,32 +1081,32 @@
  the template%22)%0A
+
     return ICanH
@@ -1125,440 +1125,4 @@
 1%5D)%0A
-%0A@register.tag%0Adef icanhaz_partial(parser, token):%0A    %22%22%22%0A    Finds the ICanHaz template partial for the given name and renders it%0A    surrounded by the requisite ICanHaz %3Cscript%3E tags.%0A%0A    %22%22%22%0A    bits = token.contents.split()%0A    if len(bits) not in %5B2, 3%5D:%0A        raise template.TemplateSyntaxError(%0A            %22'icanhaz_partial' tag takes one argument: the name/id of the template%22)%0A    return ICanHazNode(bits%5B1%5D, %22'partial'%22)%0A

Revert "Add an icanhaz_partial template tag", because ICanHaz now makes all templates available as partials in the 'module' branch, as of the commit at https://github.com/andyet/ICanHaz.js/commit/851f0e6e21b112d98b7945bba942691382748cf1