Beispiel #1
0
 def get_json(self):
     """
     :return: raw JSON file
     :rtype: str
     """
     dct = export_content(self)
     data = json_writer.dumps(dct, indent=4, ensure_ascii=False)
     return data
Beispiel #2
0
 def get_json(self):
     """
     :return: raw JSON file
     :rtype: str
     """
     dct = export_content(self)
     data = json_handler.dumps(dct, indent=4, ensure_ascii=False)
     return data
Beispiel #3
0
def publish_use_manifest(db_object, base_dir,
                         versionable_content: VersionedContent):
    base_content = export_content(versionable_content, with_text=True)

    md, metadata, __ = render_markdown(
        base_content,
        disable_jsfiddle=not db_object.js_support,
        full_json=True,
        stats=True)
    publish_container_new(db_object, base_dir, versionable_content, md)
    return metadata.get("stats", {}).get("signs", 0)
Beispiel #4
0
    def publish(self, md_file_path, base_name, **kwargs):
        published_content_entity = self.get_published_content_entity(
            md_file_path)
        if published_content_entity.content.type == "OPINION" and not settings.ZDS_APP[
                "opinions"]["allow_pdf"]:
            logger.info("PDF not allowed for opinions")
            return
        gallery_pk = published_content_entity.content.gallery.pk
        depth_to_size_map = {
            1:
            "small",  # in fact this is an "empty" tutorial (i.e it is empty or has intro and/or conclusion)
            2: "small",
            3: "middle",
            4: "big",
        }
        public_versionned_source = published_content_entity.content.load_version(
            sha=published_content_entity.sha_public)
        base_directory = Path(base_name).parent
        image_dir = base_directory / "images"
        with contextlib.suppress(FileExistsError):
            image_dir.mkdir(parents=True)
        if (settings.MEDIA_ROOT / "galleries" / str(gallery_pk)).exists():
            for image in (settings.MEDIA_ROOT / "galleries" /
                          str(gallery_pk)).iterdir():
                with contextlib.suppress(OSError):
                    shutil.copy2(str(image.absolute()), str(image_dir))
        content_type = depth_to_size_map[
            public_versionned_source.get_tree_level()]
        if self.latex_classes:
            content_type += ", " + self.latex_classes
        title = published_content_entity.title()
        authors = [a.username for a in published_content_entity.authors.all()]

        licence = published_content_entity.content.licence.code
        licence_short = licence.replace("CC", "").strip().lower()
        licence_logo = licences.get(licence_short, False)
        if licence_logo:
            licence_url = f"https://creativecommons.org/licenses/{licence_short}/4.0/legalcode"
            # we need a specific case for CC-0 as it is "public-domain"
            if licence_logo == licences["0"]:
                licence_url = "https://creativecommons.org/publicdomain/zero/1.0/legalcode.fr"
        else:
            licence = str(_("Tous droits réservés"))
            licence_logo = licences["copyright"]
            licence_url = ""

        replacement_image_url = str(settings.MEDIA_ROOT.parent)
        if not replacement_image_url.endswith("/"):
            replacement_image_url += "/"
        replaced_media_url = settings.MEDIA_URL
        if replaced_media_url.startswith("/"):
            replaced_media_url = replaced_media_url[1:]
        exported = export_content(public_versionned_source,
                                  with_text=True,
                                  ready_to_publish_only=True)
        # no title to avoid zmd to put it on the final latex
        del exported["title"]
        content, metadata, messages = render_markdown(
            exported,
            output_format="texfile",
            # latex template arguments
            content_type=content_type,
            title=title,
            authors=authors,
            license=licence,
            license_directory=str(LICENSES_BASE_PATH),
            license_logo=licence_logo,
            license_url=licence_url,
            smileys_directory=str(SMILEYS_BASE_PATH / "svg"),
            images_download_dir=str(base_directory / "images"),
            local_url_to_local_path=["/", replacement_image_url],
            heading_shift=-1,
        )
        if content == "" and messages:
            raise FailureDuringPublication(
                f"Markdown was not parsed due to {messages}")
        zmd_class_dir_path = Path(
            settings.ZDS_APP["content"]["latex_template_repo"])
        content.replace(replacement_image_url + replaced_media_url,
                        replacement_image_url)
        if zmd_class_dir_path.exists() and zmd_class_dir_path.is_dir():
            with contextlib.suppress(FileExistsError):
                zmd_class_link = base_directory / "zmdocument.cls"
                zmd_class_link.symlink_to(zmd_class_dir_path /
                                          "zmdocument.cls")
                luatex_dir_link = base_directory / "utf8.lua"
                luatex_dir_link.symlink_to(zmd_class_dir_path / "utf8.lua",
                                           target_is_directory=True)
        true_latex_extension = ".".join(
            self.extension.split(".")[:-1]) + ".tex"
        latex_file_path = base_name + true_latex_extension
        pdf_file_path = base_name + self.extension
        default_logo_original_path = Path(
            __file__
        ).parent / ".." / ".." / "assets" / "images" / "*****@*****.**"
        with contextlib.suppress(FileExistsError):
            shutil.copy(str(default_logo_original_path),
                        str(base_directory / "default_logo.png"))
        with open(latex_file_path, mode="w", encoding="utf-8") as latex_file:
            latex_file.write(content)
        shutil.copy2(latex_file_path,
                     published_content_entity.get_extra_contents_directory())

        self.full_tex_compiler_call(latex_file_path, draftmode="-draftmode")
        self.full_tex_compiler_call(latex_file_path, draftmode="-draftmode")
        self.make_glossary(base_name.split("/")[-1], latex_file_path)
        self.full_tex_compiler_call(latex_file_path)

        shutil.copy2(pdf_file_path,
                     published_content_entity.get_extra_contents_directory())