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
{
107 BasicSearchable search
= BasicSearchable
.getSearchable(searchOn
);
110 System
.out
.println(search
.searchPages(keywords
));
112 List
<MetaData
> metas
= search
.search(keywords
, page
);
115 System
.out
.println("Page " + page
+ " of stories for: "
117 displayStories(metas
);
120 if (item
<= 0 | item
> metas
.size()) {
121 throw new IOException("Index out of bounds: " + item
);
124 MetaData meta
= metas
.get(item
- 1);
131 public void searchTag(SupportType searchOn
, int page
, int item
,
132 boolean sync
, Integer
... tags
) throws IOException
{
133 BasicSearchable search
= BasicSearchable
.getSearchable(searchOn
);
134 List
<SearchableTag
> stags
= search
.getTags();
137 SearchableTag stag
= null;
138 for (Integer tagIndex
: tags
) {
140 if (tagIndex
== null || tagIndex
<= 0 | tagIndex
> stags
.size()) {
141 throw new IOException("Index out of bounds: " + tagIndex
);
144 stag
= stags
.get(tagIndex
- 1);
146 search
.fillTag(stag
);
147 stags
= stag
.getChildren();
148 if (!fqnTag
.isEmpty()) {
151 fqnTag
+= stag
.getName();
153 stags
= new ArrayList
<SearchableTag
>();
161 search
.search(stag
, 1);
162 System
.out
.println(stag
.getPages());
164 System
.out
.println(stag
.getCount());
167 List
<MetaData
> metas
= null;
168 List
<SearchableTag
> subtags
= null;
172 metas
= search
.search(stag
, page
);
173 count
= metas
.size();
175 subtags
= stag
.getChildren();
176 count
= subtags
.size();
182 MetaData meta
= metas
.get(item
- 1);
185 SearchableTag subtag
= subtags
.get(item
- 1);
188 String stories
= "stories";
189 String num
= StringUtils
.formatNumber(subtag
191 System
.out
.println(String
.format("%s (%s), %s %s",
192 subtag
.getName(), fqnTag
, num
, stories
));
195 System
.out
.println("Invalid item: only " + count
201 System
.out
.println(String
.format("Content of %s: ",
203 displayStories(metas
);
206 System
.out
.println(String
.format("Subtags of %s: ",
209 for (SearchableTag subtag
: subtags
) {
211 if (subtag
.getCount() > 0) {
212 total
= StringUtils
.formatNumber(subtag
216 if (total
.isEmpty()) {
217 System
.out
.println(String
.format("%d: %s", i
,
220 System
.out
.println(String
.format("%d: %s (%s)",
221 i
, subtag
.getName(), total
));
231 System
.out
.println("Known tags: ");
233 for (SearchableTag s
: stags
) {
234 System
.out
.println(String
.format("%d: %s", i
, s
.getName()));
240 private void displayStory(MetaData meta
) {
241 System
.out
.println(meta
.getTitle());
242 System
.out
.println();
243 System
.out
.println(meta
.getUrl());
244 System
.out
.println();
245 System
.out
.println("Tags: " + meta
.getTags());
246 System
.out
.println();
247 for (Paragraph para
: meta
.getResume()) {
248 System
.out
.println(para
.getContent());
249 System
.out
.println("");
253 private void displayStories(List
<MetaData
> metas
) {
255 for (MetaData meta
: metas
) {
256 System
.out
.println(i
+ ": " + meta
.getTitle());