Exemple #1
0
def generate_base64_file(path, filename, mode):
    if mode is None:
        mode = "css"
    else:
        mode = mode.lower()
    #declare pics traverser
    t = Traversers.PicsTraverser()

    pics = t.get_files(path)
    l = len(pics)
    if (l == 0):
        print("*" * 70)
        print("!!! no pictures found inside {}".format(path))
        print("*" * 70)
        return
    f = []

    for p in pics:
        # get extension
        ext = os.path.splitext(p)[1]
        name = os.path.basename(p)
        print("...processing picture {}".format(name))

        if mode == "css":
            f.append("/* picture: {} */".format(name))
            f.append(".{} {}".format(name.replace(".", "-"), "{"))

            # picture:
            with open(p, "rb") as image_file:
                b = base64.b64encode(image_file.read())
                # decode b to utf-8 to obtain a string from bytes
                f.append(
                    "\tbackground-image: url(\"data:{0};base64,{1}\");".format(
                        get_descriptor_by_extension(ext), b.decode("utf-8")))

            f.append("}")
        elif mode == "csv":
            with open(p, "rb") as image_file:
                b = base64.b64encode(image_file.read())
                # decode b to utf-8 to obtain a string from bytes
                f.append("{},{}".format(name, b.decode("utf-8")))

    #save css file
    code = "\n".join(f)
    outputPath = os.path.join(path, filename) + "." + mode
    print("...saving file to {}".format(outputPath))
    Scribe.write(code, outputPath)
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)
Exemple #3
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)
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)