modified: liblinkpeek.py

modified:   rst_linkpeek.py
This commit is contained in:
russellballestrini 2017-11-18 13:30:51 -05:00
parent 6b279ebe05
commit 2e223efde9
2 changed files with 57 additions and 27 deletions

View file

@ -6,16 +6,15 @@ from urllib import quote
APIKEY = '' APIKEY = ''
SECRET = '' SECRET = ''
def api_v1( uri, apikey=APIKEY, secret=SECRET, size='336' ): def api_v1(uri, apikey=APIKEY, secret=SECRET, size='336', viewport='1024'):
"""LinkPeek.com API v1 helper function""" """LinkPeek.com API v1 helper function"""
token = md5( secret + uri + size ).hexdigest() token = md5(secret + uri + size).hexdigest()
uri = quote( uri ) return 'https://linkpeek.com/api/v1?uri={}&apikey={}&token={}&size={}&viewport={}'.format(
return "http://linkpeek.com/api/v1?uri=%s&apikey=%s&token=%s&size=%s" % ( quote(uri), apikey, token, size, viewport)
uri, apikey, token, size)
if __name__ == '__main__': if __name__ == '__main__':
"""TESTS""" """TESTS"""
print api_v1( 'lostquery.com' ) print api_v1('remarkbox.com')
print api_v1( 'http://lostquery.com', size='200x400' ) print api_v1('https://www.remarkbox.com', size='200x400')
print api_v1( 'https://lostquery.com', size='400x200' ) print api_v1('https://www.remarkbox.com', size='400x200')

View file

@ -2,7 +2,30 @@
""" """
Use LinkPeek via reStructuredText Use LinkPeek via reStructuredText
================================= =================================
This plugin allows you to use LinkPeek images from within reST documents. This plugin allows you to use LinkPeek images from within reST documents.
For example:
.. code-block:: restructuredtext
.. linkpeek::
uri = http://www.remarkbox.com
action = link-image
size = 600x400
In Pelican static site generator, you can register this Docutils plugin in your ``pelicanconf.py``
.. code-block:: python
# register linkpeek docutils directive.
import sys
sys.path.append('lib')
import rst_linkpeek
linkpeek = rst_linkpeek.LinkPeek
linkpeek.apikey = LINKPEEK_APIKEY
linkpeek.secret = LINKPEEK_SECRET
linkpeek.register()
""" """
from __future__ import unicode_literals from __future__ import unicode_literals
@ -14,7 +37,6 @@ from docutils.parsers.rst import (
from collections import defaultdict from collections import defaultdict
#from .liblinkpeek import api_v1
from liblinkpeek import api_v1 from liblinkpeek import api_v1
class LinkPeek(Directive): class LinkPeek(Directive):
@ -29,7 +51,30 @@ class LinkPeek(Directive):
params = defaultdict(str) params = defaultdict(str)
def api_call(self): def api_call(self):
return api_v1(self.params['uri'], self.apikey, self.secret, self.params['size']) return api_v1(
self.params['uri'],
self.apikey,
self.secret,
self.params['size'],
self.params['viewport'],
)
def html_image(self):
"""return html image markup."""
html = '<img src="{}" title="{}" align="{}" style="{}" class="{}" alt="{}" />'
return html.format(
self.api_call(),
self.params['title'],
self.params['align'],
self.params['style'],
self.params['class'],
self.params['alt'],
)
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 _get_params(self): def _get_params(self):
""" """
@ -48,31 +93,17 @@ class LinkPeek(Directive):
@property @property
def action_registry(self): def action_registry(self):
"""return an registry (dictionary) or action methods."""
return { return {
'image' : self.html_image, 'image' : self.html_image,
'link-image' : self.html_link_image, 'link-image' : self.html_link_image,
'' : self.api_call,
} }
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="{}" align="{}" style="{}" class="{}" />'
return html.format(self.api_call(), self.params['title'], self.params['align'], 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): def run(self):
"""this method is fired when rendering.""" """this method is fired when rendering."""
self._get_params() self._get_params()
action = self.get_action(self.params['action']) action = self.action_registry.get(self.params['action'])
output = action() output = action()