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 flask import Flask
from flaskext.markdown import Markdown from flaskext.markdown import Markdown
from markdown.extensions.wikilinks import WikiLinkExtension from markdown.extensions.wikilinks import WikiLinkExtension
from . import util
def wikilink_to_url(label, base, end): def wikilink_to_url(label, base, end):
label = label.lower() label = util.canonical_wikilink(label)
label = label.replace(' ', '-')
label = label.replace('\'', '')
label = label.replace(',', '')
url = '/node/' + label url = '/node/' + label
return url return url

View file

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

View file

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