1 package be
.nikiroo
.gofetch
.output
;
5 import be
.nikiroo
.gofetch
.StringJustifier
;
6 import be
.nikiroo
.gofetch
.data
.Comment
;
7 import be
.nikiroo
.gofetch
.data
.Story
;
8 import be
.nikiroo
.gofetch
.support
.BasicSupport
.Type
;
10 public class Gopher
extends Output
{
11 static private final int LINE_SIZE
= 70;
13 private String hostname
;
16 public Gopher(Type type
, String hostname
, int port
) {
19 this.hostname
= hostname
;
24 public String
getIndexHeader() {
25 return "iHello world!\r\niThis is my news site.\r\ni\r\n";
29 public String
getIndexFooter() {
34 public String
export(Story story
) {
35 return append(new StringBuilder(), story
, false).append("i\r\ni\r\n")
40 public String
export(Story story
, List
<Comment
> comments
) {
41 StringBuilder builder
= new StringBuilder();
42 append(builder
, story
, true);
44 builder
.append("i\r\n");
46 for (Comment comment
: comments
) {
47 append(builder
, comment
, "");
50 builder
.append("i\r\n");
52 return builder
.toString();
55 private StringBuilder
append(StringBuilder builder
, Comment comment
,
58 if (space
.length() > LINE_SIZE
- 20) {
59 space
= space
.substring(0, LINE_SIZE
- 20);
62 appendLeft(builder
, comment
.getTitle(), ">> ", " ", space
);
63 appendLeft(builder
, "(" + comment
.getAuthor() + ")", " ", " ",
66 builder
.append("i\r\n");
68 appendLeft(builder
, comment
.getContent(), " ", " ", space
);
70 builder
.append("i\r\n");
71 for (Comment subComment
: comment
) {
72 append(builder
, subComment
, space
+ " ");
73 builder
.append("i\r\n");
79 private StringBuilder
append(StringBuilder builder
, Story story
,
82 appendCenter(builder
, story
.getTitle(), true);
83 builder
.append("i\r\n");
84 appendLeft(builder
, story
.getDetails(), "", "", " ");
85 builder
.append("i\r\n");
86 builder
.append("i o News link: ").append(story
.getUrlInternal());
87 builder
.append("i\r\n");
88 builder
.append("i o Source link: ").append(story
.getUrlExternal());
89 builder
.append("i\r\n");
91 builder
.append('1').append(story
.getTitle()) //
92 .append('\t').append("0").append(story
.getSelector()) //
93 .append('\t').append(hostname
) //
94 .append('\t').append(port
) //
96 appendLeft(builder
, story
.getDetails(), "", "", " ");
99 builder
.append("i\r\n");
101 appendLeft(builder
, story
.getContent(), "", "", " ");
103 builder
.append("i\r\n");
109 private static void appendCenter(StringBuilder builder
, String text
,
112 text
= text
.toUpperCase();
115 for (String line
: StringJustifier
.center(text
, LINE_SIZE
)) {
116 builder
.append("i").append(line
).append("\r\n");
121 private static void appendLeft(StringBuilder builder
, String text
,
122 String prependFirst
, String prependOthers
, String space
) {
123 String prepend
= prependFirst
;
124 for (String line
: StringJustifier
.left(text
,
125 LINE_SIZE
- space
.length())) {
126 builder
.append("i").append(space
).append(prepend
).append(line
)
128 prepend
= prependOthers
;