<commit_before>#
# Jasy - Web Tooling Framework
# Copyright 2014 <NAME>
#

import os

from jasy import UserError

import jasy.item.Abstract as AbstractItem
import jasy.item.Class as ClassItem

def templateFilter(text, item):
    js = """
        core.Main.declareNamespace("%(name)s", core.template.Compiler.compile("%(content)s"));
    """ % {
        "name": item.getId(),
        "content" : escapeContent(text)
    }
    return js


def escapeContent(content):
    return content.replace("\"", "\\\"").replace("\n", "\\n")


class TemplateItem(AbstractItem.AbstractItem):

    kind = "jasy.Template"

    def generateId(self, relpath, package):
        """
        Generates the fileId of this item as being used by other modules
        """

        if package:
            fileId = "%s/" % package
        else:
            fileId = ""

        return (fileId + os.path.splitext(relpath)[0]).replace("/", ".")


    def attach(self, path):
        result = super().attach(path)

        # Force adding an matching class item to the registry
        self.getClassItem()

        return result


    def getClassItem(self):
        """
        Returns a class representation for the template instance
        """

        classId = self.getId() + "Template"

        session = self.project.getSession()
        virtualProject = session.getVirtualProject()
        classItem = virtualProject.getItem("jasy.Class", classId)

        if classItem is None:
            classItem = ClassItem.ClassItem(virtualProject, classId)
            classItem.setTextFilter(templateFilter)

            virtualProject.addItem("jasy.Class", classItem)

        # Be sure that class item is up-to-date
        if classItem.mtime != self.mtime:
            classItem.mtime = self.mtime
            classItem.setText(self.getText())

        return classItem<commit_msg>Using new API to attach a valid path to dynamically created class items<commit_after>#
# Jasy - Web Tooling Framework
# Copyright 2014 <NAME>
#

import os

from jasy import UserError

import jasy.item.Abstract as AbstractItem
import jasy.item.Class as ClassItem

def templateFilter(text, item):
    js = """
        core.Main.declareNamespace("%(name)s", core.template.Compiler.compile("%(content)s"));
    """ % {
        "name": item.getId(),
        "content" : escapeContent(text)
    }
    return js


def escapeContent(content):
    return content.replace("\"", "\\\"").replace("\n", "\\n")


class TemplateItem(AbstractItem.AbstractItem):

    kind = "jasy.Template"

    def generateId(self, relpath, package):
        """
        Generates the fileId of this item as being used by other modules
        """

        if package:
            fileId = "%s/" % package
        else:
            fileId = ""

        return (fileId + os.path.splitext(relpath)[0]).replace("/", ".")


    def attach(self, path):
        result = super().attach(path)

        # Force adding an matching class item to the registry
        self.getClassItem()

        return result


    def getClassItem(self):
        """
        Returns a class representation for the template instance
        """

        classId = self.getId() + "Template"

        session = self.project.getSession()
        virtualProject = session.getVirtualProject()
        classItem = virtualProject.getItem("jasy.Class", classId)

        if classItem is None:
            classItem = ClassItem.ClassItem(virtualProject, classId)
            classItem.setTextFilter(templateFilter)
            classItem.setPath(session.getVirtualFilePathFromId(classId, ".js"))

            virtualProject.addItem("jasy.Class", classItem)

        # Be sure that class item is up-to-date
        if classItem.mtime != self.mtime:
            classItem.mtime = self.mtime
            classItem.setText(self.getText())

        return classItem
{'fraction_non_alphanumeric': 0.11596463858554343, 'fraction_numerical': 0.0026001040041601664, 'mean_word_length': 2.41651865008881, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5247627', 'n_tokens_mistral': 1086, 'n_tokens_neox': 1038, 'n_words': 277}