Skip to content
This repository has been archived by the owner on Jun 4, 2022. It is now read-only.

Commit

Permalink
Use {% macro %} to remove buggy duplicate queries.
Browse files Browse the repository at this point in the history
This necessitated some minor improvements to the {% macro %} tag.
Now it has better error reporting and can utilize other macros on the page,
  • Loading branch information
Adrian Cochrane committed Mar 18, 2018
1 parent dab7b04 commit 3a1d6c4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
27 changes: 13 additions & 14 deletions data/pages/history
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ and to visually communicate the paths surfers took through this history. #}
{% macro hl-change fmt %}
{% ifchanged visited_at|date:fmt %}<strong>{{visited_at|date:fmt}}</strong>{% else %}{{visited_at|date:fmt}}{% endif %}
{% endmacro %}
{% macro q-history %}
FROM page_visit WHERE {{q}} = "" OR rowid IN (SELECT rowid FROM history_fts({{q}}))
{% endmacro %}
{% macro q-paged page %}
{% q-history %} ORDER BY visited_at DESC LIMIT {{pagesize}} OFFSET {{page}}*{{pagesize}}
{% endmacro %}
<html>
<head>
<title>[document-open-recent] {% trans %}History{% endtrans %}</title>
Expand All @@ -19,13 +25,11 @@ and to visually communicate the paths surfers took through this history. #}
{% query %}INSERT OR IGNORE INTO history_fts(rowid, uri, title)
SELECT rowid, uri, title FROM page_visit;{% endquery %}
{% endif %}
<body>{% with pagesize=400 q=url.query.q %}
<body>{% with pagesize=400 q=url.query.q page=url.query.page|default:0 %}
<nav>
<aside style="float: right;">
{% query %}SELECT min(visited_at) AS earliest, max(visited_at) AS latest
FROM (SELECT visited_at FROM page_visit
ORDER BY visited_at DESC LIMIT {{pagesize}}
OFFSET {{url.query.page}}*{{pagesize}});
FROM (SELECT visited_at {% q-paged %});
{% each-row %}
{{latest|date}}–{{earliest|date}}
{% endquery %}
Expand All @@ -34,31 +38,26 @@ and to visually communicate the paths surfers took through this history. #}
<input type="search" name="q" value="{{q}}" placeholder="{% trans %}History{% endtrans %}" />
</form>
</nav>
<dl>{% query %}SELECT rowid, tab, uri, title, favicon, visited_at, referrer
FROM page_visit
WHERE {{q}} = "" OR rowid IN (SELECT rowid FROM history_fts({{q}}))
ORDER BY visited_at DESC
LIMIT {{ pagesize }} OFFSET {{ url.query.page|default:0 }}*{{ pagesize }};
<dl>{% query %}SELECT rowid, tab, uri, title, favicon, visited_at, referrer {% q-paged %};
{% each-row %}
{% ifchanged visited_at|date:"%Y%B%e" %}<dt>
{% hl-change fmt="%e" %} {% hl-change fmt="%B" %} {% hl-change fmt="%Y" %}
</dt>{% endif %}
<dd data-tab="{{tab}}" data-date="{{visited_at}}"
<dd data-tab="{{tab}}"
id="visit-{{rowid}}" {% if referrer %}aria-flowto="visit-{{referrer}}"{% endif %}>
<em>{% hl-change fmt="%k" %}:{{visited_at|date:"%M"}}</em>
<a href="{{ uri }}">{{title}}</a>
</dd>
{% empty %}
<dt>{% trans %}Invalid page number!{% endtrans %}</dt>
<dt>{% trans %}No results for this search or on this page!{% endtrans %}</dt>
{% endquery %}</dl>
<footer>
{% query %}SELECT count(*)/{{pagesize}} + 1 AS num_pages FROM page_visit;
{% query %}SELECT count(*)/{{pagesize}} + 1 AS num_pages {% q-history %};
{% each-row %}
{% for i in num_pages %}
{% if i != url.query.page %}<a href="odysseus:history?page={{i}}&q={{q}}"{% else %}<strong {% endif %}
{% query %}SELECT min(visited_at) AS earliest, max(visited_at) AS latest
FROM (SELECT visited_at FROM page_visit
ORDER BY visited_at DESC LIMIT {{pagesize}} OFFSET {{i}}*{{pagesize}});
FROM (SELECT visited_at {% q-paged page=i %});
{% each-row %}
title="{{latest|date}}–{{earliest|date}}"
{% endquery %}
Expand Down
13 changes: 10 additions & 3 deletions src/Services/database/prosody.vala
Original file line number Diff line number Diff line change
Expand Up @@ -163,22 +163,29 @@ namespace Odysseus.Database.Prosody {
// Though it *does* require it to handle {% with %}.
public Template? build(Parser parser, WordIter args) throws SyntaxError {
var name = args.next();
if (tag_lib.has_key(name) || parser.local_tag_lib.has_key(name))
throw new SyntaxError.INVALID_ARGS("{%% %s %%} already exists!", ByteUtils.to_string(name));
// Don't assert end, so templates can indicate which args they expect.

WordIter endtoken;
var body = parser.scan_until("endmacro", out endtoken);
if (endtoken == null) throw new SyntaxError.UNBALANCED_TAGS("Missing {%% endmacro %%}!");

parser.local_tag_lib[name] = new MacroBuilder(ByteUtils.strip(body));
parser.local_tag_lib[name] = new MacroBuilder(ByteUtils.strip(body), parser.local_tag_lib);
return null;
}
}
private class MacroBuilder : TagBuilder, Object {
private Bytes source;
public MacroBuilder(Bytes source) {this.source = source;}
private Gee.Map<Bytes, TagBuilder> lib;
public MacroBuilder(Bytes source, Gee.Map<Bytes, TagBuilder> lib) {
this.source = source; this.lib = lib;
}

public Template? build(Parser parser, WordIter args) throws SyntaxError {
return new Std.WithTag(Std.parse_params(args), new Parser(source).parse());
var innerParser = new Parser(source);
innerParser.local_tag_lib = lib;
return new Std.WithTag(Std.parse_params(args), innerParser.parse());
}
}

Expand Down

0 comments on commit 3a1d6c4

Please sign in to comment.