예제 #1
0
    def test_strftime_locale_dependent_turkish(self):
        # store current locale
        old_locale = locale.setlocale(locale.LC_TIME)

        if platform == "win32":
            locale.setlocale(locale.LC_TIME, str("Turkish"))
        else:
            locale.setlocale(locale.LC_TIME, str("tr_TR.UTF-8"))

        d = datetime.date(2012, 8, 29)

        # simple
        self.assertEqual(utils.strftime(d, "%d %B %Y"), "29 Ağustos 2012")
        self.assertEqual(utils.strftime(d, "%A, %d %B %Y"), "Çarşamba, 29 Ağustos 2012")

        # with text
        self.assertEqual(
            utils.strftime(d, "Yayınlanma tarihi: %A, %d %B %Y"), "Yayınlanma tarihi: Çarşamba, 29 Ağustos 2012"
        )

        # non-ascii format candidate (someone might pass it... for some reason)
        self.assertEqual(utils.strftime(d, "%Y yılında %üretim artışı"), "2012 yılında %üretim artışı")

        # restore locale back
        locale.setlocale(locale.LC_TIME, old_locale)
예제 #2
0
    def test_strftime_locale_dependent_french(self):
        # store current locale
        old_locale = locale.setlocale(locale.LC_TIME)

        if platform == "win32":
            locale.setlocale(locale.LC_TIME, str("French"))
        else:
            locale.setlocale(locale.LC_TIME, str("fr_FR.UTF-8"))

        d = datetime.date(2012, 8, 29)

        # simple
        self.assertEqual(utils.strftime(d, "%d %B %Y"), "29 août 2012")

        # depending on OS, the first letter is m or M
        self.assertTrue(utils.strftime(d, "%A") in ("mercredi", "Mercredi"))

        # with text
        self.assertEqual(utils.strftime(d, "Écrit le %d %B %Y"), "Écrit le 29 août 2012")

        # non-ascii format candidate (someone might pass it... for some reason)
        self.assertEqual(utils.strftime(d, "%écrits en %Y"), "%écrits en 2012")

        # restore locale back
        locale.setlocale(locale.LC_TIME, old_locale)
예제 #3
0
    def test_strftime_locale_dependent_french(self):
        # store current locale
        old_locale = locale.setlocale(locale.LC_ALL)

        if platform == 'win32':
            locale.setlocale(locale.LC_ALL, str('French'))
        else:
            locale.setlocale(locale.LC_ALL, str('fr_FR.UTF-8'))

        d = utils.SafeDatetime(2012, 8, 29)

        # simple
        self.assertEqual(utils.strftime(d, '%d %B %Y'), '29 août 2012')

        # depending on OS, the first letter is m or M
        self.assertTrue(utils.strftime(d, '%A') in ('mercredi', 'Mercredi'))

        # with text
        self.assertEqual(
            utils.strftime(d, 'Écrit le %d %B %Y'),
            'Écrit le 29 août 2012')

        # non-ascii format candidate (someone might pass it… for some reason)
        self.assertEqual(
            utils.strftime(d, '%écrits en %Y'),
            '%écrits en 2012')

        # restore locale back
        locale.setlocale(locale.LC_ALL, old_locale)
예제 #4
0
    def test_strftime_locale_dependent_turkish(self):
        # store current locale
        old_locale = locale.setlocale(locale.LC_ALL)

        if platform == 'win32':
            locale.setlocale(locale.LC_ALL, str('Turkish'))
        else:
            locale.setlocale(locale.LC_ALL, str('tr_TR.UTF-8'))

        d = utils.SafeDatetime(2012, 8, 29)

        # simple
        self.assertEqual(utils.strftime(d, '%d %B %Y'), '29 Ağustos 2012')
        self.assertEqual(utils.strftime(d, '%A, %d %B %Y'),
                         'Çarşamba, 29 Ağustos 2012')

        # with text
        self.assertEqual(
            utils.strftime(d, 'Yayınlanma tarihi: %A, %d %B %Y'),
            'Yayınlanma tarihi: Çarşamba, 29 Ağustos 2012')

        # non-ascii format candidate (someone might pass it… for some reason)
        self.assertEqual(
            utils.strftime(d, '%Y yılında %üretim artışı'),
            '2012 yılında %üretim artışı')

        # restore locale back
        locale.setlocale(locale.LC_ALL, old_locale)
예제 #5
0
    def test_strftime_locale_dependent_french(self):
        # store current locale
        old_locale = locale.setlocale(locale.LC_ALL)

        if platform == 'win32':
            locale.setlocale(locale.LC_ALL, str('French'))
        else:
            locale.setlocale(locale.LC_ALL, str('fr_FR.UTF-8'))

        d = utils.SafeDatetime(2012, 8, 29)

        # simple
        self.assertEqual(utils.strftime(d, '%d %B %Y'), '29 août 2012')

        # depending on OS, the first letter is m or M
        self.assertTrue(utils.strftime(d, '%A') in ('mercredi', 'Mercredi'))

        # with text
        self.assertEqual(utils.strftime(d, 'Écrit le %d %B %Y'),
                         'Écrit le 29 août 2012')

        # non-ascii format candidate (someone might pass it… for some reason)
        self.assertEqual(utils.strftime(d, '%écrits en %Y'), '%écrits en 2012')

        # restore locale back
        locale.setlocale(locale.LC_ALL, old_locale)
예제 #6
0
    def test_strftime_locale_dependent_turkish(self):
        # store current locale
        old_locale = locale.setlocale(locale.LC_ALL)

        if platform == 'win32':
            locale.setlocale(locale.LC_ALL, 'Turkish')
        else:
            locale.setlocale(locale.LC_ALL, 'tr_TR.UTF-8')

        d = utils.SafeDatetime(2012, 8, 29)

        # simple
        self.assertEqual(utils.strftime(d, '%d %B %Y'), '29 Ağustos 2012')
        self.assertEqual(utils.strftime(d, '%A, %d %B %Y'),
                         'Çarşamba, 29 Ağustos 2012')

        # with text
        self.assertEqual(
            utils.strftime(d, 'Yayınlanma tarihi: %A, %d %B %Y'),
            'Yayınlanma tarihi: Çarşamba, 29 Ağustos 2012')

        # non-ascii format candidate (someone might pass it… for some reason)
        self.assertEqual(
            utils.strftime(d, '%Y yılında %üretim artışı'),
            '2012 yılında %üretim artışı')

        # restore locale back
        locale.setlocale(locale.LC_ALL, old_locale)
예제 #7
0
    def __init__(self, *args, **kwargs):
        super(Session, self).__init__(*args, **kwargs)

        has_duration = hasattr(self, 'duration')
        has_start_date = hasattr(self, 'start_date')

        if has_duration:
            d = self.duration.split(' ')
            if len(d) <> 2 or d[1] <> "minutes":
                logger.error("Unknown duration format: %s", self.duration)
            self.duration = timedelta(minutes=int(d[0]))

        if has_start_date:
            self.start_date = get_date(self.start_date)
            self.locale_start_date = strftime(self.start_date, "%A %d")
            self.locale_start_time = strftime(self.start_date, "%H:%M")

        if has_duration and has_start_date:
            self.end_date = self.start_date + self.duration
            self.locale_end_time = strftime(self.end_date, "%H:%M")


        if not hasattr(self, 'bios'):
            bios = conference.bios.by_role_and_slug['speaker']
            self.bios = []
            for speaker in self.speakers:
                slug = slugify(speaker)
                self.bios.append(slug)
                if slug not in bios:
                    bio = Bio("", {'title': speaker}, settings=self.settings,
                                source_path="", context=self._context)
                    conference.add_bio(bio)
예제 #8
0
def filetime_from_hg(content):
    if isinstance(content, contents.Static):
        return
    if "date" in content.metadata:
        # if user did explicitely set a date, do not overwrite it
        return
    repo = hglib.open(".")
    tz_name = content.settings.get("TIMEZONE", None)

    hgtime = content.metadata.get("hgtime", "yes").lower()
    if hgtime in ("no", "off", "false", "0"):
        return

    # 1. file is not managed by hg
    #    date: fs time
    # 2. file is staged, but has no commits
    #    date: fs time
    # 3. file is managed, and clean
    #    date: first commit time, update: last commit time or None
    # 4. file is managed, but dirty
    #    date: first commit time, update: fs time
    path = content.source_path
    root = repo.root()
    filelog = repo.log(
        revrange=".:0",
        files=[
            path,
        ],
        follow=content.settings.get("HG_FILETIME_FOLLOW", False),
    )
    if filelog:
        # has commited
        content.date = set_date_tzinfo(filelog[-1][6], tz_name)
        if path in [
                os.path.join(root, mfile)
                for flag, mfile in repo.status(modified=True)
        ]:
            # file is modified in the wd
            content.modified = datetime_from_timestamp(
                os.stat(path).st_ctime, content)
        else:
            # file is not changed
            if len(filelog) > 1:
                content.modified = set_date_tzinfo(filelog[0][6], tz_name)
    else:
        # file is not managed by hg
        content.date = datetime_from_timestamp(os.stat(path).st_ctime, content)

    if not hasattr(content, "modified"):
        content.modified = content.date

    content.locale_date = strftime(content.date, content.date_format)
    content.locale_modified = strftime(content.modified, content.date_format)
    # ensure the metadata directory is synchronized. Might be used by
    # some other plugin (eg. series)
    content.metadata["modified"] = content.modified
    content.metadata["date"] = content.date
예제 #9
0
def filetime_from_git(content):
    if isinstance(content, contents.Static):
        return

    git = git_wrapper('.')
    tz_name = content.settings.get('TIMEZONE', None)

    gittime = content.metadata.get('gittime', 'yes').lower()
    gittime = gittime.replace("false", "no").replace("off", "no")
    if gittime == "no":
        return

    # 1. file is not managed by git
    #    date: fs time
    # 2. file is staged, but has no commits
    #    date: fs time
    # 3. file is managed, and clean
    #    date: first commit time, update: last commit time or None
    # 4. file is managed, but dirty
    #    date: first commit time, update: fs time
    path = content.source_path
    if git.is_file_managed_by_git(path):
        commits = git.get_commits(
            path, follow=content.settings.get('GIT_FILETIME_FOLLOW', False))

        if len(commits) == 0:
            # never commited, but staged
            content.date = datetime_from_timestamp(
                os.stat(path).st_ctime, content)
        else:
            # has commited
            content.date = git.get_commit_date(
                commits[-1], tz_name)

            if git.is_file_modified(path):
                # file has changed
                content.modified = datetime_from_timestamp(
                    os.stat(path).st_ctime, content)
            else:
                # file is not changed
                if len(commits) > 1:
                    content.modified = git.get_commit_date(
                        commits[0], tz_name)
    else:
        # file is not managed by git
        content.date = datetime_from_timestamp(os.stat(path).st_ctime, content)

    if not hasattr(content, 'modified'):
        content.modified = content.date

    if hasattr(content, 'date'):
        content.locale_date = strftime(content.date, content.date_format)

    if hasattr(content, 'modified'):
        content.locale_modified = strftime(
            content.modified, content.date_format)
예제 #10
0
def tag_article(instance):
    if not isinstance(instance, contents.Article):
        return

    ogtags = [('og:title', instance.title),
              ('og:type', 'article')]

    image = instance.metadata.get('og_image', '')
    if image:
        ogtags.append(('og:image', image))
    else:
        soup = BeautifulSoup(instance._content, 'html.parser')
        img_links = soup.find_all('img')
        if  (len(img_links) > 0):
            img_src = img_links[0].get('src')
            if not "http" in img_src:
                if instance.settings.get('SITEURL', ''):
                    img_src = instance.settings.get('SITEURL', '') + "/" + img_src
            ogtags.append(('og:image', img_src))

    url = os.path.join(instance.settings.get('SITEURL', ''), instance.url)
    ogtags.append(('og:url', url))

    ogtags.append(('og:description', instance.metadata.get('og_description',
                                                           instance.metadata.get('summary',
                                                                                 instance.summary))))

    default_locale = instance.settings.get('LOCALE', [])
    if default_locale:
        default_locale = default_locale[0]
    else:
        default_locale = ''
    ogtags.append(('og:locale', instance.metadata.get('og_locale', default_locale)))

    ogtags.append(('og:site_name', instance.settings.get('SITENAME', '')))

    ogtags.append(('article:published_time', strftime(instance.date, "%Y-%m-%d")))
    
    if hasattr(instance, 'modified'):
        ogtags.append(('article:modified_time', strftime(instance.modified, "%Y-%m-%d")))

    author_fb_profiles = instance.settings.get('AUTHOR_FB_ID', {})
    if len(author_fb_profiles) > 0:
        for author in instance.authors:
            if author.name in author_fb_profiles:
                ogtags.append(('article:author', author_fb_profiles[author.name]))

    ogtags.append(('article:section', instance.category.name))

    try:
        for tag in instance.tags:
            ogtags.append(('article:tag', tag.name))
    except AttributeError:
            pass

    instance.ogtags = ogtags
예제 #11
0
def filetime_from_git(content):
    if isinstance(content, contents.Static):
        return

    git = git_wrapper('.')
    tz_name = content.settings.get('TIMEZONE', None)

    gittime = content.metadata.get('gittime', 'yes').lower()
    gittime = gittime.replace("false", "no").replace("off", "no")
    if gittime == "no":
        return

    # 1. file is not managed by git
    #    date: fs time
    # 2. file is staged, but has no commits
    #    date: fs time
    # 3. file is managed, and clean
    #    date: first commit time, update: last commit time or None
    # 4. file is managed, but dirty
    #    date: first commit time, update: fs time
    path = content.source_path
    if git.is_file_managed_by_git(path):
        commits = git.get_commits(path,
                                  follow=content.settings.get(
                                      'GIT_FILETIME_FOLLOW', False))

        if len(commits) == 0:
            # never commited, but staged
            content.date = git.datetime_from_timestamp(
                os.stat(path).st_ctime, content)
        else:
            # has commited
            content.date = git.get_commit_date(commits[-1], tz_name)

            if git.is_file_modified(path):
                # file has changed
                content.modified = datetime_from_timestamp(
                    os.stat(path).st_ctime, content)
            else:
                # file is not changed
                if len(commits) > 1:
                    content.modified = git.get_commit_date(commits[0], tz_name)
    else:
        # file is not managed by git
        content.date = datetime_from_timestamp(os.stat(path).st_ctime, content)

    if not hasattr(content, 'modified'):
        content.modified = content.date

    if hasattr(content, 'date'):
        content.locale_date = strftime(content.date, content.date_format)

    if hasattr(content, 'modified'):
        content.locale_modified = strftime(content.modified,
                                           content.date_format)
예제 #12
0
    def test_strftime(self):
        d = utils.SafeDatetime(2012, 8, 29)

        # simple formatting
        self.assertEqual(utils.strftime(d, '%d/%m/%y'), '29/08/12')
        self.assertEqual(utils.strftime(d, '%d/%m/%Y'), '29/08/2012')

        # RFC 3339
        self.assertEqual(utils.strftime(d, '%Y-%m-%dT%H:%M:%SZ'),
                         '2012-08-29T00:00:00Z')

        # % escaped
        self.assertEqual(utils.strftime(d, '%d%%%m%%%y'), '29%08%12')
        self.assertEqual(utils.strftime(d, '%d %% %m %% %y'), '29 % 08 % 12')
        # not valid % formatter
        self.assertEqual(utils.strftime(d, '10% reduction in %Y'),
                         '10% reduction in 2012')
        self.assertEqual(utils.strftime(d, '%10 reduction in %Y'),
                         '%10 reduction in 2012')

        # with text
        self.assertEqual(utils.strftime(d, 'Published in %d-%m-%Y'),
                         'Published in 29-08-2012')

        # with non-ascii text
        self.assertEqual(
            utils.strftime(d, '%d/%m/%Y Øl trinken beim Besäufnis'),
            '29/08/2012 Øl trinken beim Besäufnis')
예제 #13
0
def filetime_from_git(content, git_content):
    '''
    Update modification and creation times from git
    '''
    if not content.settings['GIT_FILETIME_FROM_GIT']:
        # Disabled for everything
        return

    if not string_to_bool(content.metadata.get('gittime', 'yes')):
        # Disable for this content
        return

    path = content.source_path
    fs_creation_time = datetime_from_timestamp(os.stat(path).st_ctime, content)
    fs_modified_time = datetime_from_timestamp(os.stat(path).st_mtime, content)

    # 1. file is not managed by git
    #    date: fs time
    # 2. file is staged, but has no commits
    #    date: fs time
    # 3. file is managed, and clean
    #    date: first commit time, update: last commit time or None
    # 4. file is managed, but dirty
    #    date: first commit time, update: fs time
    if git_content.is_managed_by_git():
        if git_content.is_committed():
            if not hasattr(content, 'date'):
                content.date = git_content.get_oldest_commit_date()

            if git_content.is_modified():
                content.modified = fs_modified_time
            else:
                content.modified = git_content.get_newest_commit_date()
        else:
            # File isn't committed
            if not hasattr(content, 'date'):
                content.date = fs_creation_time
            content.modified = fs_creation_time

    else:
        # file is not managed by git
        if not hasattr(content, 'date'):
            content.date = fs_creation_time
        content.modified = fs_creation_time

    # Clean up content attributes
    if not hasattr(content, 'modified'):
        content.modified = content.date

    if hasattr(content, 'date'):
        content.locale_date = strftime(content.date, content.date_format)

    if hasattr(content, 'modified'):
        content.locale_modified = strftime(content.modified,
                                           content.date_format)
예제 #14
0
파일: test_utils.py 프로젝트: 0xMF/pelican
    def test_strftime(self):
        d = utils.SafeDatetime(2012, 8, 29)

        # simple formatting
        self.assertEqual(utils.strftime(d, '%d/%m/%y'), '29/08/12')
        self.assertEqual(utils.strftime(d, '%d/%m/%Y'), '29/08/2012')

        # RFC 3339
        self.assertEqual(utils.strftime(d, '%Y-%m-%dT%H:%M:%SZ'),'2012-08-29T00:00:00Z')

        # % escaped
        self.assertEqual(utils.strftime(d, '%d%%%m%%%y'), '29%08%12')
        self.assertEqual(utils.strftime(d, '%d %% %m %% %y'), '29 % 08 % 12')
        # not valid % formatter
        self.assertEqual(utils.strftime(d, '10% reduction in %Y'),
                         '10% reduction in 2012')
        self.assertEqual(utils.strftime(d, '%10 reduction in %Y'),
                         '%10 reduction in 2012')

        # with text
        self.assertEqual(utils.strftime(d, 'Published in %d-%m-%Y'),
                         'Published in 29-08-2012')

        # with non-ascii text
        self.assertEqual(utils.strftime(d, '%d/%m/%Y Øl trinken beim Besäufnis'),
                         '29/08/2012 Øl trinken beim Besäufnis')
예제 #15
0
def filetime_from_git(content):
    if isinstance(content, contents.Static) or repo is None:
        return
    gittime = content.metadata.get('gittime', 'yes').lower()
    gittime = gittime.replace("false", "no").replace("off", "no")
    if gittime == "no":
        return
    # 1. file is not managed by git
    #    date: fs time
    # 2. file is staged, but has no commits
    #    date: fs time
    # 3. file is managed, and clean
    #    date: first commit time, update: last commit time or None
    # 4. file is managed, but dirty
    #    date: first commit time, update: fs time
    path = content.source_path
    status, stdout, stderr = git.execute(
        ['git', 'ls-files', path, '--error-unmatch'],
        with_extended_output=True,
        with_exceptions=False)
    if status != 0:
        # file is not managed by git
        content.date = datetime.fromtimestamp(os.stat(path).st_ctime)
    else:
        # file is managed by git
        commits = repo.commits(path=path)
        if len(commits) == 0:
            # never commited, but staged
            content.date = datetime.fromtimestamp(os.stat(path).st_ctime)
        else:
            # has commited
            content.date = datetime.fromtimestamp(
                mktime(commits[-1].committed_date) - altzone)

            status, stdout, stderr = git.execute(
                ['git', 'diff', '--quiet', 'HEAD', path],
                with_extended_output=True,
                with_exceptions=False)
            if status != 0:
                # file has changed
                content.modified = datetime.fromtimestamp(
                    os.stat(path).st_ctime)
            else:
                # file is not changed
                if len(commits) > 1:
                    content.modified = datetime.fromtimestamp(
                        mktime(commits[0].committed_date) - altzone)
    if not hasattr(content, 'modified'):
        content.modified = content.date
    if hasattr(content, 'date'):
        content.locale_date = strftime(content.date, content.date_format)
    if hasattr(content, 'modified'):
        content.locale_modified = strftime(content.modified,
                                           content.date_format)
def filetime_from_hg(content):
    if isinstance(content, contents.Static):
        return
    if 'date' in content.metadata:
        # if user did explicitely set a date, do not overwrite it
        return
    repo = hglib.open('.')
    tz_name = content.settings.get('TIMEZONE', None)

    hgtime = content.metadata.get('hgtime', 'yes').lower()
    if hgtime in ('no', 'off', 'false', '0'):
        return

    # 1. file is not managed by hg
    #    date: fs time
    # 2. file is staged, but has no commits
    #    date: fs time
    # 3. file is managed, and clean
    #    date: first commit time, update: last commit time or None
    # 4. file is managed, but dirty
    #    date: first commit time, update: fs time
    path = content.source_path
    root = repo.root()
    filelog = repo.log(revrange='.:0', files=[path,],
                       follow=content.settings.get('HG_FILETIME_FOLLOW', False))
    if filelog:
        # has commited
        content.date = set_date_tzinfo(filelog[-1][6], tz_name)
        if path in [os.path.join(root, mfile) for flag, mfile in repo.status(modified=True)]:
            # file is modified in the wd
            content.modified = datetime_from_timestamp(
                os.stat(path).st_ctime, content)
        else:
            # file is not changed
            if len(filelog) > 1:
                content.modified = set_date_tzinfo(filelog[0][6], tz_name)
    else:
        # file is not managed by hg
        content.date = datetime_from_timestamp(os.stat(path).st_ctime, content)

    if not hasattr(content, 'modified'):
        content.modified = content.date

    content.locale_date = strftime(content.date, content.date_format)
    content.locale_modified = strftime(content.modified, content.date_format)
    # ensure the metadata directory is synchronized. Might be used by
    # some other plugin (eg. series)
    content.metadata['modified'] = content.modified
    content.metadata['date'] = content.date
예제 #17
0
def generate_date(content):
    mode = content.settings.get('GIT_TIME_LAST_MODIFIED', False)
    date = git_mtime(content.source_path, mode)

    if not date:
        # This happens usually when the file is not checked into git yet. It
        # is probably a new file and 'now' should be the correct time
        date = [datetime.now(),datetime.now()]

    content.metadata['date'] = date[0]
    content.metadata['update'] = date[1]
    content.date = date[0]
    content.update = date[1]
    content.locale_date = strftime(content.date, content.date_format)
    content.locale_update = strftime(content.update, content.date_format)
예제 #18
0
def filetime_from_git(content, git_content):
    """
    Update modification and creation times from git
    """
    if not content.settings["GIT_FILETIME_FROM_GIT"]:
        # Disabled for everything
        return

    if not string_to_bool(content.metadata.get("gittime", "yes")):
        # Disable for this content
        return

    path = content.source_path
    fs_creation_time = datetime_from_timestamp(os.stat(path).st_ctime, content)
    fs_modified_time = datetime_from_timestamp(os.stat(path).st_mtime, content)

    # 1. file is not managed by git
    #    date: fs time
    # 2. file is staged, but has no commits
    #    date: fs time
    # 3. file is managed, and clean
    #    date: first commit time, update: last commit time or None
    # 4. file is managed, but dirty
    #    date: first commit time, update: fs time
    if git_content.is_managed_by_git():
        if git_content.is_committed():
            content.date = git_content.get_oldest_commit_date()

            if git_content.is_modified():
                content.modified = fs_modified_time
            else:
                content.modified = git_content.get_newest_commit_date()
        else:
            # File isn't committed
            content.date = fs_creation_time
    else:
        # file is not managed by git
        content.date = fs_creation_time

    # Clean up content attributes
    if not hasattr(content, "modified"):
        content.modified = content.date

    if hasattr(content, "date"):
        content.locale_date = strftime(content.date, content.date_format)

    if hasattr(content, "modified"):
        content.locale_modified = strftime(content.modified, content.date_format)
예제 #19
0
def filetime_from_git(content):
    if isinstance(content, contents.Static) or repo is None:
        return
    gittime = content.metadata.get("gittime", "yes").lower()
    gittime = gittime.replace("false", "no").replace("off", "no")
    if gittime == "no":
        return
    # 1. file is not managed by git
    #    date: fs time
    # 2. file is staged, but has no commits
    #    date: fs time
    # 3. file is managed, and clean
    #    date: first commit time, update: last commit time or None
    # 4. file is managed, but dirty
    #    date: first commit time, update: fs time
    path = content.source_path
    status, stdout, stderr = git.execute(
        ["git", "ls-files", path, "--error-unmatch"], with_extended_output=True, with_exceptions=False
    )
    if status != 0:
        # file is not managed by git
        content.date = datetime_from_timestamp(os.stat(path).st_ctime, content)
    else:
        # file is managed by git
        commits = repo.commits(path=path)
        if len(commits) == 0:
            # never commited, but staged
            content.date = datetime_from_timestamp(os.stat(path).st_ctime, content)
        else:
            # has commited
            content.date = datetime_from_timestamp(mktime(commits[-1].committed_date) - altzone, content)

            status, stdout, stderr = git.execute(
                ["git", "diff", "--quiet", "HEAD", path], with_extended_output=True, with_exceptions=False
            )
            if status != 0:
                # file has changed
                content.modified = datetime_from_timestamp(os.stat(path).st_ctime, content)
            else:
                # file is not changed
                if len(commits) > 1:
                    content.modified = datetime_from_timestamp(mktime(commits[0].committed_date) - altzone, content)
    if not hasattr(content, "modified"):
        content.modified = content.date
    if hasattr(content, "date"):
        content.locale_date = strftime(content.date, content.date_format)
    if hasattr(content, "modified"):
        content.locale_modified = strftime(content.modified, content.date_format)
예제 #20
0
    def test_turkish_locale(self):
        if platform == 'win32':
            locale_string = 'Turkish'
        else:
            locale_string = 'tr_TR.UTF-8'
        settings = read_settings(
            override={
                'LOCALE': locale_string,
                'TEMPLATE_PAGES': {
                    'template/source.html': 'generated/file.html'
                }
            })

        generator = TemplatePagesGenerator(
            {'date': self.date}, settings,
            self.temp_content, '', self.temp_output)
        generator.env.filters.update({'strftime': utils.DateFormatter()})

        writer = Writer(self.temp_output, settings=settings)
        generator.generate_output(writer)

        output_path = os.path.join(
            self.temp_output, 'generated', 'file.html')

        # output file has been generated
        self.assertTrue(os.path.exists(output_path))

        # output content is correct
        with utils.pelican_open(output_path) as output_file:
            self.assertEqual(output_file,
                             utils.strftime(self.date, 'date = %A, %d %B %Y'))
예제 #21
0
def pelican_format_date(date, metadata, settings):
    """
    Format the updated date based on the configured local
    This function was adapted from Pelican's Content class in v3.3
    """
    lang = None

    if 'lang' in metadata:
        lang = metadata['lang'].lower()
    elif 'DEFAULT_LANG' in settings:
        lang = settings['DEFAULT_LANG'].lower()

    if lang is not None and lang in settings['DATE_FORMATS']:
        date_format = settings['DATE_FORMATS'][lang]
    else:
        date_format = settings['DEFAULT_DATE_FORMAT']

    if isinstance(date_format, tuple):
        locale_string = date_format[0]
        if sys.version_info < (3, ) and isinstance(locale_string,
                                                   six.text_type):
            locale_string = locale_string.encode('ascii')
        locale.setlocale(locale.LC_ALL, locale_string)
        date_format = date_format[1]

    return strftime(date, date_format)
예제 #22
0
    def test_turkish_locale(self):
        if platform == 'win32':
            locale_string = 'Turkish'
        else:
            locale_string = 'tr_TR.UTF-8'
        settings = read_settings(
            override={
                'LOCALE': locale_string,
                'TEMPLATE_PAGES': {
                    'template/source.html': 'generated/file.html'
                }
            })

        generator = TemplatePagesGenerator({'date': self.date}, settings,
                                           self.temp_content, '',
                                           self.temp_output)
        generator.env.filters.update({'strftime': utils.DateFormatter()})

        writer = Writer(self.temp_output, settings=settings)
        generator.generate_output(writer)

        output_path = os.path.join(self.temp_output, 'generated', 'file.html')

        # output file has been generated
        self.assertTrue(os.path.exists(output_path))

        # output content is correct
        with utils.pelican_open(output_path) as output_file:
            self.assertEqual(output_file,
                             utils.strftime(self.date, 'date = %A, %d %B %Y'))
예제 #23
0
def tag_article(instance):
    if not isinstance(instance, contents.Article):
        return

    ogtags = [('og:title', instance.title),
              ('og:type', 'article')]

    image = instance.metadata.get('og_image', '')
    if image:
        ogtags.append(('og:image', image))

    url = os.path.join(instance.settings.get('SITEURL', ''), instance.url)
    ogtags.append(('og:url', url))

    ogtags.append(('og:description', instance.metadata.get('og_description',
                                                           instance.metadata.get('summary',
                                                                                 instance.summary))))

    default_locale = instance.settings.get('LOCALE', [])
    if default_locale:
        default_locale = default_locale[0]
    else:
        default_locale = ''
    ogtags.append(('og:locale', instance.metadata.get('og_locale', default_locale)))

    ogtags.append(('og:site_name', instance.settings.get('SITENAME', '')))

    ogtags.append(('article:published_time', strftime(instance.date, "%Y-%m-%d")))
    
    if hasattr(instance, 'modified'):
        ogtags.append(('article:modified_time', strftime(instance.modified, "%Y-%m-%d")))

    author_fb_profiles = instance.settings.get('AUTHOR_FB_ID', {})
    if len(author_fb_profiles) > 0:
        for author in instance.authors:
            if author.name in author_fb_profiles:
                ogtags.append(('article:author', author_fb_profiles[author.name]))

    ogtags.append(('article:section', instance.category.name))

    try:
        for tag in instance.tags:
            ogtags.append(('article:tag', tag.name))
    except AttributeError:
            pass

    instance.ogtags = ogtags
예제 #24
0
def tag_article(instance):
    if not isinstance(instance, contents.Article):
        return

    ogtags = [('og:title', instance.title),
              ('og:type', 'article')]

    image = instance.metadata.get('og_image', '')
    if image:
        ogtags.append(('og:image', image))

    url = os.path.join(instance.settings.get('SITEURL', ''), instance.url)
    ogtags.append(('og:url', url))

    ogtags.append(('og:description', instance.metadata.get('og_description',
                                                           instance.metadata.get('summary',
                                                                                 ''))))

    default_locale = instance.settings.get('LOCALE', [])
    if default_locale:
        default_locale = default_locale[0]
    else:
        default_locale = ''
    ogtags.append(('og:locale', instance.metadata.get('og_locale', default_locale)))

    ogtags.append(('og:site_name', instance.settings.get('SITENAME', '')))

    ogtags.append(('article:published_time', strftime(instance.date, "%Y-%m-%d")))
    if instance.metadata.get('modified'):
        ogtags.append(('article:modified_time', strftime(instance.modified, "%Y-%m-%d")))

    author_fb_profiles = instance.settings.get('AUTHOR_FB_ID', {})
    if len(author_fb_profiles) > 0:
        for author in instance.authors:
            if author.name in author_fb_profiles:
                ogtags.append(('article:author', author_fb_profiles[author.name]))

    ogtags.append(('article:section', instance.category.name))

    for tag in instance.tags:
        ogtags.append(('article:tag', tag.name))

    instance.ogtags = ogtags
예제 #25
0
def autocorrect_date_by_filetime(content):
    if isinstance(content, contents.Static):
        return

    path = content.source_path
    fs_creation_time = datetime_from_timestamp(os.stat(path).st_ctime, content)

    if not hasattr(content, 'date'):
        content.date = fs_creation_time
        content.locale_date = utils.strftime(content.date, content.date_format)
예제 #26
0
 def test_french_strftime(self):
     # This test tries to reproduce an issue that occured with python3.3 under macos10 only
     locale.setlocale(locale.LC_ALL, str('fr_FR.UTF-8'))
     date = utils.SafeDatetime(2014,8,14)
     # we compare the lower() dates since macos10 returns "Jeudi" for %A whereas linux reports "jeudi"
     self.assertEqual( u'jeudi, 14 août 2014', utils.strftime(date, date_format="%A, %d %B %Y").lower() )
     df = utils.DateFormatter()
     self.assertEqual( u'jeudi, 14 août 2014', df(date, date_format="%A, %d %B %Y").lower() )
     # Let us now set the global locale to C:
     locale.setlocale(locale.LC_ALL, str('C'))
     # DateFormatter should still work as expected since it is the whole point of DateFormatter
     # (This is where pre-2014/4/15 code fails on macos10)
     df_date = df(date, date_format="%A, %d %B %Y").lower()
     self.assertEqual( u'jeudi, 14 août 2014', df_date )
예제 #27
0
def add_static_comments(gen, metadata):
    if gen.settings['PELICAN_COMMENT_SYSTEM'] != True:
        return

    metadata['comments_count'] = 0
    metadata['comments'] = []

    if not 'slug' in metadata:
        logger.warning(
            "pelican_comment_system: cant't locate comments files without slug tag in the article"
        )
        return

    reader = MarkdownReader(gen.settings)
    comments = []
    replies = []
    folder = os.path.join(gen.settings['PELICAN_COMMENT_SYSTEM_DIR'],
                          metadata['slug'])

    if not os.path.isdir(folder):
        logger.debug("No comments found for: " + metadata['slug'])
        return

    for file in os.listdir(folder):
        name, extension = os.path.splitext(file)
        if extension[1:].lower() in reader.file_extensions:
            content, meta = reader.read(folder + "/" + file)
            meta['locale_date'] = strftime(meta['date'],
                                           gen.settings['DEFAULT_DATE_FORMAT'])
            com = Comment(name, meta, content)
            if 'replyto' in meta:
                replies.append(com)
            else:
                comments.append(com)

    #TODO: Fix this O(n²) loop
    for reply in replies:
        for comment in chain(comments, replies):
            if comment.id == reply.metadata['replyto']:
                comment.addReply(reply)

    count = 0
    for comment in comments:
        comment.sortReplies()
        count += comment.countReplies()

    comments = sorted(comments)

    metadata['comments_count'] = len(comments) + count
    metadata['comments'] = comments
def add_static_comments(gen, metadata):
	if gen.settings['PELICAN_COMMENT_SYSTEM'] != True:
		return

	metadata['comments_count'] = 0
	metadata['comments'] = []

	if not 'slug' in metadata:
		logger.warning("pelican_comment_system: cant't locate comments files without slug tag in the article")
		return

	reader = MarkdownReader(gen.settings)
	comments = []
	replies = []
	folder = os.path.join(gen.settings['PELICAN_COMMENT_SYSTEM_DIR'], metadata['slug'])

	if not os.path.isdir(folder):
		logger.debug("No comments found for: " + metadata['slug'])
		return

	for file in os.listdir(folder):
		name, extension = os.path.splitext(file)
		if extension[1:].lower() in reader.file_extensions:
			content, meta = reader.read(folder + "/" + file)
			meta['locale_date'] = strftime(meta['date'], gen.settings['DEFAULT_DATE_FORMAT'])
			com = Comment(name, meta, content)
			if 'replyto' in meta:
				replies.append( com )
			else:
				comments.append( com )

	#TODO: Fix this O(n²) loop
	for reply in replies:
		for comment in chain(comments, replies):
			if comment.id == reply.metadata['replyto']:
				comment.addReply(reply)

	count = 0
	for comment in comments:
		comment.sortReplies()
		count += comment.countReplies()

	comments = sorted(comments)

	metadata['comments_count'] = len(comments) + count
	metadata['comments'] = comments
예제 #29
0
    def test_strftime(self):
        d = datetime.date(2012, 8, 29)

        # simple formatting
        self.assertEqual(utils.strftime(d, "%d/%m/%y"), "29/08/12")
        self.assertEqual(utils.strftime(d, "%d/%m/%Y"), "29/08/2012")

        # % escaped
        self.assertEqual(utils.strftime(d, "%d%%%m%%%y"), "29%08%12")
        self.assertEqual(utils.strftime(d, "%d %% %m %% %y"), "29 % 08 % 12")
        # not valid % formatter
        self.assertEqual(utils.strftime(d, "10% reduction in %Y"), "10% reduction in 2012")
        self.assertEqual(utils.strftime(d, "%10 reduction in %Y"), "%10 reduction in 2012")

        # with text
        self.assertEqual(utils.strftime(d, "Published in %d-%m-%Y"), "Published in 29-08-2012")

        # with non-ascii text
        self.assertEqual(
            utils.strftime(d, "%d/%m/%Y Øl trinken beim Besäufnis"), "29/08/2012 Øl trinken beim Besäufnis"
        )
예제 #30
0
def workoncontent(git,repo,content):
    gitcommits = git.log("--format=%H",content.source_path).split("\n")
    commits = []
    prevcommit = None
    prevcommitcontent = None
    for commithash in reversed(gitcommits):
        commit = repo.commit(commithash)
        tree = commit.tree
        passedcontent = False
        path = []
        for doc in content.source_path.split("/"):
            if doc == "content":
                passedcontent = True
            if passedcontent:
                path.append(doc)
                try:
                    tree = tree[doc]
                except:
                    return []
        commit_content = git.cat_file("-p",tree)
        if prevcommit == None or prevcommitcontent == None:
            diff = ["    :::markdown"]
            for i in commit_content.split("\n"):
                diff.append("     "+ i.decode('utf-8'))
            print_commit_content = "\n".join(diff)
        else:
            fromfile = ["a"] + path
            tofile = ["b"] + path
            diff = ["    :::diff"]
            for i in difflib.unified_diff(prevcommitcontent.split('\n'),commit_content.decode('utf-8').split('\n'),fromfile="/".join(fromfile),tofile="/".join(tofile)):
                diff.append("    " + i.strip("\n"))
            print_commit_content = "\n".join(diff)

        commits.append({
                'hash': commithash,
                'date': strftime(datetime.fromtimestamp(commit.authored_date), content.date_format),
                'message': commit.message,
                'content': _md.convert(print_commit_content)
            })
        prevcommit = commithash
        prevcommitcontent = commit_content.decode('utf-8')
    return commits
예제 #31
0
    def test_turkish_locale(self):
        settings = read_settings(
            override={
                "LOCALE": locale.normalize("tr_TR.UTF-8"),
                "TEMPLATE_PAGES": {"template/source.html": "generated/file.html"},
            }
        )

        generator = TemplatePagesGenerator({"date": self.date}, settings, self.temp_content, "", self.temp_output)
        generator.env.filters.update({"strftime": utils.DateFormatter()})

        writer = Writer(self.temp_output, settings=settings)
        generator.generate_output(writer)

        output_path = os.path.join(self.temp_output, "generated", "file.html")

        # output file has been generated
        self.assertTrue(os.path.exists(output_path))

        # output content is correct
        with utils.pelican_open(output_path) as output_file:
            self.assertEqual(output_file, utils.strftime(self.date, "date = %A, %d %B %Y"))
예제 #32
0
파일: multi_dates.py 프로젝트: kernc/www
def duplicate_on_dates(generator):
    """
    Articles (events) with `dates` property are recurring. Create a
    copy of the article for each date in `dates`.
    """
    articles = []
    for article in generator.articles:
        if not hasattr(article, 'dates'):
            articles.append(article)
            continue
        log.debug('Event {} has {} occurrences.'.format(article.get_relative_source_path(), len(article.dates)))
        for i, date in enumerate(article.dates, 2):
            event = copy(article)
            articles.append(event)
            event.slug += '--' + str(i)  # Create hopefully unique slug
            # The comment following '#' can be anything (e.g. visitor count)
            date, _, event.dates_comment = date.partition('#')
            # From pelican.contents.Content.__init__
            timezone = getattr(event, 'timezone', event.settings.get('TIMEZONE', 'UTC'))
            event.date = set_date_tzinfo(get_date(date), timezone)
            event.locale_date = strftime(event.date, event.date_format)
    articles.sort(key=attrgetter(generator.settings['ARTICLE_ORDER_BY']), reverse=True)
    generator.articles = articles
예제 #33
0
def filetime_from_git(content):
    if isinstance(content, contents.Static):
        logger.debug("Ignoring static content `%s'", content.source_path)
        return

    git = git_wrapper('.')
    tz_name = content.settings.get('TIMEZONE', None)

    # Do we want to retrieve the filetimes from Git?
    #
    # 1. The plugin can be disabled for a single content piece: If the content has a metadata field
    #    named "gittime" and this field is set to "off", "no" or "false" (case-insensitive), then
    #    we are not touching anything.
    gittime = content.metadata.get('gittime', 'yes').lower()
    gittime = gittime.replace("false", "no").replace("off", "no")
    if gittime == "no":
        logger.debug("Plugin explicitly disabled for `%s'", content.source_path)
        return

    # 2. Optionally, we only update those date fields which are not yet present.
    #    In other words, preserve the `date' and `modified' fields if they exist and fill them in
    #    from Git if they don't.
    only_missing_fields = content.settings.get("GIT_FILETIME_ONLY_IF_MISSING", False)
    if only_missing_fields and hasattr(content, "date") and hasattr(content, "modified"):
        logger.debug("`date' and `modified' fields are set for `%s', nothing to do.". content.source_path)
        return

    date = None
    modified = None

    # 1. file is not managed by git
    #    date: fs time
    # 2. file is staged, but has no commits
    #    date: fs time
    # 3. file is managed, and clean
    #    date: first commit time, update: last commit time or None
    # 4. file is managed, but dirty
    #    date: first commit time, update: fs time
    path = content.source_path
    if git.is_file_managed_by_git(path):
        commits = git.get_commits(
            path, follow=content.settings.get('GIT_FILETIME_FOLLOW', False))

        if len(commits) == 0:
            # never commited, but staged
            date = datetime_from_timestamp(
                os.stat(path).st_ctime, content)
        else:
            # has commited
            date = git.get_commit_date(
                commits[-1], tz_name)

            if git.is_file_modified(path):
                # file has changed
                modified = datetime_from_timestamp(
                    os.stat(path).st_ctime, content)
            else:
                # file is not changed
                if len(commits) > 1:
                    modified = git.get_commit_date(
                        commits[0], tz_name)
    else:
        # file is not managed by git
        date = datetime_from_timestamp(os.stat(path).st_ctime, content)

    if date is not None and (not hasattr(content, "date") or not only_missing_fields):
        # We got a creation date and the content either has no creation date set yet or
        # we want to override it.
        logger.debug("Setting `date' field from Git for `%s'", content.source_path)
        content.date = date

    if modified is not None and (not hasattr(content, "modified") or not only_missing_fields):
        # We got a modification date and the content either has no modification date set yet or
        # we want to override it.
        logger.debug("Setting `modified' field from Git for `%s'", content.source_path)
        content.modified = modified

    # Make sure we always have a `modified' field unless this behaviour has been disabled:
    if not hasattr(content, 'modified') and content.settings.get("GIT_FILETIME_ALWAYS_ADD_MODIFIED", True):
        content.modified = content.date

    # Make sure the `locale_date' field matches the `date' field:
    if hasattr(content, 'date'):
        content.locale_date = strftime(content.date, content.date_format)

    # Make sure the `locale_modified' field matches the `modified' field:
    if hasattr(content, 'modified'):
        content.locale_modified = strftime(
            content.modified, content.date_format)
예제 #34
0
    def __init__(self, content, metadata=None, settings=None,
                 source_path=None, context=None):
        if metadata is None:
            metadata = {}
        if settings is None:
            settings = copy.deepcopy(DEFAULT_CONFIG)

        self.settings = settings
        self._content = content
        if context is None:
            context = {}
        self._context = context
        self.translations = []

        local_metadata = dict(settings['DEFAULT_METADATA'])
        local_metadata.update(metadata)

        # set metadata as attributes
        for key, value in local_metadata.items():
            if key in ('save_as', 'url'):
                key = 'override_' + key
            setattr(self, key.lower(), value)

        # also keep track of the metadata attributes available
        self.metadata = local_metadata

        #default template if it's not defined in page
        self.template = self._get_template()

        # First, read the authors from "authors", if not, fallback to "author"
        # and if not use the settings defined one, if any.
        if not hasattr(self, 'author'):
            if hasattr(self, 'authors'):
                self.author = self.authors[0]
            elif 'AUTHOR' in settings:
                self.author = Author(settings['AUTHOR'], settings)

        if not hasattr(self, 'authors') and hasattr(self, 'author'):
            self.authors = [self.author]

        # XXX Split all the following code into pieces, there is too much here.

        # manage languages
        self.in_default_lang = True
        if 'DEFAULT_LANG' in settings:
            default_lang = settings['DEFAULT_LANG'].lower()
            if not hasattr(self, 'lang'):
                self.lang = default_lang

            self.in_default_lang = (self.lang == default_lang)

        # create the slug if not existing, from the title
        if not hasattr(self, 'slug') and hasattr(self, 'title'):
            self.slug = slugify(self.title,
                                settings.get('SLUG_SUBSTITUTIONS', ()))

        self.source_path = source_path

        # manage the date format
        if not hasattr(self, 'date_format'):
            if hasattr(self, 'lang') and self.lang in settings['DATE_FORMATS']:
                self.date_format = settings['DATE_FORMATS'][self.lang]
            else:
                self.date_format = settings['DEFAULT_DATE_FORMAT']

        if isinstance(self.date_format, tuple):
            locale_string = self.date_format[0]
            if sys.version_info < (3, ) and isinstance(locale_string,
                                                       six.text_type):
                locale_string = locale_string.encode('ascii')
            locale.setlocale(locale.LC_ALL, locale_string)
            self.date_format = self.date_format[1]

        if hasattr(self, 'date'):
            self.locale_date = strftime(self.date, self.date_format)
        if hasattr(self, 'modified'):
            self.locale_modified = strftime(self.modified, self.date_format)

        # manage status
        if not hasattr(self, 'status'):
            self.status = settings['DEFAULT_STATUS']
            if not settings['WITH_FUTURE_DATES']:
                if hasattr(self, 'date') and self.date > datetime.now():
                    self.status = 'draft'

        # store the summary metadata if it is set
        if 'summary' in metadata:
            self._summary = metadata['summary']

        signals.content_object_init.send(self)
예제 #35
0
    def __init__(self,
                 content,
                 metadata=None,
                 settings=None,
                 source_path=None,
                 context=None):
        if metadata is None:
            metadata = {}
        if settings is None:
            settings = copy.deepcopy(DEFAULT_CONFIG)

        self.settings = settings
        self._content = content
        if context is None:
            context = {}
        self._context = context
        self.translations = []

        local_metadata = dict()
        local_metadata.update(metadata)

        # set metadata as attributes
        for key, value in local_metadata.items():
            if key in ('save_as', 'url'):
                key = 'override_' + key
            setattr(self, key.lower(), value)

        # also keep track of the metadata attributes available
        self.metadata = local_metadata

        # default template if it's not defined in page
        self.template = self._get_template()

        # First, read the authors from "authors", if not, fallback to "author"
        # and if not use the settings defined one, if any.
        if not hasattr(self, 'author'):
            if hasattr(self, 'authors'):
                self.author = self.authors[0]
            elif 'AUTHOR' in settings:
                self.author = Author(settings['AUTHOR'], settings)

        if not hasattr(self, 'authors') and hasattr(self, 'author'):
            self.authors = [self.author]

        # XXX Split all the following code into pieces, there is too much here.

        # manage languages
        self.in_default_lang = True
        if 'DEFAULT_LANG' in settings:
            default_lang = settings['DEFAULT_LANG'].lower()
            if not hasattr(self, 'lang'):
                self.lang = default_lang

            self.in_default_lang = (self.lang == default_lang)

        # create the slug if not existing, generate slug according to
        # setting of SLUG_ATTRIBUTE
        if not hasattr(self, 'slug'):
            if (settings['SLUGIFY_SOURCE'] == 'title'
                    and hasattr(self, 'title')):
                self.slug = slugify(self.title,
                                    settings.get('SLUG_SUBSTITUTIONS', ()))
            elif (settings['SLUGIFY_SOURCE'] == 'basename'
                  and source_path is not None):
                basename = os.path.basename(os.path.splitext(source_path)[0])
                self.slug = slugify(basename,
                                    settings.get('SLUG_SUBSTITUTIONS', ()))

        self.source_path = source_path

        # manage the date format
        if not hasattr(self, 'date_format'):
            if hasattr(self, 'lang') and self.lang in settings['DATE_FORMATS']:
                self.date_format = settings['DATE_FORMATS'][self.lang]
            else:
                self.date_format = settings['DEFAULT_DATE_FORMAT']

        if isinstance(self.date_format, tuple):
            locale_string = self.date_format[0]
            if sys.version_info < (3, ) and isinstance(locale_string,
                                                       six.text_type):
                locale_string = locale_string.encode('ascii')
            locale.setlocale(locale.LC_ALL, locale_string)
            self.date_format = self.date_format[1]

        # manage timezone
        default_timezone = settings.get('TIMEZONE', 'UTC')
        timezone = getattr(self, 'timezone', default_timezone)

        if hasattr(self, 'date'):
            self.date = set_date_tzinfo(self.date, timezone)
            self.locale_date = strftime(self.date, self.date_format)

        if hasattr(self, 'modified'):
            self.modified = set_date_tzinfo(self.modified, timezone)
            self.locale_modified = strftime(self.modified, self.date_format)

        # manage status
        if not hasattr(self, 'status'):
            self.status = settings['DEFAULT_STATUS']
            if not settings['WITH_FUTURE_DATES'] and hasattr(self, 'date'):
                if self.date.tzinfo is None:
                    now = SafeDatetime.now()
                else:
                    now = SafeDatetime.utcnow().replace(tzinfo=pytz.utc)
                if self.date > now:
                    self.status = 'draft'

        # store the summary metadata if it is set
        if 'summary' in metadata:
            self._summary = metadata['summary']

        signals.content_object_init.send(self)
예제 #36
0
def open_graph_tag(item):

    ogtags = [('og:title', item.title), ('og:type', 'article')]

    image = item.metadata.get('og_image', '')
    if image:
        ogtags.append(('og:image', image))
    else:
        soup = BeautifulSoup(item._content, 'html.parser')
        img_links = soup.find_all('img')
        if (len(img_links) > 0):
            img_src = img_links[0].get('src')
            if not "http" in img_src:
                if item.settings.get('SITEURL', ''):
                    img_src = item.settings.get('SITEURL', '') + "/" + img_src
            ogtags.append(('og:image', img_src))

    url = item.settings.get('SITEURL', '') + "/" + item.url
    ogtags.append(('og:url', url))

    default_summary = item.summary
    description = item.metadata.get('og_description', default_summary)
    ogtags.append(('og:description', description))

    default_locale = item.settings.get('LOCALE', [])
    if default_locale:
        default_locale = default_locale[0]
    else:
        default_locale = ''
    ogtags.append(('og:locale', item.metadata.get('og_locale',
                                                  default_locale)))

    ogtags.append(('og:site_name', item.settings.get('SITENAME', '')))

    if hasattr(item, 'date'):
        ogtags.append(
            ('article:published_time', strftime(item.date, "%Y-%m-%d")))

    if hasattr(item, 'modified'):
        ogtags.append(
            ('article:modified_time', strftime(item.modified, "%Y-%m-%d")))

    if hasattr(item, 'related_posts'):
        for related_post in item.related_posts:
            url = item.settings.get('SITEURL', '') + "/" + related_post.url
            ogtags.append(('og:see_also', url))

    author_fb_profiles = item.settings.get('AUTHOR_FB_ID', {})
    if len(author_fb_profiles) > 0:
        for author in item.authors:
            if author.name in author_fb_profiles:
                ogtags.append(
                    ('article:author', author_fb_profiles[author.name]))

    ogtags.append(('article:section', item.category.name))

    try:
        for tag in item.tags:
            ogtags.append(('article:tag', tag.name))
    except AttributeError:
        pass

    item.ogtags = ogtags
예제 #37
0
    def __init__(self, content, metadata=None, settings=None,
                 source_path=None, context=None):
        if metadata is None:
            metadata = {}
        if settings is None:
            settings = copy.deepcopy(DEFAULT_CONFIG)

        self.settings = settings
        self._content = content
        if context is None:
            context = {}
        self._context = context
        self.translations = []

        local_metadata = dict(settings['DEFAULT_METADATA'])
        local_metadata.update(metadata)

        # set metadata as attributes
        for key, value in local_metadata.items():
            if key in ('save_as', 'url'):
                key = 'override_' + key
            setattr(self, key.lower(), value)

        # also keep track of the metadata attributes available
        self.metadata = local_metadata

        #default template if it's not defined in page
        self.template = self._get_template()

        # default author to the one in settings if not defined
        if not hasattr(self, 'author'):
            if 'AUTHOR' in settings:
                self.author = Author(settings['AUTHOR'], settings)

        # XXX Split all the following code into pieces, there is too much here.

        # manage languages
        self.in_default_lang = True
        if 'DEFAULT_LANG' in settings:
            default_lang = settings['DEFAULT_LANG'].lower()
            if not hasattr(self, 'lang'):
                self.lang = default_lang

            self.in_default_lang = (self.lang == default_lang)

        # create the slug if not existing, from the title
        if not hasattr(self, 'slug') and hasattr(self, 'title'):
            self.slug = slugify(self.title,
                                settings.get('SLUG_SUBSTITUTIONS', ()))

        self.source_path = source_path

        # manage the date format
        if not hasattr(self, 'date_format'):
            if hasattr(self, 'lang') and self.lang in settings['DATE_FORMATS']:
                self.date_format = settings['DATE_FORMATS'][self.lang]
            else:
                self.date_format = settings['DEFAULT_DATE_FORMAT']

        if isinstance(self.date_format, tuple):
            locale_string = self.date_format[0]
            if sys.version_info < (3, ) and isinstance(locale_string,
                                                       six.text_type):
                locale_string = locale_string.encode('ascii')
            locale.setlocale(locale.LC_ALL, locale_string)
            self.date_format = self.date_format[1]

        if hasattr(self, 'date'):
            self.locale_date = strftime(self.date, self.date_format)
        if hasattr(self, 'modified'):
            self.locale_modified = strftime(self.modified, self.date_format)

        # manage status
        if not hasattr(self, 'status'):
            self.status = settings['DEFAULT_STATUS']
            if not settings['WITH_FUTURE_DATES']:
                if hasattr(self, 'date') and self.date > datetime.now():
                    self.status = 'draft'

        # store the summary metadata if it is set
        if 'summary' in metadata:
            self._summary = metadata['summary']

        signals.content_object_init.send(self)
예제 #38
0
def format_date(value, date_format=DEFAULT_DATE_FORMAT):
    return strftime(get_date(value), date_format)
예제 #39
0
def str2defaultformat(data):
    return strftime(get_date(data), DEFAULT_DATE_FORMAT)
예제 #40
0
def open_graph_tag(item):
    ogtags = []

    ogtags.append(("og:title", item.title))
    ogtags.append(("twitter:title", item.title))
    ogtags.append(("og:type", "article"))
    ogtags.append(("twitter:card", "summary"))

    image = item.metadata.get("og_image", "")

    if image:
        ogtags.append(("og:image", image))
        ogtags.append(("twitter:image", image))
    else:
        soup = BeautifulSoup(item._content, "html.parser")
        img_links = soup.find_all("img")
        img_src = ""

        if len(img_links) > 0:
            img_src = img_links[0].get("src")
        else:
            if item.settings.get("DEFAULT_OG_IMAGE", ""):
                img_src = item.settings.get("DEFAULT_OG_IMAGE", "")

        if img_src:
            if img_src.startswith("{attach}"):
                img_path = os.path.dirname(item.source_path)
                img_filename = img_src[8:]
                img_src = os.path.join(img_path, img_filename)

                if item.settings.get("SITEURL", ""):
                    img_src = item.settings.get("SITEURL", "") + "/" + img_src
            elif img_src.startswith(("{filename}", "|filename|")):
                img_src = img_src[11:]

                if item.settings.get("SITEURL", ""):
                    img_src = item.settings.get("SITEURL", "") + "/" + img_src
            elif img_src.startswith("{static}"):
                img_src = img_src[9:]

                if item.settings.get("SITEURL", ""):
                    img_src = item.settings.get("SITEURL", "") + "/" + img_src
            elif img_src.startswith("/static"):
                img_src = img_src[8:]

                if item.settings.get("SITEURL", ""):
                    img_src = item.settings.get("SITEURL", "") + "/" + img_src
            elif img_src.startswith("data:image"):
                pass
            elif not "http" in img_src:
                if item.settings.get("SITEURL", ""):
                    img_src = item.settings.get("SITEURL", "") + "/" + img_src

        if img_src:
            ogtags.append(("og:image", img_src))
            ogtags.append(("twitter:image", img_src))

    url = os.path.join(item.settings.get("SITEURL", ""), item.url)
    ogtags.append(("og:url", url))

    default_summary = Markup(item.summary).striptags()
    description = Markup.escape(
        item.metadata.get("og_description", default_summary))
    ogtags.append(("og:description", description))
    ogtags.append(("twitter:description", description))

    default_locale = item.settings.get("LOCALE", [])

    if default_locale:
        default_locale = default_locale[0]
    else:
        default_locale = ""

    ogtags.append(("og:locale", item.metadata.get("og_locale",
                                                  default_locale)))
    ogtags.append(("og:site_name", item.settings.get("SITENAME", "")))
    ogtags.append(("twitter:site", item.settings.get("SITENAME", "")))

    if hasattr(item, "date"):
        ogtags.append(
            ("article:published_time", strftime(item.date, "%Y-%m-%d")))

    if hasattr(item, "modified"):
        ogtags.append(
            ("article:modified_time", strftime(item.modified, "%Y-%m-%d")))

    if hasattr(item, "related_posts"):
        for related_post in item.related_posts:
            url = os.path.join(item.settings.get("SITEURL", ""),
                               related_post.url)
            ogtags.append(("og:see_also", url))

    author_fb_profiles = item.settings.get("AUTHOR_FB_ID", {})

    if len(author_fb_profiles) > 0:
        for author in item.authors:
            if author.name in author_fb_profiles:
                ogtags.append(
                    ("article:author", author_fb_profiles[author.name]))

    ogtags.append(("article:section", item.category.name))

    if hasattr(item, "tags"):
        for tag in item.tags:
            ogtags.append(("article:tag", tag.name))

    item.ogtags = ogtags
예제 #41
0
파일: contents.py 프로젝트: W7PEA/pelican
    def __init__(self, content, metadata=None, settings=None, source_path=None, context=None):
        if metadata is None:
            metadata = {}
        if settings is None:
            settings = copy.deepcopy(DEFAULT_CONFIG)

        self.settings = settings
        self._content = content
        if context is None:
            context = {}
        self._context = context
        self.translations = []

        local_metadata = dict()
        local_metadata.update(metadata)

        # set metadata as attributes
        for key, value in local_metadata.items():
            if key in ("save_as", "url"):
                key = "override_" + key
            setattr(self, key.lower(), value)

        # also keep track of the metadata attributes available
        self.metadata = local_metadata

        # default template if it's not defined in page
        self.template = self._get_template()

        # First, read the authors from "authors", if not, fallback to "author"
        # and if not use the settings defined one, if any.
        if not hasattr(self, "author"):
            if hasattr(self, "authors"):
                self.author = self.authors[0]
            elif "AUTHOR" in settings:
                self.author = Author(settings["AUTHOR"], settings)

        if not hasattr(self, "authors") and hasattr(self, "author"):
            self.authors = [self.author]

        # XXX Split all the following code into pieces, there is too much here.

        # manage languages
        self.in_default_lang = True
        if "DEFAULT_LANG" in settings:
            default_lang = settings["DEFAULT_LANG"].lower()
            if not hasattr(self, "lang"):
                self.lang = default_lang

            self.in_default_lang = self.lang == default_lang

        # create the slug if not existing, generate slug according to
        # setting of SLUG_ATTRIBUTE
        if not hasattr(self, "slug"):
            if settings["SLUGIFY_SOURCE"] == "title" and hasattr(self, "title"):
                self.slug = slugify(self.title, settings.get("SLUG_SUBSTITUTIONS", ()))
            elif settings["SLUGIFY_SOURCE"] == "basename" and source_path is not None:
                basename = os.path.basename(os.path.splitext(source_path)[0])
                self.slug = slugify(basename, settings.get("SLUG_SUBSTITUTIONS", ()))

        self.source_path = source_path

        # manage the date format
        if not hasattr(self, "date_format"):
            if hasattr(self, "lang") and self.lang in settings["DATE_FORMATS"]:
                self.date_format = settings["DATE_FORMATS"][self.lang]
            else:
                self.date_format = settings["DEFAULT_DATE_FORMAT"]

        if isinstance(self.date_format, tuple):
            locale_string = self.date_format[0]
            if sys.version_info < (3,) and isinstance(locale_string, six.text_type):
                locale_string = locale_string.encode("ascii")
            locale.setlocale(locale.LC_ALL, locale_string)
            self.date_format = self.date_format[1]

        # manage timezone
        default_timezone = settings.get("TIMEZONE", "UTC")
        timezone = getattr(self, "timezone", default_timezone)

        if hasattr(self, "date"):
            self.date = set_date_tzinfo(self.date, timezone)
            self.locale_date = strftime(self.date, self.date_format)

        if hasattr(self, "modified"):
            self.modified = set_date_tzinfo(self.modified, timezone)
            self.locale_modified = strftime(self.modified, self.date_format)

        # manage status
        if not hasattr(self, "status"):
            self.status = settings["DEFAULT_STATUS"]
            if not settings["WITH_FUTURE_DATES"] and hasattr(self, "date"):
                if self.date.tzinfo is None:
                    now = SafeDatetime.now()
                else:
                    now = SafeDatetime.utcnow().replace(tzinfo=pytz.utc)
                if self.date > now:
                    self.status = "draft"

        # store the summary metadata if it is set
        if "summary" in metadata:
            self._summary = metadata["summary"]

        signals.content_object_init.send(self)
예제 #42
0
def open_graph_tag(item):

    ogtags = [("og:title", item.title), ("og:type", "article")]

    image = item.metadata.get("og_image", "")
    if image:
        ogtags.append(("og:image", image))
    else:
        soup = BeautifulSoup(item._content, "html.parser")
        img_links = soup.find_all("img")
        if len(img_links) > 0:
            img_src = img_links[0].get("src")
            if not "http" in img_src:
                if item.settings.get("SITEURL", ""):
                    img_src = item.settings.get("SITEURL", "") + "/" + img_src
            ogtags.append(("og:image", img_src))

    url = os.path.join(item.settings.get("SITEURL", ""), item.url)
    ogtags.append(("og:url", url))

    default_summary = item.summary
    description = item.metadata.get("og_description", default_summary)
    ogtags.append(("og:description", description))

    default_locale = item.settings.get("LOCALE", [])
    if default_locale:
        default_locale = default_locale[0]
    else:
        default_locale = ""
    ogtags.append(("og:locale", item.metadata.get("og_locale",
                                                  default_locale)))

    ogtags.append(("og:site_name", item.settings.get("SITENAME", "")))

    if hasattr(item, "date"):
        ogtags.append(
            ("article:published_time", strftime(item.date, "%Y-%m-%d")))

    if hasattr(item, "modified"):
        ogtags.append(
            ("article:modified_time", strftime(item.modified, "%Y-%m-%d")))

    if hasattr(item, "related_posts"):
        for related_post in item.related_posts:
            url = os.path.join(item.settings.get("SITEURL", ""),
                               related_post.url)
            ogtags.append(("og:see_also", url))

    author_fb_profiles = item.settings.get("AUTHOR_FB_ID", {})
    if len(author_fb_profiles) > 0:
        for author in item.authors:
            if author.name in author_fb_profiles:
                ogtags.append(
                    ("article:author", author_fb_profiles[author.name]))

    ogtags.append(("article:section", item.category.name))

    try:
        for tag in item.tags:
            ogtags.append(("article:tag", tag.name))
    except AttributeError:
        pass

    item.ogtags = ogtags
예제 #43
0
파일: contents.py 프로젝트: Starch/pelican
    def __init__(self, content, metadata=None, settings=None,
                 source_path=None, context=None):
        if metadata is None:
            metadata = {}
        if settings is None:
            settings = copy.deepcopy(DEFAULT_CONFIG)

        self.settings = settings
        self._content = content
        if context is None:
            context = {}
        self._context = context
        self.translations = []

        local_metadata = dict()
        local_metadata.update(metadata)

        # set metadata as attributes
        for key, value in local_metadata.items():
            if key in ('save_as', 'url'):
                key = 'override_' + key
            setattr(self, key.lower(), value)

        # also keep track of the metadata attributes available
        self.metadata = local_metadata

        # default template if it's not defined in page
        self.template = self._get_template()

        # First, read the authors from "authors", if not, fallback to "author"
        # and if not use the settings defined one, if any.
        if not hasattr(self, 'author'):
            if hasattr(self, 'authors'):
                self.author = self.authors[0]
            elif 'AUTHOR' in settings:
                self.author = Author(settings['AUTHOR'], settings)

        if not hasattr(self, 'authors') and hasattr(self, 'author'):
            self.authors = [self.author]

        # XXX Split all the following code into pieces, there is too much here.

        # manage languages
        self.in_default_lang = True
        if 'DEFAULT_LANG' in settings:
            default_lang = settings['DEFAULT_LANG'].lower()
            if not hasattr(self, 'lang'):
                self.lang = default_lang

            self.in_default_lang = (self.lang == default_lang)

        # create the slug if not existing, generate slug according to
        # setting of SLUG_ATTRIBUTE
        if not hasattr(self, 'slug'):
            if (settings['SLUGIFY_SOURCE'] == 'title' and
                    hasattr(self, 'title')):
                self.slug = slugify(self.title,
                                    settings.get('SLUG_SUBSTITUTIONS', ()))
            elif (settings['SLUGIFY_SOURCE'] == 'basename' and
                    source_path is not None):
                basename = os.path.basename(
                    os.path.splitext(source_path)[0])
                self.slug = slugify(
                    basename, settings.get('SLUG_SUBSTITUTIONS', ()))

        self.source_path = source_path

        # manage the date format
        if not hasattr(self, 'date_format'):
            if hasattr(self, 'lang') and self.lang in settings['DATE_FORMATS']:
                self.date_format = settings['DATE_FORMATS'][self.lang]
            else:
                self.date_format = settings['DEFAULT_DATE_FORMAT']

        if isinstance(self.date_format, tuple):
            locale_string = self.date_format[0]
            if sys.version_info < (3, ) and isinstance(locale_string,
                                                       six.text_type):
                locale_string = locale_string.encode('ascii')
            locale.setlocale(locale.LC_ALL, locale_string)
            self.date_format = self.date_format[1]

        # manage timezone
        default_timezone = settings.get('TIMEZONE', 'UTC')
        timezone = getattr(self, 'timezone', default_timezone)

        if hasattr(self, 'date'):
            self.date = set_date_tzinfo(self.date, timezone)
            self.locale_date = strftime(self.date, self.date_format)

        if hasattr(self, 'modified'):
            self.modified = set_date_tzinfo(self.modified, timezone)
            self.locale_modified = strftime(self.modified, self.date_format)

        # manage status
        if not hasattr(self, 'status'):
            self.status = getattr(self, 'default_status', None)

        if len(self._context.get('filenames', [])) > 0:
            self.refresh_metadata_intersite_links()

        signals.content_object_init.send(self)
예제 #44
0
def format_date(value, date_format=DEFAULT_DATE_FORMAT):
    return strftime(get_date(value), date_format)
예제 #45
0
def open_graph_tag(item):
    """
    Process single item (article or page)

    Access the following meta tags:
    og:title
    og:type
    og:image
    og:url
    og:description
    og:locale
    og:site_name
    og:see_also
    article:author
    article:published_time
    article:modified_time
    article:section
    article:tag
    """
    # Predefined tags
    og_tags = [('og:title', item.title), ('og:type', 'article')]

    # og:image explicitly or via DEFAULT_OG_IMAGE variable in settings
    default_image = item.settings.get('DEFAULT_OG_IMAGE')

    image = item.metadata.get('og_image', '')
    if image:
        image = image.replace('{static}', item.settings.get('SITEURL', ''))
        og_tags.append(('og:image', image))
    else:
        if default_image:
            image_url = urlparse(default_image)
            if not image_url.scheme:
                if item.settings.get('SITEURL', ''):
                    default_image = "{0}/{1}".format(
                        item.settings.get('SITEURL', ''), default_image)
            og_tags.append(('og:image', default_image))

    # og:url
    url = os.path.join(item.settings.get('SITEURL', ''), item.url)
    og_tags.append(('og:url', url))

    # og:description
    default_summary = item.summary
    description = item.metadata.get('og_description', default_summary)
    og_tags.append(('og:description', description))

    # og:locale
    default_locale = item.settings.get('LOCALE', [])
    if default_locale:
        default_locale = default_locale[0]
    else:
        default_locale = ''
    og_tags.append(('og:locale', item.metadata.get('og_locale',
                                                   default_locale)))

    # og:site_name
    og_tags.append(('og:site_name', item.settings.get('SITENAME', '')))

    # article:published_time
    if hasattr(item, 'date'):
        og_tags.append(
            ('article:published_time', strftime(item.date, "%Y-%m-%d")))

    # article:modified_time
    if hasattr(item, 'modified'):
        og_tags.append(
            ('article:modified_time', strftime(item.modified, "%Y-%m-%d")))

    # og:see_also
    if hasattr(item, 'related_posts'):
        for related_post in item.related_posts:
            url = os.path.join(item.settings.get('SITEURL', ''),
                               related_post.url)
            og_tags.append(('og:see_also', url))

    # article:author, link author Facebook account vie AUTHOR_FB_ID variable in settings
    author_fb_profiles = item.settings.get('AUTHOR_FB_ID', {})
    if len(author_fb_profiles) > 0:
        for author in item.authors:
            if author.name in author_fb_profiles:
                og_tags.append(
                    ('article:author', author_fb_profiles[author.name]))

    # article:section
    og_tags.append(('article:section', item.category.name))

    # article:tag
    try:
        for tag in item.tags:
            og_tags.append(('article:tag', tag.name))
    except AttributeError:
        pass

    item.og = og_tags
예제 #46
0
def str2defaultformat(data):
    return strftime(get_date(data), DEFAULT_DATE_FORMAT)