Change: less \n in the listing if no content
[gofetch.git] / src / be / nikiroo / gofetch / output / Gopher.java
1 package be.nikiroo.gofetch.output;
2
3 import be.nikiroo.gofetch.data.Comment;
4 import be.nikiroo.gofetch.data.Story;
5 import be.nikiroo.gofetch.support.BasicSupport.Type;
6 import be.nikiroo.utils.StringUtils;
7 import be.nikiroo.utils.StringUtils.Alignment;
8
9 public class Gopher extends Output {
10 static private final int LINE_SIZE = 67;
11
12 public Gopher(Type type, String hostname, String preselector, int port) {
13 super(type, hostname, preselector, port);
14 }
15
16 @Override
17 public String getIndexHeader() {
18 StringBuilder builder = new StringBuilder();
19
20 appendCenter(builder, true, "NEWS", "", true);
21 appendLeft(builder, true, "", "");
22 appendLeft(builder, true,
23 "You will find here a few pages full of news.", "");
24 appendLeft(builder, true, "", "");
25 appendLeft(
26 builder,
27 true,
28 "They are simply scrapped from their associated webpage and converted into a gopher friendly format, updated a few times a day.",
29 "");
30 appendLeft(builder, true, "", "");
31
32 return builder.toString();
33 }
34
35 @Override
36 public String getIndexFooter() {
37 return "";
38 }
39
40 @Override
41 public String exportHeader(Story story) {
42 return append(new StringBuilder(), story, true).toString();
43 }
44
45 @Override
46 public String export(Story story) {
47 StringBuilder builder = new StringBuilder();
48 append(builder, story, false);
49
50 builder.append("\r\n");
51
52 if (story.getComments() != null) {
53 for (Comment comment : story.getComments()) {
54 append(builder, false, comment, " ");
55 }
56 }
57
58 builder.append("\r\n");
59
60 return builder.toString();
61 }
62
63 private StringBuilder append(StringBuilder builder, boolean menu,
64 Comment comment, String space) {
65
66 if (space.length() > LINE_SIZE - 20) {
67 space = space.substring(0, LINE_SIZE - 20);
68 }
69
70 appendLeft(builder, menu, comment.getTitle(), "** ", " ", space);
71 appendLeft(builder, menu, "(" + comment.getAuthor() + ")", " ",
72 " ", space);
73
74 builder.append((menu ? "i" : "") + "\r\n");
75
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
92 appendLeft(builder, menu, line, prep, prep, space);
93 }
94
95 builder.append((menu ? "i" : "") + "\r\n");
96 for (Comment subComment : comment) {
97 append(builder, menu, subComment, space + " ");
98 builder.append((menu ? "i" : "") + "\r\n");
99 }
100
101 return builder;
102 }
103
104 private StringBuilder append(StringBuilder builder, Story story,
105 boolean resume) {
106 if (!resume) {
107 appendCenter(builder, false, story.getTitle(), " ", true);
108 builder.append("\r\n");
109 appendJustified(builder, false, story.getDetails(), " ");
110 builder.append("\r\n");
111
112 builder.append(" o News link: ").append(story.getUrlInternal())
113 .append("\r\n");
114 builder.append(" o Source link: ").append(story.getUrlExternal())
115 .append("\r\n");
116 builder.append("\r\n");
117
118 builder.append("\r\n");
119
120 appendJustified(builder, false, story.getFullContent(), " ");
121 builder.append("\r\n");
122 } else {
123 builder.append('0').append(story.getTitle()) //
124 .append('\t').append(story.getSelector()) //
125 .append('\t').append(hostname) //
126 .append('\t').append(port) //
127 .append("\r\n");
128 appendJustified(builder, true, story.getDetails(), " ");
129 builder.append("i\r\n");
130
131 String content = story.getContent();
132 if (!content.isEmpty()) {
133 appendJustified(builder, true, content, " ");
134 builder.append("i\r\n");
135 }
136 }
137
138 return builder;
139 }
140
141 // note: adds "i"
142 private static void appendCenter(StringBuilder builder, boolean menu,
143 String text, String space, boolean allCaps) {
144 if (allCaps) {
145 text = text.toUpperCase();
146 }
147
148 int size = LINE_SIZE - space.length();
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");
155 }
156 }
157
158 private static void appendJustified(StringBuilder builder, boolean menu,
159 String text, String space) {
160 for (String line : text.split("\n")) {
161 int size = LINE_SIZE - space.length();
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");
168 }
169 }
170 }
171
172 private static void appendLeft(StringBuilder builder, boolean menu,
173 String text, String space) {
174 appendLeft(builder, menu, text, "", "", space);
175 }
176
177 private static void appendLeft(StringBuilder builder, boolean menu,
178 String text, String prependFirst, String prependOthers, String space) {
179 String prepend = prependFirst;
180 for (String line : text.split("\n")) {
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");
188 prepend = prependOthers;
189 }
190 }
191 }
192 }