Пример #1
0
    def parse(self, text):
        """Parses and renders a text as HTML regarding current format."""
        if self.format == 'markdown':
            try:
                import markdown
            except ImportError:
                raise RuntimeError(u"Looks like markdown is not installed")

            return markdown.markdown(text)
        elif self.format == 'restructuredtext':
            try:
                from landslide.rst import html_body
            except ImportError:
                raise RuntimeError(u"Looks like docutils are not installed")
            html = html_body(text, input_encoding=self.encoding)
            html = re.sub(r'<div.*?>', r'', html, re.UNICODE)
            html = re.sub(r'</div>', r'', html, re.UNICODE)
            html = re.sub(r'<p class="system-message-\w+">.*?</p>', r'', html,
                          re.UNICODE)
            html = re.sub(r'Document or section may not begin with a transition\.',
                          r'', html, re.UNICODE)
            html = re.sub(r'<h(\d+?).*?>', r'<h\1>', html,
                          re.DOTALL | re.UNICODE)
            html = re.sub(r'<hr.*?>\n', r'<hr />\n', html,
                          re.DOTALL | re.UNICODE)

            return html.strip()
        else:
            raise NotImplementedError(u"Unsupported format %s, cannot parse"
                                      % self.format)
Пример #2
0
    def parse(self, text):
        """Parses and renders a text as HTML regarding current format.
        """
        if self.format == 'markdown':
            try:
                import markdown
            except ImportError:
                raise RuntimeError(u"Looks like markdown is not installed")

            if text.startswith(u'\ufeff'):  # check for unicode BOM
                text = text[1:]

            pattern = re.compile(r'^\.include\((.*)\):\s?(\S*).*$',
                                 re.MULTILINE)
            match = re.search(pattern, text)
            while match is not None:
                codetype = match[1]
                filename = match[2]
                try:
                    with open(filename, 'r') as f:
                        code = f.read()
                    code = code.replace('\n', '\n\t')
                    text = re.sub(pattern,
                                  r'\n\t!{}\n\t{}'.format(codetype,
                                                          code), text, 1)
                except FileNotFoundError:
                    text = re.sub(pattern,
                                  r'FileNotFound : {}'.format(filename), text,
                                  1)
                match = re.search(pattern, text)
            return markdown.markdown(text, self.md_extensions)
        elif self.format == 'restructuredtext':
            try:
                from landslide.rst import html_body
            except ImportError:
                raise RuntimeError(u"Looks like docutils are not installed")

            html = html_body(text, input_encoding=self.encoding)

            # RST generates pretty much markup to be removed in our case
            for (pattern, replacement, mode) in self.RST_REPLACEMENTS:
                html = re.sub(re.compile(pattern, mode), replacement, html, 0)

            return html.strip()
        elif self.format == 'textile':
            try:
                import textile
            except ImportError:
                raise RuntimeError(u"Looks like textile is not installed")

            text = text.replace('\n---\n', '\n<hr />\n')

            return textile.textile(text, encoding=self.encoding)
        else:
            raise NotImplementedError(u"Unsupported format %s, cannot parse" %
                                      self.format)
Пример #3
0
    def parse(self, text):
        """Parses and renders a text as HTML regarding current format.
        """

        if self.format == 'markdown':
            try:
                import markdown
            except ImportError:
                raise RuntimeError(u"Looks like markdown is not installed")

            if text.startswith(u'\ufeff'):  # check for unicode BOM
                text = text[1:]

            return markdown.markdown(text, self.md_extensions)
        elif self.format == 'restructuredtext':
            try:
                from landslide.rst import html_body
            except ImportError:
                raise RuntimeError(u"Looks like docutils are not installed")
            for ext in self.rst_extensions:
                import_ext(ext)

            html = html_body(text, input_encoding=self.encoding)

            # RST generates pretty much markup to be removed in our case
            for (pattern, replacement, mode) in self.RST_REPLACEMENTS:
                html = re.sub(re.compile(pattern, mode), replacement, html, 0)

            return html.strip()
        elif self.format == 'textile':
            try:
                import textile
            except ImportError:
                raise RuntimeError(u"Looks like textile is not installed")

            text = text.replace('\n---\n', '\n<hr />\n')

            return textile.textile(text, encoding=self.encoding)
        else:
            raise NotImplementedError(u"Unsupported format %s, cannot parse" %
                                      self.format)
Пример #4
0
    def parse(self, text):
        """Parses and renders a text as HTML regarding current format.
        """
        if self.format == 'markdown':
            try:
                import markdown
            except ImportError:
                raise RuntimeError(u"Looks like markdown is not installed")

            if text.startswith(u'\ufeff'):  # check for unicode BOM
                text = text[1:]

            return markdown.markdown(text, self.md_extensions)
        elif self.format == 'restructuredtext':
            try:
                from landslide.rst import html_body
            except ImportError:
                raise RuntimeError(u"Looks like docutils are not installed")

            html = html_body(text, input_encoding=self.encoding)

            # RST generates pretty much markup to be removed in our case
            for (pattern, replacement, mode) in self.RST_REPLACEMENTS:
                html = re.sub(re.compile(pattern, mode), replacement, html, 0)

            return html.strip()
        elif self.format == 'textile':
            try:
                import textile
            except ImportError:
                raise RuntimeError(u"Looks like textile is not installed")

            text = text.replace('\n---\n', '\n<hr />\n')

            return textile.textile(text, encoding=self.encoding)
        else:
            raise NotImplementedError(u"Unsupported format %s, cannot parse"
                                      % self.format)