X-Git-Url: http://git.nikiroo.be/?p=fanfix.git;a=blobdiff_plain;f=library%2FWebLibraryServerHtml.java;h=69d8671736d7e2e4cf8c2922ca61358803f6bd2f;hp=8895fb83a07b0eb6ff7116b3998a7a89a2acfb15;hb=e992c260c059c53c4aabc980db85efd58f190205;hpb=002972e9de731678035d56304d75a6d9e8233635 diff --git a/library/WebLibraryServerHtml.java b/library/WebLibraryServerHtml.java index 8895fb8..69d8671 100644 --- a/library/WebLibraryServerHtml.java +++ b/library/WebLibraryServerHtml.java @@ -22,6 +22,7 @@ import be.nikiroo.fanfix.data.Paragraph.ParagraphType; import be.nikiroo.fanfix.data.Story; import be.nikiroo.fanfix.library.WebLibraryServer.WLoginResult; import be.nikiroo.fanfix.library.web.WebLibraryServerIndex; +import be.nikiroo.fanfix.library.web.templates.WebLibraryServerTemplates; import be.nikiroo.fanfix.reader.TextOutput; import be.nikiroo.utils.IOUtils; import be.nikiroo.utils.NanoHTTPD; @@ -173,13 +174,13 @@ abstract class WebLibraryServerHtml implements Runnable { } Response rep = null; - if (!login.isSuccess() - && WebLibraryUrls.isSupportedUrl(uri, true)) { - rep = loginPage(login, uri); - } + try { + if (!login.isSuccess() + && WebLibraryUrls.isSupportedUrl(uri, true)) { + rep = loginPage(login, uri); + } - if (rep == null) { - try { + if (rep == null) { if (WebLibraryUrls.isSupportedUrl(uri, false)) { if (WebLibraryUrls.INDEX_URL.equals(uri)) { rep = root(session, cookies, login); @@ -253,13 +254,12 @@ abstract class WebLibraryServerHtml implements Runnable { NanoHTTPD.MIME_PLAINTEXT, "Not Found"); } } - } catch (Exception e) { - Instance.getInstance().getTraceHandler().error( - new IOException("Cannot process web request", - e)); - rep = newFixedLengthResponse(Status.INTERNAL_ERROR, - NanoHTTPD.MIME_PLAINTEXT, "An error occured"); } + } catch (Exception e) { + Instance.getInstance().getTraceHandler().error( + new IOException("Cannot process web request", e)); + rep = newFixedLengthResponse(Status.INTERNAL_ERROR, + NanoHTTPD.MIME_PLAINTEXT, "An error occured"); } return rep; @@ -309,31 +309,34 @@ abstract class WebLibraryServerHtml implements Runnable { this.tracer = tracer; } - private Response loginPage(WLoginResult login, String uri) { + private Response loginPage(WLoginResult login, String uri) + throws IOException { StringBuilder builder = new StringBuilder(); - appendPreHtml(builder, true); + builder.append(getTemplateIndexPreBanner(true)); if (login.isBadLogin()) { - builder.append("
Bad login or password
"); + builder.append( + "\t\t
Bad login or password
"); } else if (login.isBadCookie()) { - builder.append("
Your session timed out
"); + builder.append( + "\t\t
Your session timed out
"); } if (WebLibraryUrls.LOGOUT_URL.equals(uri)) { uri = WebLibraryUrls.INDEX_URL; } + builder.append("\t\t
\n"); builder.append( - "\n"); - builder.append( - "

You must be logged into the system to see the stories.

"); - builder.append("\t\n"); - builder.append("\t\n"); - builder.append("\t\n"); - builder.append("
\n"); + "\t\t\t

You must be logged into the system to see the stories.

"); + builder.append("\t\t\t\n"); + builder.append("\t\t\t\n"); + builder.append("\t\t\t\n"); + builder.append("\t\t\n"); - appendPostHtml(builder); + builder.append(getTemplate("index.post")); return NanoHTTPD.newFixedLengthResponse(Status.FORBIDDEN, NanoHTTPD.MIME_HTML, builder.toString()); @@ -345,7 +348,7 @@ abstract class WebLibraryServerHtml implements Runnable { MetaResultList result = new MetaResultList(metas(login)); StringBuilder builder = new StringBuilder(); - appendPreHtml(builder, true); + builder.append(getTemplateIndexPreBanner(true)); Map params = session.getParms(); @@ -369,45 +372,56 @@ abstract class WebLibraryServerHtml implements Runnable { // TODO: javascript in realtime, using visible=false + hide [submit] - builder.append("
\n"); - builder.append("\n"); + String sel = "selected='selected'"; + builder.append(getTemplate("browser") // + .replace("${sourcesSelected}", sourcesSel ? sel : "") // + .replace("${authorsSelected}", authorsSel ? sel : "") // + .replace("${tagsSelected}", tagsSel ? sel : "") // + .replace("${filter}", filter) // + .replace("${selects}", selects.toString()) // + ); - // TODO: javascript in realtime, using visible=false + hide [submit] - builder.append("
\n"); - builder.append("\tFilter: \n"); - builder.append( - "\t\n"); - builder.append("\t\n"); - builder.append("\t"); - builder.append( - "\t\n"); - builder.append("
\n"); - builder.append("
\n"); - - builder.append("\t
"); + builder.append("\t\t
\n"); for (MetaData meta : result.getMetas()) { if (!filter.isEmpty() && !meta.getTitle().toLowerCase() .contains(filter.toLowerCase())) { @@ -478,38 +487,33 @@ abstract class WebLibraryServerHtml implements Runnable { continue; } - builder.append("\n"); + + builder.append(getTemplate("bookline") // + .replace("${href}", + WebLibraryUrls.getViewUrl(meta.getLuid(), null, + null)) // + .replace("${luid}", meta.getLuid()) // + .replace("${title}", meta.getTitle()) // + .replace("${author}", author) // + .replace("${cachedClass}", cachedClass) // + .replace("${cached}", cached) // + ); } - builder.append("
"); + builder.append("\t\t
\n"); + + builder.append(getTemplate("index.post")); - appendPostHtml(builder); return NanoHTTPD.newFixedLengthResponse(builder.toString()); } @@ -563,9 +567,6 @@ abstract class WebLibraryServerHtml implements Runnable { NanoHTTPD.MIME_PLAINTEXT, "Story not found"); } - StringBuilder builder = new StringBuilder(); - appendPreHtml(builder, false); - // For images documents, always go to the images if not chap 0 desc if (story.getMeta().isImageDocument()) { if (chapter > 0 && paragraph <= 0) @@ -586,7 +587,7 @@ abstract class WebLibraryServerHtml implements Runnable { String first, previous, next, last; - StringBuilder content = new StringBuilder(); + String viewer = ""; String disabledLeft = ""; String disabledRight = ""; @@ -604,35 +605,37 @@ abstract class WebLibraryServerHtml implements Runnable { last = WebLibraryUrls.getViewUrl(luid, story.getChapters().size(), null); - StringBuilder desc = new StringBuilder(); - + String desc = ""; if (chapter <= 0) { - desc.append("

"); - desc.append(story.getMeta().getTitle()); - desc.append("

\n"); - desc.append("
\n"); - desc.append("\t\n"); - desc.append("\t\t\n"); - desc.append("\t\n"); - desc.append("\t\n"); + StringBuilder desclines = new StringBuilder(); Map details = BasicLibrary .getMetaDesc(story.getMeta()); for (String key : details.keySet()) { - appendTableRow(desc, 2, key, details.get(key)); + desclines.append(getTemplate("viewer.descline") // + .replace("${key}", key) // + .replace("${value}", details.get(key)) // + ); } - desc.append("\t
\n"); - desc.append("
\n"); - desc.append("

Description

\n"); + + desc = getTemplate("viewer.desc") // + .replace("${title}", story.getMeta().getTitle()) // + .replace("${href}", next) // + .replace("${cover}", + WebLibraryUrls.getStoryUrlCover(luid)) // + .replace("${details}", desclines.toString()) // + ; } - content.append("
\n"); - content.append(desc); - String description = new TextOutput(false).convert(chap, - chapter > 0); - content.append(chap.getParagraphs().size() <= 0 - ? "No content provided." - : description); - content.append("
\n"); + viewer = getTemplate("viewer.text") // + .replace("${desc}", desc) // + ; + if (chap.getParagraphs().size() <= 0) { + viewer = viewer.replace("${content}", + "No content provided."); + } else { + viewer = viewer.replace("${content}", + new TextOutput(false).convert(chap, chapter > 0)); + } if (chapter <= 0) disabledLeft = " disabled='disbaled'"; @@ -689,44 +692,39 @@ abstract class WebLibraryServerHtml implements Runnable { } } - String javascript = "document.getElementById(\"previous\").click(); return false;"; - content.append(String.format("" // - + "" - + "" - + "", // - javascript, // - next, // - zoomStyle, // - WebLibraryUrls.getStoryUrl(luid, chapter, - paragraph))); + viewer = getTemplate("viewer.image") // + .replace("${href}", next) // + .replace("${zoomStyle}", zoomStyle) // + .replace("${src}", WebLibraryUrls.getStoryUrl(luid, + chapter, paragraph)) // + ; } else { - content.append(String.format("" // - + "
%s
", // - para.getContent())); + viewer = getTemplate("viewer.text") // + .replace("${desc}", "") // + .replace("${content}", + new TextOutput(false).convert(para)) // + ; } } - builder.append(String.format("" // - + "\n", // - disabledRight, next, // - disabledRight, last // - )); - - builder.append(content); + // Buttons on the optionbar - builder.append("
\n"); - builder.append(" BACK\n"); - if (paragraph > 0) { - builder.append(String.format("" // - + "\tREAL\n"// - + "\tWIDTH\n"// - + "\tHEIGHT\n"// - + "
\n", // - disabledZoomReal, - uri + "?optionName=zoom&optionValue=real", // - disabledZoomWidth, - uri + "?optionName=zoom&optionValue=width", // - disabledZoomHeight, - uri + "?optionName=zoom&optionValue=height" // - )); - } + // Full content + + StringBuilder builder = new StringBuilder(); + + builder.append(getTemplateIndexPreBanner(false)); + + builder.append(getTemplate("viewer.navbar") // + .replace("${disabledFirst}", disabledLeft) // + .replace("${disabledPrevious}", disabledLeft) // + .replace("${disabledNext}", disabledRight) // + .replace("${disabledLast}", disabledRight) // + .replace("${hrefFirst}", first) // + .replace("${hrefPrevious}", previous) // + .replace("${hrefNext}", next) // + .replace("${hrefLast}", last) // + .replace("${current}", + "" + (paragraph > 0 ? paragraph : chapter)) // + .replace("${links}", links.toString()) // + ); + + builder.append(viewer); + + builder.append(getTemplate("viewer.optionbar") // + .replace("${classSize}", (paragraph > 0 ? "s4" : "s1")) // + .replace("${buttons}", buttons.toString()) // + ); + + builder.append(getTemplate("index.post")); - appendPostHtml(builder); return NanoHTTPD.newFixedLengthResponse(Status.OK, NanoHTTPD.MIME_HTML, builder.toString()); } catch (IOException e) { @@ -799,22 +825,8 @@ abstract class WebLibraryServerHtml implements Runnable { return NanoHTTPD.newChunkedResponse(Status.OK, mimeType, in); } - private String getContentOf(String file) { - InputStream in = IOUtils.openResource(WebLibraryServerIndex.class, - file); - if (in != null) { - try { - return IOUtils.readSmallStream(in); - } catch (IOException e) { - Instance.getInstance().getTraceHandler().error( - new IOException("Cannot get file: index.pre.html", e)); - } - } - - return ""; - } - - private void appendPreHtml(StringBuilder builder, boolean banner) { + private String getTemplateIndexPreBanner(boolean banner) + throws IOException { String favicon = "favicon.ico"; String icon = Instance.getInstance().getUiConfig() .getString(UiConfig.PROGRAM_ICON); @@ -822,70 +834,44 @@ abstract class WebLibraryServerHtml implements Runnable { favicon = "icon_" + icon.replace("-", "_") + ".png"; } - builder.append( - getContentOf("index.pre.html").replace("favicon.ico", favicon)); + String html = getTemplate("index.pre") // + .replace("${title}", "Fanfix") // + .replace("${favicon}", favicon) // + ; if (banner) { - builder.append("\n"); + html += getTemplate("index.banner") // + .replace("${favicon}", favicon) // + .replace("${version}", + Version.getCurrentVersion().toString()) // + ; } - } - private void appendPostHtml(StringBuilder builder) { - builder.append(getContentOf("index.post.html")); + return html; } - private void appendOption(StringBuilder builder, int depth, String name, - String value, String selected) { - for (int i = 0; i < depth; i++) { - builder.append("\t"); - } - builder.append("\n"); - } - - private void appendTableRow(StringBuilder builder, int depth, - String... tds) { - for (int i = 0; i < depth; i++) { - builder.append("\t"); + selectedAttribute = " selected='selected'"; } - int col = 1; - builder.append(""); - for (String td : tds) { - builder.append(""); - builder.append(td); - builder.append(""); - } - builder.append("\n"); + return getTemplate("browser.option" // + .replace("${value}", value) // + .replace("${selected}", selectedAttribute) // + .replace("${name}", name) // + ); } - private void appendItemA(StringBuilder builder, int depth, String link, - String name, boolean selected) { - for (int i = 0; i < depth; i++) { - builder.append("\t"); - } - - builder.append(""); - builder.append(name); - builder.append("\n"); } }