File tree Expand file tree Collapse file tree
custom-plugins/random_articles Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 %}
Original file line number Diff line number Diff line change 1+ from .random_articles import *
Original file line number Diff line number Diff line change 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 )
Original file line number Diff line number Diff line change 22# -*- coding: utf-8 -*- #
33from __future__ import unicode_literals
44import os
5+
6+
57BASE = os .path .dirname (__file__ )
68
79AUTHOR = u'PythonClub'
5456
5557# Plugins
5658PLUGIN_PATHS = [
57- 'pelican-plugins'
59+ 'pelican-plugins' ,
60+ 'custom-plugins'
5861]
5962
6063PLUGINS = [
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
7178SITEMAP = {
7279 'format' : 'xml' ,
Original file line number Diff line number Diff 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 ) {
Original file line number Diff line number Diff 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 ">
You can’t perform that action at this time.
0 commit comments