dd5e3482f3c18b487097b595709cc211b3f8082b
1 package be
.nikiroo
.gofetch
.output
;
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
;
8 public class Gopher
extends Output
{
9 static private final int LINE_SIZE
= 67;
11 public Gopher(Type type
, String hostname
, String preselector
, int port
) {
12 super(type
, hostname
, preselector
, port
);
16 public String
getIndexHeader() {
17 StringBuilder builder
= new StringBuilder();
19 appendCenter(builder
, "NEWS", true);
20 appendLeft(builder
, "", "");
21 appendLeft(builder
, "You will find here a few pages full of news.", "");
22 appendLeft(builder
, "", "");
25 "They are simply scrapped from their associated webpage and converted into a gopher friendly format, updated a few times a day.",
27 appendLeft(builder
, "", "");
29 return builder
.toString();
33 public String
getIndexFooter() {
38 public String
exportHeader(Story story
) {
39 return append(new StringBuilder(), story
, true).append("i\r\ni\r\n")
44 public String
export(Story story
) {
45 StringBuilder builder
= new StringBuilder();
46 append(builder
, story
, false);
48 builder
.append("i\r\n");
50 if (story
.getComments() != null) {
51 for (Comment comment
: story
.getComments()) {
52 append(builder
, comment
, "");
56 builder
.append("i\r\n");
58 return builder
.toString();
61 private StringBuilder
append(StringBuilder builder
, Comment comment
,
64 if (space
.length() > LINE_SIZE
- 20) {
65 space
= space
.substring(0, LINE_SIZE
- 20);
68 appendLeft(builder
, comment
.getTitle(), ">> ", " ", space
);
69 appendLeft(builder
, "(" + comment
.getAuthor() + ")", " ", " ",
72 builder
.append("i\r\n");
74 appendLeft(builder
, comment
.getContent(), " ", " ", space
);
76 builder
.append("i\r\n");
77 for (Comment subComment
: comment
) {
78 append(builder
, subComment
, space
+ " ");
79 builder
.append("i\r\n");
85 private StringBuilder
append(StringBuilder builder
, Story story
,
88 appendCenter(builder
, story
.getTitle(), true);
89 builder
.append("i\r\n");
90 appendJustified(builder
, story
.getDetails(), " ");
91 builder
.append("i\r\n");
93 builder
.append("i o News link: ").append(story
.getUrlInternal())
95 builder
.append("i o Source link: ").append(story
.getUrlExternal())
97 builder
.append("i\r\n");
99 builder
.append("i\r\n");
101 appendJustified(builder
, story
.getFullContent(), " ");
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
) //
108 appendJustified(builder
, story
.getDetails(), " ");
109 builder
.append("i\r\n");
111 appendJustified(builder
, story
.getContent(), " ");
114 builder
.append("i\r\n");
120 private static void appendCenter(StringBuilder builder
, String text
,
123 text
= text
.toUpperCase();
126 for (String line
: StringJustifier
.center(text
, LINE_SIZE
)) {
127 builder
.append("i").append(line
).append("\r\n");
132 private static void appendJustified(StringBuilder builder
, String text
,
134 for (String line
: StringJustifier
.full(text
,
135 LINE_SIZE
- space
.length())) {
136 builder
.append("i").append(line
).append("\r\n");
141 private static void appendLeft(StringBuilder builder
, String text
,
143 appendLeft(builder
, text
, "", "", space
);
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
)
154 prepend
= prependOthers
;