Skip to content

Commit 88d02f9

Browse files
committed
Merge pull request pythonclub#177 from LucasMagnum/feature/dynamic_random_articles
Home - Artigos randomicos
2 parents 03479f8 + ee1a4be commit 88d02f9

10 files changed

Lines changed: 98 additions & 74 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Json Articles Plugin For Pelican
2+
========================
3+
4+
This plugin insert a variable called `json_articles` in the page context.
5+
6+
This variable contains all articles in a json format.
7+
8+
9+
In settings we can define the number of random_articles:
10+
11+
RANDOM_ARTICLES = 3
12+
13+
14+
Usage
15+
-----
16+
17+
Add `dynamic_random_articles.js` in your template, in our case, just do that:
18+
<script src="{{ SITEURL }}/theme/js/dynamic_random_articles.js"></script>
19+
20+
In our sidebar.html was inserted:
21+
22+
div class="section articles-random">
23+
<h1 class="tagline">Não deixe de ver!</h1>
24+
<div id="random-articles"></div>
25+
</div>
26+
27+
We use `articles-random` class to hide it in mobile.
28+
29+
And then, we call the javascript function that render articles:
30+
31+
$(function(){
32+
show_random_articles($('#random-articles'), {{ json_articles }}, {{ RANDOM_ARTICLES }});
33+
});
34+
35+
Note that we pass a element to function, this element can be changed any time.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .json_articles import *
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from __future__ import unicode_literals
4+
5+
import json
6+
7+
from pelican import signals
8+
9+
10+
def inject_articles(generator, metadata=None):
11+
articles = generator.context['articles']
12+
site_url = generator.settings['SITEURL']
13+
14+
json_articles = []
15+
16+
for article in articles:
17+
json_articles.append({
18+
'title': article.title,
19+
'url': '{}/{}'.format(site_url, article.url)
20+
})
21+
22+
generator.context['json_articles'] = json.dumps(json_articles)
23+
24+
25+
def register():
26+
signals.page_generator_context.connect(inject_articles)

custom-plugins/random_articles/Readme.md

Lines changed: 0 additions & 28 deletions
This file was deleted.

custom-plugins/random_articles/__init__.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

custom-plugins/random_articles/random_articles.py

Lines changed: 0 additions & 37 deletions
This file was deleted.

pelicanconf.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,13 @@
6565
'sitemap',
6666
'pelican_youtube', # funciona somente com arquivos rst
6767
'pelican_vimeo', # funciona somente com arquivos rst
68-
'random_articles',
68+
'json_articles',
6969
'gzip_cache' # deve ser o ultimo plugin
7070
# 'pdf', # funciona somente com arquivos rst
7171

7272
]
7373

7474
RANDOM_ARTICLES = 10
75-
SKIP_ARTICLES = 10
7675

7776

7877
SITEMAP = {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function shuffleArray(array) {
2+
for (var i = array.length - 1; i > 0; i--) {
3+
var j = Math.floor(Math.random() * (i + 1));
4+
var temp = array[i];
5+
array[i] = array[j];
6+
array[j] = temp;
7+
}
8+
return array;
9+
}
10+
11+
function show_random_articles(element, articles, articles_number){
12+
articles = shuffleArray(articles);
13+
14+
var paragraph = $('<p class="article-link tagline"></p>');
15+
16+
for (index in articles){
17+
var article = articles[index];
18+
var article_html = '<a href="' + article.url + '">' + article.title + '</a>';
19+
var article_paragraph = paragraph.clone().append(article_html)
20+
element.append(article_paragraph);
21+
22+
if (index >= articles_number -1)
23+
break;
24+
}
25+
26+
}

theme/templates/base.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.0.3/css/font-awesome.min.css">
2929
<link rel="stylesheet" href="{{ SITEURL }}/theme/css/pure.css">
3030
<link rel="stylesheet" href="{{ SITEURL }}/theme/css/custom.css">
31+
3132
{% block head_css %}{% endblock %}
3233

3334

@@ -52,6 +53,8 @@
5253

5354

5455
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
56+
<script src="{{ SITEURL }}/theme/js/dynamic_random_articles.js"></script>
57+
5558
{% block bottom_js %}{% endblock %}
5659

5760
<script>
@@ -77,6 +80,11 @@
7780
$('body').on('click', 'a[href="#"]', function(event) {
7881
event.preventDefault();
7982
});
83+
84+
$(function(){
85+
show_random_articles($('#random-articles'), {{ json_articles }}, {{ RANDOM_ARTICLES }});
86+
});
87+
8088
</script>
8189
</body>
8290
</html>

theme/templates/sidebar.html

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,7 @@ <h1 class="brand-main"><a href="/"><img src="{{ SITELOGO }}" alt="{{ SITENAME }}
1111

1212
<div class="section articles-random">
1313
<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 %}
14+
<div id="random-articles"></div>
2015
</div>
2116

2217
<p class="social">

0 commit comments

Comments
 (0)