"""
Summary
-------

This plugin allows easy, variable length summaries directly embedded into the
body of your articles.
"""

from __future__ import unicode_literals
from pelican import signals
from pelican.generators import ArticlesGenerator, StaticGenerator, PagesGenerator

def initialized(pelican):
    from pelican.settings import DEFAULT_CONFIG
    DEFAULT_CONFIG.setdefault('SUMMARY_BEGIN_MARKER',
                              '<!-- PELICAN_BEGIN_SUMMARY -->')
    DEFAULT_CONFIG.setdefault('SUMMARY_END_MARKER',
                              '<!-- PELICAN_END_SUMMARY -->')
    if pelican:
        pelican.settings.setdefault('SUMMARY_BEGIN_MARKER',
                                    '<!-- PELICAN_BEGIN_SUMMARY -->')
        pelican.settings.setdefault('SUMMARY_END_MARKER',
                                    '<!-- PELICAN_END_SUMMARY -->')

def extract_summary(instance):
    # if summary is already specified, use it
    # if there is no content, there's nothing to do
    if hasattr(instance, '_summary'):
        instance.has_summary = True
        return

    if not instance._content:
        instance.has_summary = False
        return

    begin_marker = instance.settings['SUMMARY_BEGIN_MARKER']
    end_marker   = instance.settings['SUMMARY_END_MARKER']

    content = instance._content
    begin_summary = -1
    end_summary = -1
    if begin_marker:
        begin_summary = content.find(begin_marker)
    if end_marker:
        end_summary = content.find(end_marker)

    if begin_summary == -1 and end_summary == -1:
        instance.has_summary = False
        return

    # skip over the begin marker, if present
    if begin_summary == -1:
        begin_summary = 0
    else:
        begin_summary = begin_summary + len(begin_marker)

    if end_summary == -1:
        end_summary = None

    summary = content[begin_summary:end_summary]

    # remove the markers from the content
    if begin_summary:
        content = content.replace(begin_marker, '', 1)
    if end_summary:
        content = content.replace(end_marker, '', 1)

    instance._content = content
    instance._summary = summary
    instance.has_summary = True


def run_plugin(generators):
    for generator in generators:
        if isinstance(generator, ArticlesGenerator):
            for article in generator.articles:
                extract_summary(article)
        elif isinstance(generator, PagesGenerator):
            for page in generator.pages:
                extract_summary(page)


def register():
    signals.initialized.connect(initialized)
    try:
        signals.all_generators_finalized.connect(run_plugin)
    except AttributeError:
        # NOTE: This results in #314 so shouldn't really be relied on
        # https://github.com/getpelican/pelican-plugins/issues/314
        signals.content_object_init.connect(extract_summary)

@@ -2488,16 +2488,2447 @@
 (page)%0A%0A
+    def _get_content(self):%0A        content = self._content%0A        if self.settings%5B'SUMMARY_BEGIN_MARKER'%5D:%0A            content = content.replace(%0A                self.settings%5B'SUMMARY_BEGIN_MARKER'%5D, '', 1)%0A        if self.settings%5B'SUMMARY_END_MARKER'%5D:%0A            content = content.replace(%0A                self.settings%5B'SUMMARY_END_MARKER'%5D, '', 1)%0A        return content%0A    instance._get_content = types.MethodType(_get_content, instance)%0A%0A    # extract out our summary%0A    if not hasattr(instance, '_summary') and instance._content is not None:%0A        content = instance._content%0A        begin_summary = -1%0A        end_summary = -1%0A        last_end = 0%0A        summary = None%0A        while last_end != -1 and last_end %3C len(content):%0A            if instance.settings%5B'SUMMARY_BEGIN_MARKER'%5D:%0A                begin_summary = content.find(instance.settings%5B'SUMMARY_BEGIN_MARKER'%5D, last_end)%0A            if instance.settings%5B'SUMMARY_END_MARKER'%5D:%0A                end_summary = content.find(instance.settings%5B'SUMMARY_END_MARKER'%5D, last_end)%0A            if end_summary == -1:%0A                last_end = -1%0A            else:%0A                last_end = end_summary + len(instance.settings%5B'SUMMARY_END_MARKER'%5D)%0A            if summary is None:  # first time%0A                if begin_summary != -1 or end_summary != -1:%0A                    # the beginning position has to take into account the length%0A                    # of the marker%0A                    begin_summary = (begin_summary +%0A                                    len(instance.settings%5B'SUMMARY_BEGIN_MARKER'%5D)%0A                                    if begin_summary != -1 else 0)%0A                    end_summary = end_summary if end_summary != -1 else None%0A                    summary = instance._update_content(content%5Bbegin_summary:end_summary%5D, instance._context.get('localsiteurl', ''))%0A            else:%0A                if begin_summary == -1 or end_summary == -1:%0A                    break%0A                begin_summary = (begin_summary +%0A                                len(instance.settings%5B'SUMMARY_BEGIN_MARKER'%5D)%0A                                if begin_summary != -1 else 0)%0A                end_summary = end_summary if end_summary != -1 else None%0A                summary += instance._update_content(content%5Bbegin_summary:end_summary%5D, instance._context.get('localsiteurl', ''))%0A        if summary is not None:%0A            instance._summary = summary%0A
 %0Adef reg

support multiple summary block merge