1 package be
.nikiroo
.fanfix
.reader
;
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
.searchable
.BasicSearchable
;
13 import be
.nikiroo
.fanfix
.searchable
.SearchableTag
;
14 import be
.nikiroo
.fanfix
.supported
.SupportType
;
15 import be
.nikiroo
.utils
.Image
;
16 import be
.nikiroo
.utils
.StringUtils
;
19 * Command line {@link Story} reader.
21 * Will output stories to the console.
25 public class CliReader
extends BasicReader
{
26 public void listBooks(String source
) throws IOException
{
27 List
<MetaData
> stories
= Instance
.getInstance().getLibrary().getList()
28 .filter(source
, null, null);
30 for (MetaData story
: stories
) {
32 if (story
.getAuthor() != null && !story
.getAuthor().isEmpty()) {
33 author
= " (" + story
.getAuthor() + ")";
37 story
.getLuid() + ": " + story
.getTitle() + author
);
41 public void listChapters(Story story
) throws IOException
{
42 if (story
== null || story
.getMeta() == null) {
43 throw new IOException("No story to read");
45 MetaData meta
= story
.getMeta();
47 throw new IOException("No story to read");
53 if (meta
.getTitle() != null) {
54 title
= meta
.getTitle();
57 if (meta
.getAuthor() != null) {
58 author
= "©" + meta
.getAuthor();
59 if (meta
.getDate() != null && !meta
.getDate().isEmpty()) {
60 author
= author
+ " (" + meta
.getDate() + ")";
64 System
.out
.println(title
);
65 System
.out
.println(author
);
66 System
.out
.println("");
68 for (Chapter chap
: story
) {
69 if (chap
.getName() != null && !chap
.getName().isEmpty()) {
70 System
.out
.println(Instance
.getInstance().getTrans().getString(
71 StringId
.CHAPTER_NAMED
, chap
.getNumber(),
74 System
.out
.println(Instance
.getInstance().getTrans()
75 .getString(StringId
.CHAPTER_UNNAMED
, chap
.getNumber()));
80 public void printChapter(Story story
, int chapter
) throws IOException
{
81 if (story
== null || story
.getMeta() == null) {
82 throw new IOException("No story to read");
84 MetaData meta
= story
.getMeta();
86 throw new IOException("No story to read");
89 if (chapter
<= 0 || chapter
> story
.getChapters().size()) {
90 System
.err
.println("Chapter " + chapter
+ ": no such chapter");
92 Chapter chap
= story
.getChapters().get(chapter
- 1);
94 "Chapter " + chap
.getNumber() + ": " + chap
.getName());
97 for (Paragraph para
: chap
) {
98 Image img
= para
.getContentImage();
100 String sz
= StringUtils
.formatNumber(img
.getSize(), 1);
101 System
.out
.println("[Image: " + sz
+ "]");
104 para
.getContent() == null ?
"" : para
.getContent());
106 System
.out
.println("");
111 public void listSearchables() throws IOException
{
112 for (SupportType type
: SupportType
.values()) {
113 if (BasicSearchable
.getSearchable(type
) != null) {
114 System
.out
.println(type
);
119 public void searchBooksByKeyword(SupportType searchOn
, String keywords
,
120 int page
, int item
) throws IOException
{
121 BasicSearchable search
= BasicSearchable
.getSearchable(searchOn
);
124 System
.out
.println(search
.searchPages(keywords
));
126 List
<MetaData
> metas
= search
.search(keywords
, page
);
130 "Page " + page
+ " of stories for: " + keywords
);
131 displayStories(metas
);
134 if (item
<= 0 || item
> metas
.size()) {
135 throw new IOException("Index out of bounds: " + item
);
138 MetaData meta
= metas
.get(item
- 1);
144 public void searchBooksByTag(SupportType searchOn
, int page
, int item
,
145 Integer
... tags
) throws IOException
{
147 BasicSearchable search
= BasicSearchable
.getSearchable(searchOn
);
148 SearchableTag stag
= search
.getTag(tags
);
152 System
.out
.println("Known tags: ");
154 for (SearchableTag s
: search
.getTags()) {
155 System
.out
.println(String
.format("%d: %s", i
, s
.getName()));
161 System
.out
.println(search
.searchPages(stag
));
163 System
.out
.println(stag
.getCount());
166 List
<MetaData
> metas
= null;
167 List
<SearchableTag
> subtags
= null;
171 metas
= search
.search(stag
, page
);
172 count
= metas
.size();
174 subtags
= stag
.getChildren();
175 count
= subtags
.size();
181 MetaData meta
= metas
.get(item
- 1);
184 SearchableTag subtag
= subtags
.get(item
- 1);
189 "Invalid item: only " + count
+ " items found");
194 System
.out
.println(String
.format("Content of %s: ",
196 displayStories(metas
);
199 System
.out
.println(String
.format("Subtags of %s: ",
201 displayTags(subtags
);
208 private void displayTag(SearchableTag subtag
) {
210 String stories
= "stories";
211 String num
= StringUtils
.formatNumber(subtag
.getCount());
212 System
.out
.println(String
.format("%s (%s), %s %s", subtag
.getName(),
213 subtag
.getFqName(), num
, stories
));
216 private void displayStory(MetaData meta
) {
217 System
.out
.println(meta
.getTitle());
218 System
.out
.println();
219 System
.out
.println(meta
.getUrl());
220 System
.out
.println();
221 System
.out
.println("Tags: " + meta
.getTags());
222 System
.out
.println();
223 for (Paragraph para
: meta
.getResume()) {
224 System
.out
.println(para
.getContent());
225 System
.out
.println("");
229 private void displayTags(List
<SearchableTag
> subtags
) {
231 for (SearchableTag subtag
: subtags
) {
233 if (subtag
.getCount() > 0) {
234 total
= StringUtils
.formatNumber(subtag
.getCount());
237 if (total
.isEmpty()) {
239 .println(String
.format("%d: %s", i
, subtag
.getName()));
241 System
.out
.println(String
.format("%d: %s (%s)", i
,
242 subtag
.getName(), total
));
249 private void displayStories(List
<MetaData
> metas
) {
251 for (MetaData meta
: metas
) {
252 System
.out
.println(i
+ ": " + meta
.getTitle());