Beispiel #1
0
    def setup_md_to_html_converter(self):
        """Create Markdown converter.

        The converter is created with custom processors, html templates,
        and extensions.
        """
        templates = self.load_template_files()
        extensions = [
            "markdown.extensions.fenced_code",
            "markdown.extensions.codehilite", "markdown.extensions.sane_lists",
            "markdown.extensions.tables",
            mdx_math.MathExtension()
        ]
        self.converter = Verto(html_templates=templates, extensions=extensions)
    def setup_md_to_html_converter(self):
        """Create Markdown converter.

        The converter is created with custom processors, html templates,
        and extensions.
        """
        templates = self.load_template_files()
        extensions = [
            "markdown.extensions.fenced_code",
            "markdown.extensions.codehilite", "markdown.extensions.sane_lists",
            "markdown.extensions.tables",
            mdx_math.MathExtension()
        ]
        settings = {
            "add_default_interactive_thumbnails_to_required_files": False,
        }
        self.converter = Verto(html_templates=templates,
                               extensions=extensions,
                               custom_settings=settings)
Beispiel #3
0
    def convert_md_file(self,
                        md_file_path,
                        config_file_path,
                        heading_required=True,
                        remove_title=True):
        """Return the Verto object for a given Markdown file.

        Args:
            md_file_path: Location of Markdown file to convert (str).
            config_file_path: Path to related the config file (str).
            heading_required: Boolean if the file requires a heading (bool).
            remove_title: Boolean if the file's first heading should be removed (bool).

        Returns:
            VertoResult object

        Raises:
            CouldNotFindMarkdownFileError: when a given Markdown file cannot be found.
            NoHeadingFoundInMarkdownFileError: when no heading can be found in a given
                Markdown file.
            EmptyMarkdownFileError: when no content can be found in a given Markdown
                file.
            VertoConversionError: when a verto StyleError is thrown.
        """
        try:
            # Check file exists
            content = open(md_file_path, encoding="UTF-8").read()
        except FileNotFoundError:
            raise CouldNotFindMarkdownFileError(md_file_path, config_file_path)
        """ Below is a hack to make the image-inline tag not require alt text to be
            given when the language is not in English.
            TODO: Remove this hack once translations are complete.
        """
        directories = md_file_path.split('/')
        if 'en' not in directories:
            custom_argument_rules = {
                "image-container": {
                    "alt": False
                },
                "image-inline": {
                    "alt": False
                },
                "image-tag": {
                    "alt": False
                }
            }
        else:
            custom_argument_rules = {
                "image-container": {
                    "alt": True
                },
                "image-inline": {
                    "alt": True
                },
                "image-tag": {
                    "alt": True
                }
            }

        custom_processors = self.converter.processor_defaults()
        if remove_title:
            custom_processors.add("remove-title")

        templates = self.load_template_files()
        extensions = [
            "markdown.extensions.fenced_code",
            "markdown.extensions.codehilite", "markdown.extensions.sane_lists",
            "markdown.extensions.tables",
            mdx_math.MathExtension()
        ]
        self.converter = Verto(html_templates=templates,
                               extensions=extensions,
                               custom_argument_rules=custom_argument_rules,
                               processors=custom_processors)
        """ End of hack. """
        result = None
        try:
            result = self.converter.convert(content)
        except VertoError as e:
            raise VertoConversionError(md_file_path, e) from e

        if heading_required:
            if result.title is None:
                raise NoHeadingFoundInMarkdownFileError(md_file_path)

        if len(result.html_string) == 0:
            raise EmptyMarkdownFileError(md_file_path)

        if not self.lite_loader:
            check_converter_required_files(result.required_files, md_file_path)
            check_converter_glossary_links(result.required_glossary_terms,
                                           md_file_path)
        return result