Change: less \n in the listing if no content
[gofetch.git] / src / be / nikiroo / gofetch / output / Gopher.java
CommitLineData
73785268
NR
1package be.nikiroo.gofetch.output;
2
73785268
NR
3import be.nikiroo.gofetch.data.Comment;
4import be.nikiroo.gofetch.data.Story;
5import be.nikiroo.gofetch.support.BasicSupport.Type;
accfac8e
NR
6import be.nikiroo.utils.StringUtils;
7import be.nikiroo.utils.StringUtils.Alignment;
73785268
NR
8
9public 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
NR
70 appendLeft(builder, menu, comment.getTitle(), "** ", " ", space);
71 appendLeft(builder, menu, "(" + comment.getAuthor() + ")", " ",
72 " ", space);
73785268 73
029feaed 74 builder.append((menu ? "i" : "") + "\r\n");
73785268 75
27008a87
NR
76 for (String line : comment.getContentLines()) {
77 int depth = 0;
78 while (line.length() > depth && line.charAt(depth) == '>') {
79 depth++;
80 }
81 line = line.substring(depth).trim();
82
83 String prep = " ";
84 for (int i = 0; i < depth; i++) {
85 prep += ">";
86 }
87
88 if (depth > 0) {
89 prep += " ";
90 }
91
029feaed 92 appendLeft(builder, menu, line, prep, prep, space);
27008a87 93 }
73785268 94
136d034b 95 builder.append((menu ? "i" : "") + "\r\n");
73785268 96 for (Comment subComment : comment) {
029feaed 97 append(builder, menu, subComment, space + " ");
136d034b 98 builder.append((menu ? "i" : "") + "\r\n");
73785268
NR
99 }
100
101 return builder;
102 }
103
104 private StringBuilder append(StringBuilder builder, Story story,
5c056aad
NR
105 boolean resume) {
106 if (!resume) {
136d034b 107 appendCenter(builder, false, story.getTitle(), " ", true);
029feaed
NR
108 builder.append("\r\n");
109 appendJustified(builder, false, story.getDetails(), " ");
110 builder.append("\r\n");
5c056aad 111
029feaed 112 builder.append(" o News link: ").append(story.getUrlInternal())
70b18499 113 .append("\r\n");
029feaed 114 builder.append(" o Source link: ").append(story.getUrlExternal())
70b18499 115 .append("\r\n");
029feaed 116 builder.append("\r\n");
5c056aad 117
029feaed 118 builder.append("\r\n");
5c056aad 119
029feaed 120 appendJustified(builder, false, story.getFullContent(), " ");
75f6f0c2 121 builder.append("\r\n");
73785268 122 } else {
029feaed 123 builder.append('0').append(story.getTitle()) //
93e09a08 124 .append('\t').append(story.getSelector()) //
73785268
NR
125 .append('\t').append(hostname) //
126 .append('\t').append(port) //
127 .append("\r\n");
029feaed 128 appendJustified(builder, true, story.getDetails(), " ");
5c056aad 129 builder.append("i\r\n");
73785268 130
75f6f0c2
NR
131 String content = story.getContent();
132 if (!content.isEmpty()) {
133 appendJustified(builder, true, content, " ");
134 builder.append("i\r\n");
135 }
5c056aad 136 }
73785268 137
73785268
NR
138 return builder;
139 }
140
141 // note: adds "i"
029feaed 142 private static void appendCenter(StringBuilder builder, boolean menu,
136d034b 143 String text, String space, boolean allCaps) {
73785268
NR
144 if (allCaps) {
145 text = text.toUpperCase();
146 }
accfac8e 147
136d034b 148 int size = LINE_SIZE - space.length();
accfac8e
NR
149 for (String line : StringUtils
150 .justifyText(text, size, Alignment.CENTER)) {
151 builder.append(menu ? "i" : "") //
152 .append(space) //
153 .append(line) //
154 .append("\r\n");
73785268
NR
155 }
156 }
157
029feaed
NR
158 private static void appendJustified(StringBuilder builder, boolean menu,
159 String text, String space) {
100a8395 160 for (String line : text.split("\n")) {
136d034b 161 int size = LINE_SIZE - space.length();
accfac8e
NR
162 for (String subline : StringUtils.justifyText(line, size,
163 Alignment.JUSTIFY)) {
164 builder.append(menu ? "i" : "") //
165 .append(space) //
166 .append(subline) //
167 .append("\r\n");
100a8395 168 }
301dfeb2
NR
169 }
170 }
171
029feaed
NR
172 private static void appendLeft(StringBuilder builder, boolean menu,
173 String text, String space) {
174 appendLeft(builder, menu, text, "", "", space);
70b18499
NR
175 }
176
029feaed
NR
177 private static void appendLeft(StringBuilder builder, boolean menu,
178 String text, String prependFirst, String prependOthers, String space) {
73785268 179 String prepend = prependFirst;
100a8395 180 for (String line : text.split("\n")) {
accfac8e
NR
181 for (String subline : StringUtils.justifyText(line, LINE_SIZE
182 - space.length(), Alignment.LEFT)) {
183 builder.append(menu ? "i" : "") //
184 .append(space) //
185 .append(prepend) //
186 .append(subline) //
187 .append("\r\n");
100a8395
NR
188 prepend = prependOthers;
189 }
73785268
NR
190 }
191 }
192}