Skip to content

Commit bb6406e

Browse files
author
Lucas Magnum
committed
Artigos aleatórios sendo mostrados na Home. Removido plugin random_articles e criado plugin json_articles.
1 parent 03479f8 commit bb6406e

10 files changed

Lines changed: 75 additions & 74 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Random Articles Plugin For Pelican
2+
========================
3+
4+
This plugin insert a variable called `json_articles` in the page context.
5+
Only published articles are listed.
6+
7+
8+
Usage
9+
-----
10+
11+
You can access all articles calling `json_articles` variable in your template.
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: 9 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,12 @@
7780
$('body').on('click', 'a[href="#"]', function(event) {
7881
event.preventDefault();
7982
});
83+
84+
$(function(){
85+
articles = {{ json_articles }};
86+
show_random_articles($('#random-articles'), {{ json_articles }}, {{ RANDOM_ARTICLES }});
87+
});
88+
8089
</script>
8190
</body>
8291
</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)