Example #1
0
 def handleMatch(self, match):
     el = super(ImageDownloadPattern, self).handleMatch(match)
     urlparts = urllib.parse.urlparse(el.attrib["src"])
     if urlparts.netloc:
         response = requests.get(urlparts.geturl())
         response.raise_for_status()
         filename = rfc6266.parse_requests_response(response).filename_unsafe
         with open(os.path.join(settings.get("folder"), filename), "wb") as f:
             f.write(response.content)
             el.attrib["src"] = filename
         utilities.fix_image(os.path.join(settings.get("folder"), filename), settings.get("features")["width"])
     return el
Example #2
0
 def handleMatch(self, match):
     el = super(ImageDownloadPattern, self).handleMatch(match)
     urlparts = urllib.parse.urlparse(el.attrib["src"])
     if urlparts.netloc:
         response = requests.get(urlparts.geturl())
         response.raise_for_status()
         filename = rfc6266.parse_requests_response(
             response).filename_unsafe
         with open(os.path.join(settings.get("folder"), filename),
                   "wb") as f:
             f.write(response.content)
             el.attrib["src"] = filename
         utilities.fix_image(os.path.join(settings.get("folder"), filename),
                             settings.get("features")["width"])
     return el
Example #3
0
def generate(trello_list):
    articles = []

    pool = multiprocessing.Pool(processes=10)
    articles = pool.map(get_artice, trello_list)

    # Print status
    for number, (trello_card, article) in enumerate(zip(trello_list, articles), start=1):
        print("\n" + str(number) + ".", trello_card["name"] + ":")

        if article is None:
            print(" - Status:", colorama.Fore.RED + "Warning (no content)", colorama.Fore.RESET)
            continue

        images_str = ", ".join(map(lambda x: x["name"], article["images"]))
        print(" - Images:", colorama.Fore.CYAN + images_str, colorama.Fore.RESET)
        labels_str = ", ".join(article["labels"])
        print(" - Labels:", colorama.Fore.MAGENTA + labels_str, colorama.Fore.RESET)
        print(" - Status:", colorama.Fore.GREEN + "Ok", colorama.Fore.RESET)

    # Remove empty articles
    articles = list(filter(None, articles))

    # Create output folder
    if not os.path.exists(settings.get("folder")):
        os.makedirs(settings.get("folder"))

    # Generate html from articles
    html_section_template = Template(open(settings.get("template-section")).read())
    markdown_instance = markdown.Markdown(extensions=list(settings.get("extensions")),
                                          extension_configs=settings.get("extensions"), output_format="html5")
    html = ""
    for index, article in enumerate(articles):
        labels = ""
        if settings.get("features")["labels"]:
            labels = " ".join(article["labels"])

        open(os.path.join(settings.get("folder"), ".t2w-temp-" + str(index) + ".md"), "w").write(article["content"])
        article_html = markdown_instance.reset().convert(article["content"])
        html += html_section_template.substitute(content=article_html, labels=labels)

        if settings.get("features")["lines"] and "noline" not in article["labels"] and index != len(articles) - 1:
            line_html = markdown_instance.reset().convert("\n\n---\n\n")
            html += html_section_template.substitute(content=line_html, labels="")

    # Save images
    for article in articles:
        for image in article["images"]:
            image_filename = os.path.join(settings.get("folder"), image["name"])
            open(image_filename, "wb").write(image["content"])
            utilities.fix_image(image_filename, settings.get("features")["width"])

    # Generate CSS
    css_generated = ""
    for css_file in settings.get("css"):
        css_generated += open(css_file).read() + "\n\n"

    # Add generated Markdown to HTML template
    html_template = Template(open(settings.get("template")).read())
    html_generated = html_template.safe_substitute(title=settings.get("title"), content=html, css=css_generated)

    result_template = Template(html_generated)
    extra_args = {}
    if "markdown_smarttoc" in settings.get("extensions"):
        extra_args["toc"] = markdown_instance.toc
    result_generated = result_template.safe_substitute(title=settings.get("title"),
                                                       width=settings.get("features")["width"], **extra_args)

    # Run premailer
    if settings.get("features")["premailer"]:
        open(os.path.join(settings.get("folder"), ".t2w-temp-" + settings.get("basename") + "-orignal.html"),
             "w").write(result_generated)
        premail_instance = premailer.Premailer(result_generated, keep_style_tags=True)
        result_generated = premail_instance.transform()

    open(os.path.join(settings.get("folder"), settings.get("basename") + ".html"), "w").write(result_generated)

    print("\nPreview: file://" + urllib.request.pathname2url(
        os.path.abspath(os.path.join(settings.get("folder"), settings.get("basename") + ".html"))))
Example #4
0
def generate(trello_list):
    articles = []

    pool = multiprocessing.Pool(processes=10)
    articles = pool.map(get_artice, trello_list)

    # Print status
    for number, (trello_card, article) in enumerate(zip(trello_list, articles),
                                                    start=1):
        print("\n" + str(number) + ".", trello_card["name"] + ":")

        if article is None:
            print(" - Status:", colorama.Fore.RED + "Warning (no content)",
                  colorama.Fore.RESET)
            continue

        images_str = ", ".join(map(lambda x: x["name"], article["images"]))
        print(" - Images:", colorama.Fore.CYAN + images_str,
              colorama.Fore.RESET)
        labels_str = ", ".join(article["labels"])
        print(" - Labels:", colorama.Fore.MAGENTA + labels_str,
              colorama.Fore.RESET)
        print(" - Status:", colorama.Fore.GREEN + "Ok", colorama.Fore.RESET)

    # Remove empty articles
    articles = list(filter(None, articles))

    # Create output folder
    if not os.path.exists(settings.get("folder")):
        os.makedirs(settings.get("folder"))

    # Generate html from articles
    html_section_template = Template(
        open(settings.get("template-section")).read())
    markdown_instance = markdown.Markdown(
        extensions=list(settings.get("extensions")),
        extension_configs=settings.get("extensions"),
        output_format="html5")
    html = ""
    for index, article in enumerate(articles):
        labels = ""
        if settings.get("features")["labels"]:
            labels = " ".join(article["labels"])

        open(
            os.path.join(settings.get("folder"),
                         ".t2w-temp-" + str(index) + ".md"),
            "w").write(article["content"])
        article_html = markdown_instance.reset().convert(article["content"])
        html += html_section_template.substitute(content=article_html,
                                                 labels=labels)

        if settings.get("features")["lines"] and "noline" not in article[
                "labels"] and index != len(articles) - 1:
            line_html = markdown_instance.reset().convert("\n\n---\n\n")
            html += html_section_template.substitute(content=line_html,
                                                     labels="")

    # Save images
    for article in articles:
        for image in article["images"]:
            image_filename = os.path.join(settings.get("folder"),
                                          image["name"])
            open(image_filename, "wb").write(image["content"])
            utilities.fix_image(image_filename,
                                settings.get("features")["width"])

    # Generate CSS
    css_generated = ""
    for css_file in settings.get("css"):
        css_generated += open(css_file).read() + "\n\n"

    # Add generated Markdown to HTML template
    html_template = Template(open(settings.get("template")).read())
    html_generated = html_template.safe_substitute(title=settings.get("title"),
                                                   content=html,
                                                   css=css_generated)

    result_template = Template(html_generated)
    extra_args = {}
    if "markdown_smarttoc" in settings.get("extensions"):
        extra_args["toc"] = markdown_instance.toc
    result_generated = result_template.safe_substitute(
        title=settings.get("title"),
        width=settings.get("features")["width"],
        **extra_args)

    # Run premailer
    if settings.get("features")["premailer"]:
        open(
            os.path.join(
                settings.get("folder"),
                ".t2w-temp-" + settings.get("basename") + "-orignal.html"),
            "w").write(result_generated)
        premail_instance = premailer.Premailer(result_generated,
                                               keep_style_tags=True)
        result_generated = premail_instance.transform()

    open(
        os.path.join(settings.get("folder"),
                     settings.get("basename") + ".html"),
        "w").write(result_generated)

    print("\nPreview: file://" + urllib.request.pathname2url(
        os.path.abspath(
            os.path.join(settings.get("folder"),
                         settings.get("basename") + ".html"))))