Use String justification from nikiroo-utils
[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(), " ");
73785268 121 } else {
029feaed 122 builder.append('0').append(story.getTitle()) //
93e09a08 123 .append('\t').append(story.getSelector()) //
73785268
NR
124 .append('\t').append(hostname) //
125 .append('\t').append(port) //
126 .append("\r\n");
029feaed 127 appendJustified(builder, true, story.getDetails(), " ");
5c056aad 128 builder.append("i\r\n");
73785268 129
029feaed 130 appendJustified(builder, true, story.getContent(), " ");
5c056aad 131 }
73785268 132
136d034b 133 builder.append(resume ? "i" : "").append("\r\n");
73785268
NR
134
135 return builder;
136 }
137
138 // note: adds "i"
029feaed 139 private static void appendCenter(StringBuilder builder, boolean menu,
136d034b 140 String text, String space, boolean allCaps) {
73785268
NR
141 if (allCaps) {
142 text = text.toUpperCase();
143 }
accfac8e 144
136d034b 145 int size = LINE_SIZE - space.length();
accfac8e
NR
146 for (String line : StringUtils
147 .justifyText(text, size, Alignment.CENTER)) {
148 builder.append(menu ? "i" : "") //
149 .append(space) //
150 .append(line) //
151 .append("\r\n");
73785268
NR
152 }
153 }
154
029feaed
NR
155 private static void appendJustified(StringBuilder builder, boolean menu,
156 String text, String space) {
100a8395 157 for (String line : text.split("\n")) {
136d034b 158 int size = LINE_SIZE - space.length();
accfac8e
NR
159 for (String subline : StringUtils.justifyText(line, size,
160 Alignment.JUSTIFY)) {
161 builder.append(menu ? "i" : "") //
162 .append(space) //
163 .append(subline) //
164 .append("\r\n");
100a8395 165 }
301dfeb2
NR
166 }
167 }
168
029feaed
NR
169 private static void appendLeft(StringBuilder builder, boolean menu,
170 String text, String space) {
171 appendLeft(builder, menu, text, "", "", space);
70b18499
NR
172 }
173
029feaed
NR
174 private static void appendLeft(StringBuilder builder, boolean menu,
175 String text, String prependFirst, String prependOthers, String space) {
73785268 176 String prepend = prependFirst;
100a8395 177 for (String line : text.split("\n")) {
accfac8e
NR
178 for (String subline : StringUtils.justifyText(line, LINE_SIZE
179 - space.length(), Alignment.LEFT)) {
180 builder.append(menu ? "i" : "") //
181 .append(space) //
182 .append(prepend) //
183 .append(subline) //
184 .append("\r\n");
100a8395
NR
185 prepend = prependOthers;
186 }
73785268
NR
187 }
188 }
189}