Fix some bugs, fix some TODO text
authorNiki Roo <niki@nikiroo.be>
Sun, 6 Aug 2017 11:17:57 +0000 (13:17 +0200)
committerNiki Roo <niki@nikiroo.be>
Sun, 6 Aug 2017 11:17:57 +0000 (13:17 +0200)
src/be/nikiroo/gofetch/Fetcher.java
src/be/nikiroo/gofetch/Main.java
src/be/nikiroo/gofetch/output/Gopher.java
src/be/nikiroo/gofetch/output/Html.java
src/be/nikiroo/gofetch/output/Output.java
src/be/nikiroo/gofetch/support/Slashdot.java

index 5a9c774aa05fb109ed4a5a2d61017ee2a45c78e3..ce68558d9fd6aa0c23bcf259f73a674caccc3e87 100644 (file)
@@ -77,8 +77,8 @@ public class Fetcher {
                File cacheHtml = new File(cache, "index.html");
                cache = new File(cache, ".cache");
 
-               Output gopher = new Gopher(null, hostname, port);
-               Output html = new Html(null);
+               Output gopher = new Gopher(null, hostname, preselector, port);
+               Output html = new Html(null, hostname, preselector, port);
 
                FileWriter writer = new FileWriter(cache);
                try {
@@ -87,17 +87,13 @@ public class Fetcher {
                                writer.append(gopher.getIndexHeader());
                                writerHtml.append(html.getIndexHeader());
 
-                               Type types[];
-                               if (type == null) {
-                                       types = Type.values();
-                               } else {
-                                       types = new Type[] { type };
-                               }
-
                                BasicSupport.setPreselector(preselector);
-                               for (Type type : types) {
+                               for (Type type : Type.values()) {
                                        BasicSupport support = BasicSupport.getSupport(type);
-                                       list(support);
+
+                                       if (type == this.type || this.type == null) {
+                                               list(support);
+                                       }
 
                                        writer.append("1" + support.getDescription()).append("\t")
                                                        .append("1" + support.getSelector()) //
@@ -132,8 +128,9 @@ public class Fetcher {
         *             in case of I/O error
         **/
        private void list(BasicSupport support) throws IOException {
-               Output gopher = new Gopher(support.getType(), hostname, port);
-               Output html = new Html(support.getType());
+               Output gopher = new Gopher(support.getType(), hostname, preselector,
+                               port);
+               Output html = new Html(support.getType(), hostname, preselector, port);
 
                new File(dir, support.getSelector()).mkdirs();
 
index 590d4a0cd1046ffedf816052921d34cfb1784465..7aa94053d10236932897c872e34c236e93560b53 100644 (file)
@@ -52,7 +52,10 @@ public class Main {
                        System.exit(1);
                }
 
-               // Selector base (empty is ok, DO NOT end with /)
+               // Selector base :
+               // - empty is ok
+               // - DO NOT end with /
+               // - always starts with / if not empty
                String preselector = "";
                if (preselectorStr != null && !preselectorStr.startsWith("/")) {
                        preselector = "/" + preselectorStr;
@@ -65,7 +68,7 @@ public class Main {
                Type type = null;
                if (!"ALL".equals(typeStr)) {
                        try {
-                               Type.valueOf(typeStr.toUpperCase());
+                               type = Type.valueOf(typeStr.toUpperCase());
                        } catch (IllegalArgumentException e) {
                                System.err.println("Invalid type: " + typeStr);
                                System.exit(1);
index 8cce4a571d1c5c11f51b0e52bd75eac18588f770..10714b171235332ac30f88a8ffbd14932e176504 100644 (file)
@@ -10,19 +10,25 @@ import be.nikiroo.gofetch.support.BasicSupport.Type;
 public class Gopher extends Output {
        static private final int LINE_SIZE = 70;
 
-       private String hostname;
-       private int port;
-
-       public Gopher(Type type, String hostname, int port) {
-               super(type);
-
-               this.hostname = hostname;
-               this.port = port;
+       public Gopher(Type type, String hostname, String preselector, int port) {
+               super(type, hostname, preselector, port);
        }
 
        @Override
        public String getIndexHeader() {
-               return "iHello world!\r\niThis is my news site.\r\ni\r\n";
+               StringBuilder builder = new StringBuilder();
+
+               appendCenter(builder, "NEWS", true);
+               appendLeft(builder, "", "");
+               appendLeft(builder, "You will find here a few pages full of news.", "");
+               appendLeft(builder, "", "");
+               appendLeft(
+                               builder,
+                               "They are simply scrapped from their associated webpage and converted into a gopher friendly format, updated a few times a day.",
+                               "");
+               appendLeft(builder, "", "");
+
+               return builder.toString();
        }
 
        @Override
@@ -43,8 +49,10 @@ public class Gopher extends Output {
 
                builder.append("i\r\n");
 
-               for (Comment comment : comments) {
-                       append(builder, comment, "");
+               if (comments != null) {
+                       for (Comment comment : comments) {
+                               append(builder, comment, "");
+                       }
                }
 
                builder.append("i\r\n");
@@ -81,11 +89,12 @@ public class Gopher extends Output {
                if (links) {
                        appendCenter(builder, story.getTitle(), true);
                        builder.append("i\r\n");
-                       appendLeft(builder, story.getDetails(), "", "", "  ");
-                       builder.append("i\r\n");
-                       builder.append("i  o News link: ").append(story.getUrlInternal());
+                       appendLeft(builder, story.getDetails(), "  ");
                        builder.append("i\r\n");
-                       builder.append("i  o Source link: ").append(story.getUrlExternal());
+                       builder.append("i  o News link: ").append(story.getUrlInternal())
+                                       .append("\r\n");
+                       builder.append("i  o Source link: ").append(story.getUrlExternal())
+                                       .append("\r\n");
                        builder.append("i\r\n");
                } else {
                        builder.append('1').append(story.getTitle()) //
@@ -93,12 +102,12 @@ public class Gopher extends Output {
                                        .append('\t').append(hostname) //
                                        .append('\t').append(port) //
                                        .append("\r\n");
-                       appendLeft(builder, story.getDetails(), "", "", "  ");
+                       appendLeft(builder, story.getDetails(), "  ");
                }
 
                builder.append("i\r\n");
 
-               appendLeft(builder, story.getContent(), "", "", "    ");
+               appendLeft(builder, story.getContent(), "    ");
 
                builder.append("i\r\n");
 
@@ -117,6 +126,12 @@ public class Gopher extends Output {
                }
        }
 
+       // note: adds "i"
+       private static void appendLeft(StringBuilder builder, String text,
+                       String space) {
+               appendLeft(builder, text, "", "", space);
+       }
+
        // note: adds "i"
        private static void appendLeft(StringBuilder builder, String text,
                        String prependFirst, String prependOthers, String space) {
index ab7489b47dedf593f409139057be1a24b9d03a72..c087ef24d91ced582827d5cac32f9dcf8574424f 100644 (file)
@@ -7,13 +7,23 @@ import be.nikiroo.gofetch.data.Story;
 import be.nikiroo.gofetch.support.BasicSupport.Type;
 
 public class Html extends Output {
-       public Html(Type type) {
-               super(type);
+       public Html(Type type, String hostname, String preselector, int port) {
+               super(type, hostname, preselector, port);
        }
 
        @Override
        public String getIndexHeader() {
-               return "<h1>Hello world!</h1><br/>TODO\n";
+               String gopherUrl = "gopher://" + hostname + preselector + ":" + port;
+
+               return "<h1>News</h1>\n"//
+                               + "<p>You will find here a few pages full of news, mirroring <a href='"
+                               + gopherUrl + "'>"
+                               + gopherUrl
+                               + "</a>.</p>\n"//
+                               + "<br/>\n"//
+                               + "<p>They are simply scrapped from their associated webpage and updated a few times a day.</p>\n"//
+                               + "<br/>\n"//
+               ;
        }
 
        @Override
@@ -23,22 +33,32 @@ public class Html extends Output {
 
        @Override
        public String export(Story story) {
-               return appendHtml(new StringBuilder(), story, true).append("<hr/>\n")
-                               .toString();
+               StringBuilder builder = new StringBuilder();
+
+               builder.append("<div class='story-header'>\n");
+               appendHtml(builder, story, true);
+               builder.append("<hr/>\n");
+               builder.append("</div>\n");
+
+               return builder.toString();
        }
 
        @Override
        public String export(Story story, List<Comment> comments) {
                StringBuilder builder = new StringBuilder();
-               appendHtml(builder, story, false);
 
-               // TODO: ext link and link
+               builder.append("<div class='story'>\n");
+               appendHtml(builder, story, false);
+               builder.append("<hr/>\n");
 
-               builder.append("<hr/>");
-               for (Comment comment : comments) {
-                       appendHtml(builder, comment, "  ");
+               if (comments != null) {
+                       for (Comment comment : comments) {
+                               appendHtml(builder, comment, "  ");
+                       }
                }
 
+               builder.append("</div>\n");
+
                return builder.toString();
        }
 
@@ -59,22 +79,32 @@ public class Html extends Output {
        }
 
        private StringBuilder appendHtml(StringBuilder builder, Story story,
-                       boolean links) {
-               // TODO
-               builder.append("<div class='story'>");
-               if (links) {
+                       boolean resume) {
+               if (resume) {
                        builder.append("        <h1><a href='" + story.getId() + ".html'>"
-                                       + story.getTitle() + "</a></h1>");
+                                       + story.getTitle() + "</a></h1>\n");
                } else {
-                       builder.append("        <h1>" + story.getTitle() + "</h1>");
+                       builder.append("        <h1>" + story.getTitle() + "</h1>\n");
                }
                builder.append("        <div class='details'>(" + story.getDetails()
-                               + ")</div>");
-               builder.append("        <br/>");
-               builder.append("        <div class='content'>");
-               builder.append("                " + story.getContent());
-               builder.append("        </div>");
-               builder.append("</div>");
+                               + ")</div>\n");
+               builder.append("        <br/>\n");
+
+               if (!resume) {
+                       builder.append("    <ul>\n");
+                       builder.append("        <li>News link: <a href='"
+                                       + story.getUrlInternal() + "'>" + story.getUrlInternal()
+                                       + "</a></li>\n");
+                       builder.append("        <li>Source link: <a href='"
+                                       + story.getUrlExternal() + "'>" + story.getUrlExternal()
+                                       + "</a></li>\n");
+                       builder.append("    </ul>\n");
+                       builder.append("        <br/>\n");
+               }
+
+               builder.append("        <div class='content'>\n");
+               builder.append("                " + story.getContent() + "\n");
+               builder.append("        </div>\n");
 
                return builder;
        }
index b12596e38ac1958945211534df62e03f06066329..f293b047df8d351fc59971713a82f1f303f32f6c 100644 (file)
@@ -17,14 +17,38 @@ public abstract class Output {
         */
        protected Type type;
 
+       /**
+        * The gopher hostname to use.
+        */
+       protected String hostname;
+
+       /**
+        * The sub directory and (pre-)selector to use for the resources.
+        */
+       protected String preselector;
+
+       /**
+        * The Gopher port to use.
+        */
+       protected int port;
+
        /**
         * Create a new {@link Output} class for the given type (which can be NULL).
         * 
         * @param type
         *            the type or NULL for no type
+        * @param hostname
+        *            the gopher hostname to use
+        * @param preselector
+        *            the sub directory and (pre-)selector to use for the resources
+        * @param port
+        *            the Gopher port to use
         */
-       public Output(Type type) {
+       public Output(Type type, String hostname, String preselector, int port) {
                this.type = type;
+               this.hostname = hostname;
+               this.preselector = preselector;
+               this.port = port;
        }
 
        /**
index 5dfa03b227e30e2786d0a3dacf0195c6e5d66096..5b5612fba705a769e50f3cbbbb0216a295e5a956 100644 (file)
@@ -14,6 +14,11 @@ import org.jsoup.select.Elements;
 import be.nikiroo.gofetch.data.Comment;
 import be.nikiroo.gofetch.data.Story;
 
+/**
+ * Support <a href='https://slashdot.org/'>https://slashdot.org/</a>.
+ * 
+ * @author niki
+ */
 public class Slashdot extends BasicSupport {
        @Override
        public String getDescription() {
@@ -33,6 +38,7 @@ public class Slashdot extends BasicSupport {
                        if (titles.size() == 0) {
                                continue;
                        }
+
                        Element title = titles.get(0);
 
                        String id = "" + title.attr("id");