
from pelican import signals
from pelican.contents import Content, Article
from BeautifulSoup import BeautifulSoup
from six import text_type

def summary_footnotes(instance):
    if "SUMMARY_FOOTNOTES_MODE" in instance.settings:
        mode = instance.settings["SUMMARY_FOOTNOTES_MODE"]
    else:
        mode = 'link'

    if type(instance) == Article:
        summary = BeautifulSoup(instance.summary)
        footnote_links = summary.findAll('a', {'rel':'footnote'})
        if footnote_links:
            for link in footnote_links:
                if mode == 'remove':
                    link.extract()
                elif mode == 'link':
                    link['href'] = "%s/%s%s" % (instance.settings["SITEURL"],
                                                instance.url,
                                                link['href'])
                else:
                    raise Exception("Unknown summary footnote mode: %s" % mode)
            instance._summary = text_type(summary)

def register():
    signals.content_object_init.connect(summary_footnotes)


from pelican import signals
from pelican.contents import Content, Article
from BeautifulSoup import BeautifulSoup
from six import text_type

def initialized(pelican):
    from pelican.settings import DEFAULT_CONFIG
    DEFAULT_CONFIG.setdefault('SUMMARY_FOOTNOTES_MODE',
                              'link')
    if pelican:
        pelican.settings.setdefault('SUMMARY_FOOTNOTES_MODE',
                                    'link')

def transform_summary(summary, article_url, site_url, mode):
    summary = BeautifulSoup(summary)
    footnote_links = summary.findAll('a', {'rel':'footnote'})
    if footnote_links:
        for link in footnote_links:
            if mode == 'remove':
                link.extract()
            elif mode == 'link':
                link['href'] = "%s/%s%s" % (site_url,
                                            article_url,
                                            link['href'])
            else:
                raise Exception("Unknown summary_footnote mode: %s" % mode)
        return text_type(summary)
    return None

def summary_footnotes(instance):
    mode = instance.settings["SUMMARY_FOOTNOTES_MODE"]

    if type(instance) == Article:
        # Monkeypatch in the rewrite on the summary because when this is run
        # the content might not be ready yet if it depends on other files
        # being loaded.
        instance._orig_get_summary = instance._get_summary
        
        def _get_summary(self):
            summary = self._orig_get_summary()
            new_summary = transform_summary(summary,
                                            self.url,
                                            self.settings['SITEURL'],
                                            mode)
            if new_summary is not None:
                return new_summary
            else:
                return summary

        funcType = type(instance._get_summary)
        instance._get_summary = funcType(_get_summary, instance, Article)

def register():
    signals.initialized.connect(initialized)
    signals.content_object_init.connect(summary_footnotes)

Rewrite summary as late as possible.
Rewrite summary as late as possible.

Fixes issue where {filename} links would sometimes not work.

  
  from pelican import signals
  from pelican.contents import Content, Article
  from BeautifulSoup import BeautifulSoup
  from six import text_type
  
+ def initialized(pelican):
+     from pelican.settings import DEFAULT_CONFIG
+     DEFAULT_CONFIG.setdefault('SUMMARY_FOOTNOTES_MODE',
+                               'link')
+     if pelican:
+         pelican.settings.setdefault('SUMMARY_FOOTNOTES_MODE',
+                                     'link')
+ 
+ def transform_summary(summary, article_url, site_url, mode):
+     summary = BeautifulSoup(summary)
+     footnote_links = summary.findAll('a', {'rel':'footnote'})
+     if footnote_links:
+         for link in footnote_links:
+             if mode == 'remove':
+                 link.extract()
+             elif mode == 'link':
+                 link['href'] = "%s/%s%s" % (site_url,
+                                             article_url,
+                                             link['href'])
+             else:
+                 raise Exception("Unknown summary_footnote mode: %s" % mode)
+         return text_type(summary)
+     return None
+ 
  def summary_footnotes(instance):
-     if "SUMMARY_FOOTNOTES_MODE" in instance.settings:
-         mode = instance.settings["SUMMARY_FOOTNOTES_MODE"]
+     mode = instance.settings["SUMMARY_FOOTNOTES_MODE"]
-     else:
-         mode = 'link'
  
      if type(instance) == Article:
-         summary = BeautifulSoup(instance.summary)
-         footnote_links = summary.findAll('a', {'rel':'footnote'})
-         if footnote_links:
-             for link in footnote_links:
-                 if mode == 'remove':
-                     link.extract()
-                 elif mode == 'link':
-                     link['href'] = "%s/%s%s" % (instance.settings["SITEURL"],
+         # Monkeypatch in the rewrite on the summary because when this is run
+         # the content might not be ready yet if it depends on other files
+         # being loaded.
+         instance._orig_get_summary = instance._get_summary
+         
+         def _get_summary(self):
+             summary = self._orig_get_summary()
+             new_summary = transform_summary(summary,
-                                                 instance.url,
+                                             self.url,
+                                             self.settings['SITEURL'],
-                                                 link['href'])
+                                             mode)
+             if new_summary is not None:
+                 return new_summary
-                 else:
+             else:
-                     raise Exception("Unknown summary footnote mode: %s" % mode)
-             instance._summary = text_type(summary)
+                 return summary
+ 
+         funcType = type(instance._get_summary)
+         instance._get_summary = funcType(_get_summary, instance, Article)
  
  def register():
+     signals.initialized.connect(initialized)
      signals.content_object_init.connect(summary_footnotes)
  
Rewrite summary as late as possible.
## Code Before:

from pelican import signals
from pelican.contents import Content, Article
from BeautifulSoup import BeautifulSoup
from six import text_type

def summary_footnotes(instance):
    if "SUMMARY_FOOTNOTES_MODE" in instance.settings:
        mode = instance.settings["SUMMARY_FOOTNOTES_MODE"]
    else:
        mode = 'link'

    if type(instance) == Article:
        summary = BeautifulSoup(instance.summary)
        footnote_links = summary.findAll('a', {'rel':'footnote'})
        if footnote_links:
            for link in footnote_links:
                if mode == 'remove':
                    link.extract()
                elif mode == 'link':
                    link['href'] = "%s/%s%s" % (instance.settings["SITEURL"],
                                                instance.url,
                                                link['href'])
                else:
                    raise Exception("Unknown summary footnote mode: %s" % mode)
            instance._summary = text_type(summary)

def register():
    signals.content_object_init.connect(summary_footnotes)

## Instruction:
Rewrite summary as late as possible.
## Code After:

from pelican import signals
from pelican.contents import Content, Article
from BeautifulSoup import BeautifulSoup
from six import text_type

def initialized(pelican):
    from pelican.settings import DEFAULT_CONFIG
    DEFAULT_CONFIG.setdefault('SUMMARY_FOOTNOTES_MODE',
                              'link')
    if pelican:
        pelican.settings.setdefault('SUMMARY_FOOTNOTES_MODE',
                                    'link')

def transform_summary(summary, article_url, site_url, mode):
    summary = BeautifulSoup(summary)
    footnote_links = summary.findAll('a', {'rel':'footnote'})
    if footnote_links:
        for link in footnote_links:
            if mode == 'remove':
                link.extract()
            elif mode == 'link':
                link['href'] = "%s/%s%s" % (site_url,
                                            article_url,
                                            link['href'])
            else:
                raise Exception("Unknown summary_footnote mode: %s" % mode)
        return text_type(summary)
    return None

def summary_footnotes(instance):
    mode = instance.settings["SUMMARY_FOOTNOTES_MODE"]

    if type(instance) == Article:
        # Monkeypatch in the rewrite on the summary because when this is run
        # the content might not be ready yet if it depends on other files
        # being loaded.
        instance._orig_get_summary = instance._get_summary
        
        def _get_summary(self):
            summary = self._orig_get_summary()
            new_summary = transform_summary(summary,
                                            self.url,
                                            self.settings['SITEURL'],
                                            mode)
            if new_summary is not None:
                return new_summary
            else:
                return summary

        funcType = type(instance._get_summary)
        instance._get_summary = funcType(_get_summary, instance, Article)

def register():
    signals.initialized.connect(initialized)
    signals.content_object_init.connect(summary_footnotes)
