.cache -> gophermap
[gofetch.git] / src / be / nikiroo / gofetch / output / Gopher.java
index 8cce4a571d1c5c11f51b0e52bd75eac18588f770..3fa6035c328d3f0731f5bc3a81ce7e38f3220abb 100644 (file)
@@ -1,28 +1,32 @@
 package be.nikiroo.gofetch.output;
 
-import java.util.List;
-
 import be.nikiroo.gofetch.StringJustifier;
 import be.nikiroo.gofetch.data.Comment;
 import be.nikiroo.gofetch.data.Story;
 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;
+       static private final int LINE_SIZE = 67;
 
-       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
@@ -31,20 +35,22 @@ public class Gopher extends Output {
        }
 
        @Override
-       public String export(Story story) {
-               return append(new StringBuilder(), story, false).append("i\r\ni\r\n")
+       public String exportHeader(Story story) {
+               return append(new StringBuilder(), story, true).append("i\r\ni\r\n")
                                .toString();
        }
 
        @Override
-       public String export(Story story, List<Comment> comments) {
+       public String export(Story story) {
                StringBuilder builder = new StringBuilder();
-               append(builder, story, true);
+               append(builder, story, false);
 
                builder.append("i\r\n");
 
-               for (Comment comment : comments) {
-                       append(builder, comment, "");
+               if (story.getComments() != null) {
+                       for (Comment comment : story.getComments()) {
+                               append(builder, comment, "");
+                       }
                }
 
                builder.append("i\r\n");
@@ -77,28 +83,33 @@ public class Gopher extends Output {
        }
 
        private StringBuilder append(StringBuilder builder, Story story,
-                       boolean links) {
-               if (links) {
+                       boolean resume) {
+               if (!resume) {
                        appendCenter(builder, story.getTitle(), true);
                        builder.append("i\r\n");
-                       appendLeft(builder, story.getDetails(), "", "", "  ");
+                       appendJustified(builder, story.getDetails(), "  ");
                        builder.append("i\r\n");
-                       builder.append("i  o News link: ").append(story.getUrlInternal());
+
+                       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");
-                       builder.append("i  o Source link: ").append(story.getUrlExternal());
+
                        builder.append("i\r\n");
+
+                       appendJustified(builder, story.getFullContent(), "    ");
                } else {
                        builder.append('1').append(story.getTitle()) //
-                                       .append('\t').append("0").append(story.getSelector()) //
+                                       .append('\t').append(story.getSelector()) //
                                        .append('\t').append(hostname) //
                                        .append('\t').append(port) //
                                        .append("\r\n");
-                       appendLeft(builder, story.getDetails(), "", "", "  ");
-               }
-
-               builder.append("i\r\n");
+                       appendJustified(builder, story.getDetails(), "  ");
+                       builder.append("i\r\n");
 
-               appendLeft(builder, story.getContent(), "", "", "    ");
+                       appendJustified(builder, story.getContent(), "    ");
+               }
 
                builder.append("i\r\n");
 
@@ -117,6 +128,21 @@ public class Gopher extends Output {
                }
        }
 
+       // note: adds "i"
+       private static void appendJustified(StringBuilder builder, String text,
+                       String space) {
+               for (String line : StringJustifier.full(text,
+                               LINE_SIZE - space.length())) {
+                       builder.append("i").append(line).append("\r\n");
+               }
+       }
+
+       // 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) {