dd5e3482f3c18b487097b595709cc211b3f8082b
[gofetch.git] / src / be / nikiroo / gofetch / output / Gopher.java
1 package be.nikiroo.gofetch.output;
2
3 import be.nikiroo.gofetch.StringJustifier;
4 import be.nikiroo.gofetch.data.Comment;
5 import be.nikiroo.gofetch.data.Story;
6 import be.nikiroo.gofetch.support.BasicSupport.Type;
7
8 public class Gopher extends Output {
9 static private final int LINE_SIZE = 67;
10
11 public Gopher(Type type, String hostname, String preselector, int port) {
12 super(type, hostname, preselector, port);
13 }
14
15 @Override
16 public String getIndexHeader() {
17 StringBuilder builder = new StringBuilder();
18
19 appendCenter(builder, "NEWS", true);
20 appendLeft(builder, "", "");
21 appendLeft(builder, "You will find here a few pages full of news.", "");
22 appendLeft(builder, "", "");
23 appendLeft(
24 builder,
25 "They are simply scrapped from their associated webpage and converted into a gopher friendly format, updated a few times a day.",
26 "");
27 appendLeft(builder, "", "");
28
29 return builder.toString();
30 }
31
32 @Override
33 public String getIndexFooter() {
34 return "";
35 }
36
37 @Override
38 public String exportHeader(Story story) {
39 return append(new StringBuilder(), story, true).append("i\r\ni\r\n")
40 .toString();
41 }
42
43 @Override
44 public String export(Story story) {
45 StringBuilder builder = new StringBuilder();
46 append(builder, story, false);
47
48 builder.append("i\r\n");
49
50 if (story.getComments() != null) {
51 for (Comment comment : story.getComments()) {
52 append(builder, comment, "");
53 }
54 }
55
56 builder.append("i\r\n");
57
58 return builder.toString();
59 }
60
61 private StringBuilder append(StringBuilder builder, Comment comment,
62 String space) {
63
64 if (space.length() > LINE_SIZE - 20) {
65 space = space.substring(0, LINE_SIZE - 20);
66 }
67
68 appendLeft(builder, comment.getTitle(), ">> ", " ", space);
69 appendLeft(builder, "(" + comment.getAuthor() + ")", " ", " ",
70 space);
71
72 builder.append("i\r\n");
73
74 appendLeft(builder, comment.getContent(), " ", " ", space);
75
76 builder.append("i\r\n");
77 for (Comment subComment : comment) {
78 append(builder, subComment, space + " ");
79 builder.append("i\r\n");
80 }
81
82 return builder;
83 }
84
85 private StringBuilder append(StringBuilder builder, Story story,
86 boolean resume) {
87 if (!resume) {
88 appendCenter(builder, story.getTitle(), true);
89 builder.append("i\r\n");
90 appendJustified(builder, story.getDetails(), " ");
91 builder.append("i\r\n");
92
93 builder.append("i o News link: ").append(story.getUrlInternal())
94 .append("\r\n");
95 builder.append("i o Source link: ").append(story.getUrlExternal())
96 .append("\r\n");
97 builder.append("i\r\n");
98
99 builder.append("i\r\n");
100
101 appendJustified(builder, story.getFullContent(), " ");
102 } else {
103 builder.append('1').append(story.getTitle()) //
104 .append('\t').append("0").append(story.getSelector()) //
105 .append('\t').append(hostname) //
106 .append('\t').append(port) //
107 .append("\r\n");
108 appendJustified(builder, story.getDetails(), " ");
109 builder.append("i\r\n");
110
111 appendJustified(builder, story.getContent(), " ");
112 }
113
114 builder.append("i\r\n");
115
116 return builder;
117 }
118
119 // note: adds "i"
120 private static void appendCenter(StringBuilder builder, String text,
121 boolean allCaps) {
122 if (allCaps) {
123 text = text.toUpperCase();
124 }
125
126 for (String line : StringJustifier.center(text, LINE_SIZE)) {
127 builder.append("i").append(line).append("\r\n");
128 }
129 }
130
131 // note: adds "i"
132 private static void appendJustified(StringBuilder builder, String text,
133 String space) {
134 for (String line : StringJustifier.full(text,
135 LINE_SIZE - space.length())) {
136 builder.append("i").append(line).append("\r\n");
137 }
138 }
139
140 // note: adds "i"
141 private static void appendLeft(StringBuilder builder, String text,
142 String space) {
143 appendLeft(builder, text, "", "", space);
144 }
145
146 // note: adds "i"
147 private static void appendLeft(StringBuilder builder, String text,
148 String prependFirst, String prependOthers, String space) {
149 String prepend = prependFirst;
150 for (String line : StringJustifier.left(text,
151 LINE_SIZE - space.length())) {
152 builder.append("i").append(space).append(prepend).append(line)
153 .append("\r\n");
154 prepend = prependOthers;
155 }
156 }
157 }