Skip to content

Commit 03479f8

Browse files
committed
Merge pull request pythonclub#176 from LucasMagnum/feature/custom_plugins
Adicionado plugin customizado para mostrar posts randomicos.
2 parents d88f20d + d425e09 commit 03479f8

6 files changed

Lines changed: 104 additions & 13 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Random Articles Plugin For Pelican
2+
========================
3+
4+
This plugin insert a variable called `random_articles` in the page context.
5+
Only published articles are listed.
6+
7+
8+
Settings options:
9+
10+
RANDOM_ARTICLES = 3 # show only 3 random articles
11+
SKIP_ARTICLES = 10 # skip 10 first articles before randomize
12+
13+
14+
Usage
15+
-----
16+
17+
To change number of random articles, put on your settings:
18+
19+
RANDOM_ARTICLES = 3
20+
21+
Then in some template you'll have a variable called `random_articles`.
22+
23+
Ex:
24+
{% for article in random_articles %}
25+
<p class="article-link tagline">
26+
<a href="{{ SITEURL }}/{{ article.url}}">{{ article.title }}</a>
27+
</p>
28+
{% endfor %}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .random_articles import *
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from __future__ import unicode_literals
4+
5+
import random
6+
7+
from pelican import signals
8+
9+
10+
def is_published(article):
11+
""" Return if article is published"""
12+
return getattr(article, 'status', 'published') == 'published'
13+
14+
15+
def get_articles(generator):
16+
skip_articles = generator.settings.get('SKIP_ARTICLES', 0)
17+
articles = generator.context['articles']
18+
return articles[skip_articles:]
19+
20+
21+
def register_articles(generator, metadata):
22+
articles = filter(is_published, get_articles(generator))
23+
articles_number = len(articles)
24+
25+
random_articles_number = generator.settings.get('RANDOM_ARTICLES', 3)
26+
27+
# we should return only articles number that exists
28+
sample_number = random_articles_number
29+
if articles_number < random_articles_number:
30+
sample_number = articles_number
31+
32+
random_articles = random.sample(articles, sample_number)
33+
generator.context['random_articles'] = random_articles
34+
35+
36+
def register():
37+
signals.page_generator_context.connect(register_articles)

pelicanconf.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# -*- coding: utf-8 -*- #
33
from __future__ import unicode_literals
44
import os
5+
6+
57
BASE = os.path.dirname(__file__)
68

79
AUTHOR = u'PythonClub'
@@ -54,19 +56,24 @@
5456

5557
# Plugins
5658
PLUGIN_PATHS = [
57-
'pelican-plugins'
59+
'pelican-plugins',
60+
'custom-plugins'
5861
]
5962

6063
PLUGINS = [
6164
'gravatar',
6265
'sitemap',
63-
'pelican_youtube', # funciona somente com arquivos rst
64-
'pelican_vimeo', # funciona somente com arquivos rst
65-
'gzip_cache', # deve ser o ultimo plugin
66+
'pelican_youtube', # funciona somente com arquivos rst
67+
'pelican_vimeo', # funciona somente com arquivos rst
68+
'random_articles',
69+
'gzip_cache' # deve ser o ultimo plugin
6670
# 'pdf', # funciona somente com arquivos rst
6771

6872
]
6973

74+
RANDOM_ARTICLES = 10
75+
SKIP_ARTICLES = 10
76+
7077

7178
SITEMAP = {
7279
'format': 'xml',

theme/static/css/custom.css

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ code {
8383
width: 80%;
8484
}
8585

86+
.articles-random {
87+
font-size: 0.8em;
88+
}
89+
90+
.article-link {
91+
margin-top: 0px;
92+
line-height: 1em;
93+
}
94+
95+
8696
@media (max-width: 767px) {
8797
.brand-main a img {
8898
display: inline;
@@ -97,6 +107,12 @@ code {
97107
.google-search {
98108
width: 100%;
99109
}
110+
111+
.articles-random, .articles-link {
112+
display: none;
113+
}
114+
115+
100116
}
101117

102118
@media (max-width: 480px) {

theme/templates/sidebar.html

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,24 @@ <h1 class="brand-main"><a href="/"><img src="{{ SITELOGO }}" alt="{{ SITENAME }}
88
{% for title, link in MENUITEMS %}
99
<p class="links"><a href="{{ SITEURL }}/{{ link }}">{{ title }}</a></p>
1010
{% endfor %}
11+
12+
<div class="section articles-random">
13+
<h1 class="tagline">Não deixe de ver!</h1>
14+
15+
{% for article in random_articles %}
16+
<p class="article-link tagline">
17+
<a href="{{ SITEURL }}/{{ article.url}}">{{ article.title }}</a>
18+
</p>
19+
{% endfor %}
20+
</div>
21+
1122
<p class="social">
1223
{% for title, link in SOCIAL %}
1324
<a href="{{ link }}">
1425
<i class="fa fa-{{ title }} fa-3x"></i>
1526
</a>
1627
{% endfor %}
1728
</p>
18-
<div class="section">
19-
<span><a href="http://2014.pythonbrasil.org.br/">#Python2014, vamos?!</a></span>
20-
<div class="section-body">
21-
<a href="http://2014.pythonbrasil.org.br/" >
22-
<img src="https://pybr.s3.amazonaws.com/static/images/badges/badge-pt-br.2ab99f7699bf.png"
23-
width="250" height="276" alt="Eu vou na PythonBrasil[10]"/>
24-
</a>
25-
</div>
26-
</div>
2729
<p class="tagline">
2830
Esta comunidade é signatária do
2931
<a target="_blank" href="http://smallactsmanifesto.org" title="Small Acts Manifesto">

0 commit comments

Comments
 (0)