-
Notifications
You must be signed in to change notification settings - Fork 3
/
lektor_disqus_comments.py
68 lines (58 loc) · 2.3 KB
/
lektor_disqus_comments.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# -*- coding: utf-8 -*-
from markupsafe import Markup
from lektor.pluginsystem import Plugin
from lektor.context import get_ctx, url_to
from lektor.utils import htmlsafe_json_dump
SCRIPT = '''
<div id="disqus_thread"></div>
<script>
var disqus_config = function() { %(config)s };
(function() {
var d = document, s = d.createElement('script');
s.src = '//%(shortname)s.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
</script>
<noscript>
Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript"
rel="nofollow">comments powered by Disqus.</a>
</noscript>
'''
class DisqusCommentsPlugin(Plugin):
name = u'Disqus Comments'
description = u'Lektor plugin to add Disqus comments to a website.'
def get_disqus_config(self, identifier=None, url=None, title=None,
category_id=None):
configs = []
ctx = get_ctx()
if identifier is None:
if ctx.source is not None and ctx.source.path is not None:
identifier = ctx.source.path
if identifier is not None:
configs.append('this.page.identifier = %s;' %
htmlsafe_json_dump(identifier))
if url is None and ctx.source is not None:
try:
url = url_to(ctx.source, external=True)
except RuntimeError:
url = None
if url is not None:
configs.append('this.page.url = %s;' % htmlsafe_json_dump(url))
if title is not None:
configs.append('this.page.title = %s;' % htmlsafe_json_dump(title))
if category_id is not None:
configs.append('this.page.category_id = %s;'
% htmlsafe_json_dump(category_id))
return ' '.join(configs)
def on_setup_env(self, **extra):
shortname = self.get_config().get('shortname')
if shortname is None:
raise RuntimeError('Disqus shortname not configured')
def disqus(identifier=None, url=None):
config = self.get_disqus_config(identifier, url)
return Markup(SCRIPT % {
'config': config,
'shortname': shortname,
})
self.env.jinja_env.globals['render_disqus_comments'] = disqus