From e402343ec68a759ccfcb5889ceecf974ddbe3612 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Tue, 8 Aug 2017 19:20:27 +0200 Subject: [PATCH] Show [more] links when more stories are present --- src/be/nikiroo/gofetch/Fetcher.java | 91 +++++++++++++++++++---------- 1 file changed, 60 insertions(+), 31 deletions(-) diff --git a/src/be/nikiroo/gofetch/Fetcher.java b/src/be/nikiroo/gofetch/Fetcher.java index 725355c..496b2f7 100644 --- a/src/be/nikiroo/gofetch/Fetcher.java +++ b/src/be/nikiroo/gofetch/Fetcher.java @@ -4,7 +4,9 @@ import java.io.File; import java.io.FileWriter; import java.io.FilenameFilter; import java.io.IOException; +import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.List; import be.nikiroo.gofetch.data.Story; @@ -82,18 +84,16 @@ public class Fetcher { list(support); } - gopherBuilder.append("1" + support.getDescription()).append("\t") - .append("1" + support.getSelector()) // - .append("\t").append(hostname) // - .append("\t").append(Integer.toString(port)) // - .append("\r\n"); + gopherBuilder.append(getLink(support.getDescription(), + support.getSelector(), false)); String ref = support.getSelector(); while (ref.startsWith("/")) { ref = ref.substring(1); } - htmlBuilder.append("
" - + support.getDescription() + "
\n"); + ref = "../" + ref + "/index.html"; + + htmlBuilder.append(getLink(support.getDescription(), ref, true)); } File gopherCache = new File(dir, preselector); @@ -175,39 +175,68 @@ public class Fetcher { } }); - // Finding which ones to show: - int from = 0; - int to = 0; - if (headers.length > 0) { - Arrays.sort(headers); - from = headers.length - 1; - to = headers.length - maxStories; - if (to < 0) { - to = 0; + // Reverse sort: + Arrays.sort(headers); + List tmp = Arrays.asList(headers); + Collections.reverse(tmp); + headers = tmp.toArray(new String[] {}); + // + + // Write the index (with "MORE" links if needed) + int page = 0; + List gopherLines = new ArrayList(); + List htmlLines = new ArrayList(); + for (i = 0; i < headers.length; i++) { + gopherLines + .add(IOUtils.readSmallFile(new File(varDir, headers[i]))); + htmlLines.add(IOUtils.readSmallFile(new File(varDir, headers[i] + + ".html"))); + + boolean enoughStories = (i > 0 && i % maxStories == 0); + boolean last = i == headers.length - 1; + if (enoughStories || last) { + if (!last) { + gopherLines.add(getLink("More", "/.cache_" + (page + 1), + false)); + htmlLines.add(getLink("More", "index_" + (page + 1) + + ".html", true)); + } + + write(gopherLines, varDir, ".cache", "", page); + write(htmlLines, varDir, "index", ".html", page); + gopherLines = new ArrayList(); + htmlLines = new ArrayList(); + page++; } } + } - // Writing the cache/index files with the stories: - File gopherCache = new File(varDir, ".cache"); - FileWriter writer = new FileWriter(gopherCache); + private void write(List lines, File varDir, String basename, + String ext, int page) throws IOException { + File file = new File(varDir, basename + (page > 0 ? "_" + page : "") + + ext); + + FileWriter writer = new FileWriter(file); try { - for (i = from; i >= to; i--) { - writer.append(IOUtils - .readSmallFile(new File(varDir, headers[i]))); + for (String line : lines) { + writer.append(line).append("\r\n"); } } finally { writer.close(); } + } - File htmlIndex = new File(varDir, "index.html"); - writer = new FileWriter(htmlIndex); - try { - for (i = from; i >= to; i--) { - writer.append(IOUtils.readSmallFile(new File(varDir, headers[i] - + ".html"))); - } - } finally { - writer.close(); + private String getLink(String name, String ref, boolean html) { + if (!html) { + return new StringBuilder().append("1" + name).append("\t") + .append("1" + ref) // + .append("\t").append(hostname) // + .append("\t").append(Integer.toString(port)) // + .append("\r\n").toString(); } + + return new StringBuilder().append( + "\n").toString(); } } -- 2.27.0