barrysteyn / pelican_plugin-render_math

A plugin for the pelican blog to allow it render mathematics in Latex.
67 stars 25 forks source link

Math doesn't always render in summaries #40

Open szhorvat opened 7 years ago

szhorvat commented 7 years ago

Math does not always render in article summaries. In particular, when I use the setting RELATIVE_URLS=False, math won't render in summaries.

This is the line that inserts the script necessary to show math in summaries:

https://github.com/barrysteyn/pelican_plugin-render_math/blob/master/render_math.py#L336

I asked about this in #pelican on freenode, and I got the following response from Avaris:

Avaris: problem is the summary being memoized and there is no easy way to 'get' auto-generated summaries without triggering that Avaris: whenever you access .summary or in this case ._get_summary(), it's recorded so that it won't have to regenerated over and over if you request it again Avaris: that's an optimization for site generation but that creates the problem of not being able to manipulate summaries from a plugin

As a quick fix he suggested replicating this method:

https://github.com/getpelican/pelican/blob/master/pelican/contents.py#L293

in place of this:

https://github.com/barrysteyn/pelican_plugin-render_math/blob/master/render_math.py#L324

I did this and it works. But this is still just a hack. If another plugin calls get_summary before render_math, the memoization will still kick and the modification to article._summary will be ignored.

Thus I won't send a pull request until further comment by @barrysteyn

jerryasher commented 7 years ago

Can you clarify a bit? I am pretty new to pelican. What are the summary pages? Are they the index pages served at SITE_URL/index.html or something different?

I would think what Avaris is suggesting is taking

@memoized
    def get_summary(self, siteurl):
        """Returns the summary of an article.
        This is based on the summary metadata if set, otherwise truncate the
        content.
        """
        if hasattr(self, '_summary'):
            return self._update_content(self._summary, siteurl)

        if self.settings['SUMMARY_MAX_LENGTH'] is None:
            return self.content

        return truncate_html_words(self.content,
                                   self.settings['SUMMARY_MAX_LENGTH'])

and creating something like this:

    def get_summary_new(self, siteurl):
        """Returns a new summary of an article.
        """

        if self.settings['SUMMARY_MAX_LENGTH'] is None:
            return self.content

        return truncate_html_words(self.content,
                                   self.settings['SUMMARY_MAX_LENGTH'])

What did you try?

szhorvat commented 7 years ago

Summaries are what you see at the bottom of the page here: https://blog.getpelican.com/category/news.html When they are auto-generated, they are just excerpts from the beginning of each article.

This is what I did based on Avaris's suggestion:

https://github.com/szhorvat/pelican_plugin-render_math/commit/e0f48b3362f2d51f0e5e8326a85bff039a37ca01

(This includes your changes as well.)

This just replaces the call to article.summary with what that call would usually do—sans the memoization. It is a hack because if .summary gets called by something else (e.g. another plugin) before render_math would call it, it would still trigger memoization. I.e. any modifications to ._summary won't take effect if .summary or .get_summary() have been already called at least once.