f0c6f6dad69d90d2dd09d20baad1b4fec82fca67
1 package be
.nikiroo
.gofetch
.output
;
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
;
9 public class Gopher
extends Output
{
10 static private final int LINE_SIZE
= 67;
12 public Gopher(Type type
, String hostname
, String preselector
, int port
) {
13 super(type
, hostname
, preselector
, port
);
17 public String
getIndexHeader() {
18 StringBuilder builder
= new StringBuilder();
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, "", "");
28 "They are simply scrapped from their associated webpage and converted into a gopher friendly format, updated a few times a day.",
30 appendLeft(builder
, true, "", "");
32 return builder
.toString();
36 public String
getIndexFooter() {
41 public String
exportHeader(Story story
) {
42 return append(new StringBuilder(), story
, true).toString();
46 public String
export(Story story
) {
47 StringBuilder builder
= new StringBuilder();
48 append(builder
, story
, false);
50 builder
.append("\r\n");
52 if (story
.getComments() != null) {
53 for (Comment comment
: story
.getComments()) {
54 append(builder
, false, comment
, " ");
58 builder
.append("\r\n");
60 return builder
.toString();
63 private StringBuilder
append(StringBuilder builder
, boolean menu
,
64 Comment comment
, String space
) {
66 if (space
.length() > LINE_SIZE
- 20) {
67 space
= space
.substring(0, LINE_SIZE
- 20);
70 appendLeft(builder
, menu
, comment
.getTitle(), "** ", " ", space
);
72 if (comment
.getAuthor() != null
73 && !comment
.getAuthor().trim().isEmpty()) {
74 appendLeft(builder
, menu
, "(" + comment
.getAuthor() + ")", " ",
76 builder
.append((menu ?
"i" : "") + "\r\n");
79 for (String line
: comment
.getContentLines()) {
81 while (line
.length() > depth
&& line
.charAt(depth
) == '>') {
84 line
= line
.substring(depth
).trim();
87 for (int i
= 0; i
< depth
; i
++) {
95 appendLeft(builder
, menu
, line
, prep
, prep
, space
);
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");
107 private StringBuilder
append(StringBuilder builder
, Story story
,
110 appendCenter(builder
, false, story
.getTitle(), " ", true);
111 builder
.append("\r\n");
112 appendJustified(builder
, false, story
.getDetails(), " ");
113 builder
.append("\r\n");
115 builder
.append(" o News link: ").append(story
.getUrlInternal())
117 builder
.append(" o Source link: ").append(story
.getUrlExternal())
119 builder
.append("\r\n");
121 builder
.append("\r\n");
123 appendJustified(builder
, false, story
.getFullContent(), " ");
124 builder
.append("\r\n");
126 builder
.append('0').append(story
.getTitle()) //
127 .append('\t').append(story
.getSelector()) //
128 .append('\t').append(hostname
) //
129 .append('\t').append(port
) //
131 appendJustified(builder
, true, story
.getDetails(), " ");
132 builder
.append("i\r\n");
134 String content
= story
.getContent();
135 if (!content
.isEmpty()) {
136 appendJustified(builder
, true, content
, " ");
137 builder
.append("i\r\n");
145 private static void appendCenter(StringBuilder builder
, boolean menu
,
146 String text
, String space
, boolean allCaps
) {
148 text
= text
.toUpperCase();
151 int size
= LINE_SIZE
- space
.length();
152 for (String line
: StringUtils
153 .justifyText(text
, size
, Alignment
.CENTER
)) {
154 builder
.append(menu ?
"i" : "") //
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" : "") //
175 private static void appendLeft(StringBuilder builder
, boolean menu
,
176 String text
, String space
) {
177 appendLeft(builder
, menu
, text
, "", "", space
);
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" : "") //
191 prepend
= prependOthers
;