Absolute URLs on ilustr
authorRadek Czajka <radekczajka@wolnelektury.pl>
Mon, 16 Nov 2026 07:39:55 +0000 (08:39 +0100)
committerRadek Czajka <radekczajka@wolnelektury.pl>
Mon, 16 Nov 2026 07:39:55 +0000 (08:39 +0100)
src/documents/ilustr.py [new file with mode: 0644]
src/documents/models/book.py
src/documents/templates/documents/base.html
src/documents/templates/documents/book_detail.html
src/documents/urls.py
src/documents/views.py
src/redakcja/static/js/wiki/view_properties.js
src/wiki/static/wiki/scss/visual.scss

diff --git a/src/documents/ilustr.py b/src/documents/ilustr.py
new file mode 100644 (file)
index 0000000..30102ad
--- /dev/null
@@ -0,0 +1,76 @@
+# This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
+# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
+#
+"""Replacing relative addresses of illustrations with absolute URLs."""
+import os.path
+import re
+from urllib.parse import quote, unquote, urljoin
+from django.conf import settings
+
+
+SRC_RE = re.compile(r'''(<ilustr\b[^>]*?\bsrc\s*=\s*)(["'])(.*?)\2''', re.S)
+
+
+def is_absolute(src):
+    return src.startswith(('http://', 'https://'))
+
+
+def absolutize(text, gallery_url, gallery_dir):
+    """Replaces relative `src` of illustrations in text with absolute URLs.
+
+    Only replaces addresses of files which exist in the gallery.
+    Returns the new text, a list of replaced and a list of unresolved addresses.
+    """
+    fixed = []
+    unresolved = []
+
+    def repl(m):
+        src = m.group(3)
+        if is_absolute(src):
+            return m.group(0)
+        path = os.path.normpath(os.path.join(gallery_dir, unquote(src)))
+        if not src or not os.path.isfile(path):
+            unresolved.append(src)
+            return m.group(0)
+        fixed.append(src)
+        url = urljoin(gallery_url, quote(src, safe='/%'))
+        return m.group(1) + m.group(2) + url + m.group(2)
+
+    return SRC_RE.sub(repl, text), fixed, unresolved
+
+
+class IlustrFixer:
+    def __init__(self, base_url, **author):
+        self.images_url = urljoin(base_url, settings.MEDIA_URL + settings.IMAGE_DIR)
+        self.images_dir = os.path.join(settings.MEDIA_ROOT, settings.IMAGE_DIR)
+        self.author = author
+        self.stats = dict.fromkeys(['books', 'fixed', 'unresolved', 'conflicts'], 0)
+
+    def fix_book(self, book):
+        if not book.gallery:
+            gallery_url = gallery_dir = None
+        else:
+            gallery_url = urljoin(self.images_url, quote(book.gallery) + '/')
+            gallery_dir = os.path.join(self.images_dir, book.gallery)
+
+        for chunk in book.chunk_set.all():
+            head = chunk.head
+            if head is None:
+                continue
+            head_text = head.materialize()
+            if gallery_url is None:
+                unresolved = [m.group(3) for m in SRC_RE.finditer(head_text)
+                              if not is_absolute(m.group(3))]
+                if unresolved:
+                    self.stats['unresolved'] += len(unresolved)
+                continue
+
+            new_text, fixed, unresolved = absolutize(head_text, gallery_url, gallery_dir)
+            self.stats['fixed'] += len(fixed)
+            self.stats['unresolved'] += len(unresolved)
+
+            if fixed:
+                chunk.commit(
+                    new_text, description='Pełne adresy ilustracji.',
+                    publishable=head.publishable,
+                    **self.author)
index aa4e776..1245cea 100644 (file)
@@ -307,6 +307,26 @@ class Book(models.Model):
         valid_about = self.correct_about()
         assert bi.about == valid_about, _("rdf:about is not") + " " + valid_about
 
         valid_about = self.correct_about()
         assert bi.about == valid_about, _("rdf:about is not") + " " + valid_about
 
+        srcs = self.relative_ilustr_srcs(changes=changes)
+        assert not srcs, 'Ilustracje bez pełnego adresu: ' + ', '.join(
+            src or '(pusty)' for src in srcs)
+
+    def relative_ilustr_srcs(self, changes=None):
+        """Returns `src` of illustrations which aren't absolute URLs.
+
+        Those only work relative to the gallery, so they break
+        outside of it. Checks the current text, unless `changes` are given.
+        """
+        from lxml import etree
+        try:
+            tree = etree.fromstring(self.materialize(changes=changes).encode('utf-8'))
+        except (self.NoTextError, etree.XMLSyntaxError):
+            return []
+        return [
+            elem.get('src', '') for elem in tree.iter('ilustr')
+            if not elem.get('src', '').startswith(('http://', 'https://'))
+        ]
+
     def publishable_error(self):
         try:
             return self.assert_publishable()
     def publishable_error(self):
         try:
             return self.assert_publishable()
index f836888..c2c7243 100644 (file)
 </nav>
 
 <div class="container mt-4 mb-4">
 </nav>
 
 <div class="container mt-4 mb-4">
+  {% if messages %}
+    {% for message in messages %}
+      <div class="alert alert-{% if message.level_tag == 'error' %}danger{% else %}{{ message.level_tag|default:'info' }}{% endif %} alert-dismissible" role="alert">
+        {{ message }}
+        <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
+      </div>
+    {% endfor %}
+  {% endif %}
   {% block content %}
     <div class="row">
       <div class="col-lg-8">
   {% block content %}
     <div class="row">
       <div class="col-lg-8">
index a9c4d2b..8bf4329 100644 (file)
             {% endif %}
           </p>
 
             {% endif %}
           </p>
 
+          {% with srcs=book.relative_ilustr_srcs %}
+            {% if srcs %}
+              <form class="alert alert-warning" method="POST" action="{% url 'documents_fix_ilustr' book.slug %}">
+                {% csrf_token %}
+                Ilustracje bez pełnego adresu:
+                {% for src in srcs %}<tt>{{ src|default:"(pusty)" }}</tt>{% if not forloop.last %}, {% endif %}{% endfor %}
+                {% if user.is_authenticated %}
+                  <button type="submit" class="btn btn-sm btn-warning">Napraw</button>
+                {% endif %}
+              </form>
+            {% endif %}
+          {% endwith %}
+
           {% if publishable %}
             <p>
               <a href="{% url 'documents_book_xml' book.slug %}" rel="nofollow">{% trans "Full XML" %}</a><br/>
           {% if publishable %}
             <p>
               <a href="{% url 'documents_book_xml' book.slug %}" rel="nofollow">{% trans "Full XML" %}</a><br/>
index 60aa3fa..f23aad8 100644 (file)
@@ -31,6 +31,7 @@ urlpatterns = [
     path('create/', views.create_missing, name='documents_create_missing'),
 
     path('book/<slug:slug>/publish', views.publish, name="documents_publish"),
     path('create/', views.create_missing, name='documents_create_missing'),
 
     path('book/<slug:slug>/publish', views.publish, name="documents_publish"),
+    path('book/<slug:slug>/fix-ilustr', views.fix_ilustr, name="documents_fix_ilustr"),
 
     path('book/<slug:slug>/', views.book, name="documents_book"),
     path('book/<slug:slug>/gallery/',
 
     path('book/<slug:slug>/', views.book, name="documents_book"),
     path('book/<slug:slug>/gallery/',
index e87dc6d..cab58be 100644 (file)
@@ -11,7 +11,7 @@ import os
 from urllib.parse import quote_plus, unquote, urlsplit, urlunsplit
 
 from django.conf import settings
 from urllib.parse import quote_plus, unquote, urlsplit, urlunsplit
 
 from django.conf import settings
-from django.contrib import auth
+from django.contrib import auth, messages
 from django.contrib.auth.models import User
 from django.contrib.auth.decorators import login_required, permission_required
 from django.urls import reverse
 from django.contrib.auth.models import User
 from django.contrib.auth.decorators import login_required, permission_required
 from django.urls import reverse
@@ -676,6 +676,22 @@ def publish(request, slug):
         return http.HttpResponseRedirect(book.get_absolute_url())
 
 
         return http.HttpResponseRedirect(book.get_absolute_url())
 
 
+@require_POST
+@login_required
+def fix_ilustr(request, slug):
+    from .ilustr import IlustrFixer
+    book = get_object_or_404(Book, slug=slug)
+    protocol = 'https://' if request.is_secure() else 'http://'
+    fixer = IlustrFixer(protocol + request.get_host(), author=request.user)
+    with transaction.atomic():
+        fixer.fix_book(book)
+    if fixer.stats['unresolved'] or fixer.stats['conflicts']:
+        messages.warning(request, 'Nie wszystkie ilustracje udało się naprawić.')
+    elif fixer.stats['fixed']:
+        messages.success(request, 'Naprawiono adresy ilustracji.')
+    return http.HttpResponseRedirect(book.get_absolute_url())
+
+
 @require_POST
 @login_required
 def publish_image(request, slug):
 @require_POST
 @login_required
 def publish_image(request, slug):
index dde914f..6ae6e57 100644 (file)
 
                     self.doc.refreshImageGallery({
                         success: function(galleryImages) {
 
                     self.doc.refreshImageGallery({
                         success: function(galleryImages) {
-                            $.each(self.doc.galleryImages, (i, imgItem) => {
+                            $.each(galleryImages, (i, imgItem) => {
                                 let img = $("<img>").attr("src", imgItem.thumb).attr('title', imgItem.url).data('url', imgItem.url).on('click', function() {
                                     imglist.find('img').removeClass('active');
                                     $(this).addClass('active');
                                 let img = $("<img>").attr("src", imgItem.thumb).attr('title', imgItem.url).data('url', imgItem.url).on('click', function() {
                                     imglist.find('img').removeClass('active');
                                     $(this).addClass('active');
index 027340a..3130a15 100644 (file)
@@ -4,5 +4,18 @@
         font-size: 1rem;
         line-height: 1.5;
     }
         font-size: 1rem;
         line-height: 1.5;
     }
+
+    // Images require absolute src.
+    [x-node="ilustr"]:not([x-a-wl-src^="http://"]):not([x-a-wl-src^="https://"]) {
+        outline: 3px solid #dc3545;
+
+        &::before {
+            content: "Ilustracja bez pełnego adresu";
+            display: block;
+            color: #dc3545;
+            font-family: sans-serif;
+            font-size: .8rem;
+        }
+    }
 }
     
 }