Skip to content

Commit

Permalink
Add 'Parsing until another block tag' template tag
Browse files Browse the repository at this point in the history
  • Loading branch information
erwinmatijsen committed Jan 17, 2024
1 parent 4934bd3 commit 65cbb22
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
33 changes: 33 additions & 0 deletions markdownify/templatetags/markdownify.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,36 @@ def markdownify(text, custom_settings="default"):
html = cleaner.clean(html)

return mark_safe(html)


def do_markdownify(parser, token):
# Set up the nodelist and parse till we hit the endmarkdownify block
nodelist = parser.parse(("endmarkdownify",))
parser.delete_first_token()

# Get the settings from the tag
try:
markdownify_settings = token.split_contents()[1]
except IndexError:
markdownify_settings = "default"

return MarkDownifyNode(nodelist, markdownify_settings)


class MarkDownifyNode(template.Node):
def __init__(self, nodelist, markdownify_settings):
self.nodelist = nodelist
self.markdownify_settings = markdownify_settings

def render(self, context):

# Build new nodelist with rendered and markdownified nodes
node_list = []
for node in self.nodelist:
md_node = markdownify(node.render(context), custom_settings=self.markdownify_settings)
node_list.append(md_node)

return "".join(node_list)


register.tag("markdownify", do_markdownify)
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setup(
name='django-markdownify',
version='0.9.3',
version='0.9.4',
packages=['markdownify'],
package_dir={'markdownify': 'markdownify'},
package_data={'markdownify': ['tests/*.md']},
Expand All @@ -18,7 +18,7 @@
long_description=README,
long_description_content_type="text/markdown",
url='https://github.com/erwinmatijsen/django-markdownify',
download_url='https://github.com/erwinmatijsen/django-markdownify/archive/0.9.3.tar.gz',
download_url='https://github.com/erwinmatijsen/django-markdownify/archive/0.9.4.tar.gz',
author='Erwin Matijsen, R Moelker',
author_email='[email protected]',
classifiers=[
Expand Down

0 comments on commit 65cbb22

Please sign in to comment.