Add util.py, a bag of holding (hack hack).

This commit is contained in:
Flancian 2020-11-16 15:26:03 +01:00
parent d21d463a4e
commit a74bef14fa
4 changed files with 25 additions and 12 deletions

View file

@ -17,12 +17,10 @@ import os
from flask import Flask
from flaskext.markdown import Markdown
from markdown.extensions.wikilinks import WikiLinkExtension
from . import util
def wikilink_to_url(label, base, end):
label = label.lower()
label = label.replace(' ', '-')
label = label.replace('\'', '')
label = label.replace(',', '')
label = util.canonical_wikilink(label)
url = '/node/' + label
return url

View file

@ -16,6 +16,7 @@ import glob
import re
import os
from . import config
from . import util
from collections import defaultdict
from fuzzywuzzy import fuzz
from operator import attrgetter
@ -72,7 +73,7 @@ class Subnode:
self.url = '/subnode/' + path_to_uri(path)
# Subnodes are attached to the node matching their wikilink.
# i.e. if two users contribute subnodes titled [[foo]], they both show up when querying node [[foo]].
self.wikilink = canonical_wikilink(path_to_wikilink(path))
self.wikilink = util.canonical_wikilink(path_to_wikilink(path))
self.user = path_to_user(path)
with open(path) as f:
self.content = f.read()
@ -112,11 +113,6 @@ def path_to_user(path):
else:
return 'agora'
def canonical_wikilink(wikilink):
# hack hack
wikilink = wikilink.lower().replace(' ', '-').replace('\'', '').replace(',', '')
return wikilink
def path_to_wikilink(path):
return os.path.splitext(os.path.basename(path))[0]
@ -124,7 +120,7 @@ def content_to_outlinks(content):
# hack hack.
match = RE_WIKILINKS.findall(content)
if match:
return [canonical_wikilink(m) for m in match]
return [util.canonical_wikilink(m) for m in match]
else:
return []
@ -192,5 +188,5 @@ def nodes_by_outlink(wikilink):
def subnodes_by_outlink(wikilink):
# This doesn't work. It matches too much/too little for some reason. Debug someday?
# subnodes = [subnode for subnode in all_subnodes() if [wikilink for wikilink in subnode.outlinks if fuzz.ratio(subnode.wikilink, wikilink) > FUZZ_FACTOR]]
subnodes = [subnode for subnode in all_subnodes() if canonical_wikilink(wikilink) in subnode.outlinks]
subnodes = [subnode for subnode in all_subnodes() if util.canonical_wikilink(wikilink) in subnode.outlinks]
return subnodes

View file

@ -39,6 +39,7 @@
<a href="#" class="theme-toggle">theme</a>
<a href="/node/agora-help">help</a>
</span>
<br />
<hr>
{% block content %}
{% endblock %}

18
app/util.py Normal file
View file

@ -0,0 +1,18 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
def canonical_wikilink(wikilink):
# hack hack
wikilink = wikilink.lower().replace(' ', '-').replace('\'', '').replace(',', '')
return wikilink