Implement [[push]]. Hacky, but it seems to work to the point it feels
useful.
This commit is contained in:
parent
7f718f499d
commit
aa712e0622
5 changed files with 135 additions and 19 deletions
24
app/agora.py
24
app/agora.py
|
@ -23,7 +23,6 @@ from . import util
|
||||||
bp = Blueprint('agora', __name__)
|
bp = Blueprint('agora', __name__)
|
||||||
G = db.G
|
G = db.G
|
||||||
|
|
||||||
|
|
||||||
# Special
|
# Special
|
||||||
@bp.route('/index')
|
@bp.route('/index')
|
||||||
@bp.route('/')
|
@bp.route('/')
|
||||||
|
@ -83,18 +82,19 @@ def go(node):
|
||||||
|
|
||||||
return redirect(links[0])
|
return redirect(links[0])
|
||||||
|
|
||||||
@bp.route('/pull/<node>')
|
@bp.route('/push/<node>/<other>')
|
||||||
def pull(node):
|
def push(node, other):
|
||||||
"""In the context of a node, "pulls attention" from the parameter node to the current subnode.
|
#import pprint
|
||||||
|
#response = []
|
||||||
|
#response.append("node: {}".format(node))
|
||||||
|
#response.append("other: {}".format(other))
|
||||||
|
n = G.node(node)
|
||||||
|
o = G.node(other)
|
||||||
|
pushing = n.pushing(o)
|
||||||
|
#response.append(pushing)
|
||||||
|
|
||||||
Here it "broadcasts": it renders all nodes that pull from a given node.
|
#return Response(pprint.pformat(response))
|
||||||
|
return Response(pushing)
|
||||||
Unclear at this point if this should exist at all, or whether it should do something else.
|
|
||||||
|
|
||||||
TODO: probably remove this, [[pull]] changed.
|
|
||||||
"""
|
|
||||||
|
|
||||||
return redirect('/node/{}'.format(node))
|
|
||||||
|
|
||||||
@bp.route('/jump')
|
@bp.route('/jump')
|
||||||
def jump():
|
def jump():
|
||||||
|
|
105
app/db.py
105
app/db.py
|
@ -23,6 +23,10 @@ from collections import defaultdict
|
||||||
from fuzzywuzzy import fuzz
|
from fuzzywuzzy import fuzz
|
||||||
from operator import attrgetter
|
from operator import attrgetter
|
||||||
|
|
||||||
|
# For [[push]] parsing, perhaps move elsewhere?
|
||||||
|
import lxml.html
|
||||||
|
import lxml.etree
|
||||||
|
|
||||||
|
|
||||||
# TODO: move action extractor regex here as well.
|
# TODO: move action extractor regex here as well.
|
||||||
RE_WIKILINKS = re.compile('\[\[(.*?)\]\]')
|
RE_WIKILINKS = re.compile('\[\[(.*?)\]\]')
|
||||||
|
@ -85,6 +89,18 @@ class Graph:
|
||||||
# return sorted(nodes, key=lambda x: -x.size())
|
# return sorted(nodes, key=lambda x: -x.size())
|
||||||
return sorted(nodes, key=lambda x: x.wikilink.lower())
|
return sorted(nodes, key=lambda x: x.wikilink.lower())
|
||||||
|
|
||||||
|
# The following method is unused; it is far too slow given the current control flow.
|
||||||
|
# Running something like this would be ideal eventually though.
|
||||||
|
# It might also work better once all pulling/pushing logic moves to Graph, where it belongs,
|
||||||
|
# and can make use of more sensible algorithms.
|
||||||
|
@cachetools.func.ttl_cache(maxsize=2, ttl=20)
|
||||||
|
def compute_transclusion(self, include_journals=True):
|
||||||
|
|
||||||
|
# Add artisanal virtual subnodes (resulting from transclusion/[[push]]) to all nodes.
|
||||||
|
for node in self.nodes():
|
||||||
|
pushed_subnodes = node.pushed_subnodes()
|
||||||
|
node.subnodes.extend(pushed_subnodes)
|
||||||
|
|
||||||
# does this belong here?
|
# does this belong here?
|
||||||
@cachetools.func.ttl_cache(maxsize=1, ttl=20)
|
@cachetools.func.ttl_cache(maxsize=1, ttl=20)
|
||||||
def subnodes(self, sort=lambda x: x.uri.lower()):
|
def subnodes(self, sort=lambda x: x.uri.lower()):
|
||||||
|
@ -129,6 +145,12 @@ class Node:
|
||||||
def __gt__(self, other):
|
def __gt__(self, other):
|
||||||
return self.wikilink.lower() > other.wikilink.lower()
|
return self.wikilink.lower() > other.wikilink.lower()
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.wikilink.lower()
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return "node: {}".format(self.wikilink.lower())
|
||||||
|
|
||||||
def size(self):
|
def size(self):
|
||||||
return len(self.subnodes)
|
return len(self.subnodes)
|
||||||
|
|
||||||
|
@ -181,13 +203,66 @@ class Node:
|
||||||
nodes = []
|
nodes = []
|
||||||
for backlink in self.back_links():
|
for backlink in self.back_links():
|
||||||
n = G.node(backlink)
|
n = G.node(backlink)
|
||||||
if self.wikilink in [n.wikilink for n in n.push_nodes()]:
|
if self.wikilink == n.wikilink:
|
||||||
|
# ignore nodes pushing to themselves.
|
||||||
|
continue
|
||||||
|
if self.wikilink != n.wikilink and self.wikilink in [n.wikilink for n in n.push_nodes()]:
|
||||||
nodes.append(n)
|
nodes.append(n)
|
||||||
return nodes
|
return nodes
|
||||||
|
|
||||||
def back_links(self):
|
def pushing(self, other):
|
||||||
return sorted([x.wikilink for x in nodes_by_outlink(self.wikilink)])
|
# returns the blocks that this node pushes to one other as "virtual subnodes"
|
||||||
|
# [[push]] as in anagora.org/node/push.
|
||||||
|
#
|
||||||
|
# arg other should be a Node.
|
||||||
|
# TODO: actually add type annotations, this is 2021.
|
||||||
|
#
|
||||||
|
# TLDR:
|
||||||
|
# - [[push]] [[other]]
|
||||||
|
# pushes all children (indented subitems) to [[other]].
|
||||||
|
#
|
||||||
|
# TODO: implement also:
|
||||||
|
# - [[push]] [[other]] foo
|
||||||
|
# pushes foo to [[other]]
|
||||||
|
#
|
||||||
|
# Congratulations! You've gotten to the hackiest place in the [[agora]].
|
||||||
|
# ...as of the time of writing :)
|
||||||
|
subnodes = []
|
||||||
|
if other in self.push_nodes():
|
||||||
|
for subnode in self.subnodes:
|
||||||
|
# I tried parsing the marko tree but honestly this seemed easier/simpler.
|
||||||
|
html = render.markdown(subnode.content)
|
||||||
|
tree = lxml.html.fromstring(html)
|
||||||
|
for link in tree.iterlinks():
|
||||||
|
# link is of the form (element, attribute, link, pos) -- see https://lxml.de/3.1/lxmlhtml.html.
|
||||||
|
if link[2] == 'push':
|
||||||
|
# ugly, but hey, it works... for now.
|
||||||
|
# this is *flaky* as it depends on an exact number of html elements to separate
|
||||||
|
# [[push]] and its [[target node]].
|
||||||
|
# could be easily improved by just looking for the next <a>.
|
||||||
|
try:
|
||||||
|
argument = link[0].getnext().getnext().getnext().text_content()
|
||||||
|
if re.search(other.wikilink, argument, re.IGNORECASE) or re.search(other.wikilink.replace('-', ' '), argument, re.IGNORECASE):
|
||||||
|
# go one level up to find the <li>
|
||||||
|
parent = link[0].getparent()
|
||||||
|
# the block to be pushed is this level and its children.
|
||||||
|
# TODO: replace [[push]] [[other]] with something like [[pushed from]] [[node]], which makes more sense in the target.
|
||||||
|
block = lxml.etree.tostring(parent)
|
||||||
|
subnodes.append(VirtualSubnode(subnode, other, block))
|
||||||
|
except AttributeError:
|
||||||
|
# Better luck next time -- or when I fix this code :)
|
||||||
|
pass
|
||||||
|
return subnodes
|
||||||
|
|
||||||
|
def back_links(self):
|
||||||
|
return sorted([x.wikilink for x in nodes_by_outlink(self.wikilink) if x.wikilink != self.wikilink])
|
||||||
|
|
||||||
|
def pushed_subnodes(self):
|
||||||
|
subnodes = []
|
||||||
|
for node in self.pushing_nodes():
|
||||||
|
for subnode in node.pushing(self):
|
||||||
|
subnodes.append(subnode)
|
||||||
|
return subnodes
|
||||||
|
|
||||||
|
|
||||||
class Subnode:
|
class Subnode:
|
||||||
|
@ -223,6 +298,8 @@ class Subnode:
|
||||||
# Initiate node for wikilink if this is the first subnode, append otherwise.
|
# Initiate node for wikilink if this is the first subnode, append otherwise.
|
||||||
# G.addsubnode(self)
|
# G.addsubnode(self)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
# hack hack
|
# hack hack
|
||||||
if fuzz.ratio(self.wikilink, other.wikilink) > FUZZ_FACTOR:
|
if fuzz.ratio(self.wikilink, other.wikilink) > FUZZ_FACTOR:
|
||||||
|
@ -296,6 +373,28 @@ class Subnode:
|
||||||
push_nodes = content_to_forward_links("\n".join(push_blocks))
|
push_nodes = content_to_forward_links("\n".join(push_blocks))
|
||||||
return [G.node(node) for node in push_nodes]
|
return [G.node(node) for node in push_nodes]
|
||||||
|
|
||||||
|
class VirtualSubnode(Subnode):
|
||||||
|
# For instantiating a virtual subnode -- a subnode derived from another subnode.
|
||||||
|
# Used by [[push]] (transclusion).
|
||||||
|
def __init__(self, source_subnode, target_node, block):
|
||||||
|
"""
|
||||||
|
source_subnode: where this virtual subnode came from.
|
||||||
|
target_node: where this virtual subnode will attach (go to).
|
||||||
|
block: the actual payload, as pre rendered html."""
|
||||||
|
self.uri = source_subnode.uri
|
||||||
|
self.url = '/subnode/virtual'
|
||||||
|
# Virtual subnodes are attached to their target
|
||||||
|
self.wikilink = target_node.wikilink
|
||||||
|
self.user = source_subnode.user
|
||||||
|
# Only text transclusion supported.
|
||||||
|
self.mediatype = 'text/plain'
|
||||||
|
|
||||||
|
self.content = block.decode('UTF-8')
|
||||||
|
self.forward_links = content_to_forward_links(self.content)
|
||||||
|
|
||||||
|
self.mtime = source_subnode.mtime
|
||||||
|
self.node = self.wikilink
|
||||||
|
|
||||||
|
|
||||||
def subnode_to_actions(subnode, action):
|
def subnode_to_actions(subnode, action):
|
||||||
# hack hack.
|
# hack hack.
|
||||||
|
|
|
@ -25,10 +25,10 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="pushlinks">
|
<div class="pushlinks">
|
||||||
<span class="pushlinks-header">↑ pushing</span><br />
|
<span class="pushlinks-header">↑ pushing to this node</span><br />
|
||||||
{% if pushing_nodes %}
|
{% if pushing_nodes %}
|
||||||
{% for node in pushing_nodes %}
|
{% for node in pushing_nodes %}
|
||||||
<a href="/node/{{link}}">{{node.uri}}</a><br />
|
<a href="/node/{{node.uri}}">{{node.uri}}</a><br />
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
{% else %}
|
{% else %}
|
||||||
(none)
|
(none)
|
||||||
|
@ -45,7 +45,7 @@
|
||||||
<br />
|
<br />
|
||||||
-->
|
-->
|
||||||
|
|
||||||
<span class="pulllinks-header">↓ pulling</span><br />
|
<span class="pulllinks-header">↓ pulling this node</span><br />
|
||||||
{% if pulling_nodes %}
|
{% if pulling_nodes %}
|
||||||
{% for node in pulling_nodes %}
|
{% for node in pulling_nodes %}
|
||||||
<a href="/node/{{node.uri}}">{{node.uri}}</a><br />
|
<a href="/node/{{node.uri}}">{{node.uri}}</a><br />
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
{% extends "base.html" %}
|
{% extends "base.html" %}
|
||||||
{% block content %}
|
{% block content %}
|
||||||
|
|
||||||
{% if not node.subnodes %}
|
{% if not node.subnodes and not node.pushed_subnodes() %}
|
||||||
<div class="not-found">
|
<div class="not-found">
|
||||||
No node found at [[{{node.uri}}]].
|
No node found at [[{{node.uri}}]].
|
||||||
<br /><br />
|
<br /><br />
|
||||||
|
@ -43,6 +43,21 @@ Try listing all <a href="/nodes">nodes</a> or perhaps <a href="/search">search</
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
|
{% if node.pushed_subnodes() %}
|
||||||
|
{% for subnode in node.pushed_subnodes() %}
|
||||||
|
<div class="subnode">
|
||||||
|
<div class="subnode-header">
|
||||||
|
<span class="subnode-id"><strong>Subnode</strong> <a href="/@{{subnode.user}}/{{node.uri}}">[[@{{subnode.user}}/{{node.uri}}]]</a></span><br />
|
||||||
|
<span class="subnode-links"><strong>pushed from</strong> <a href="/subnode/{{subnode.uri}}">{{subnode.uri}}</a> by <a href="/@{{subnode.user}}">@{{subnode.user}}</a></span>
|
||||||
|
</div>
|
||||||
|
<ul>
|
||||||
|
{{ subnode.render()|linkify|safe }}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
{% for node in pull_nodes %}
|
{% for node in pull_nodes %}
|
||||||
<div class="node">
|
<div class="node">
|
||||||
<span class="node-header"><strong>Pulled node</strong> <a href="/node/{{node.uri}}">[[{{node.uri}}]]</a></span>
|
<span class="node-header"><strong>Pulled node</strong> <a href="/node/{{node.uri}}">[[{{node.uri}}]]</a></span>
|
||||||
|
|
|
@ -2,6 +2,7 @@ bleach==3.2.1
|
||||||
cachetools==4.2.0
|
cachetools==4.2.0
|
||||||
click==7.1.2
|
click==7.1.2
|
||||||
dateparser==1.0.0
|
dateparser==1.0.0
|
||||||
|
filetype==1.0.7
|
||||||
Flask==1.1.2
|
Flask==1.1.2
|
||||||
Flask-Markdown==0.3
|
Flask-Markdown==0.3
|
||||||
Flask-WTF==0.14.3
|
Flask-WTF==0.14.3
|
||||||
|
@ -10,6 +11,7 @@ importlib-metadata==2.0.0
|
||||||
itsdangerous==1.1.0
|
itsdangerous==1.1.0
|
||||||
jedi==0.17.2
|
jedi==0.17.2
|
||||||
Jinja2==2.11.2
|
Jinja2==2.11.2
|
||||||
|
lxml==4.6.2
|
||||||
Markdown==3.3.3
|
Markdown==3.3.3
|
||||||
marko==0.9.1
|
marko==0.9.1
|
||||||
MarkupSafe==1.1.1
|
MarkupSafe==1.1.1
|
||||||
|
|
Loading…
Reference in a new issue