Skip to content

Commit

Permalink
mod_export: add export in application/atom+xml format. Fixes #1546 Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mworrell committed Jan 11, 2017
1 parent 95128ad commit ac764d5
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 1 deletion.
3 changes: 2 additions & 1 deletion modules/mod_export/support/export_encoder.erl
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ encoders() ->
[
export_encoder_csv,
export_encoder_xlsx,
export_encoder_ics
export_encoder_ics,
export_encoder_atom
].

do_header(StreamState, Context) ->
Expand Down
71 changes: 71 additions & 0 deletions modules/mod_export/support/export_encoder_atom.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
%% @author Marc Worrell <marc@worrell.nl>
%% @copyright 2017 Marc Worrell <marc@worrell.nl>
%% @doc Format exports for atom format

%% Copyright 2017 Marc Worrell
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.

-module(export_encoder_atom).
-author("Marc Worrell <marc@worrell.nl>").

-record(state, {
id
}).

-export([
extension/0,
mime/0,
init/2,
header/3,
row/3,
footer/3
]).

-include_lib("zotonic.hrl").

extension() ->
[ "atom" ].

mime() ->
[ "application/atom+xml" ].

init(Options, _Context) ->
{ok, #state{
id = proplists:get_value(id, Options)
}}.

header(Header, #state{id=Id} = State, Context) ->
Vars = [
{id, Id},
{title, Header}
],
{Bin, _Context} = z_template:render_to_iolist("atom/feed_start.tpl", Vars, Context),
{ok, iolist_to_binary(Bin), State}.

row(Row, #state{} = State, Context) when is_integer(Row) ->
Vars = [
{id, Row}
],
{Bin, _Context} = z_template:render_to_iolist({cat, "atom/entry.tpl"}, Vars, Context),
{ok, iolist_to_binary(Bin), State};
row(Row, #state{} = State, Context) ->
Vars = [
{id, Row}
],
{Bin, _Context} = z_template:render_to_iolist("atom/entry.tpl", Vars, Context),
{ok, iolist_to_binary(Bin), State}.

footer(_Data, #state{}, Context) ->
{Bin, _Context} = z_template:render_to_iolist("atom/feed_end.tpl", [], Context),
{ok, iolist_to_binary(Bin)}.
41 changes: 41 additions & 0 deletions modules/mod_export/templates/atom/entry.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{% if id and id.is_visible %}
{% with id.medium as medium %}
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gd="http://schemas.google.com/g/2005" xml:lang="{{ z_language }}">
<id>{{ id.uri|escapexml }}</id>
<updated>{{ id.modified|date:"c" }}</updated>
<published>{{ id.publication_start|date:"c" }}</published>
<author>{% with id.o.author[1]|default:id.creator_id as author_id %}
<name>{% filter escapexml %}{% include "_name.tpl" id=author_id %}{% endfilter %}</name>
<uri>{{ author_id.uri|escapexml }}</uri>
{% endwith %}</author>
<link rel="alternate" type="text/html" href="{{ id.page_url_abs }}"/>
{% if medium.filename %}
<link rel="enclosure" type="{{ medium.mime }}" href="{% url media_attachment star=medium.filename use_abolute_url %}" />
{% elseif medium %}
<link rel="enclosure" type="image/jpeg" href="{% image_url id width=400 height=400 use_absolute_url %}" />
{% endif %}
{% for media_id in id.media %}
<link rel="enclosure" type="image/jpeg" href="{% image_url media_id width=400 height=400 use_absolute_url %}" />
{% endfor %}
<title>{{ id.title|escapexml }}</title>
{% if id.body %}
<summary>{{ id.summary|escapexml }}</summary>
<content type="html">{{ id.body|escapexml }}</content>
{% else %}
<content type="text">{{ id.summary|escapexml }}</content>
{% endif %}
{% if medium.filename %}
<content type="{{ medium.mime }}" src="{% url media_attachment star=medium.filename use_absolute_url %}" />
{% endif %}
{% if id.is_a.event and id.date_start %}
{# http://code.google.com/apis/gdata/docs/2.0/elements.html#gdEventKind #}
<category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/g/2005#event" />
<gd:eventStatus value="http://schemas.google.com/g/2005#event.confirmed" />
<gd:when startTime="{{ id.date_start|date:"c" }}" endTime="{{ id.date_end|date:"c" }}" />
{# <gd:where>My Living Room</gd:where> #}
{% endif %}

<category term="{{ id.category.name }}" scheme="http://zotonic.com/id/category" />
</entry>
{% endwith %}
{% endif %}
1 change: 1 addition & 0 deletions modules/mod_export/templates/atom/feed_end.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
</feed>
7 changes: 7 additions & 0 deletions modules/mod_export/templates/atom/feed_start.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<feed xmlns="http://www.w3.org/2005/Atom" xml:base="{% url home use_absolute_url %}" xml:lang="{{ z_language }}">
<generator uri="http://zotonic.com/">Zotonic - Atom Feed Module</generator>
<updated>{{ now|date:"c" }}</updated>
<logo />
{# <link rel="self" type="application/atom+xml" href="{% url atom_feed cat=cat %}" /> #}
<id>{% url home use_absolute_url %}</id>
<title>{{ m.config.site.title.value|escapexml }}{% if title %} - {{ title|escapexml }}{% endif %}</title>

0 comments on commit ac764d5

Please sign in to comment.