From 029feaedb06a22016a2c20df90d6d57d2e5e3b55 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Mon, 28 Aug 2017 06:04:12 +0200 Subject: [PATCH] Gopher: (WIP): menu -> text for news --- src/be/nikiroo/gofetch/Fetcher.java | 13 +++- src/be/nikiroo/gofetch/output/Gopher.java | 90 +++++++++++------------ 2 files changed, 55 insertions(+), 48 deletions(-) diff --git a/src/be/nikiroo/gofetch/Fetcher.java b/src/be/nikiroo/gofetch/Fetcher.java index 7866fe0..920b48a 100644 --- a/src/be/nikiroo/gofetch/Fetcher.java +++ b/src/be/nikiroo/gofetch/Fetcher.java @@ -227,10 +227,19 @@ public class Fetcher { } } + /** + * + * @param name + * @param ref + * @param index + * this link is in an index (and link to a NON index) + * @param html + * @return + */ private String getLink(String name, String ref, boolean index, boolean html) { if (!html) { - return new StringBuilder().append("1" + name).append("\t") - .append(ref) // + return new StringBuilder().append((index ? "0" : "1") + name) + .append("\t").append(ref) // .append("\t").append(hostname) // .append("\t").append(Integer.toString(port)) // .append("\r\n").toString(); diff --git a/src/be/nikiroo/gofetch/output/Gopher.java b/src/be/nikiroo/gofetch/output/Gopher.java index 2fa0c91..877dfa2 100644 --- a/src/be/nikiroo/gofetch/output/Gopher.java +++ b/src/be/nikiroo/gofetch/output/Gopher.java @@ -16,15 +16,17 @@ public class Gopher extends Output { public String getIndexHeader() { StringBuilder builder = new StringBuilder(); - appendCenter(builder, "NEWS", true); - appendLeft(builder, "", ""); - appendLeft(builder, "You will find here a few pages full of news.", ""); - appendLeft(builder, "", ""); + appendCenter(builder, true, "NEWS", true); + appendLeft(builder, true, "", ""); + appendLeft(builder, true, + "You will find here a few pages full of news.", ""); + appendLeft(builder, true, "", ""); appendLeft( builder, + true, "They are simply scrapped from their associated webpage and converted into a gopher friendly format, updated a few times a day.", ""); - appendLeft(builder, "", ""); + appendLeft(builder, true, "", ""); return builder.toString(); } @@ -36,8 +38,7 @@ public class Gopher extends Output { @Override public String exportHeader(Story story) { - return append(new StringBuilder(), story, true).append("i\r\ni\r\n") - .toString(); + return append(new StringBuilder(), story, true).toString(); } @Override @@ -45,31 +46,31 @@ public class Gopher extends Output { StringBuilder builder = new StringBuilder(); append(builder, story, false); - builder.append("i\r\n"); + builder.append("\r\n"); if (story.getComments() != null) { for (Comment comment : story.getComments()) { - append(builder, comment, ""); + append(builder, false, comment, ""); } } - builder.append("i\r\n"); + builder.append("\r\n"); return builder.toString(); } - private StringBuilder append(StringBuilder builder, Comment comment, - String space) { + private StringBuilder append(StringBuilder builder, boolean menu, + Comment comment, String space) { if (space.length() > LINE_SIZE - 20) { space = space.substring(0, LINE_SIZE - 20); } - appendLeft(builder, comment.getTitle(), "** ", " ", space); - appendLeft(builder, "(" + comment.getAuthor() + ")", " ", " ", - space); + appendLeft(builder, menu, comment.getTitle(), "** ", " ", space); + appendLeft(builder, menu, "(" + comment.getAuthor() + ")", " ", + " ", space); - builder.append("i\r\n"); + builder.append((menu ? "i" : "") + "\r\n"); for (String line : comment.getContentLines()) { int depth = 0; @@ -87,12 +88,12 @@ public class Gopher extends Output { prep += " "; } - appendLeft(builder, line, prep, prep, space); + appendLeft(builder, menu, line, prep, prep, space); } builder.append("i\r\n"); for (Comment subComment : comment) { - append(builder, subComment, space + " "); + append(builder, menu, subComment, space + " "); builder.append("i\r\n"); } @@ -102,74 +103,71 @@ public class Gopher extends Output { private StringBuilder append(StringBuilder builder, Story story, boolean resume) { if (!resume) { - appendCenter(builder, story.getTitle(), true); - builder.append("i\r\n"); - appendJustified(builder, story.getDetails(), " "); - builder.append("i\r\n"); + appendCenter(builder, false, story.getTitle(), true); + builder.append("\r\n"); + appendJustified(builder, false, story.getDetails(), " "); + builder.append("\r\n"); - builder.append("i o News link: ").append(story.getUrlInternal()) + builder.append(" o News link: ").append(story.getUrlInternal()) .append("\r\n"); - builder.append("i o Source link: ").append(story.getUrlExternal()) + builder.append(" o Source link: ").append(story.getUrlExternal()) .append("\r\n"); - builder.append("i\r\n"); + builder.append("\r\n"); - builder.append("i\r\n"); + builder.append("\r\n"); - appendJustified(builder, story.getFullContent(), " "); + appendJustified(builder, false, story.getFullContent(), " "); } else { - builder.append('1').append(story.getTitle()) // + builder.append('0').append(story.getTitle()) // .append('\t').append(story.getSelector()) // .append('\t').append(hostname) // .append('\t').append(port) // .append("\r\n"); - appendJustified(builder, story.getDetails(), " "); + appendJustified(builder, true, story.getDetails(), " "); builder.append("i\r\n"); - appendJustified(builder, story.getContent(), " "); + appendJustified(builder, true, story.getContent(), " "); } - builder.append("i\r\n"); + builder.append((resume ? "i" : "") + "\r\n"); return builder; } // note: adds "i" - private static void appendCenter(StringBuilder builder, String text, - boolean allCaps) { + private static void appendCenter(StringBuilder builder, boolean menu, + String text, boolean allCaps) { if (allCaps) { text = text.toUpperCase(); } for (String line : StringJustifier.center(text, LINE_SIZE)) { - builder.append("i").append(line).append("\r\n"); + builder.append(menu ? "i" : "").append(line).append("\r\n"); } } - // note: adds "i" - private static void appendJustified(StringBuilder builder, String text, - String space) { + private static void appendJustified(StringBuilder builder, boolean menu, + String text, String space) { for (String line : text.split("\n")) { for (String subline : StringJustifier.full(line, LINE_SIZE - space.length())) { - builder.append("i").append(subline).append("\r\n"); + builder.append(menu ? "i" : "").append(subline).append("\r\n"); } } } - // note: adds "i" - private static void appendLeft(StringBuilder builder, String text, - String space) { - appendLeft(builder, text, "", "", space); + private static void appendLeft(StringBuilder builder, boolean menu, + String text, String space) { + appendLeft(builder, menu, text, "", "", space); } - // note: adds "i" - private static void appendLeft(StringBuilder builder, String text, - String prependFirst, String prependOthers, String space) { + private static void appendLeft(StringBuilder builder, boolean menu, + String text, String prependFirst, String prependOthers, String space) { String prepend = prependFirst; for (String line : text.split("\n")) { for (String subline : StringJustifier.left(line, LINE_SIZE - space.length())) { - builder.append("i").append(space).append(prepend) + builder.append(menu ? "i" : "").append(space).append(prepend) .append(subline).append("\r\n"); prepend = prependOthers; } -- 2.27.0