1 package be
.nikiroo
.fanfix
.reader
.cli
;
3 import java
.io
.IOException
;
4 import java
.util
.ArrayList
;
7 import be
.nikiroo
.fanfix
.Instance
;
8 import be
.nikiroo
.fanfix
.bundles
.StringId
;
9 import be
.nikiroo
.fanfix
.data
.Chapter
;
10 import be
.nikiroo
.fanfix
.data
.MetaData
;
11 import be
.nikiroo
.fanfix
.data
.Paragraph
;
12 import be
.nikiroo
.fanfix
.data
.Story
;
13 import be
.nikiroo
.fanfix
.reader
.BasicReader
;
14 import be
.nikiroo
.fanfix
.searchable
.BasicSearchable
;
15 import be
.nikiroo
.fanfix
.searchable
.SearchableTag
;
16 import be
.nikiroo
.fanfix
.supported
.SupportType
;
17 import be
.nikiroo
.utils
.StringUtils
;
20 * Command line {@link Story} reader.
22 * Will output stories to the console.
26 class CliReader
extends BasicReader
{
28 public void read(boolean sync
) throws IOException
{
29 MetaData meta
= getMeta();
32 throw new IOException("No story to read");
38 if (meta
.getTitle() != null) {
39 title
= meta
.getTitle();
42 if (meta
.getAuthor() != null) {
43 author
= "©" + meta
.getAuthor();
44 if (meta
.getDate() != null && !meta
.getDate().isEmpty()) {
45 author
= author
+ " (" + meta
.getDate() + ")";
49 System
.out
.println(title
);
50 System
.out
.println(author
);
51 System
.out
.println("");
54 for (Chapter chap
: getStory(null)) {
55 if (chap
.getName() != null && !chap
.getName().isEmpty()) {
56 System
.out
.println(Instance
.getTrans().getString(
57 StringId
.CHAPTER_NAMED
, chap
.getNumber(),
60 System
.out
.println(Instance
.getTrans().getString(
61 StringId
.CHAPTER_UNNAMED
, chap
.getNumber()));
66 public void read(int chapter
) throws IOException
{
67 MetaData meta
= getMeta();
70 throw new IOException("No story to read");
74 if (chapter
> getStory(null).getChapters().size()) {
75 System
.err
.println("Chapter " + chapter
+ ": no such chapter");
77 Chapter chap
= getStory(null).getChapters().get(chapter
- 1);
78 System
.out
.println("Chapter " + chap
.getNumber() + ": "
81 for (Paragraph para
: chap
) {
82 System
.out
.println(para
.getContent());
83 System
.out
.println("");
89 public void browse(String source
) {
90 List
<MetaData
> stories
;
91 stories
= getLibrary().getListBySource(source
);
93 for (MetaData story
: stories
) {
95 if (story
.getAuthor() != null && !story
.getAuthor().isEmpty()) {
96 author
= " (" + story
.getAuthor() + ")";
99 System
.out
.println(story
.getLuid() + ": " + story
.getTitle()
105 public void search(SupportType searchOn
, String keywords
, int page
,
106 int item
, boolean sync
) throws IOException
{
111 public void searchTag(SupportType searchOn
, int page
, int item
,
112 boolean sync
, Integer
... tags
) throws IOException
{
113 BasicSearchable search
= BasicSearchable
.getSearchable(searchOn
);
114 List
<SearchableTag
> stags
= search
.getTags();
117 SearchableTag stag
= null;
118 for (Integer tagIndex
: tags
) {
120 if (tagIndex
== null || tagIndex
<= 0 | tagIndex
> stags
.size()) {
121 throw new IOException("Index out of bounds: " + tagIndex
);
124 stag
= stags
.get(tagIndex
- 1);
126 search
.fillTag(stag
);
127 stags
= stag
.getChildren();
128 if (!fqnTag
.isEmpty()) {
131 fqnTag
+= stag
.getName();
133 stags
= new ArrayList
<SearchableTag
>();
141 search
.search(stag
, 1);
142 System
.out
.println(stag
.getPages());
144 System
.out
.println(stag
.getCount());
147 List
<MetaData
> metas
= null;
148 List
<SearchableTag
> subtags
= null;
152 metas
= search
.search(stag
, page
);
153 count
= metas
.size();
155 subtags
= stag
.getChildren();
156 count
= subtags
.size();
162 MetaData meta
= metas
.get(item
- 1);
163 System
.out
.println(page
+ "/" + item
+ ": "
165 System
.out
.println();
166 System
.out
.println(meta
.getUrl());
167 System
.out
.println();
168 System
.out
.println("Tags: " + meta
.getTags());
169 System
.out
.println();
170 for (Paragraph para
: meta
.getResume()) {
171 System
.out
.println(para
.getContent());
172 System
.out
.println("");
175 SearchableTag subtag
= subtags
.get(item
- 1);
178 String stories
= "stories";
179 String num
= StringUtils
.formatNumber(subtag
181 System
.out
.println(String
.format("%s (%s), %s %s",
182 subtag
.getName(), fqnTag
, num
, stories
));
185 System
.out
.println("Invalid item: only " + count
191 System
.out
.println(String
.format("Content of %s: ",
194 for (MetaData meta
: metas
) {
195 System
.out
.println(i
+ ": " + meta
.getTitle());
200 System
.out
.println(String
.format("Subtags of %s: ",
203 for (SearchableTag subtag
: subtags
) {
205 if (subtag
.getCount() > 0) {
206 total
= StringUtils
.formatNumber(subtag
210 if (total
.isEmpty()) {
211 System
.out
.println(String
.format("%d: %s", i
,
214 System
.out
.println(String
.format("%d: %s (%s)",
215 i
, subtag
.getName(), total
));
225 System
.out
.println("Known tags: ");
227 for (SearchableTag s
: stags
) {
228 System
.out
.println(String
.format("%d: %s", i
, s
.getName()));