Skip to content
This repository has been archived by the owner on Aug 18, 2021. It is now read-only.

Commit

Permalink
Add random article
Browse files Browse the repository at this point in the history
  • Loading branch information
xuxiaodong committed Apr 20, 2016
1 parent eedfb76 commit 7364bc6
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 78 deletions.
5 changes: 3 additions & 2 deletions pelicanconf.py
Expand Up @@ -26,7 +26,7 @@

#PAGE_ICONS = ['folder', 'book', 'support', 'mail', 'heart', 'exclamationCircle']
#RSS_ICONS = ['rss']
MENUITEMS_AFTER = (('Comments', '/pages/comments.html'), ('RSS', '/feeds/all.atom.xml'),)
MENUITEMS_AFTER = (('Comments', '/pages/comments.html'), ('Random', '/random.html'), ('RSS', '/feeds/all.atom.xml'),)

GITHUB_SOURCE = 'https://github.com/xuxiaodong/linuxtoy.org/blob/master/content'

Expand All @@ -47,10 +47,11 @@
THEME = 'themes/itoy'

PLUGIN_PATHS = ['plugins']
PLUGINS = ['pelican-page-order', 'summary', 'neighbors', 'related_posts']
PLUGINS = ['pelican-page-order', 'summary', 'neighbors', 'related_posts', 'random_article']

CACHE_CONTENT = True
LOAD_CONTENT_CACHE = True
CHECK_MODIFIED_METHOD = 'mtime'

RELATED_POSTS_MAX = 5
RANDOM = 'random.html'
34 changes: 0 additions & 34 deletions plugins/cjk-auto-spacing/README.md

This file was deleted.

1 change: 0 additions & 1 deletion plugins/cjk-auto-spacing/__init__.py

This file was deleted.

41 changes: 0 additions & 41 deletions plugins/cjk-auto-spacing/cjk_auto_spacing.py

This file was deleted.

Binary file removed plugins/cjk-auto-spacing/screenshot1.png
Binary file not shown.
Binary file removed plugins/cjk-auto-spacing/screenshot2.png
Binary file not shown.
19 changes: 19 additions & 0 deletions plugins/random_article/Readme.md
@@ -0,0 +1,19 @@
Random Article Plugin For Pelican
========================

This plugin generates a html file which redirect to a random article
using javascript's `window.location`. The generated html file is
saved at `SITEURL`.

Only published articles are listed to redirect.

Usage
-----

To use it you have to add in your config file the name of the file to use:

RANDOM = 'random.html'

Then in some template you add:

<a href="{{ SITEURL }}/{{ RANDOM }}">random article</a>
1 change: 1 addition & 0 deletions plugins/random_article/__init__.py
@@ -0,0 +1 @@
from .random_article import *
89 changes: 89 additions & 0 deletions plugins/random_article/random_article.py
@@ -0,0 +1,89 @@
# -*- coding: utf-8 -*-
"""
Random Article Plugin For Pelican
========================
This plugin generates a html file which redirect to a random article
using javascript's window.location. The generated html file is
saved at SITEURL.
"""

from __future__ import unicode_literals

import os.path

from logging import info
from codecs import open

from pelican import signals

HTML_TOP = """
<!DOCTYPE html>
<head>
<title>random</title>
<script type="text/javascript">
function redirect(){
var urls = [
"""

HTML_BOTTOM = """
""];
var index = Math.floor(Math.random() * (urls.length-1));
window.location = urls[index];
}
</script>
<body onload="redirect()">
</body>
</html>
"""

ARTICLE_URL = """ "{0}/{1}",
"""


class RandomArticleGenerator(object):
"""
The structure is derived from sitemap plugin
"""

def __init__(self, context, settings, path, theme, output_path, *null):

self.output_path = output_path
self.context = context
self.siteurl = settings.get('SITEURL')
self.randomurl = settings.get('RANDOM')

def write_url(self, article, fd):
if getattr(article, 'status', 'published') != 'published':
return

page_path = os.path.join(self.output_path, article.url)
if not os.path.exists(page_path):
return

fd.write(ARTICLE_URL.format(self.siteurl, article.url))


def generate_output(self, writer):
path = os.path.join(self.output_path, self.randomurl)
articles = self.context['articles']
info('writing {0}'.format(path))

if len(articles) == 0:
return

with open(path, 'w', encoding='utf-8') as fd:
fd.write(HTML_TOP)

for art in articles:
self.write_url(art, fd)

fd.write(HTML_BOTTOM)

def get_generators(generators):
return RandomArticleGenerator


def register():
signals.get_generators.connect(get_generators)

0 comments on commit 7364bc6

Please sign in to comment.