diff options
| author | Bobby <[email protected]> | 2023-03-10 00:39:59 -0500 |
|---|---|---|
| committer | Bobby <[email protected]> | 2023-03-10 00:39:59 -0500 |
| commit | 69719b9d2696251bf6a21c2e2293601bd0efeb81 (patch) | |
| tree | f924525217f3851d98ea3eb36652b97f6deb5498 | |
| parent | 286d976dc6cdce95a43cf70db88ad4571d13897a (diff) | |
| download | thatcomputerscientist-69719b9d2696251bf6a21c2e2293601bd0efeb81.tar.xz thatcomputerscientist-69719b9d2696251bf6a21c2e2293601bd0efeb81.zip | |
Optimize image views for older browsers
| -rw-r--r-- | blog/context_processors.py | 1 | ||||
| -rw-r--r-- | blog_admin/views.py | 1 | ||||
| -rw-r--r-- | ignis/views.py | 33 | ||||
| -rw-r--r-- | templates/blog/partials/base.html | 2 | ||||
| -rw-r--r-- | templates/blog/partials/mathjax.html | 54 | ||||
| -rw-r--r-- | templates/blog/post.html | 2 |
6 files changed, 35 insertions, 58 deletions
diff --git a/blog/context_processors.py b/blog/context_processors.py index 895bdb7c..53a16417 100644 --- a/blog/context_processors.py +++ b/blog/context_processors.py @@ -60,7 +60,6 @@ def highlight_code_blocks(code_block): cb = code_block.string except: cb = code_block - print(cb) cb = cb.replace(u'\xa0', u' ') # guess the language as there is no data-lang attribute diff --git a/blog_admin/views.py b/blog_admin/views.py index 68447179..29b1f0aa 100644 --- a/blog_admin/views.py +++ b/blog_admin/views.py @@ -74,7 +74,6 @@ def new_post(request): post.save() return redirect(reverse('blog-admin:edit-post', kwargs = { 'slug': post.slug })) except Exception as e: - print(e) messages.error(request, 'Some error occured while creating post.', extra_tags='error') return redirect('blog-admin:posts') diff --git a/ignis/views.py b/ignis/views.py index b198b4f5..1b8b6be1 100644 --- a/ignis/views.py +++ b/ignis/views.py @@ -69,21 +69,20 @@ def post_image(request, size, post_id): @csrf_exempt def get_image(request, post_id, image_name): - if 'rpi_' in post_id: - # post has random post identifier => means it's a new post and not saved yet - # get image from temp_post_id - pi = PostImage.objects.filter(temp_post_id=post_id, name=image_name) - else: - # post has id => means it's an existing post - # get image from post_id - pi = PostImage.objects.filter(post=Post.objects.get(id=post_id), name=image_name) + # get image from post_id + pi = PostImage.objects.filter(post=Post.objects.get(id=post_id), name=image_name) if not pi: return HttpResponse('No image found!', status=404) # open image and return image = pi[0].image with open(image.path, 'rb') as f: - return HttpResponse(f.read(), content_type='image/{}'.format(image.name.split('.')[-1])) + image_file = f.read() + # convert to gif + image = Image.open(BytesIO(image_file)) + output = BytesIO() + image.save(output, format='GIF') + return HttpResponse(output.getvalue(), content_type='image/gif') @csrf_exempt def cover_image(request, repository): @@ -99,7 +98,6 @@ def cover_image(request, repository): # image is not in RepositoryTitles # get image url = 'https://socialify.thatcomputerscientist.com/luciferreeves/{}/png?font=KoHo&language=1&language2=1&name=1&theme=Dark&pattern=Solid'.format(repository) - print("Getting image for repository: {}".format(repository)) image = requests.get(url).content # reduce image size to 320x160 @@ -142,14 +140,13 @@ def upload_image(request): # upload image to PostImage model image = request.FILES['image'] post_id = request.POST['id'] - if 'rpi_' in post_id: - # post has random post identifier => means it's a new post and not saved yet - # save image to temp_post_id - pi = PostImage(image=image, temp_post_id=post_id, post=None, name=image.name) - else: - # post has id => means it's an existing post - # save image to post_id - pi = PostImage(image=image, post=Post.objects.get(id=post_id), name=image.name) + # check if image already exists + pi = PostImage.objects.filter(post=Post.objects.get(id=post_id), name=image.name) + if pi: + # image already exists, delete it + pi[0].delete() + # save image to post_id + pi = PostImage(image=image, post=Post.objects.get(id=post_id), name=image.name) pi.save() response = { 'url': '/ignis/image/{}/{}'.format(post_id, pi.name) diff --git a/templates/blog/partials/base.html b/templates/blog/partials/base.html index 2f395b1b..a04eae3c 100644 --- a/templates/blog/partials/base.html +++ b/templates/blog/partials/base.html @@ -49,7 +49,7 @@ <span><img src="{{'buttons'|ad }}" alt="Ad" style="width: 88px; height: 31px;" /></span> {% endfor %} <br /> - <center> + </center> <p style="text-align: center; margin-top: 1rem">© {% now "Y" %} That Computer Scientist. Source code available on <a href="https://github.com/luciferreeves/thatcomputerscientist" >GitHub diff --git a/templates/blog/partials/mathjax.html b/templates/blog/partials/mathjax.html index f580c4f5..04f18104 100644 --- a/templates/blog/partials/mathjax.html +++ b/templates/blog/partials/mathjax.html @@ -1,39 +1,21 @@ {% load static %} -<script type="text/javascript" src="{% static 'js/MathJax/MathJax.js' %}?config=TeX-AMS-MML_HTMLorMML-full"> -</script> +<script type="text/javascript" src="{% static 'js/MathJax/MathJax.js' %}?config=TeX-AMS-MML_HTMLorMML"></script> <script type="text/x-mathjax-config"> - MathJax.Hub.Register.StartupHook("End Jax",function () { - var BROWSER = MathJax.Hub.Browser; - - var canUseMML = (BROWSER.isFirefox && BROWSER.versionAtLeast("1.5")) || - (BROWSER.isMSIE && BROWSER.hasMathPlayer) || - (BROWSER.isSafari && BROWSER.versionAtLeast("5.0")) || - (BROWSER.isOpera && BROWSER.versionAtLeast("9.52") && - !BROWSER.versionAtLeast("14.0")); - - var CONFIG = MathJax.Hub.CombineConfig("MMLorHTML",{ - prefer: { - MSIE:"MML", Firefox:"HTML", Opera:"HTML", Chrome:"HTML", Safari:"HTML", - other:"HTML" - } - }); - - var jax = CONFIG.prefer[BROWSER] || CONFIG.prefer.other; - if (jax === "HTML") jax = "HTML-CSS"; else if (jax === "MML") jax = "NativeMML"; - if (jax === "NativeMML" && !canUseMML) jax = CONFIG.prefer.other; - return MathJax.Hub.setRenderer(jax); - }); - MathJax.Hub.Config({ - "tex2jax": { - "inlineMath": [['$','$'], ['\\(','\\)']], - }, - "fast-preview": { - disabled: true - }, - showMathMenu: false, - showMathMenuMSIE: false, - errorSettings: { - message: ["[Math Processing Error]"] - } - }); + MathJax.Hub.Config({ + jax: ["input/TeX","output/HTML-CSS", "output/NativeMML", "output/PreviewHTML"], + extensions: ["tex2jax.js"], + TeX: { + extensions: ["AMSmath.js","AMSsymbols.js","noErrors.js","noUndefined.js"] + }, + "HTML-CSS": { availableFonts: ["TeX"] }, + tex2jax: { + inlineMath: [ ['$','$'], ["\\(","\\)"] ], + displayMath: [ ['$$','$$'], ["\\[","\\]"] ], + processEscapes: true + }, + "fast-preview": { disabled: true }, + showProcessingMessages: false, + showMathMenu: false, + showMathMenuMSIE: false + }); </script>
\ No newline at end of file diff --git a/templates/blog/post.html b/templates/blog/post.html index 2cd87644..b43e0a89 100644 --- a/templates/blog/post.html +++ b/templates/blog/post.html @@ -113,7 +113,6 @@ <p><em>You must be logged in to leave a comment.</em></p> </div> {% endif %} -{% include 'blog/partials/mathjax.html' %} <script> function editComment(id) { document.getElementById('comment-body-' + id).style.display = 'none'; @@ -186,4 +185,5 @@ }); } </script> +{% include 'blog/partials/mathjax.html' %} {% endblock %} |
