Merge branch 'feature/27', override thread id

This commit is contained in:
Martin Zimmermann 2013-12-12 13:07:57 +01:00
commit 150726df13
7 changed files with 48 additions and 23 deletions

View File

@ -50,3 +50,13 @@ data-isso-reply-to-self
----------------------- -----------------------
Set to `true` when spam guard is configured with `reply-to-self = true`. Set to `true` when spam guard is configured with `reply-to-self = true`.
data-isso-id
------------
Set a custom thread id, defaults to current URI. If you a comment counter, add
this attribute to the link tag, too.
.. code-block:: html
<section data-isso-id="test.abc" id="isso-thread"></section>

View File

@ -111,8 +111,8 @@ define(["q"], function(Q) {
return rv.substring(0, rv.length - 1); // chop off trailing "&" return rv.substring(0, rv.length - 1); // chop off trailing "&"
}; };
var create = function(data) { var create = function(tid, data) {
return curl("POST", endpoint + "/new?" + qs({uri: location}), JSON.stringify(data)).then( return curl("POST", endpoint + "/new?" + qs({uri: tid || location}), JSON.stringify(data)).then(
function (rv) { return JSON.parse(rv.body); }); function (rv) { return JSON.parse(rv.body); });
}; };
@ -137,9 +137,9 @@ define(["q"], function(Q) {
}); });
}; };
var fetch = function(plain) { var fetch = function(tid) {
return curl("GET", endpoint + "/?" + qs({uri: location, plain: plain}), null).then(function (rv) { return curl("GET", endpoint + "/?" + qs({uri: tid || location}), null).then(function (rv) {
if (rv.status === 200) { if (rv.status === 200) {
return JSON.parse(rv.body); return JSON.parse(rv.body);
} else { } else {
@ -148,8 +148,8 @@ define(["q"], function(Q) {
}); });
}; };
var count = function(uri) { var count = function(tid) {
return curl("GET", endpoint + "/count?" + qs({uri: uri}), null).then(function(rv) { return curl("GET", endpoint + "/count?" + qs({uri: tid || location}), null).then(function(rv) {
return JSON.parse(rv.body); return JSON.parse(rv.body);
}); });
}; };

View File

@ -5,9 +5,11 @@ define(["app/api", "app/dom", "app/markup"], function(api, $, Mark) {
return; return;
} }
var uri = el.href.match("^(.+)#isso-thread$")[1] var tid = el.getAttribute("data-isso-id") ||
el.href.match("^(.+)#isso-thread$")[1]
.replace(/^.*\/\/[^\/]+/, ''); .replace(/^.*\/\/[^\/]+/, '');
api.count(uri).then(function(rv) {
api.count(tid).then(function(rv) {
el.textContent = Mark.up("{{ i18n-num-comments | pluralize : `n` }}", {n: rv}); el.textContent = Mark.up("{{ i18n-num-comments | pluralize : `n` }}", {n: rv});
}); });
}); });

View File

@ -59,7 +59,7 @@ define(["app/text/html", "app/dom", "app/utils", "app/config", "app/api", "app/m
return; return;
} }
api.create({ api.create($("#isso-thread").getAttribute("data-isso-id"), {
author: $("[name=author]", el).value || null, author: $("[name=author]", el).value || null,
email: $("[name=email]", el).value || null, email: $("[name=email]", el).value || null,
text: $("textarea", el).value, text: $("textarea", el).value,

View File

@ -26,7 +26,7 @@ require(["ready", "app/config", "app/api", "app/isso", "app/count", "app/dom", "
$("#isso-thread").append(new isso.Postbox(null)); $("#isso-thread").append(new isso.Postbox(null));
$("#isso-thread").append('<div id="isso-root"></div>'); $("#isso-thread").append('<div id="isso-root"></div>');
api.fetch().then(function(rv) { api.fetch($("#isso-thread").getAttribute("data-isso-id")).then(function(rv) {
if (! rv.length) { if (! rv.length) {
$("#isso-thread > h4").textContent = Mark.up("{{ i18n-no-comments }}"); $("#isso-thread > h4").textContent = Mark.up("{{ i18n-no-comments }}");

View File

@ -76,14 +76,14 @@ def host(name):
return (rv.netloc.rsplit(':')[0], rv.port or 80, rv.scheme == 'https') return (rv.netloc.rsplit(':')[0], rv.port or 80, rv.scheme == 'https')
def title(data, default=u"Untitled."): def thread(data, default=u"Untitled.", id=None):
""" """
Extract <h1> title from web page. The title is *probably* the text node, Extract <h1> title from web page. The title is *probably* the text node,
which is the nearest H1 node in context to an element with the `isso-thread` id. which is the nearest H1 node in context to an element with the `isso-thread` id.
>>> title("asdf") # doctest: +IGNORE_UNICODE >>> thread("asdf") # doctest: +IGNORE_UNICODE
'Untitled.' (None, 'Untitled.')
>>> title(''' >>> thread('''
... <html> ... <html>
... <head> ... <head>
... <title>Foo!</title> ... <title>Foo!</title>
@ -102,14 +102,22 @@ def title(data, default=u"Untitled."):
... </article> ... </article>
... </body> ... </body>
... </html>''') # doctest: +IGNORE_UNICODE ... </html>''') # doctest: +IGNORE_UNICODE
'Can you find me?' (None, 'Can you find me?')
>>> title(''' >>> thread('''
... <html> ... <html>
... <body> ... <body>
... <h1>I'm the real title!1 ... <h1>I'm the real title!1
... <section data-title="No way%21" id="isso-thread"> ... <section data-title="No way%21" id="isso-thread">
... ''') # doctest: +IGNORE_UNICODE ... ''') # doctest: +IGNORE_UNICODE
'No way!' (None, 'No way!')
>>> thread('''
... <section id="isso-thread" data-title="Test" data-isso-id="test">
... ''') # doctest: +IGNORE_UNICODE
('test', 'Test')
>>> thread('''
... <section id="isso-thread" data-isso-id="Fuu.">
... ''') # doctest: +IGNORE_UNICODE
('Fuu.', 'Untitled.')
""" """
html = html5lib.parse(data, treebuilder="dom") html = html5lib.parse(data, treebuilder="dom")
@ -123,7 +131,7 @@ def title(data, default=u"Untitled."):
chain(*map(html.getElementsByTagName, ("div", "section")))))) chain(*map(html.getElementsByTagName, ("div", "section"))))))
if not el: if not el:
return default return id, default
el = el[0] el = el[0]
visited = [] visited = []
@ -146,7 +154,12 @@ def title(data, default=u"Untitled."):
yield item yield item
try: try:
return unquote(el.attributes["data-title"].value) id = unquote(el.attributes["data-isso-id"].value)
except (KeyError, AttributeError):
pass
try:
return id, unquote(el.attributes["data-title"].value)
except (KeyError, AttributeError): except (KeyError, AttributeError):
pass pass
@ -156,8 +169,8 @@ def title(data, default=u"Untitled."):
rv = recurse(el) rv = recurse(el)
if rv: if rv:
return ''.join(gettext(rv)).strip() return id, ''.join(gettext(rv)).strip()
el = el.parentNode el = el.parentNode
return default return id, default

View File

@ -142,7 +142,7 @@ class API(object):
if uri not in self.threads: if uri not in self.threads:
with http.curl('GET', local("origin"), uri) as resp: with http.curl('GET', local("origin"), uri) as resp:
if resp and resp.status == 200: if resp and resp.status == 200:
title = parse.title(resp.read()) uri, title = parse.thread(resp.read(), id=uri)
else: else:
return NotFound('URI does not exist') return NotFound('URI does not exist')