예제 #1
0
class JinjaMarkdownReader(MarkdownReader):

    def __init__(self, *args, **kwargs):
        super(JinjaMarkdownReader, self).__init__(*args, **kwargs)

        # will look first in 'JINJA2CONTENT_TEMPLATES', by default the
        # content root path, then in the theme's templates
        # local_templates_dirs = self.settings.get('JINJA2CONTENT_TEMPLATES', ['.'])
        # local_templates_dirs = path.join(self.settings['PATH'], local_templates_dirs)
        local_dirs = self.settings.get('JINJA2CONTENT_TEMPLATES', ['.'])
        local_dirs = [path.join(self.settings['PATH'], folder)
                      for folder in local_dirs]
        theme_dir = path.join(self.settings['THEME'], 'templates')

        loaders = [FileSystemLoader(_dir) for _dir
                   in local_dirs + [theme_dir]]
        if 'JINJA_ENVIRONMENT' in self.settings: # pelican 3.7
            jinja_environment = self.settings['JINJA_ENVIRONMENT']
        else:
            jinja_environment = {
                'trim_blocks': True,
                'lstrip_blocks': True,
                'extensions': self.settings['JINJA_EXTENSIONS']
            }

        self.env = Environment(
            loader=ChoiceLoader(loaders),
            **jinja_environment)

        # if filters not in self.env:
        self.env.filters['markdown'] = self.markdown

    def markdown(self, input):
        # return "contents--" + input + "--contents"
        self._md = Markdown(extensions=self.settings['MARKDOWN']['extensions'])
        return self._md.convert(input)

    def read(self, source_path):
        """Parse content and metadata of markdown files.

        Rendering them as jinja templates first.

        """

        self._source_path = source_path
        self._md = Markdown(extensions=self.settings['MARKDOWN']['extensions'])

        with pelican_open(source_path) as text:
            text = self.env.from_string(text).render()
            content = self._md.convert(text)

        metadata = self._parse_metadata(self._md.Meta)
        return content, metadata
class JinjaMarkdownReader(MarkdownReader):
    def __init__(self, *args, **kwargs):
        super(JinjaMarkdownReader, self).__init__(*args, **kwargs)

        # will look first in 'JINJA2CONTENT_TEMPLATES', by default the
        # content root path, then in the theme's templates
        # local_templates_dirs = self.settings.get('JINJA2CONTENT_TEMPLATES', ['.'])
        # local_templates_dirs = path.join(self.settings['PATH'], local_templates_dirs)
        local_dirs = self.settings.get("JINJA2CONTENT_TEMPLATES", ["."])
        local_dirs = [path.join(self.settings["PATH"], folder) for folder in local_dirs]
        theme_dir = path.join(self.settings["THEME"], "templates")

        loaders = [FileSystemLoader(_dir) for _dir in local_dirs + [theme_dir]]
        self.env = Environment(
            trim_blocks=True,
            lstrip_blocks=True,
            extensions=self.settings["JINJA_EXTENSIONS"],
            loader=ChoiceLoader(loaders),
        )

    def read(self, source_path):
        """Parse content and metadata of markdown files.

        Rendering them as jinja templates first.

        """
        self._source_path = source_path
        self._md = Markdown(extensions=self.extensions)

        with pelican_open(source_path) as text:
            text = self.env.from_string(text).render()
            content = self._md.convert(text)

        metadata = self._parse_metadata(self._md.Meta)
        return content, metadata
예제 #3
0
class JinjaMarkdownReader(MarkdownReader):

    def __init__(self, *args, **kwargs):
        super(JinjaMarkdownReader, self).__init__(*args, **kwargs)

        templates_dir = os.path.join(self.settings['THEME'], 'templates')
        self._env = Environment(
            trim_blocks=True, lstrip_blocks=True,
            loader=FileSystemLoader(templates_dir),
            extensions=self.settings['JINJA_EXTENSIONS'])

    def read(self, source_path):
        """Parse content and metadata of markdown files.

        Rendering them as jinja templates first.

        """

        self._source_path = source_path
        self._md = Markdown(extensions=self.extensions)
        with pelican_open(source_path) as text:
            text = self._env.from_string(text).render()
            content = self._md.convert(text)

        metadata = self._parse_metadata(self._md.Meta)
        return content, metadata
예제 #4
0
class JinjaMarkdownReader(MarkdownReader):
    def __init__(self, *args, **kwargs):
        super(JinjaMarkdownReader, self).__init__(*args, **kwargs)

        # will look first in 'JINJA2CONTENT_TEMPLATES', by default the
        # content root path, then in the theme's templates
        # local_templates_dirs = self.settings.get('JINJA2CONTENT_TEMPLATES', ['.'])
        # local_templates_dirs = path.join(self.settings['PATH'], local_templates_dirs)
        local_dirs = self.settings.get('JINJA2CONTENT_TEMPLATES', ['.'])
        local_dirs = [
            path.join(self.settings['PATH'], folder) for folder in local_dirs
        ]
        theme_dir = path.join(self.settings['THEME'], 'templates')

        loaders = [FileSystemLoader(_dir) for _dir in local_dirs + [theme_dir]]
        self.env = Environment(trim_blocks=True,
                               lstrip_blocks=True,
                               extensions=self.settings['JINJA_EXTENSIONS'],
                               loader=ChoiceLoader(loaders))

    def read(self, source_path):
        """Parse content and metadata of markdown files.

        Rendering them as jinja templates first.

        """
        self._source_path = source_path
        self._md = Markdown(extensions=self.extensions)

        with pelican_open(source_path) as text:
            text = self.env.from_string(text).render()
            content = self._md.convert(text)

        metadata = self._parse_metadata(self._md.Meta)
        return content, metadata
예제 #5
0
class JinjaMarkdownReader(MarkdownReader):
    def __init__(self, *args, **kwargs):
        super(JinjaMarkdownReader, self).__init__(*args, **kwargs)

        templates_dir = os.path.join(self.settings['THEME'], 'templates')
        self._env = Environment(trim_blocks=True,
                                lstrip_blocks=True,
                                loader=FileSystemLoader(templates_dir),
                                extensions=self.settings['JINJA_EXTENSIONS'])

    def read(self, source_path):
        """Parse content and metadata of markdown files.

        Rendering them as jinja templates first.

        """

        self._source_path = source_path
        self._md = Markdown(extensions=self.extensions)
        with pelican_open(source_path) as text:
            text = self._env.from_string(text).render()
            content = self._md.convert(text)

        metadata = self._parse_metadata(self._md.Meta)
        return content, metadata
예제 #6
0
class SimpleReader(MarkdownReader):
    enabled = True

    file_extensions = ["md"]

    def __init__(self, *args, **kwargs):
        super(SimpleReader, self).__init__(*args, **kwargs)
        self.settings["MARKDOWN"]["extensions"].append("markdown.extensions.toc")

    def read(self, source_path):
        self._source_path = source_path
        self._md = Markdown(**self.settings["MARKDOWN"])
        if "Lectures" in source_path:
            self._md.preprocessors.register(
                BlockquotesPreprocessor(self._md), "blockquotes", 10
            )

        with pelican_open(source_path) as text:
            content = self._md.convert(text)

        if hasattr(self._md, "Meta"):
            metadata = self._parse_metadata(self._md.Meta)
        else:
            metadata = {}

        # Add the TOC to the metadata.
        if len(self._md.toc) > 300:
            metadata["table_of_contents"] = self._md.toc

        # Get the title from the first h1
        if "title" not in metadata and len(self._md.toc_tokens):
            first_title = self._md.toc_tokens[0]
            metadata["title"] = first_title["name"]
            content = content.replace(
                '<h1 id="{id}">{name}</h1>'.format(**first_title), ""
            )

        # Get the date from the filename, if possible.
        parts = os.path.splitext(os.path.basename(source_path))[0].split("-")
        if "read_on" in metadata:
            metadata["date"] = datetime.strptime(metadata["read_on"], "%B %Y")
        elif len(parts) >= 3:
            metadata["date"] = get_date("-".join(parts[:3]))

        if "slug" not in metadata:
            metadata["slug"] = slugify(
                metadata["title"], self.settings.get("SLUG_REGEX_SUBSTITUTIONS", [])
            )

        category = os.path.basename(
            os.path.abspath(os.path.join(source_path, os.pardir))
        )
        metadata["category"] = self.process_metadata("category", category)
        return content, metadata
def register_filter(obj):
    if 'JINJA_ENVIRONMENT' in obj.settings:  # pelican 3.7
        jinja_environment = obj.settings['JINJA_ENVIRONMENT']
    else:
        jinja_environment = {
            'trim_blocks': True,
            'lstrip_blocks': True,
            'extensions': self.settings['JINJA_EXTENSIONS']
        }

    env = Environment(**jinja_environment)

    md = Markdown(extensions=obj.settings['MARKDOWN']['extensions'])

    # if filters not in self.env:
    env.filters['markdown'] = lambda input: md.convert(input)
예제 #8
0
class JinjaMarkdownReader(MarkdownReader):

    def __init__(self, *args, **kwargs):
        super(JinjaMarkdownReader, self).__init__(*args, **kwargs)

        # will look first in 'JINJA2CONTENT_TEMPLATES', by default the
        # content root path, then in the theme's templates
        # local_templates_dirs = self.settings.get('JINJA2CONTENT_TEMPLATES', ['.'])
        # local_templates_dirs = path.join(self.settings['PATH'], local_templates_dirs)
        local_dirs = self.settings.get('JINJA2CONTENT_TEMPLATES', ['.'])
        local_dirs = [path.join(self.settings['PATH'], folder)
                      for folder in local_dirs]
        theme_dir = path.join(self.settings['THEME'], 'templates')

        loaders = [FileSystemLoader(_dir) for _dir
                   in local_dirs + [theme_dir]]
        if 'JINJA_ENVIRONMENT' in self.settings: # pelican 3.7
            jinja_environment = self.settings['JINJA_ENVIRONMENT']
        else:
            jinja_environment = {
                'trim_blocks': True,
                'lstrip_blocks': True,
                'extensions': self.settings['JINJA_EXTENSIONS']
            }
        self.env = Environment(
            loader=ChoiceLoader(loaders),
            **jinja_environment)
        if 'JINJA_FILTERS' in self.settings: # I've added this.
            self.env.filters = self.settings['JINJA_FILTERS']

    def read(self, source_path):
        """Parse content and metadata of markdown files.

        Rendering them as jinja templates first.

        """

        self._source_path = source_path
        self._md = Markdown(extensions=self.settings['MARKDOWN']['extensions'])

        with pelican_open(source_path) as text:
            text = self.env.from_string(text).render()
            content = self._md.convert(text)

        metadata = self._parse_metadata(self._md.Meta)
        return content, metadata
예제 #9
0
class CustomMarkdownReader(MarkdownReader):
    def read(self, source_path):
        self._source_path = source_path
        default_extensions = self.settings['MARKDOWN']['extensions']
        default_extensions.append(
            MarkdownInclude(
                configs={
                    'base_path': self.settings['CONTENT_PATH'],
                    'source_path': os.path.dirname(source_path)
                }))
        self._md = Markdown(extensions=default_extensions)

        with pelican_open(source_path) as text:
            content = self._md.convert(text)

        if hasattr(self._md, 'Meta'):
            metadata = self._parse_metadata(self._md.Meta)
        else:
            metadata = {}

        return content, metadata
예제 #10
0
class CustomMarkdownReader(MarkdownReader):

    def read(self, source_path):
        self._source_path = source_path
        default_extensions = self.settings['MARKDOWN']['extensions']
        default_extensions.append(
            MarkdownInclude(configs={
                'base_path': self.settings['CONTENT_PATH'],
                'source_path': os.path.dirname(source_path)
            })
        )
        self._md = Markdown(extensions=default_extensions)

        with pelican_open(source_path) as text:
            content = self._md.convert(text)

        if hasattr(self._md, 'Meta'):
            metadata = self._parse_metadata(self._md.Meta)
        else:
            metadata = {}

        return content, metadata