From d425e093ec49fa69110b2945c21820e01ecf7eec Mon Sep 17 00:00:00 2001 From: Lucas Magnum Date: Tue, 13 Jan 2015 15:31:23 -0200 Subject: [PATCH] Adicionado plugin customizado para mostrar posts randomicos. --- custom-plugins/random_articles/Readme.md | 28 ++++++++++++++ custom-plugins/random_articles/__init__.py | 1 + .../random_articles/random_articles.py | 37 +++++++++++++++++++ pelicanconf.py | 15 ++++++-- theme/static/css/custom.css | 16 ++++++++ theme/templates/sidebar.html | 20 +++++----- 6 files changed, 104 insertions(+), 13 deletions(-) create mode 100644 custom-plugins/random_articles/Readme.md create mode 100644 custom-plugins/random_articles/__init__.py create mode 100644 custom-plugins/random_articles/random_articles.py diff --git a/custom-plugins/random_articles/Readme.md b/custom-plugins/random_articles/Readme.md new file mode 100644 index 000000000..aaec8644d --- /dev/null +++ b/custom-plugins/random_articles/Readme.md @@ -0,0 +1,28 @@ +Random Articles Plugin For Pelican +======================== + +This plugin insert a variable called `random_articles` in the page context. +Only published articles are listed. + + +Settings options: + + RANDOM_ARTICLES = 3 # show only 3 random articles + SKIP_ARTICLES = 10 # skip 10 first articles before randomize + + +Usage +----- + +To change number of random articles, put on your settings: + + RANDOM_ARTICLES = 3 + +Then in some template you'll have a variable called `random_articles`. + +Ex: + {% for article in random_articles %} +

+ {{ article.title }} +

+ {% endfor %} diff --git a/custom-plugins/random_articles/__init__.py b/custom-plugins/random_articles/__init__.py new file mode 100644 index 000000000..50695ab55 --- /dev/null +++ b/custom-plugins/random_articles/__init__.py @@ -0,0 +1 @@ +from .random_articles import * diff --git a/custom-plugins/random_articles/random_articles.py b/custom-plugins/random_articles/random_articles.py new file mode 100644 index 000000000..641cca02f --- /dev/null +++ b/custom-plugins/random_articles/random_articles.py @@ -0,0 +1,37 @@ +# -*- coding: utf-8 -*- + +from __future__ import unicode_literals + +import random + +from pelican import signals + + +def is_published(article): + """ Return if article is published""" + return getattr(article, 'status', 'published') == 'published' + + +def get_articles(generator): + skip_articles = generator.settings.get('SKIP_ARTICLES', 0) + articles = generator.context['articles'] + return articles[skip_articles:] + + +def register_articles(generator, metadata): + articles = filter(is_published, get_articles(generator)) + articles_number = len(articles) + + random_articles_number = generator.settings.get('RANDOM_ARTICLES', 3) + + # we should return only articles number that exists + sample_number = random_articles_number + if articles_number < random_articles_number: + sample_number = articles_number + + random_articles = random.sample(articles, sample_number) + generator.context['random_articles'] = random_articles + + +def register(): + signals.page_generator_context.connect(register_articles) diff --git a/pelicanconf.py b/pelicanconf.py index 7e0a2221c..68bc7c6df 100644 --- a/pelicanconf.py +++ b/pelicanconf.py @@ -2,6 +2,8 @@ # -*- coding: utf-8 -*- # from __future__ import unicode_literals import os + + BASE = os.path.dirname(__file__) AUTHOR = u'PythonClub' @@ -54,19 +56,24 @@ # Plugins PLUGIN_PATHS = [ - 'pelican-plugins' + 'pelican-plugins', + 'custom-plugins' ] PLUGINS = [ 'gravatar', 'sitemap', - 'pelican_youtube', # funciona somente com arquivos rst - 'pelican_vimeo', # funciona somente com arquivos rst - 'gzip_cache', # deve ser o ultimo plugin + 'pelican_youtube', # funciona somente com arquivos rst + 'pelican_vimeo', # funciona somente com arquivos rst + 'random_articles', + 'gzip_cache' # deve ser o ultimo plugin # 'pdf', # funciona somente com arquivos rst ] +RANDOM_ARTICLES = 10 +SKIP_ARTICLES = 10 + SITEMAP = { 'format': 'xml', diff --git a/theme/static/css/custom.css b/theme/static/css/custom.css index 03f403195..7cbcea377 100644 --- a/theme/static/css/custom.css +++ b/theme/static/css/custom.css @@ -85,6 +85,16 @@ code { width: 80%; } +.articles-random { + font-size: 0.8em; +} + +.article-link { + margin-top: 0px; + line-height: 1em; +} + + @media (max-width: 767px) { .brand-main a img { display: inline; @@ -99,6 +109,12 @@ code { .google-search { width: 100%; } + + .articles-random, .articles-link { + display: none; + } + + } @media (max-width: 480px) { diff --git a/theme/templates/sidebar.html b/theme/templates/sidebar.html index 6f4c33e2f..b1d713df3 100644 --- a/theme/templates/sidebar.html +++ b/theme/templates/sidebar.html @@ -8,6 +8,17 @@

{{ SITENAME }}
           {% for title, link in MENUITEMS %}
           <p class={{ title }}

{% endfor %} + +
+

Não deixe de ver!

+ + {% for article in random_articles %} +

+ {{ article.title }} +

+ {% endfor %} +
+

{{ SITENAME }}
             </a>
             {% endfor %}
           </p>
-          <div class= - #Python2014, vamos?! -
- - Eu vou na PythonBrasil[10] - -
-

Esta comunidade é signatária do