コード例 #1
0
def load_resources_config():
    """
    Loads the resources configuration.
    """
    try:
        base_path = path.abspath(path.join(path.dirname(__file__), pardir))
        scripts_config = path.join(base_path, "configuration", "scripts.js")
        contents = Scribe.read(scripts_config)
        # extract the information that we care about
        contents = remove_comments(contents).replace("module.exports = {", "{")
        contents = Text.remove_line_breaks(contents)
        contents = re.sub(";\s*$", "", contents)
        data = json.loads(contents)
        return data
    except Exception as ex:
        print("ERROR: while loading the scripts configuration for the resources helper.")
        print(str(ex))
        raise
コード例 #2
0
def load_resources_config():
    """
    Loads the resources configuration.
    :rtype : object
    """
    try:
        base_path = path.abspath(path.join(path.dirname(__file__), pardir))
        scripts_config = path.join(base_path, "configuration", "scripts.js")
        contents = Scribe.read(scripts_config)
        # extract the information that we care about
        contents = remove_comments(contents).replace("module.exports = {", "{")
        contents = Text.remove_line_breaks(contents)
        contents = re.sub(";\s*$", "", contents)
        data = json.loads(contents)
        return data
    except Exception as ex:
        print("ERROR: while loading the scripts configuration for the resources helper.")
        print(ex.message)
        raise
コード例 #3
0
def resources(names, conf=None):
    """
        Defines an helper function to generate links to scripts elements, by set names.
        1. it reads the file /configuration/scripts.js to generate the required script elements.
        2. if bundling is enabled, a single script element per set is generated, for bundled files.
        3. if also minification is enabled, a single script element per set is generated, for minified files.
        4. the same configuration file is read by Grunt.js to generate bundled and minified scripts upon publishing.
    """
    global CONFIGURATION

    if Text.isstring(names):
        names = [names]

    if conf is None:
        if CONFIGURATION is None:
            CONFIGURATION = load_resources_config()
        conf = CONFIGURATION

    bundling = conf["bundling"]
    minification = conf["minification"]
    sets = conf["sets"]

    a = []
    for name in names:
        if not name in sets:
            raise Exception(
                "The set `{}` is not configured inside /configuration/scripts.js"
                .format(name))
        if minification:
            a.append("<script src=\"/scripts/{}{}\"></script>".format(
                name, ".min.js"))
        elif bundling:
            a.append("<script src=\"/scripts/{}{}\"></script>".format(
                name, ".built.js"))
        else:
            files = sets[name]
            for f in files:
                a.append("<script src=\"{}\"></script>".format(f))

    return "\n".join(a)
コード例 #4
0
def resources(names, conf=None):
    """
        Defines an helper function to generate links to scripts elements, by set names.
        1. it reads the file /configuration/scripts.js to generate the required script elements.
        2. if bundling is enabled, a single script element per set is generated, for bundled files.
        3. if also minification is enabled, a single script element per set is generated, for minified files.
        4. the same configuration file is read by Grunt.js to generate bundled and minified scripts upon publishing.
    """
    global CONFIGURATION

    if Text.isstring(names):
        names = [names]

    if conf is None:
        if CONFIGURATION is None:
            CONFIGURATION = load_resources_config()
        conf = CONFIGURATION

    bundling = conf["bundling"]
    minification = conf["minification"]
    sets = conf["sets"]

    a = []
    for name in names:
        if not name in sets:
            raise Exception("The set `{}` is not configured inside /configuration/scripts.js".format(name))
        if minification:
            a.append('<script src="/scripts/{}{}"></script>'.format(name, ".min.js"))
        elif bundling:
            a.append('<script src="/scripts/{}{}"></script>'.format(name, ".built.js"))
        else:
            files = sets[name]
            for f in files:
                a.append('<script src="{}"></script>'.format(f))

    return "\n".join(a)
コード例 #5
0
def get_templates_for_angular(path, all_html_files, appname, underscore_js_compile, comment):
    """
        Creates templates.js files for AngularJs
    """
    f = []
    pat = r"<!--template=\"([a-zA-Z0-9\\-]+)\"-->"
    f.append("//")
    f.append("//Knight generated templates file.")
    if comment is not None:
        f.append("// * ")
        f.append("// * " + comment)
        f.append("// * ")
    f.append("//")
    f.append("(function () {")

    f.append("  var o = {")
    k = len(all_html_files)
    i = 0
    for h in all_html_files:
        i += 1
        # get file content
        txt = Scribe.read(h)

        # check if the rx matches the contents
        m = re.search(pat, txt)
        if m:
            # get the template name from the group
            name = m.group(1)
            # remove the template name comment
            txt = re.sub(pat, "", txt)
        else:
            # get the filename with extension
            name = os.path.basename(h)
            # remove extension
            name = os.path.splitext(name)[0]

        # escape single quotes
        txt = re.sub("'", "\\'", txt)
        # condensate
        txt = Text.condensate(txt)

        f.append("    \'{0}\': \'{1}\'{2}".format(name, txt, "," if i < k else ""))
        
    f.append("  };")

    f.append("  var f = function(a) {")
    if underscore_js_compile is None or underscore_js_compile == "":
        #plain templates
        f.append("    var x;")
        f.append("    for (x in o) {")
        f.append("      a.put(x, o[x]);")
        f.append("    }")
    else:
        #templates run into UnderscoreJs template function
        f.append("    var ctx = {};".format(underscore_js_compile))
        f.append("    _.each(o, function (v, k) {")
        f.append("      a.put(k, _.template(v, ctx));")
        f.append("    });")

    f.append("  };")
    f.append("  f.$inject = ['$templateCache'];")
    f.append("  {0}.run(f);".format(appname))
    f.append("})();")

    code = "\n".join(f)
    # save templates.js
    outputPath = os.path.join(path, "templates.js")
    print("...saving file {}".format(outputPath))
    Scribe.write(code, outputPath)
コード例 #6
0
def get_templates_for_angular(path, all_html_files, appname,
                              underscore_js_compile, comment):
    """
        Creates templates.js files for AngularJs
    """
    f = []
    pat = r"<!--template=\"([a-zA-Z0-9\\-]+)\"-->"
    f.append("//")
    f.append("//Knight generated templates file.")
    if comment is not None:
        f.append("// * ")
        f.append("// * " + comment)
        f.append("// * ")
    f.append("//")
    f.append("(function () {")

    f.append("  var o = {")
    k = len(all_html_files)
    i = 0
    for h in all_html_files:
        i += 1
        # get file content
        txt = Scribe.read(h)

        # check if the rx matches the contents
        m = re.search(pat, txt)
        if m:
            # get the template name from the group
            name = m.group(1)
            # remove the template name comment
            txt = re.sub(pat, "", txt)
        else:
            # get the filename with extension
            name = os.path.basename(h)
            # remove extension
            name = os.path.splitext(name)[0]

        # escape single quotes
        txt = re.sub("'", "\\'", txt)
        # condensate
        txt = Text.condensate(txt)

        f.append("    \'{0}\': \'{1}\'{2}".format(name, txt,
                                                  "," if i < k else ""))

    f.append("  };")

    f.append("  var f = function(a) {")
    if underscore_js_compile is None or underscore_js_compile == "":
        #plain templates
        f.append("    var x;")
        f.append("    for (x in o) {")
        f.append("      a.put(x, o[x]);")
        f.append("    }")
    else:
        #templates run into UnderscoreJs template function
        f.append("    var ctx = {};".format(underscore_js_compile))
        f.append("    _.each(o, function (v, k) {")
        f.append("      a.put(k, _.template(v, ctx));")
        f.append("    });")

    f.append("  };")
    f.append("  f.$inject = ['$templateCache'];")
    f.append("  {0}.run(f);".format(appname))
    f.append("})();")

    code = "\n".join(f)
    # save templates.js
    outputPath = os.path.join(path, "templates.js")
    print("...saving file {}".format(outputPath))
    Scribe.write(code, outputPath)
コード例 #7
-1
def get_templates(path, all_html_files, underscore_js_compile, templates_variable, comment):
    """
        Creates templates.js files for KnockOut
    """
    f = []
    pat = r"<!--template=\"([a-zA-Z0-9\\-]+)\"-->"
    f.append("//")
    f.append("//Knight generated templates file.")
    if comment is not None:
        f.append("// * ")
        f.append("// * " + comment)
        f.append("// * ")
    f.append("//")
    f.append("if (!" + templates_variable + ") " + templates_variable + " = {};")
    f.append("(function (templates) {")

    f.append("  var o = {")

    k = len(all_html_files)
    i = 0
    for h in all_html_files:
        i += 1
        # get file content
        txt = Scribe.read(h)

        # check if the rx matches the contents
        m = re.search(pat, txt)
        if m:
            # get the template name from the group
            name = m.group(1)
            # remove the template name comment
            txt = re.sub(pat, "", txt)
        else:
            # get the filename with extension
            name = os.path.basename(h)
            # remove extension
            name = os.path.splitext(name)[0]

        # escape single quotes
        txt = re.sub("'", "\\'", txt)

        # condensate
        txt = Text.condensate(txt)

        f.append("    '{}': '{}'{}".format(name, txt, "," if i < k else ""))

    f.append("  };")

    if underscore_js_compile is None or underscore_js_compile == "":
        # plain templates
        f.append("  var x;")
        f.append("  for (x in o) {")
        f.append("    templates[x] = o[x];")
        f.append("  }")
    else:
        # templates run into UnderscoreJs template function
        f.append("  var ctx = {};".format(underscore_js_compile))
        f.append("  _.each(o, function (v, k) {")
        f.append("    x[k] = _.template(v, ctx);")
        f.append("  });")

    f.append("})(" + templates_variable + ");")

    code = "\n".join(f)

    # save templates.js
    outputPath = os.path.join(path, "templates.js")
    print("...saving file {}".format(outputPath))
    Scribe.write(code, outputPath)