renamed: plugins/__init__.py -> lib/__init__.py

renamed:    plugins/liblinkpeek.py -> lib/liblinkpeek.py
	renamed:    plugins/rst_linkpeek.py -> lib/rst_linkpeek.py
	modified:   pelicanconf.py
This commit is contained in:
russellballestrini 2016-05-24 18:15:09 -04:00
parent 0a85c97ae7
commit 27ae7689b4
4 changed files with 1 additions and 1 deletions

0
lib/__init__.py Normal file
View file

21
lib/liblinkpeek.py Normal file
View file

@ -0,0 +1,21 @@
#!/usr/bin/python
from hashlib import md5
from urllib import quote
# signup for an APIKEY here: https://linkpeek.com/signup
APIKEY = ''
SECRET = ''
def api_v1( uri, apikey=APIKEY, secret=SECRET, size='336' ):
"""LinkPeek.com API v1 helper function"""
token = md5( secret + uri + size ).hexdigest()
uri = quote( uri )
return "http://linkpeek.com/api/v1?uri=%s&apikey=%s&token=%s&size=%s" % (
uri, apikey, token, size)
if __name__ == '__main__':
"""TESTS"""
print api_v1( 'lostquery.com' )
print api_v1( 'http://lostquery.com', size='200x400' )
print api_v1( 'https://lostquery.com', size='400x200' )

87
lib/rst_linkpeek.py Normal file
View file

@ -0,0 +1,87 @@
# -*- coding: utf-8 -*-
"""
Use LinkPeek via reStructuredText
=================================
This plugin allows you to use LinkPeek images from within reST documents.
"""
from __future__ import unicode_literals
from docutils import nodes
from docutils.parsers.rst import (
directives,
Directive,
)
from collections import defaultdict
#from .liblinkpeek import api_v1
from liblinkpeek import api_v1
class LinkPeek(Directive):
required_arguments = 0
optional_arguments = 0
final_argument_whitespace = True
has_content = True
apikey = None
secret = None
params = defaultdict(str)
def api_call(self):
return api_v1(self.params['uri'], self.apikey, self.secret, self.params['size'])
def _get_params(self):
"""
turn [u'uri = linkpeek.com', u'size = 200x200']
into {u'uri': u'linkpeek.com', u'size': u'200x200'}
"""
params = defaultdict(str)
for c in self.content:
# split on = and then remove leading/trailing whitespace.
key, value = c.split('=')
key = key.strip()
value = value.strip()
params[key] = value
self.params = params
return params
@property
def action_registry(self):
"""return an registry (dictionary) or action methods."""
return {
'image' : self.html_image,
'link-image' : self.html_link_image,
}
def get_action(self, action_name):
"""get action method from action registry or use default."""
return self.action_registry.get(action_name, self.api_call)
def html_image(self):
"""return html image markup."""
html = '<img src="{}" title="{}" style="{}" class="{}" />'
return html.format(self.api_call(), self.params['title'], self.params['style'], self.params['class'])
def html_link_image(self):
"""return html link image markup."""
html = '<a href="{}" target="_blank">{}</a>'
return html.format(self.params['uri'], self.html_image())
def run(self):
"""this method is fired when rendering."""
self._get_params()
action = self.get_action(self.params['action'])
output = action()
return [nodes.raw('', output, format='html')]
@classmethod
def register(cls):
"""register this directive/class with docutils."""
# this allows us to modify apikey and secret attributes before registering.
directives.register_directive('linkpeek', cls)