Add history page.

This commit is contained in:
Imran Sobir 2017-02-07 00:04:32 +05:00
parent 5e58764326
commit 748ec7e7a1
2 changed files with 153 additions and 0 deletions

View File

@ -24,6 +24,7 @@ Module attributes:
_HANDLERS: The handlers registered via decorators. _HANDLERS: The handlers registered via decorators.
""" """
import datetime
import urllib.parse import urllib.parse
import qutebrowser import qutebrowser
@ -158,6 +159,69 @@ def qute_bookmarks(_url):
return 'text/html', html return 'text/html', html
@add_handler('history')
def qute_history(url):
"""Handler for qute:history. Display history."""
# Get current date from query parameter, if not given choose today.
curr_date = datetime.date.today()
try:
for query in url.query().split("&"):
kv = query.split("=", 2)
if kv[0] == "date":
curr_date = datetime.datetime.strptime(kv[1], "%Y-%m-%d")
break
except Exception:
log.misc.debug("Invalid query string passed to qute://history")
ONE_DAY = datetime.timedelta(days=1)
next_date = curr_date + ONE_DAY
prev_date = curr_date - ONE_DAY
def history_iter():
for item in objreg.get('web-history').history_dict.values():
# Convert timestamp
item_date = None
try:
item_date = datetime.datetime.fromtimestamp(item.atime)
except Exception:
log.misc.error("Invalid timestamp {}.".format(item.atime))
continue
display_url = item.url.toDisplayString()
# Skip items not on curr_date
# Skip redirects and items without title
# Skip qute:// links
has_no_title = len(item.title) <= 0
is_internal = display_url.startswith("qute://")
is_redirect = item.redirect
is_not_today = (
item_date.year != curr_date.year or
item_date.month != curr_date.month or
item_date.day != curr_date.day
)
if (is_redirect or is_internal or has_no_title or is_not_today):
continue
display_atime = item_date.strftime("%X")
yield (display_url, item.title, display_atime)
history = [item for item in history_iter()]
try:
history = reversed(history)
except TypeError: # Python < 3.5
history = reversed(list(history))
html = jinja.render('history.html',
title='History',
history=history,
curr_date=curr_date.strftime("%a, %d %B %Y"),
next_date=next_date.strftime("%Y-%m-%d"),
prev_date=prev_date.strftime("%Y-%m-%d"))
return 'text/html', html
@add_handler('pyeval') @add_handler('pyeval')
def qute_pyeval(_url): def qute_pyeval(_url):
"""Handler for qute:pyeval.""" """Handler for qute:pyeval."""

View File

@ -0,0 +1,89 @@
{% extends "base.html" %}
{% block style %}
body {
background: #fefefe;
font-family: sans-serif;
margin: 0 auto;
max-width: 1440px;
padding-left: 20px;
padding-right: 20px;
}
h1 {
color: #444;
font-weight: normal;
}
table {
border-collapse: collapse;
width: 100%;
}
td {
max-width: 50%;
padding: 2px 5px;
text-align: left;
}
td.title {
word-break: break-all;
}
td.time {
color: #555;
white-space: nowrap;
}
a {
text-decoration: none;
color: #2562dc
}
a:hover {
text-decoration: underline;
}
tr:nth-child(odd) {
background-color: #f8f8f8;
}
.date {
color: #888;
font-size: 14pt;
padding-left: 25px;
}
.pagination-link {
display: inline-block;
margin-bottom: 10px;
margin-top: 10px;
padding-right: 10px;
}
.pagination-link > a {
color: #333;
font-weight: bold;
}
{% endblock %}
{% block content %}
<h1>Browsing history <span class="date">{{curr_date}}</span></h1>
<table>
<tbody>
{% for url, title, time in history %}
<tr>
<td class="title"><a href="{{url}}">{{title}}</a></td>
<td class="time">{{time}}</td>
</tr>
{% endfor %}
</tbody>
</table>
<span class="pagination-link"><a href="qute://history/?date={{prev_date}}">Previous</a></span>
<span class="pagination-link"><a href="qute://history/?date={{next_date}}">Next</a></span>
{% endblock %}