Skip to content

Commit

Permalink
refac: Customize head section and add more favicons
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolay-Lysenko committed Jul 4, 2024
1 parent 14282fc commit e42bf31
Show file tree
Hide file tree
Showing 16 changed files with 67 additions and 73 deletions.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "readingbricks"
version = "0.2.0"
version = "0.2.1"
description = "Flask app for reading and searching notes from a personal knowledge base"
readme = "README.md"
keywords = ["knowledge_base", "lecture_notes", "search_engine", "tf_idf", "zettelkasten"]
Expand Down Expand Up @@ -33,5 +33,5 @@ packages = ["readingbricks"]

[tool.setuptools.package-data]
"readingbricks.static.css" = ["*.css"]
"readingbricks.static.images" = ["*.png"]
"readingbricks.static.images.favicons" = ["*.ico", "*.png"]
"readingbricks.templates" = ["*.html"]
10 changes: 9 additions & 1 deletion readingbricks/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@
"""


LANGUAGES_FOR_STEMMER = {'en': 'english', 'ru': 'russian'}
LANGUAGES_FOR_STEMMER = {"en": "english", "ru": "russian"}
MARKDOWN_DIR_NAME = "markdown_notes"
TAG_COUNTS_FILE_NAME = "tag_counts.tsv"
TAG_TO_NOTES_DB_FILE_NAME = "tag_to_notes.db"
TF_IDF_DB_FILE_NAME = "tf_idf.db"

TITLES_BY_TEMPLATE_AND_LANG = {
"404.html": {"en": "Page not found", "ru": "Страница не найдена"},
"about.html": {"en": "About", "ru": "О проекте"},
"empty_result.html": {"en": "Nothing found", "ru": "Ничего не найдено"},
"index.html": {"en": "Index", "ru": "Главная"},
"invalid_query.html": {"en": "Check your query", "ru": "Проверьте запрос"},
}
File renamed without changes.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
Binary file not shown.
7 changes: 1 addition & 6 deletions readingbricks/templates/404.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<html>
<head>
<meta charset="utf-8">
<title>Страница не найдена</title>
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='css/baseline_style.css') }}"/>
<link rel=icon type=image/png href="{{ url_for('static', filename='images/favicon.png') }}"/>
</head>
{% include 'head.html' %}
<body>
<nav id="frozen_button_one"><a href="{{ url_for('index') }}" class="button">/</a></nav>
<nav id="frozen_button_two"><a href="{{ url_for('about') }}" class="button">?</a></nav>
Expand Down
7 changes: 1 addition & 6 deletions readingbricks/templates/about.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<html>
<head>
<meta charset="utf-8">
<title>ReadingBricks - О проекте</title>
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='css/baseline_style.css') }}"/>
<link rel=icon type=image/png href="{{ url_for('static', filename='images/favicon.png') }}"/>
</head>
{% include 'head.html' %}
<body>
<nav id="frozen_button_one"><a href="{{ url_for('index') }}" class="button">/</a></nav>
<div id="central">
Expand Down
7 changes: 1 addition & 6 deletions readingbricks/templates/empty_result.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<html>
<head>
<meta charset="utf-8">
<title>ReadingBricks - Ничего не найдено</title>
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='css/baseline_style.css') }}"/>
<link rel=icon type=image/png href="{{ url_for('static', filename='images/favicon.png') }}"/>
</head>
{% include 'head.html' %}
<body>
<nav id="frozen_button_one"><a href="{{ url_for('index') }}" class="button">/</a></nav>
<nav id="frozen_button_two"><a href="{{ field_url }}" class="button"></a></nav>
Expand Down
7 changes: 1 addition & 6 deletions readingbricks/templates/field.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<html>
<head>
<meta charset="utf-8">
<title>ReadingBricks - {{ field_alias }}</title>
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='css/baseline_style.css') }}"/>
<link rel=icon type=image/png href="{{ url_for('static', filename='images/favicon.png') }}"/>
</head>
{% include 'head.html' %}
<body>
<nav id="frozen_button_one"><a href="{{ url_for('index') }}" class="button">/</a></nav>
<nav id="frozen_button_two"><a href="{{ url_for('about') }}" class="button">?</a></nav>
Expand Down
18 changes: 18 additions & 0 deletions readingbricks/templates/head.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{ page_title }} | ReadingBricks</title>
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='css/base.css') }}">
<link rel="icon" type="image/png" sizes="32x32" href="{{ url_for('static', filename='images/favicons/favicon-32x32.png') }}">
<link rel="icon" type="image/png" sizes="16x16" href="{{ url_for('static', filename='images/favicons/favicon-16x16.png') }}">
<link rel="shortcut icon" type="image/x-icon" href="{{ url_for('static', filename='images/favicons/favicon.ico') }}">
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
CommonHTML: { linebreaks: { automatic: true } },
"HTML-CSS": { linebreaks: { automatic: true } },
SVG: { linebreaks: { automatic: true } }
});
</script>
<script type="text/javascript" async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS_HTML"></script>
</head>
7 changes: 1 addition & 6 deletions readingbricks/templates/index.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<html>
<head>
<meta charset="utf-8">
<title>ReadingBricks - Главная</title>
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='css/baseline_style.css') }}"/>
<link rel=icon type=image/png href="{{ url_for('static', filename='images/favicon.png') }}"/>
</head>
{% include 'head.html' %}
<body>
<nav id="frozen_button_one"><a href="{{ url_for('about') }}" class="button">?</a></nav>
<div id="narrow_central">
Expand Down
7 changes: 1 addition & 6 deletions readingbricks/templates/invalid_query.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<html>
<head>
<meta charset="utf-8">
<title>ReadingBricks - Проверьте запрос</title>
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='css/baseline_style.css') }}"/>
<link rel=icon type=image/png href="{{ url_for('static', filename='images/favicon.png') }}"/>
</head>
{% include 'head.html' %}
<body>
<nav id="frozen_button_one"><a href="{{ url_for('index') }}" class="button">/</a></nav>
<nav id="frozen_button_two"><a href="{{ field_url }}" class="button"></a></nav>
Expand Down
15 changes: 1 addition & 14 deletions readingbricks/templates/regular_page.html
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
<html>
<head>
<meta charset="utf-8">
<title>{{ page_title }}</title>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
CommonHTML: { linebreaks: { automatic: true } },
"HTML-CSS": { linebreaks: { automatic: true } },
SVG: { linebreaks: { automatic: true } }
});
</script>
<script type="text/javascript" async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.2/MathJax.js?config=TeX-AMS_HTML"></script>
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='css/baseline_style.css') }}"/>
<link rel=icon type=image/png href="{{ url_for('static', filename='images/favicon.png') }}"/>
</head>
{% include 'head.html' %}
<body>
<nav id="frozen_button_one"><a href="{{ url_for('index') }}" class="button">/</a></nav>
<nav id="frozen_button_two"><a href="{{ field_url }}" class="button"></a></nav>
Expand Down
47 changes: 29 additions & 18 deletions readingbricks/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from markupsafe import Markup

from readingbricks import app
from readingbricks.constants import TITLES_BY_TEMPLATE_AND_LANG
from readingbricks.query_processing import QueriesProcessor
from readingbricks.utils import compress

Expand All @@ -27,6 +28,16 @@
markdown_preprocessor.init_app(app)


def render_localized_template(template_filename: str, **kwargs) -> str:
"""Render Jinja2 template with proper localization."""
language = app.config["LANGUAGE"]
if template_filename in TITLES_BY_TEMPLATE_AND_LANG:
page_title = TITLES_BY_TEMPLATE_AND_LANG[template_filename][language]
else:
page_title = kwargs.pop("page_title")
return render_template(template_filename, **locals(), **kwargs)


@app.route('/')
def index() -> RESPONSE_TYPE:
"""Render home page."""
Expand All @@ -38,27 +49,27 @@ def index() -> RESPONSE_TYPE:
link = f'<div>{link}</div>\n'
links_to_fields.append(link)
fields_cloud = Markup(''.join(links_to_fields))
contents_with_css = render_template('index.html', **locals())
contents_with_css = render_localized_template('index.html', **locals())
return contents_with_css


@app.route('/about')
def about() -> RESPONSE_TYPE:
"""Render page with info about the project."""
return render_template('about.html')
return render_localized_template('about.html')


@app.route('/<field_name>')
def field(field_name: str) -> RESPONSE_TYPE:
"""Render page of a particular field."""
if field_name not in app.config['FIELDS']:
return render_template('404.html'), 404
field_alias = app.config['FIELD_TO_ALIAS'].get(field_name, field_name)
return render_localized_template('404.html'), 404
page_title = app.config['FIELD_TO_ALIAS'].get(field_name, field_name)
search_prompt = app.config['FIELD_TO_SEARCH_PROMPT'].get(field_name, "")

tags_with_counts = []
path_to_tag_counts = os.path.join(
app.config.get("RESOURCES_DIR"), field_name, app.config.get("TAG_COUNTS_FILE_NAME")
app.config["RESOURCES_DIR"], field_name, app.config["TAG_COUNTS_FILE_NAME"]
)
with open(path_to_tag_counts) as source_file:
for line in source_file: # pragma: no branch
Expand All @@ -71,7 +82,7 @@ def field(field_name: str) -> RESPONSE_TYPE:
]
tags_cloud = Markup(''.join(links_to_tags))

contents_with_css = render_template('field.html', **locals())
contents_with_css = render_localized_template('field.html', **locals())
return contents_with_css


Expand Down Expand Up @@ -112,7 +123,7 @@ def convert_note_from_markdown_to_html(field_name: str, hashed_title: str) -> Op
If requested note does not exist, return `None`.
"""
dir_path = os.path.join(
app.config.get("RESOURCES_DIR"), field_name, app.config.get("MARKDOWN_DIR_NAME")
app.config["RESOURCES_DIR"], field_name, app.config["MARKDOWN_DIR_NAME"]
)
abs_requested_path = os.path.join(dir_path, f'{hashed_title}.md')
if not os.path.isfile(abs_requested_path):
Expand All @@ -136,9 +147,9 @@ def page_with_note(field_name: str, note_title: str) -> RESPONSE_TYPE:
hashed_title = compress(note_title)
contents_in_html = convert_note_from_markdown_to_html(field_name, hashed_title)
if contents_in_html is None:
return render_template('404.html'), 404
return render_localized_template('404.html'), 404
page_title = note_title
contents_with_css = render_template('regular_page.html', **locals())
contents_with_css = render_localized_template('regular_page.html', **locals())
contents_with_css = contents_with_css.replace('</p>\n\n<ul>', '</p>\n<ul>')
return contents_with_css

Expand All @@ -158,7 +169,7 @@ def page_with_list_of_notes(
for note_content in notes_contents:
table_of_contents += note_content.split('\n')[0].replace('h2', 'p') + '\n'

contents_with_css = render_template('regular_page.html', **locals())
contents_with_css = render_localized_template('regular_page.html', **locals())
contents_with_css = contents_with_css.replace('</p>\n\n<ul>', '</p>\n<ul>')
return contents_with_css

Expand All @@ -167,7 +178,7 @@ def page_with_list_of_notes(
def page_for_tag(field_name: str, tag: str) -> RESPONSE_TYPE:
"""Render in HTML a page with all notes that have the specified tag."""
path_to_db = os.path.join(
app.config.get("RESOURCES_DIR"), field_name, app.config.get("TAG_TO_NOTES_DB_FILE_NAME")
app.config["RESOURCES_DIR"], field_name, app.config["TAG_TO_NOTES_DB_FILE_NAME"]
)
try:
with contextlib.closing(sqlite3.connect(path_to_db)) as connection:
Expand All @@ -176,7 +187,7 @@ def page_for_tag(field_name: str, tag: str) -> RESPONSE_TYPE:
query_result = cursor.fetchall()
hashed_titles = [x[0] for x in query_result]
except sqlite3.OperationalError:
return render_template('404.html'), 404
return render_localized_template('404.html'), 404
page_title = (tag[0].upper() + tag[1:]).replace('_', ' ')
contents_with_css = page_with_list_of_notes(field_name, page_title, hashed_titles)
return contents_with_css
Expand All @@ -196,27 +207,27 @@ def page_for_query(field_name: str) -> RESPONSE_TYPE:
query = query or default_query

path_to_tag_to_notes_db = os.path.join(
app.config.get("RESOURCES_DIR"), field_name, app.config.get("TAG_TO_NOTES_DB_FILE_NAME")
app.config["RESOURCES_DIR"], field_name, app.config["TAG_TO_NOTES_DB_FILE_NAME"]
)
path_to_tf_idf_db = os.path.join(
app.config.get("RESOURCES_DIR"), field_name, app.config.get("TF_IDF_DB_FILE_NAME")
app.config["RESOURCES_DIR"], field_name, app.config["TF_IDF_DB_FILE_NAME"]
)
queries_processor = QueriesProcessor(
path_to_tag_to_notes_db, path_to_tf_idf_db, app.config.get("LANGUAGE")
path_to_tag_to_notes_db, path_to_tf_idf_db, app.config["LANGUAGE"]
)
try:
hashed_titles = queries_processor.find_notes(query)
except sqlite3.OperationalError:
return render_template('invalid_query.html', **locals())
return render_localized_template('invalid_query.html', **locals())

if len(hashed_titles) > 0:
contents_with_css = page_with_list_of_notes(field_name, query, hashed_titles)
else:
contents_with_css = render_template('empty_result.html', **locals())
contents_with_css = render_localized_template('empty_result.html', **locals())
return contents_with_css


@app.errorhandler(404)
def page_not_found(_) -> RESPONSE_TYPE:
"""Render template for unknown page."""
return render_template('404.html'), 404
return render_localized_template('404.html'), 404
4 changes: 2 additions & 2 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_root_page(test_client: flask.testing.FlaskClient) -> None:
def test_info_page(test_client: flask.testing.FlaskClient) -> None:
"""Test page with info about the project."""
result = test_client.get('/about').data.decode('utf-8')
assert "ReadingBricks - О проекте" in result
assert "О проекте | ReadingBricks" in result
assert "/" in result


Expand Down Expand Up @@ -61,7 +61,7 @@ def test_page_not_found(test_client: flask.testing.FlaskClient, url: str) -> Non
response = test_client.get(url)
result = response.data.decode('utf-8')
status_code = response.status_code
assert '<title>Страница не найдена</title>' in result
assert '<title>Страница не найдена | ReadingBricks</title>' in result
assert status_code == 404


Expand Down

0 comments on commit e42bf31

Please sign in to comment.