From 26ecbf1007a5d87c47566c13efe45a1fd902461b Mon Sep 17 00:00:00 2001 From: TiynGER Date: Fri, 12 Jun 2020 23:49:59 +0200 Subject: [PATCH] search: preview added - The search result list now has previews - The Dockerfile doesnt need git anymore --- docker/Dockerfile => Dockerfile | 8 +++---- README.md | 6 ++++- src/.gitignore | 1 + src/app.py | 9 ++++---- src/content.py | 39 +++++++++++++++++++++++---------- src/forms.py | 2 +- src/static/css/style.css | 14 ++++++++++++ src/templates/index.html | 9 ++++++++ src/templates/search.html | 8 ++++--- 9 files changed, 72 insertions(+), 24 deletions(-) rename docker/Dockerfile => Dockerfile (52%) create mode 100644 src/templates/index.html diff --git a/docker/Dockerfile b/Dockerfile similarity index 52% rename from docker/Dockerfile rename to Dockerfile index 2a663fe..c03ba83 100644 --- a/docker/Dockerfile +++ b/Dockerfile @@ -4,15 +4,15 @@ MAINTAINER Tiyn tiyn@martenkante.eu RUN apt-get update -RUN apt-get install python3 python3-pip git -y +RUN apt-get install python3 python3-pip -y -RUN git clone https://github.com/tiyn/tiyny-wiki /wiki +COPY src /wiki -WORKDIR /wiki/src +WORKDIR /wiki RUN pip3 install -r requirements.txt -VOLUME /wiki/src/templates/entry +VOLUME /wiki/templates/entry EXPOSE 5000 diff --git a/README.md b/README.md index 9f2e91f..ae5e7fa 100644 --- a/README.md +++ b/README.md @@ -11,12 +11,16 @@ I however just want to put my markdown files in a directory and get a working wi - [x] Markdown Files (.md) - [x] Entry page - [ ] Option to get plain text file + - [ ] Optimize CSS for code +- [x] Start page - [x] Search page - [x] Full-text search - - [ ] Show first few lines of each match (description) + - [x] Show first few lines of each match (preview) + - [ ] Better CSS - [ ] Navigation - [ ] More advanced namespaces - [x] Header + - [ ] Random article - [ ] Search bar in header - [x] Footer - [x] Switchable CSS diff --git a/src/.gitignore b/src/.gitignore index 5235e9d..91f9f0b 100644 --- a/src/.gitignore +++ b/src/.gitignore @@ -1,2 +1,3 @@ __pycache__/ indexdir/ +templates/entry diff --git a/src/app.py b/src/app.py index 7132aa6..ecfe801 100644 --- a/src/app.py +++ b/src/app.py @@ -1,7 +1,7 @@ import os from flask import Flask, flash, make_response, render_template, request, redirect, abort -import content as con_gen +import content as cont import config import search as src from forms import SearchForm, csrf @@ -24,22 +24,23 @@ def page_not_found(e): @app.route('/') @app.route('/index.html') def index(): - return 'placeholder for index', 200 + return render_template('index.html', title=TITLE, style=STYLE), 200 @app.route('/search', methods=['GET', 'POST']) +@app.route('/search.html', methods=['GET', 'POST']) def search(): form = SearchForm() if request.method == 'POST': query_str = request.form['query_str'] - content = con_gen.gen_query_res_string(query_str) + content = cont.gen_query_res_string(query_str) return render_template('search.html', title=TITLE, style=STYLE, form=form, content=content), 200 return render_template('search.html', title=TITLE, style=STYLE, form=form, content=''), 200 @app.route('/entry/') def entry(fullurl): - content = con_gen.gen_stand_string(fullurl) + content = cont.gen_stand_string(fullurl) return render_template('entry.html', title=TITLE, style=STYLE, content=content), 200 diff --git a/src/content.py b/src/content.py index d604c06..859af97 100644 --- a/src/content.py +++ b/src/content.py @@ -15,19 +15,19 @@ WEBSITE = config.WEBSITE def gen_stand_string(path_ex): filename = os.path.join(ENTRY_DIR, path_ex) - content_string = '' + result = '' if path.exists(filename): title = open(filename).readline().rstrip('\n') text = open(filename).readlines()[1:] filename_no_end = filename.split('.', 1)[0] - content_string += '

' + title + '

\n' + result += '

' + title + '

\n' if filename.endswith('.md'): - content_string += gen_md_content(filename, 1) - return content_string + result += gen_md_content(filename, 1) + return result def gen_md_content(path_ex, depth): - content_string = '' + result = '' if path.exists(path_ex): filename = path_ex.split('.', 1) fileend = filename[len(filename) - 1] @@ -35,14 +35,13 @@ def gen_md_content(path_ex, depth): for i in range(depth): header += '#' header += ' ' - markdown_lines = open(path_ex, "r").readlines()[1:] + markdown_lines = open(path_ex, 'r').readlines()[1:] markdown_text = '' for line in markdown_lines: markdown_text += line.replace('# ', header) - content_string = markdown.markdown( - markdown_text, extensions=["fenced_code", "tables"] - ) - return content_string + result = markdown.markdown( + markdown_text, extensions=["fenced_code", "tables", "nl2br"]) + return result def gen_query_res_string(query_str): @@ -52,7 +51,25 @@ def gen_query_res_string(query_str): for result in src_results: title = result['title'] path = result['path'] + preview = create_preview(path) path = '/entry/' + path.split('/', 2)[2] - res_string += '
  • ' + title + '
  • ' + res_string += '
  • ' + title + '
    ' + res_string += '
    ' + preview + '
    ' + res_string += '
  • ' res_string += '\n' return res_string + + +def create_preview(path): + file = open(path, 'r') + first_lines = file.readlines() + preview = '' + preview_length = 3 + for i, line in enumerate(first_lines): + if i > preview_length: + break + if not line.isspace(): + preview += line + '
    ' + else: + preview_length += 1 + return preview diff --git a/src/forms.py b/src/forms.py index 136de1c..d4390ab 100644 --- a/src/forms.py +++ b/src/forms.py @@ -8,4 +8,4 @@ csrf = CSRFProtect() class SearchForm(FlaskForm): query_str = TextField( "Query", [validators.Required("Please enter the search term")]) - submit = SubmitField("Send") + submit = SubmitField("Search") diff --git a/src/static/css/style.css b/src/static/css/style.css index 4ec6cfe..5175c92 100644 --- a/src/static/css/style.css +++ b/src/static/css/style.css @@ -29,6 +29,16 @@ html { overflow-x: hidden; } +code { + border-radius: 25px; + padding-left: 20px; + padding-right: 20px; + page-break-inside: avoid; + font-family: monospace; + white-space: pre; + display: inline-block +} + footer { height: 100px; padding-top: 20px; @@ -38,6 +48,10 @@ footer .center { text-align: center; } +ul { + padding-left: 10px; +} + .container { min-height: 100%; padding-bottom: 50px; diff --git a/src/templates/index.html b/src/templates/index.html new file mode 100644 index 0000000..15e8b7c --- /dev/null +++ b/src/templates/index.html @@ -0,0 +1,9 @@ +{% extends "template.html" %} +{% block content %} +
    +
    + This is the
    + {{ title }} +
    +
    +{% endblock %} diff --git a/src/templates/search.html b/src/templates/search.html index cc558f5..0076692 100644 --- a/src/templates/search.html +++ b/src/templates/search.html @@ -7,9 +7,11 @@ {{ form.query_str }} {{ form.submit }} - {% autoescape off %} - {{ content }} - {% endautoescape %} +
    + {% autoescape off %} + {{ content }} + {% endautoescape %} +
    {% endblock %}