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

Inline image and lightbox template files #84

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

pierresaramito
Copy link

Hi all !

In brief:

  • support for optional "inline_image.html" and "image_lightbox.html" templates
  • SITEURL is now replaced in all "inline_*.html" templates

In order to increase the flexibility of inline images and lightboxes,
I've introduced two template file provided by default as:
PHOTO_INLINE_IMAGE_TEMPLATE = "inline_image.html"
PHOTO_INLINE_LIGHTBOX_TEMPLATE = "image_lightbox.html"
Indeed, these two files was suggested in the photos.py code,
but not used in practice: perhaps it was planed for future works?
This is now achieved and the README.md doc is completed accordingly.

Also, the SITEURL variable was not propagated in "inline_gallery.html"
ind thus, it was complicated to compute full path files.
Now, SITEURL is propagated in this template file
and, moreover, it honors the RELATIVE_URLS variable from pelicanconf.py:
when RELATIVE_URLS is True, then SITEURL is propagated as ".".

Pirogh [email protected]

@pierresaramito
Copy link
Author

Hi all !

Yet another new patch, in brief:

the PHOTO_INLINE_GALLERY_PATTERN python regex also define an "options"
dict that is transmitted to the inline_gallery.html template file

Both this patch and the previous one are 100% backward-compatible.

In order to increase the flexibility of inline galleries,
I've introduced an optional "options" in the
PHOTO_INLINE_GALLERY_PATTERN python regex. When detected, the
options are transmitted to the inline_gallery.html file as
a python dict of pairs of key-value.

Hope you will enjoy it!

Pirogh [email protected]

@pierresaramito
Copy link
Author

Hi all !

Is there somebody alive here ?

Pierre

@pierresaramito
Copy link
Author

re-open, hope that something new could happen ?

@pierresaramito pierresaramito reopened this Apr 1, 2023
@Ockenfuss
Copy link

Ockenfuss commented May 3, 2023

@pierresaramito I am a user of this plugin and have the problem, that the SITEURL is not substituted in inline_gallery.html. I guess this is a bug, which is fixed by your PR? The other features you included look pretty amazing to me as well!

Unfortunately, I don't know if this repo is still alive (would be very sad if not!). Have you opened an issue regarding the bugs you fixed? Maybe this will create some attention and document the currently existing bugs. Let`s hope it will be merged soon!

Paul

@Ockenfuss
Copy link

I opened an issue for this now: #87

Copy link
Contributor

@offbyone offbyone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a few small things that should change before landing this, but by and large this looks solid.

  • if you can, some tests for the options parser would be awesome.
  • tests for the template loader aren't a bad idea either.
  • log when templates can't be loaded please

@@ -220,6 +223,162 @@ For example, add the following to the template `index.html`, inside the `entry-c
{% endif %}
```

## The new optional templates files for inline galleries, images and lightboxes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## The new optional templates files for inline galleries, images and lightboxes
## Optional templates files for inline galleries, images and lightboxes


The template file to render inline galleries is loaded.
The name of this file is by default ``"inline_gallery.html"```
and could be changed with the `PHOTO_INLINE_GALLERY_TEMPLATE` variable.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
and could be changed with the `PHOTO_INLINE_GALLERY_TEMPLATE` variable.
and can be changed with the `PHOTO_INLINE_GALLERY_TEMPLATE` variable.

</div>
```

The ``options`` variable is a python ``dict``:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The ``options`` variable is a python ``dict``:
The ``options`` variable is a python ``dict[str,Union[str,bool]]``:

When there is no explicit value, the value is a ``bool``, set to ``True``.
When the value is explicitely ``False``, it is also converted to ``bool``,
otherwise it is left as a ``string``.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be helpful to list the context variables available to the template here, the way you've done for the image template, below.

Comment on lines +1895 to +1920
gallery_name = m.group("gallery_name")
options_strg = m.group("options")
options = {}
if options_strg is not None:
options_list = re.compile(r"[\^,]+").split(options_strg)
r_pair = re.compile(r"(?P<variable>\w+)=(?P<value>\w+)")
r_flag = re.compile(r"(?P<flag>\w+)")
for opt in options_list:
m_pair = r_pair.match(opt)
m_flag = r_flag.match(opt)
if m_pair:
d = m_pair.groupdict()
value = d["value"]
# convert string to bool when possible
if value == "True":
value = True
elif value == "False":
value = False
options[d["variable"]] = value
elif m_flag:
d = m_flag.groupdict()
options[d["flag"]] = True
else:
logger.error(
f"unexpected inline gallery <options> = {options_strg}"
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you split this parsing out into a function? It would be easier to test (and if you put a test in for it, that'd be grand, too)

Comment on lines +2138 to +2139
except Exception:
template = None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case at least please log a warning or error about the issue, rather than silently bailing.

)
),
lightbox_attrs = " ".join(lightbox_attr_list)
content._content = content._content.replace(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I gotta wonder... would it make sense to define these as template strings in the package somewhere, and then just have one path through here (if a template is set, load it, otherwise load the default template, and then render them both)?

@justinmayer
Copy link
Contributor

Hi Pierre. Thanks for submitting this pull request. Could you take a moment to address the feedback that @offbyone provided so we can get your contribution merged?

@justinmayer
Copy link
Contributor

Hi Pierre. Just checking in about your contribution. What do you think?

@phibos
Copy link
Collaborator

phibos commented Mar 5, 2024

I do not recommend merging this PR because it has some drawbacks.

Use deprecated gallery include

First of all it tries to improve a deprecated feature, the 'gallery' pattern. If the galleries are included into a page or article in the old way, like in the example below.

gallery::name_of_the_gallery

The markdown parser adds a <p> tag around the pattern. So the result will look like.

<p>gallery::name_of_the_gallery</p>

In the next step the photos plugin replaces the gallery pattern with the inline gallery template. This will result in a gallery inside a <p> tag.

<p>
    <div class="my-gallery">
        <ul>
            <li><img src=""/></li>
            <li><img src=""/></li>
        </ul>
    </div>
</p>

This might result in structural issues in the browser. Also if the photos plugin can't find the gallery it will result in the gallery pattern displayed on the output.

<p>gallery::name_of_the_gallery</p>

New pattern

Just use the pattern introduced in version 1.1.0

<div gallery="name_of_the_gallery"></div>

This pattern is not modified by the markdown parser and the photos plugin can replace it with the correct inline gallery template.

<div class="my-gallery">
    <ul>
        <li><img src=""/></li>
        <li><img src=""/></li>
    </ul>
</div>

If something went wrong and the photos plugin can't find the gallery. The div tag is still present in the output but won't be visible for your readers.

Additional parameters

By default PHOTO_INLINE_PARSE_HTML is set to true. See the example below.

<div gallery="name_of_the_gallery" profile="profile_name" additional_attribute="test123"></div>

This will use the inline_gallery template and the image profile from your settings. Also this will make the attribute additional_attribute available in the template as {{ html_attributes.additional_attributes }}

Inline Gallery, Image or lightbox

Just use one of the patterns.

<div gallery="name_of_the_gallery"></div>
<div image="image.jpg"></div>
<div lightbox="name_of_the_gallery"></div>

You can provide additional attributes/values by following the instructions from the additional parameters example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants