Use String justification from nikiroo-utils
[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 } else {
122 builder.append('0').append(story.getTitle()) //
123 .append('\t').append(story.getSelector()) //
124 .append('\t').append(hostname) //
125 .append('\t').append(port) //
126 .append("\r\n");
127 appendJustified(builder, true, story.getDetails(), " ");
128 builder.append("i\r\n");
129
130 appendJustified(builder, true, story.getContent(), " ");
131 }
132
133 builder.append(resume ? "i" : "").append("\r\n");
134
135 return builder;
136 }
137
138 // note: adds "i"
139 private static void appendCenter(StringBuilder builder, boolean menu,
140 String text, String space, boolean allCaps) {
141 if (allCaps) {
142 text = text.toUpperCase();
143 }
144
145 int size = LINE_SIZE - space.length();
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");
152 }
153 }
154
155 private static void appendJustified(StringBuilder builder, boolean menu,
156 String text, String space) {
157 for (String line : text.split("\n")) {
158 int size = LINE_SIZE - space.length();
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");
165 }
166 }
167 }
168
169 private static void appendLeft(StringBuilder builder, boolean menu,
170 String text, String space) {
171 appendLeft(builder, menu, text, "", "", space);
172 }
173
174 private static void appendLeft(StringBuilder builder, boolean menu,
175 String text, String prependFirst, String prependOthers, String space) {
176 String prepend = prependFirst;
177 for (String line : text.split("\n")) {
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");
185 prepend = prependOthers;
186 }
187 }
188 }
189 }