1 package be
.nikiroo
.fanfix
.reader
.cli
;
3 import java
.io
.IOException
;
6 import be
.nikiroo
.fanfix
.Instance
;
7 import be
.nikiroo
.fanfix
.bundles
.StringId
;
8 import be
.nikiroo
.fanfix
.data
.Chapter
;
9 import be
.nikiroo
.fanfix
.data
.MetaData
;
10 import be
.nikiroo
.fanfix
.data
.Paragraph
;
11 import be
.nikiroo
.fanfix
.data
.Story
;
12 import be
.nikiroo
.fanfix
.reader
.BasicReader
;
13 import be
.nikiroo
.fanfix
.searchable
.BasicSearchable
;
14 import be
.nikiroo
.fanfix
.searchable
.SearchableTag
;
15 import be
.nikiroo
.fanfix
.supported
.SupportType
;
16 import be
.nikiroo
.utils
.StringUtils
;
19 * Command line {@link Story} reader.
21 * Will output stories to the console.
25 class CliReader
extends BasicReader
{
27 public void read(boolean sync
) throws IOException
{
28 MetaData meta
= getMeta();
31 throw new IOException("No story to read");
37 if (meta
.getTitle() != null) {
38 title
= meta
.getTitle();
41 if (meta
.getAuthor() != null) {
42 author
= "©" + meta
.getAuthor();
43 if (meta
.getDate() != null && !meta
.getDate().isEmpty()) {
44 author
= author
+ " (" + meta
.getDate() + ")";
48 System
.out
.println(title
);
49 System
.out
.println(author
);
50 System
.out
.println("");
53 for (Chapter chap
: getStory(null)) {
54 if (chap
.getName() != null && !chap
.getName().isEmpty()) {
55 System
.out
.println(Instance
.getTrans().getString(
56 StringId
.CHAPTER_NAMED
, chap
.getNumber(),
59 System
.out
.println(Instance
.getTrans().getString(
60 StringId
.CHAPTER_UNNAMED
, chap
.getNumber()));
65 public void read(int chapter
) throws IOException
{
66 MetaData meta
= getMeta();
69 throw new IOException("No story to read");
73 if (chapter
> getStory(null).getChapters().size()) {
74 System
.err
.println("Chapter " + chapter
+ ": no such chapter");
76 Chapter chap
= getStory(null).getChapters().get(chapter
- 1);
77 System
.out
.println("Chapter " + chap
.getNumber() + ": "
80 for (Paragraph para
: chap
) {
81 System
.out
.println(para
.getContent());
82 System
.out
.println("");
88 public void browse(String source
) {
89 List
<MetaData
> stories
;
90 stories
= getLibrary().getListBySource(source
);
92 for (MetaData story
: stories
) {
94 if (story
.getAuthor() != null && !story
.getAuthor().isEmpty()) {
95 author
= " (" + story
.getAuthor() + ")";
98 System
.out
.println(story
.getLuid() + ": " + story
.getTitle()
104 public void search(boolean sync
) throws IOException
{
105 for (SupportType type
: SupportType
.values()) {
106 if (BasicSearchable
.getSearchable(type
) != null) {
107 System
.out
.println(type
);
113 public void search(SupportType searchOn
, String keywords
, int page
,
114 int item
, boolean sync
) throws IOException
{
115 BasicSearchable search
= BasicSearchable
.getSearchable(searchOn
);
118 System
.out
.println(search
.searchPages(keywords
));
120 List
<MetaData
> metas
= search
.search(keywords
, page
);
123 System
.out
.println("Page " + page
+ " of stories for: "
125 displayStories(metas
);
128 if (item
<= 0 || item
> metas
.size()) {
129 throw new IOException("Index out of bounds: " + item
);
132 MetaData meta
= metas
.get(item
- 1);
139 public void searchTag(SupportType searchOn
, int page
, int item
,
140 boolean sync
, Integer
... tags
) throws IOException
{
142 BasicSearchable search
= BasicSearchable
.getSearchable(searchOn
);
143 SearchableTag stag
= search
.getTag(tags
);
147 System
.out
.println("Known tags: ");
149 for (SearchableTag s
: search
.getTags()) {
150 System
.out
.println(String
.format("%d: %s", i
, s
.getName()));
156 System
.out
.println(search
.searchPages(stag
));
158 System
.out
.println(stag
.getCount());
161 List
<MetaData
> metas
= null;
162 List
<SearchableTag
> subtags
= null;
166 metas
= search
.search(stag
, page
);
167 count
= metas
.size();
169 subtags
= stag
.getChildren();
170 count
= subtags
.size();
176 MetaData meta
= metas
.get(item
- 1);
179 SearchableTag subtag
= subtags
.get(item
- 1);
183 System
.out
.println("Invalid item: only " + count
189 System
.out
.println(String
.format("Content of %s: ",
191 displayStories(metas
);
194 System
.out
.println(String
.format("Subtags of %s: ",
196 displayTags(subtags
);
203 private void displayTag(SearchableTag subtag
) {
205 String stories
= "stories";
206 String num
= StringUtils
.formatNumber(subtag
.getCount());
207 System
.out
.println(String
.format("%s (%s), %s %s", subtag
.getName(),
208 subtag
.getFqName(), num
, stories
));
211 private void displayStory(MetaData meta
) {
212 System
.out
.println(meta
.getTitle());
213 System
.out
.println();
214 System
.out
.println(meta
.getUrl());
215 System
.out
.println();
216 System
.out
.println("Tags: " + meta
.getTags());
217 System
.out
.println();
218 for (Paragraph para
: meta
.getResume()) {
219 System
.out
.println(para
.getContent());
220 System
.out
.println("");
224 private void displayTags(List
<SearchableTag
> subtags
) {
226 for (SearchableTag subtag
: subtags
) {
228 if (subtag
.getCount() > 0) {
229 total
= StringUtils
.formatNumber(subtag
.getCount());
232 if (total
.isEmpty()) {
234 .println(String
.format("%d: %s", i
, subtag
.getName()));
236 System
.out
.println(String
.format("%d: %s (%s)", i
,
237 subtag
.getName(), total
));
244 private void displayStories(List
<MetaData
> metas
) {
246 for (MetaData meta
: metas
) {
247 System
.out
.println(i
+ ": " + meta
.getTitle());