Update nikiroo-utils, use it, fix 2 bugs:
[gofetch.git] / src / be / nikiroo / gofetch / Fetcher.java
index 7490037c201929aba38297d809e7f9c917af1e95..5f81edabcb887cdb9e211c558331bab8a78339ec 100644 (file)
@@ -93,14 +93,14 @@ public class Fetcher {
                        }
                        ref = "../" + ref + "/index.html";
 
-                       htmlBuilder.append(getLink(support.getDescription(), ref, false,
+                       htmlBuilder.append(getLink(support.getDescription(), ref, true,
                                        true));
                }
 
                File gopherCache = new File(dir, preselector);
                gopherCache.mkdirs();
                File htmlIndex = new File(gopherCache, "index.html");
-               gopherCache = new File(gopherCache, ".cache");
+               gopherCache = new File(gopherCache, "gophermap");
 
                Output gopher = new Gopher(null, hostname, preselector, port);
                Output html = new Html(null, hostname, preselector, port);
@@ -188,22 +188,26 @@ public class Fetcher {
                List<String> gopherLines = new ArrayList<String>();
                List<String> htmlLines = new ArrayList<String>();
                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")));
+                       File gopherFile = new File(varDir, headers[i]);
+                       File htmlFile = new File(varDir, headers[i] + ".html");
+
+                       if (gopherFile.exists())
+                               gopherLines.add(IOUtils.readSmallFile(gopherFile));
+                       if (htmlFile.exists())
+                               htmlLines.add(IOUtils.readSmallFile(htmlFile));
 
                        boolean enoughStories = (i > 0 && i % maxStories == 0);
                        boolean last = i == headers.length - 1;
                        if (enoughStories || last) {
                                if (!last) {
                                        gopherLines.add(getLink("More", support.getSelector()
-                                                       + ".cache_" + (page + 1), false, false));
+                                                       + "gophermap_" + (page + 1), true, false));
+
                                        htmlLines.add(getLink("More", "index_" + (page + 1)
-                                                       + ".html", false, true));
+                                                       + ".html", true, true));
                                }
 
-                               write(gopherLines, varDir, ".cache", "", page);
+                               write(gopherLines, varDir, "gophermap", "", page);
                                write(htmlLines, varDir, "index", ".html", page);
                                gopherLines = new ArrayList<String>();
                                htmlLines = new ArrayList<String>();
@@ -227,10 +231,24 @@ public class Fetcher {
                }
        }
 
-       private String getLink(String name, String ref, boolean index, boolean html) {
+       /**
+        * Create a link.
+        * 
+        * @param name
+        *            the link name (what the user will see)
+        * @param ref
+        *            the actual link reference (the target)
+        * @param menu
+        *            menu (gophermap, i) mode -- not used in html mode
+        * @param html
+        *            TRUE for html mode, FALSE for gopher mode
+        * 
+        * @return the ready-to-use link in a {@link String}
+        */
+       private String getLink(String name, String ref, boolean menu, boolean html) {
                if (!html) {
-                       return new StringBuilder().append("1" + name).append("\t")
-                                       .append((index ? "1" : "0") + ref) //
+                       return new StringBuilder().append((menu ? "1" : "0") + name)
+                                       .append("\t").append(ref) //
                                        .append("\t").append(hostname) //
                                        .append("\t").append(Integer.toString(port)) //
                                        .append("\r\n").toString();