• R/O
  • SSH
  • HTTPS

catalpa: Commit


Commit MetaInfo

Revisão105 (tree)
Hora2022-09-06 15:02:30
Autorhirukawa_ryo

Mensagem de Log

* catalpa 0.9
blogのページャー(前の記事・次の記事)でサムネイル画像のURL(thumbnail)を参照できるようにしました。
記事から関連記事(related)を参照できるようにしました。関連記事はカテゴリーの一致する数 > 日付の近い順で関連が強いと判定します。

Mudança Sumário

Diff

--- catalpa/trunk/src/main/java/net/osdn/catalpa/addon/blog/BlogAddOn.java (revision 104)
+++ catalpa/trunk/src/main/java/net/osdn/catalpa/addon/blog/BlogAddOn.java (revision 105)
@@ -16,6 +16,8 @@
1616 import java.time.ZoneOffset;
1717 import java.time.ZonedDateTime;
1818 import java.time.format.DateTimeParseException;
19+import java.time.temporal.ChronoUnit;
20+import java.util.AbstractMap;
1921 import java.util.ArrayList;
2022 import java.util.Base64;
2123 import java.util.Comparator;
@@ -23,6 +25,7 @@
2325 import java.util.Iterator;
2426 import java.util.LinkedHashMap;
2527 import java.util.LinkedHashSet;
28+import java.util.LinkedList;
2629 import java.util.List;
2730 import java.util.Map;
2831 import java.util.Set;
@@ -98,7 +101,7 @@
98101 @Override
99102 public void prepare(Path inputPath, Path outputPath, Map<String, Object> config, Map<String, Object> options, Context context) throws IOException {
100103 if(options == null) {
101- options = new HashMap<String, Object>();
104+ options = new HashMap<>();
102105 }
103106 Object obj;
104107
@@ -155,7 +158,43 @@
155158 options.put("_DEFAULT_URL", post.getUrl());
156159 }
157160 }
158-
161+
162+ { // create related posts
163+ for(Post post : posts) {
164+ List<Map.Entry<Long, Post>> list = new LinkedList<>();
165+ for(Post other : posts) {
166+ long score = 0;
167+
168+ if(other == post) {
169+ continue;
170+ }
171+ for(Category category : post.getCategories()) {
172+ for(Category otherCategory : other.getCategories()) {
173+ if(category.getId().equals(otherCategory.getId())) {
174+ score += 100000;
175+ }
176+ }
177+ }
178+ if(post.getDate() != null && other.getDate() != null) {
179+ long days = ChronoUnit.DAYS.between(post.getDate(), other.getDate());
180+ if(days > 0) {
181+ score += days * (-2) + 1;
182+ }
183+ if(days < 0) {
184+ score += days * 2;
185+ }
186+ }
187+ list.add(new AbstractMap.SimpleEntry<>(score, other));
188+ }
189+ list.sort((o1, o2) -> { return (int)(o2.getKey().longValue() - o1.getKey().longValue()); });
190+ List<Post> related = new ArrayList<>();
191+ for(Entry<Long, Post> entry : list) {
192+ related.add(entry.getValue());
193+ }
194+ post.setRelated(related);
195+ }
196+ }
197+
159198 obj = context.getDataModel().get("title");
160199 if(obj instanceof String) {
161200 blogDataModel.put("title", obj);
--- catalpa/trunk/src/main/java/net/osdn/catalpa/addon/blog/Link.java (revision 104)
+++ catalpa/trunk/src/main/java/net/osdn/catalpa/addon/blog/Link.java (revision 105)
@@ -7,6 +7,8 @@
77 private LocalDate date;
88 private String title;
99 private String url;
10+
11+ private String thumbnail;
1012
1113 public Link(String title, String url) {
1214 this.title = title;
@@ -18,6 +20,14 @@
1820 this.title = title;
1921 this.url = url;
2022 }
23+
24+ public Link(LocalDate date, String title, String url, String thumbnail) {
25+ this.date = date;
26+ this.title = title;
27+ this.url = url;
28+ this.thumbnail = thumbnail;
29+ }
30+
2131
2232 public LocalDate getDate() {
2333 return date;
@@ -30,4 +40,8 @@
3040 public String getUrl() {
3141 return url;
3242 }
43+
44+ public String getThumbnail() {
45+ return thumbnail;
46+ }
3347 }
--- catalpa/trunk/src/main/java/net/osdn/catalpa/addon/blog/Pager.java (revision 104)
+++ catalpa/trunk/src/main/java/net/osdn/catalpa/addon/blog/Pager.java (revision 105)
@@ -87,7 +87,8 @@
8787 Path fromPath = rootOutputPath.relativize(outputPath);
8888 Path toPath = rootInputPath.relativize(Util.replaceFileExtension(previous.getPath(), TemplateHandler.APPLICABLE_EXTENSIONS, ".html"));
8989 String url = fromPath.getParent().relativize(toPath.getParent()).resolve(toPath.getFileName()).toString().replace('\\', '/');
90- Link link = new Link(previous.getDate(), previous.getTitle(), url);
90+ String thumbnail = previous.getThumbnail();
91+ Link link = new Link(previous.getDate(), previous.getTitle(), url, thumbnail);
9192 return link;
9293 }
9394 }
@@ -127,7 +128,8 @@
127128 Path fromPath = rootOutputPath.relativize(outputPath);
128129 Path toPath = rootInputPath.relativize(Util.replaceFileExtension(next.getPath(), TemplateHandler.APPLICABLE_EXTENSIONS, ".html"));
129130 String url = fromPath.getParent().relativize(toPath.getParent()).resolve(toPath.getFileName()).toString().replace('\\', '/');
130- Link link = new Link(next.getDate(), next.getTitle(), url);
131+ String thumbnail = next.getThumbnail();
132+ Link link = new Link(next.getDate(), next.getTitle(), url, thumbnail);
131133 return link;
132134 }
133135 }
--- catalpa/trunk/src/main/java/net/osdn/catalpa/addon/blog/Post.java (revision 104)
+++ catalpa/trunk/src/main/java/net/osdn/catalpa/addon/blog/Post.java (revision 105)
@@ -3,6 +3,8 @@
33 import java.nio.file.Files;
44 import java.nio.file.Path;
55 import java.time.LocalDate;
6+import java.util.Collections;
7+import java.util.List;
68 import java.util.Set;
79
810 public class Post {
@@ -18,6 +20,8 @@
1820 private String image;
1921 private boolean isMore;
2022 private boolean isDraft;
23+
24+ private List<Post> related;
2125
2226 protected Post(Path path, String url, LocalDate date, String title, Set<Category> categories, String leading, boolean isMore) {
2327 this.path = path;
@@ -103,8 +107,14 @@
103107 return isDraft;
104108 }
105109
110+ public List<Post> getRelated() {
111+ return related != null ? related : Collections.emptyList();
112+ }
106113
107-
114+ /* package private */ void setRelated(List<Post> related) {
115+ this.related = related;
116+ }
117+
108118 protected static final String[] APPLICABLE_EXTENSIONS = new String[] {
109119 ".markdown",
110120 ".markdown.txt",
Show on old repository browser