12a420cd84482b988cde4775c6b29f3962fe574b
[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.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
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 }
78
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
95 appendLeft(builder, menu, line, prep, prep, space);
96 }
97
98 builder.append((menu ? "i" : "") + "\r\n");
99 for (Comment subComment : comment) {
100 append(builder, menu, subComment, space + " ");
101 builder.append((menu ? "i" : "") + "\r\n");
102 }
103
104 return builder;
105 }
106
107 private StringBuilder append(StringBuilder builder, Story story,
108 boolean resume) {
109 if (!resume) {
110 appendCenter(builder, false, story.getTitle(), " ", true);
111 builder.append("\r\n");
112 appendJustified(builder, false, story.getDetails(), " ");
113 builder.append("\r\n");
114
115 builder.append(" o News link: ").append(story.getUrlInternal())
116 .append("\r\n");
117 builder.append(" o Source link: ").append(story.getUrlExternal())
118 .append("\r\n");
119 builder.append("\r\n");
120
121 builder.append("\r\n");
122
123 appendJustified(builder, false, story.getFullContent(), " ");
124 builder.append("\r\n");
125 } else {
126 builder.append('0').append(story.getTitle()) //
127 .append('\t').append(story.getSelector()) //
128 .append('\t').append(hostname) //
129 .append('\t').append(port) //
130 .append("\r\n");
131 appendJustified(builder, true, story.getDetails(), " ");
132 builder.append("i\r\n");
133
134 String content = story.getContent();
135 if (!content.isEmpty()) {
136 appendJustified(builder, true, content, " ");
137 builder.append("i\r\n");
138 }
139 }
140
141 return builder;
142 }
143
144 // note: adds "i"
145 private static void appendCenter(StringBuilder builder, boolean menu,
146 String text, String space, boolean allCaps) {
147 if (allCaps) {
148 text = text.toUpperCase();
149 }
150
151 int size = LINE_SIZE - space.length();
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");
158 }
159 }
160
161 private static void appendJustified(StringBuilder builder, boolean menu,
162 String text, String space) {
163 for (String line : text.split("\n")) {
164 int size = LINE_SIZE - space.length();
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");
171 }
172 }
173 }
174
175 private static void appendLeft(StringBuilder builder, boolean menu,
176 String text, String space) {
177 appendLeft(builder, menu, text, "", "", space);
178 }
179
180 private static void appendLeft(StringBuilder builder, boolean menu,
181 String text, String prependFirst, String prependOthers, String space) {
182 String prepend = prependFirst;
183 for (String line : text.split("\n")) {
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");
191 prepend = prependOthers;
192 }
193 }
194 }
195 }