Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Markup import. Modified get_partial() function which failed on some HTML #6

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions examples/bootstrap4/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Flask
Flask-Modals
Bootstrap-Flask==1.5.2
Flask-Login==0.5.0
Flask-WTF==0.14.3
email-validator==1.1.2
Bootstrap-Flask
Flask-Login
Flask-WTF
email-validator
4 changes: 2 additions & 2 deletions examples/bootstrap5/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Flask
Flask-Modals
Flask-WTF==0.14.3
email-validator==1.1.2
Flask-WTF
email-validator
4 changes: 2 additions & 2 deletions flask_modals/modal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

from flask import (Blueprint, render_template, get_flashed_messages,
_app_ctx_stack, request)
from jinja2.utils import markupsafe
from markupsafe import Markup
from flask_modals.partial import get_partial


def modal_messages():
'''This will be available in the app templates for use in the modal
body.
'''
return markupsafe.Markup(render_template('modals/modalMessages.html'))
return Markup(render_template('modals/modalMessages.html'))


def render_template_modal(*args, **kwargs):
Expand Down
34 changes: 10 additions & 24 deletions flask_modals/partial.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,15 @@
import re

from lxml import etree, html
from flask import render_template


def get_partial(modal, *args, **kwargs):
def get_partial(modal_id, *args, **kwargs):
'''Return the modal body.'''

html = render_template(*args, **kwargs)
lines = html.splitlines(keepends=True)

found_modal = False
found_modal_body = False
div_count = 0
partial = ''
for line in lines:
if f'id="{modal}"' in line:
found_modal = True
if found_modal:
if 'class="modal-body' in line:
found_modal_body = True
if found_modal_body:
partial += line
startdivs = re.findall(r'<div.*?>', line, re.IGNORECASE)
enddivs = re.findall(r'</div>', line, re.IGNORECASE)
div_count += len(startdivs) - len(enddivs)
if div_count <= 0:
break
return partial
doc = render_template(*args, **kwargs)
root_node = html.fromstring(doc)
xpath = f'//div[@id="{modal_id}"]' # modal window
modal_node = root_node.xpath(xpath)[0]
xpath = './/div[@class="modal-body"]' # modal body
body_node = modal_node.xpath(xpath)[0]
body = str(etree.tostring(body_node)).replace('\\n', '').replace('\n', '')
return body
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
'static/js/main.js',
'static/css/progressBarStyle.css']},
include_package_data=True,
install_requires=['Flask'],
install_requires=['Flask', 'lxml'],
zip_safe=False,
classifiers=[
'Development Status :: 3 - Alpha',
Expand Down