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