Skip to content

Commit

Permalink
#5 Publish anki cards from search results
Browse files Browse the repository at this point in the history
  • Loading branch information
zonArt committed Jan 1, 2017
1 parent a66a7fd commit a6c45fd
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@
<artifactId>rome</artifactId>
<version>1.5.1</version>
</dependency>
<dependency>
<groupId>ch.ledcom.javalib</groupId>
<artifactId>ankilib</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.rometools</groupId>
<artifactId>rome-modules</artifactId>
Expand Down
60 changes: 60 additions & 0 deletions src/main/java/ch/ledcom/signs/AnkiView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Copyright © 2016 Guillaume Lederrey ([email protected])
*
* 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.
*/
package ch.ledcom.signs;

import lombok.NonNull;
import ch.ledcom.javalib.AnkiWriter;
import ch.ledcom.javalib.Deck;
import ch.ledcom.javalib.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.View;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Component("search.anki")
public class AnkiView implements View {

@NonNull
private final AnkiWriter ankiWriter = new AnkiWriter();

@Override
public String getContentType() {
return "anki+zip";
}

@Override
public void render(
Map<String, ?> model,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=signs.apkg");

Deck deck = new Deck();

for (Sign sign : (Iterable<Sign>) model.get("signs")) {

String signName = sign.getName();
Svg signImage = sign.getImage();

deck.addCard(signName, new Resource(signImage));
}

ankiWriter.write(deck, response.getOutputStream());
}
}
1 change: 1 addition & 0 deletions src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ admin.metrics=metrics
admin.swagger=swagger
admin.thread.dump=thread dump
admin.trace=trace
anki=Anki
app.title=Signs
atom=Atom (XML)
configuration=Configuration
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/messages_fr.properties
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ admin.metrics=metrics
admin.swagger=swagger
admin.thread.dump=thread dump
admin.trace=trace
anki=Anki
app.title=Signes
atom=Atom (XML)
configuration=Configuration
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/templates/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
</li>
<li><a th:text="#{atom}" th:href="@{${currentUrlNoFormat}(format=atom)}">Atom (XML)</a></li>
<li><a th:text="#{json}" th:href="@{${currentUrlNoFormat}(format=json)}">JSON</a></li>
<li><a th:text="#{anki}" th:href="@{${currentUrlNoFormat}(format=anki)}">Anki</a></li>
</ul>
</div>
</div>
Expand Down

0 comments on commit a6c45fd

Please sign in to comment.