Commit | Line | Data |
---|---|---|
73785268 NR |
1 | package be.nikiroo.gofetch.output; |
2 | ||
73785268 NR |
3 | import be.nikiroo.gofetch.data.Comment; |
4 | import be.nikiroo.gofetch.data.Story; | |
3e62b034 | 5 | import be.nikiroo.gofetch.support.Type; |
accfac8e NR |
6 | import be.nikiroo.utils.StringUtils; |
7 | import be.nikiroo.utils.StringUtils.Alignment; | |
73785268 NR |
8 | |
9 | public class Gopher extends Output { | |
25271075 | 10 | static private final int LINE_SIZE = 67; |
73785268 | 11 | |
70b18499 NR |
12 | public Gopher(Type type, String hostname, String preselector, int port) { |
13 | super(type, hostname, preselector, port); | |
73785268 NR |
14 | } |
15 | ||
16 | @Override | |
17 | public String getIndexHeader() { | |
70b18499 NR |
18 | StringBuilder builder = new StringBuilder(); |
19 | ||
136d034b | 20 | appendCenter(builder, true, "NEWS", "", true); |
029feaed NR |
21 | appendLeft(builder, true, "", ""); |
22 | appendLeft(builder, true, | |
23 | "You will find here a few pages full of news.", ""); | |
24 | appendLeft(builder, true, "", ""); | |
70b18499 NR |
25 | appendLeft( |
26 | builder, | |
029feaed | 27 | true, |
70b18499 NR |
28 | "They are simply scrapped from their associated webpage and converted into a gopher friendly format, updated a few times a day.", |
29 | ""); | |
029feaed | 30 | appendLeft(builder, true, "", ""); |
70b18499 NR |
31 | |
32 | return builder.toString(); | |
73785268 NR |
33 | } |
34 | ||
35 | @Override | |
36 | public String getIndexFooter() { | |
37 | return ""; | |
38 | } | |
39 | ||
40 | @Override | |
5c056aad | 41 | public String exportHeader(Story story) { |
029feaed | 42 | return append(new StringBuilder(), story, true).toString(); |
73785268 NR |
43 | } |
44 | ||
45 | @Override | |
5c056aad | 46 | public String export(Story story) { |
73785268 | 47 | StringBuilder builder = new StringBuilder(); |
0774a513 | 48 | append(builder, story, false); |
73785268 | 49 | |
029feaed | 50 | builder.append("\r\n"); |
73785268 | 51 | |
5c056aad NR |
52 | if (story.getComments() != null) { |
53 | for (Comment comment : story.getComments()) { | |
136d034b | 54 | append(builder, false, comment, " "); |
70b18499 | 55 | } |
73785268 NR |
56 | } |
57 | ||
029feaed | 58 | builder.append("\r\n"); |
73785268 NR |
59 | |
60 | return builder.toString(); | |
61 | } | |
62 | ||
029feaed NR |
63 | private StringBuilder append(StringBuilder builder, boolean menu, |
64 | Comment comment, String space) { | |
73785268 NR |
65 | |
66 | if (space.length() > LINE_SIZE - 20) { | |
67 | space = space.substring(0, LINE_SIZE - 20); | |
68 | } | |
69 | ||
029feaed | 70 | appendLeft(builder, menu, comment.getTitle(), "** ", " ", space); |
73785268 | 71 | |
86d03b2e NR |
72 | if (comment.getAuthor() != null |
73 | && !comment.getAuthor().trim().isEmpty()) { | |
74 | appendLeft(builder, menu, "(" + comment.getAuthor() + ")", " ", | |
75 | " ", space); | |
76 | builder.append((menu ? "i" : "") + "\r\n"); | |
77 | } | |
73785268 | 78 | |
27008a87 NR |
79 | for (String line : comment.getContentLines()) { |
80 | int depth = 0; | |
81 | while (line.length() > depth && line.charAt(depth) == '>') { | |
82 | depth++; | |
83 | } | |
84 | line = line.substring(depth).trim(); | |
85 | ||
86 | String prep = " "; | |
87 | for (int i = 0; i < depth; i++) { | |
88 | prep += ">"; | |
89 | } | |
90 | ||
91 | if (depth > 0) { | |
92 | prep += " "; | |
93 | } | |
94 | ||
029feaed | 95 | appendLeft(builder, menu, line, prep, prep, space); |
27008a87 | 96 | } |
73785268 | 97 | |
136d034b | 98 | builder.append((menu ? "i" : "") + "\r\n"); |
73785268 | 99 | for (Comment subComment : comment) { |
029feaed | 100 | append(builder, menu, subComment, space + " "); |
136d034b | 101 | builder.append((menu ? "i" : "") + "\r\n"); |
73785268 NR |
102 | } |
103 | ||
104 | return builder; | |
105 | } | |
106 | ||
107 | private StringBuilder append(StringBuilder builder, Story story, | |
5c056aad NR |
108 | boolean resume) { |
109 | if (!resume) { | |
136d034b | 110 | appendCenter(builder, false, story.getTitle(), " ", true); |
029feaed NR |
111 | builder.append("\r\n"); |
112 | appendJustified(builder, false, story.getDetails(), " "); | |
113 | builder.append("\r\n"); | |
5c056aad | 114 | |
029feaed | 115 | builder.append(" o News link: ").append(story.getUrlInternal()) |
70b18499 | 116 | .append("\r\n"); |
029feaed | 117 | builder.append(" o Source link: ").append(story.getUrlExternal()) |
70b18499 | 118 | .append("\r\n"); |
029feaed | 119 | builder.append("\r\n"); |
5c056aad | 120 | |
029feaed | 121 | builder.append("\r\n"); |
5c056aad | 122 | |
029feaed | 123 | appendJustified(builder, false, story.getFullContent(), " "); |
75f6f0c2 | 124 | builder.append("\r\n"); |
73785268 | 125 | } else { |
029feaed | 126 | builder.append('0').append(story.getTitle()) // |
93e09a08 | 127 | .append('\t').append(story.getSelector()) // |
73785268 NR |
128 | .append('\t').append(hostname) // |
129 | .append('\t').append(port) // | |
130 | .append("\r\n"); | |
029feaed | 131 | appendJustified(builder, true, story.getDetails(), " "); |
5c056aad | 132 | builder.append("i\r\n"); |
73785268 | 133 | |
75f6f0c2 NR |
134 | String content = story.getContent(); |
135 | if (!content.isEmpty()) { | |
136 | appendJustified(builder, true, content, " "); | |
137 | builder.append("i\r\n"); | |
138 | } | |
5c056aad | 139 | } |
73785268 | 140 | |
73785268 NR |
141 | return builder; |
142 | } | |
143 | ||
144 | // note: adds "i" | |
029feaed | 145 | private static void appendCenter(StringBuilder builder, boolean menu, |
136d034b | 146 | String text, String space, boolean allCaps) { |
73785268 NR |
147 | if (allCaps) { |
148 | text = text.toUpperCase(); | |
149 | } | |
accfac8e | 150 | |
136d034b | 151 | int size = LINE_SIZE - space.length(); |
accfac8e NR |
152 | for (String line : StringUtils |
153 | .justifyText(text, size, Alignment.CENTER)) { | |
154 | builder.append(menu ? "i" : "") // | |
155 | .append(space) // | |
156 | .append(line) // | |
157 | .append("\r\n"); | |
73785268 NR |
158 | } |
159 | } | |
160 | ||
029feaed NR |
161 | private static void appendJustified(StringBuilder builder, boolean menu, |
162 | String text, String space) { | |
100a8395 | 163 | for (String line : text.split("\n")) { |
136d034b | 164 | int size = LINE_SIZE - space.length(); |
accfac8e NR |
165 | for (String subline : StringUtils.justifyText(line, size, |
166 | Alignment.JUSTIFY)) { | |
167 | builder.append(menu ? "i" : "") // | |
168 | .append(space) // | |
169 | .append(subline) // | |
170 | .append("\r\n"); | |
100a8395 | 171 | } |
301dfeb2 NR |
172 | } |
173 | } | |
174 | ||
029feaed NR |
175 | private static void appendLeft(StringBuilder builder, boolean menu, |
176 | String text, String space) { | |
177 | appendLeft(builder, menu, text, "", "", space); | |
70b18499 NR |
178 | } |
179 | ||
029feaed NR |
180 | private static void appendLeft(StringBuilder builder, boolean menu, |
181 | String text, String prependFirst, String prependOthers, String space) { | |
73785268 | 182 | String prepend = prependFirst; |
100a8395 | 183 | for (String line : text.split("\n")) { |
accfac8e NR |
184 | for (String subline : StringUtils.justifyText(line, LINE_SIZE |
185 | - space.length(), Alignment.LEFT)) { | |
186 | builder.append(menu ? "i" : "") // | |
187 | .append(space) // | |
188 | .append(prepend) // | |
189 | .append(subline) // | |
190 | .append("\r\n"); | |
100a8395 NR |
191 | prepend = prependOthers; |
192 | } | |
73785268 NR |
193 | } |
194 | } | |
195 | } |