97 lines
2.4 KiB
Python
97 lines
2.4 KiB
Python
# from misc import SaveFileMapping, SaveFileObject
|
|
from .api import EtherpadLiteLazyAPI
|
|
from slugify import slugify
|
|
from lazymappingstorage import LazyMappingStorage, LazyStorageObject
|
|
from flask import Blueprint, make_response, render_template
|
|
import bs4
|
|
import re
|
|
from dataclasses import dataclass
|
|
from anytree import NodeMixin
|
|
import settings
|
|
|
|
# import html
|
|
|
|
ep = EtherpadLiteLazyAPI(
|
|
url=settings.ETHERPAD_API,
|
|
exturl=settings.ETHERPAD_EXT,
|
|
keyfile=settings.ETHERPAD_KEYFILE,
|
|
groupmapper=settings.ETHERPAD_GROUP,
|
|
)
|
|
|
|
|
|
def clean_path(path):
|
|
path = path.split("/")
|
|
return "/".join([slugify(p) for p in path])
|
|
|
|
|
|
def parse_intern_page(text):
|
|
if not text:
|
|
return text
|
|
soup = bs4.BeautifulSoup(text, "html.parser")
|
|
textNodes = soup.findAll(text=True)
|
|
|
|
entities = {
|
|
"internallinks": re.findall(r"\[(\w+)\]", text),
|
|
"tags": re.findall(r"#([\w-]+)\s?", text),
|
|
}
|
|
if not textNodes:
|
|
return text
|
|
for textNode in textNodes:
|
|
text = re.sub(r"\[(\w+)\]", r'<a href="/wiki/\g<1>">\g<1></a>', str(textNode))
|
|
# text = re.sub(r"^(\w+)\:\s?(.+)$",'config param - \g<1> : \g<2> ',text)
|
|
text = re.sub(r"#([\w-]+)\s?", r'<a href="/tag/\g<1>">#\g<1></a>', text)
|
|
print(text)
|
|
textNode.replaceWith(bs4.BeautifulSoup(text, "html.parser"))
|
|
|
|
return str(soup), entities
|
|
|
|
|
|
@dataclass
|
|
class Page(LazyStorageObject, NodeMixin):
|
|
path: str = None
|
|
count: int = 1
|
|
|
|
@property
|
|
def id(self):
|
|
return slugify(self.path)
|
|
|
|
@property
|
|
def editlink(self):
|
|
return ep.getPadLink(self.id)
|
|
|
|
@property
|
|
def content(self):
|
|
return parse_intern_page(ep.getPadHTML(self.id))
|
|
|
|
|
|
class PageManager(LazyMappingStorage):
|
|
filename = "pages.yaml"
|
|
object_class = Page
|
|
|
|
def __getitem__(self, key):
|
|
o = super().__getitem__(clean_path(key))
|
|
o.path = clean_path(key)
|
|
if not o.path == "":
|
|
o.parent = self["/".join(o.path.split("/")[:-1])]
|
|
|
|
return o
|
|
|
|
def __setitem__(self, key, value):
|
|
super().__setitem__(clean_path(key), value)
|
|
|
|
|
|
pages = PageManager()
|
|
bp = Blueprint("wiki", __name__, url_prefix="/wiki")
|
|
|
|
|
|
@bp.route("/")
|
|
@bp.route("/<path:path>")
|
|
def web(path=""):
|
|
|
|
page = pages[path]
|
|
pages.to_file()
|
|
|
|
response = make_response(render_template("wiki.html", page=page))
|
|
response.set_cookie("sessionID", str(ep.getSessionID("andis")), path="/etherpad")
|
|
return response
|