Skip to content

Commit

Permalink
Replace parser with string matching
Browse files Browse the repository at this point in the history
  • Loading branch information
deb17 committed Oct 31, 2021
1 parent a0cfd7b commit 7f60326
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 82 deletions.
2 changes: 1 addition & 1 deletion examples/bootstrap4/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

bootstrap = Bootstrap(app)
login = LoginManager(app)
login.login_view = 'login'
login.login_view = 'index'
modal = Modal(app)


Expand Down
17 changes: 5 additions & 12 deletions flask_modals/modal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from flask import (Blueprint, render_template, get_flashed_messages,
_app_ctx_stack, request)
from jinja2 import Markup
from flask_modals.parser import ModalParser
from flask_modals.partial import get_partial


def modal_messages():
Expand All @@ -28,23 +28,16 @@ def render_template_modal(*args, **kwargs):
# prevent flash messages from showing both outside and
# inside the modal
ctx._modal = True
html = render_template(*args, **kwargs)
parser = ModalParser(html, modal)
parser.feed(html)
return f'<template>{parser.stream}</template>'
partial = get_partial(modal, *args, **kwargs)
return f'<template>{partial}</template>'
else:
return render_template(*args, **kwargs)


def can_stream():
'''Returns `True` if the client accepts streams.'''

stream_mimetype = 'text/modal-stream.html'
if stream_mimetype not in request.accept_mimetypes.values():
return False
best = request.accept_mimetypes.best_match([
stream_mimetype, 'text/html'])
return best == stream_mimetype
return 'text/modal-stream.html' in request.accept_mimetypes.values()


def response(template=None):
Expand Down Expand Up @@ -107,7 +100,7 @@ def show_flashed_messages(*args, **kwargs):

return get_flashed_messages(*args, **kwargs)

def load(self, url=None):
def load(self):
'''Load the following markup:
1. nprogress.html - NProgress js library for progress bar
Expand Down
64 changes: 0 additions & 64 deletions flask_modals/parser.py

This file was deleted.

29 changes: 29 additions & 0 deletions flask_modals/partial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import re

from flask import render_template


def get_partial(modal, *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
14 changes: 10 additions & 4 deletions flask_modals/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@
if (modalBodyEl) {
el.addEventListener('submit', e => {
e.preventDefault()
fetchData(el, modalBodyEl)
fetchData(el, modalBodyEl, e.submitter)
})
}
})

function fetchData(el, modalBodyEl) {
function fetchData(el, modalBodyEl, submitter) {
let url
const body = new FormData(el)
if (submitter) {
const name = submitter.getAttribute('name')
const value = submitter.getAttribute('value')
body.append(name, value)
}
NProgress.start()
fetch(el.action, {
method: el.method,
body: new FormData(el),
body: body,
headers: {
Accept: 'text/modal-stream.html'
}
Expand All @@ -40,7 +46,7 @@
const el = modalBodyEl.querySelector('form')
el.addEventListener('submit', e => {
e.preventDefault()
fetchData(el, modalBodyEl)
fetchData(el, modalBodyEl, e.submitter)
})
} else {
if (location.href !== url) {
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name='Flask-Modals',
version='0.4.0',
version='0.4.1',
author='Debashish Palit',
author_email='[email protected]',
description='Use forms in Bootstrap modals with Flask.',
Expand Down

0 comments on commit 7f60326

Please sign in to comment.