From 70b18499defd9bd4c32f1d60ac44b971678308c2 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Sun, 6 Aug 2017 13:17:57 +0200 Subject: [PATCH] Fix some bugs, fix some TODO text --- src/be/nikiroo/gofetch/Fetcher.java | 23 +++--- src/be/nikiroo/gofetch/Main.java | 7 +- src/be/nikiroo/gofetch/output/Gopher.java | 49 ++++++++----- src/be/nikiroo/gofetch/output/Html.java | 74 ++++++++++++++------ src/be/nikiroo/gofetch/output/Output.java | 26 ++++++- src/be/nikiroo/gofetch/support/Slashdot.java | 6 ++ 6 files changed, 130 insertions(+), 55 deletions(-) diff --git a/src/be/nikiroo/gofetch/Fetcher.java b/src/be/nikiroo/gofetch/Fetcher.java index 5a9c774..ce68558 100644 --- a/src/be/nikiroo/gofetch/Fetcher.java +++ b/src/be/nikiroo/gofetch/Fetcher.java @@ -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(); diff --git a/src/be/nikiroo/gofetch/Main.java b/src/be/nikiroo/gofetch/Main.java index 590d4a0..7aa9405 100644 --- a/src/be/nikiroo/gofetch/Main.java +++ b/src/be/nikiroo/gofetch/Main.java @@ -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); diff --git a/src/be/nikiroo/gofetch/output/Gopher.java b/src/be/nikiroo/gofetch/output/Gopher.java index 8cce4a5..10714b1 100644 --- a/src/be/nikiroo/gofetch/output/Gopher.java +++ b/src/be/nikiroo/gofetch/output/Gopher.java @@ -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) { diff --git a/src/be/nikiroo/gofetch/output/Html.java b/src/be/nikiroo/gofetch/output/Html.java index ab7489b..c087ef2 100644 --- a/src/be/nikiroo/gofetch/output/Html.java +++ b/src/be/nikiroo/gofetch/output/Html.java @@ -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 "

Hello world!


TODO\n"; + String gopherUrl = "gopher://" + hostname + preselector + ":" + port; + + return "

News

\n"// + + "

You will find here a few pages full of news, mirroring " + + gopherUrl + + ".

\n"// + + "
\n"// + + "

They are simply scrapped from their associated webpage and updated a few times a day.

\n"// + + "
\n"// + ; } @Override @@ -23,22 +33,32 @@ public class Html extends Output { @Override public String export(Story story) { - return appendHtml(new StringBuilder(), story, true).append("
\n") - .toString(); + StringBuilder builder = new StringBuilder(); + + builder.append("
\n"); + appendHtml(builder, story, true); + builder.append("
\n"); + builder.append("
\n"); + + return builder.toString(); } @Override public String export(Story story, List comments) { StringBuilder builder = new StringBuilder(); - appendHtml(builder, story, false); - // TODO: ext link and link + builder.append("
\n"); + appendHtml(builder, story, false); + builder.append("
\n"); - builder.append("
"); - for (Comment comment : comments) { - appendHtml(builder, comment, " "); + if (comments != null) { + for (Comment comment : comments) { + appendHtml(builder, comment, " "); + } } + builder.append("
\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("
"); - if (links) { + boolean resume) { + if (resume) { builder.append("

" - + story.getTitle() + "

"); + + story.getTitle() + "\n"); } else { - builder.append("

" + story.getTitle() + "

"); + builder.append("

" + story.getTitle() + "

\n"); } builder.append("
(" + story.getDetails() - + ")
"); - builder.append("
"); - builder.append("
"); - builder.append(" " + story.getContent()); - builder.append("
"); - builder.append("
"); + + ")\n"); + builder.append("
\n"); + + if (!resume) { + builder.append(" \n"); + builder.append("
\n"); + } + + builder.append("
\n"); + builder.append(" " + story.getContent() + "\n"); + builder.append("
\n"); return builder; } diff --git a/src/be/nikiroo/gofetch/output/Output.java b/src/be/nikiroo/gofetch/output/Output.java index b12596e..f293b04 100644 --- a/src/be/nikiroo/gofetch/output/Output.java +++ b/src/be/nikiroo/gofetch/output/Output.java @@ -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; } /** diff --git a/src/be/nikiroo/gofetch/support/Slashdot.java b/src/be/nikiroo/gofetch/support/Slashdot.java index 5dfa03b..5b5612f 100644 --- a/src/be/nikiroo/gofetch/support/Slashdot.java +++ b/src/be/nikiroo/gofetch/support/Slashdot.java @@ -14,6 +14,11 @@ import org.jsoup.select.Elements; import be.nikiroo.gofetch.data.Comment; import be.nikiroo.gofetch.data.Story; +/** + * Support https://slashdot.org/. + * + * @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"); -- 2.27.0