diff options
author | luxagraf <sng@luxagraf.net> | 2016-03-23 22:16:44 -0400 |
---|---|---|
committer | luxagraf <sng@luxagraf.net> | 2016-03-23 22:16:44 -0400 |
commit | a5efbdabe83599f3d036360fc1fea8cc86dc8a94 (patch) | |
tree | 5e79eb4746e6346182f32c5301aa189ce568d686 /app | |
parent | 3c11cb262bf447fb6b69eed16c704604f9fe820e (diff) |
added a new image parser to pull in images from the LuxImage model and
inline them with size and srcset support
Diffstat (limited to 'app')
-rw-r--r-- | app/jrnl/models.py | 34 |
1 files changed, 32 insertions, 2 deletions
diff --git a/app/jrnl/models.py b/app/jrnl/models.py index 834b643..511ac8c 100644 --- a/app/jrnl/models.py +++ b/app/jrnl/models.py @@ -1,10 +1,13 @@ import datetime import os +import re from django.contrib.gis.db import models from django.utils.html import format_html from django.core.urlresolvers import reverse +from django.template.loader import render_to_string from django.conf import settings +from django.template import Context from django.contrib.syndication.views import Feed from django.contrib.sitemaps import Sitemap from django import forms @@ -14,7 +17,7 @@ from django.template.defaultfilters import slugify import markdown from bs4 import BeautifulSoup -from photos.models import PhotoGallery +from photos.models import PhotoGallery, LuxImage from locations.models import Location @@ -31,6 +34,30 @@ def image_url_replace(s): return s +def parse_image(s): + soup = BeautifulSoup(s.group(), "lxml") + for img in soup.find_all('img'): + print(img) + src = img['src'].split("images/")[1] + print(src) + i = LuxImage.objects.get(image__icontains=src) + cl = img['class'] + caption = False + if len(cl) > 1: + css_class = cl[0] + if cl[1] == 'caption': + caption = True + else: + css_class = cl[0] + c = Context({'image': i, 'caption': caption}) + return render_to_string("lib/img_%s.html" % css_class, c) + + +def render_images(s): + s = re.sub('<img(.*)/>', parse_image, s) + return s + + def extract_images(s): soup = BeautifulSoup(s, "lxml") imgs = [] @@ -159,7 +186,10 @@ class Entry(models.Model): return self.get_next_by_pub_date(status__exact=1) def save(self): - md = image_url_replace(self.body_markdown) + if self.pk >= 161: + md = render_images(self.body_markdown) + else: + md = image_url_replace(self.body_markdown) self.body_html = markdown.markdown(md, extensions=['extra'], safe_mode=False) self.dek == markdown.markdown(self.dek, safe_mode=False) try: |