From: Niki Roo Date: Sat, 16 May 2020 10:12:29 +0000 (+0200) Subject: weblib: use more templats X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=83001824bb702296a45cdf400312e34f69951e91;p=fanfix-swing.git weblib: use more templats --- diff --git a/src/be/nikiroo/fanfix/library/WebLibraryServerHtml.java b/src/be/nikiroo/fanfix/library/WebLibraryServerHtml.java index e530d3f0..69d86717 100644 --- a/src/be/nikiroo/fanfix/library/WebLibraryServerHtml.java +++ b/src/be/nikiroo/fanfix/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,10 +309,11 @@ 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( @@ -335,7 +336,7 @@ abstract class WebLibraryServerHtml implements Runnable { 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()); @@ -347,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(); @@ -388,7 +389,8 @@ abstract class WebLibraryServerHtml implements Runnable { // TODO: if 1 group -> no group Map> sources = result.getSourcesGrouped(); for (String source : sources.keySet()) { - appendOption(options, 5, source, source, browser2); + options.append( + getTemplateBrowserOption(source, source, browser2)); } } else if (browser.equals("authors")) { authorsSel = true; @@ -397,14 +399,16 @@ abstract class WebLibraryServerHtml implements Runnable { // TODO: if 1 group -> no group Map> authors = result.getAuthorsGrouped(); for (String author : authors.keySet()) { - appendOption(options, 5, author, author, browser2); + options.append( + getTemplateBrowserOption(author, author, browser2)); } } else if (browser.equals("tags")) { tagsSel = true; filterTag = browser2.isEmpty() ? filterTag : browser2; for (String tag : result.getTags()) { - appendOption(options, 5, tag, tag, browser2); + options.append( + getTemplateBrowserOption(tag, tag, browser2)); } } @@ -426,7 +430,8 @@ abstract class WebLibraryServerHtml implements Runnable { if (sources != null && !sources.isEmpty()) { // TODO: single empty value for (String source : sources) { - appendOption(options, 5, source, source, browser3); + options.append(getTemplateBrowserOption(source, source, + browser3)); } } } else if (browser.equals("authors")) { @@ -437,7 +442,8 @@ abstract class WebLibraryServerHtml implements Runnable { if (authors != null && !authors.isEmpty()) { // TODO: single empty value for (String author : authors) { - appendOption(options, 5, author, author, browser3); + options.append(getTemplateBrowserOption(author, author, + browser3)); } } } @@ -506,7 +512,8 @@ abstract class WebLibraryServerHtml implements Runnable { } builder.append("\t\t\n"); - appendPostHtml(builder); + builder.append(getTemplate("index.post")); + return NanoHTTPD.newFixedLengthResponse(builder.toString()); } @@ -560,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) @@ -583,7 +587,7 @@ abstract class WebLibraryServerHtml implements Runnable { String first, previous, next, last; - StringBuilder content = new StringBuilder(); + String viewer = ""; String disabledLeft = ""; String disabledRight = ""; @@ -601,41 +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("\t\t\t

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

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

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("\t\t
\n"); - content.append(desc); + viewer = getTemplate("viewer.text") // + .replace("${desc}", desc) // + ; if (chap.getParagraphs().size() <= 0) { - content.append("\t\t\tNo content provided.\n"); + viewer = viewer.replace("${content}", + "No content provided."); } else { - content.append( - "\t\t\t\n"); // - content.append("\t\t\t").append( - new TextOutput(false).convert(chap, chapter > 0)) - .append("\n"); + viewer = viewer.replace("${content}", + new TextOutput(false).convert(chap, chapter > 0)); } - content.append("\t\t
\n"); if (chapter <= 0) disabledLeft = " disabled='disbaled'"; @@ -692,46 +692,39 @@ abstract class WebLibraryServerHtml implements Runnable { } } - String javascript = "document.getElementById(\"previous\").click(); return false;"; - content.append(String.format("" // - + "\t\t\n" - + "\t\t\t\n" - + "\t\t\n", // - 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("" // - + "\t\t
" // - + "\t\t\t%s\n" // - + "\t\t
\n", // - para.getContent())); + viewer = getTemplate("viewer.text") // + .replace("${desc}", "") // + .replace("${content}", + new TextOutput(false).convert(para)) // + ; } } - builder.append(String.format("" // - + "\t\t\n", // - disabledRight, next, // - disabledRight, last // - )); - - builder.append(content); + // Buttons on the optionbar - builder.append("\t\t
\n"); - builder.append("\t\t\tBACK\n"); - if (paragraph > 0) { - builder.append(String.format("" // - + "\t\t\tREAL\n"// - + "\t\t\tWIDTH\n"// - + "\t\t\tHEIGHT\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("\t\t
\n"); + 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) { @@ -805,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); @@ -828,75 +834,39 @@ 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("\t\t\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"); - } - - 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"); + return getTemplate("browser.option" // + .replace("${value}", value) // + .replace("${selected}", selectedAttribute) // + .replace("${name}", name) // + ); } private String getTemplate(String template) throws IOException { - InputStream in = IOUtils.openResource(WebLibraryServerIndex.class, + // TODO: check if it is "slow" -> map cache + InputStream in = IOUtils.openResource(WebLibraryServerTemplates.class, template + ".html"); try { return IOUtils.readSmallStream(in); diff --git a/src/be/nikiroo/fanfix/library/web/package-info.java b/src/be/nikiroo/fanfix/library/web/package-info.java new file mode 100644 index 00000000..69bff54a --- /dev/null +++ b/src/be/nikiroo/fanfix/library/web/package-info.java @@ -0,0 +1 @@ +package be.nikiroo.fanfix.library.web; \ No newline at end of file diff --git a/src/be/nikiroo/fanfix/library/web/style.css b/src/be/nikiroo/fanfix/library/web/style.css index 4196aeba..b12a24fa 100644 --- a/src/be/nikiroo/fanfix/library/web/style.css +++ b/src/be/nikiroo/fanfix/library/web/style.css @@ -310,13 +310,13 @@ a.viewer.link:hover { display: block; } -.desc .details .col1 { +.desc .details .col.key { font-weight: bold; padding-left: 5px; padding-right: 5px; } -.desc .details .col2 { +.desc .details .col.value { color: #AAA391; } diff --git a/src/be/nikiroo/fanfix/library/web/templates/WebLibraryServerTemplates.java b/src/be/nikiroo/fanfix/library/web/templates/WebLibraryServerTemplates.java new file mode 100644 index 00000000..482a1b82 --- /dev/null +++ b/src/be/nikiroo/fanfix/library/web/templates/WebLibraryServerTemplates.java @@ -0,0 +1,4 @@ +package be.nikiroo.fanfix.library.web.templates; + +public class WebLibraryServerTemplates { +} diff --git a/src/be/nikiroo/fanfix/library/web/bookline.html b/src/be/nikiroo/fanfix/library/web/templates/bookline.html similarity index 100% rename from src/be/nikiroo/fanfix/library/web/bookline.html rename to src/be/nikiroo/fanfix/library/web/templates/bookline.html diff --git a/src/be/nikiroo/fanfix/library/web/browser.html b/src/be/nikiroo/fanfix/library/web/templates/browser.html similarity index 100% rename from src/be/nikiroo/fanfix/library/web/browser.html rename to src/be/nikiroo/fanfix/library/web/templates/browser.html diff --git a/src/be/nikiroo/fanfix/library/web/templates/browser.option.html b/src/be/nikiroo/fanfix/library/web/templates/browser.option.html new file mode 100644 index 00000000..49f7b5b1 --- /dev/null +++ b/src/be/nikiroo/fanfix/library/web/templates/browser.option.html @@ -0,0 +1 @@ + diff --git a/src/be/nikiroo/fanfix/library/web/browser.select.html b/src/be/nikiroo/fanfix/library/web/templates/browser.select.html similarity index 100% rename from src/be/nikiroo/fanfix/library/web/browser.select.html rename to src/be/nikiroo/fanfix/library/web/templates/browser.select.html diff --git a/src/be/nikiroo/fanfix/library/web/templates/index.banner.html b/src/be/nikiroo/fanfix/library/web/templates/index.banner.html new file mode 100644 index 00000000..c2cd1e7f --- /dev/null +++ b/src/be/nikiroo/fanfix/library/web/templates/index.banner.html @@ -0,0 +1,5 @@ + diff --git a/src/be/nikiroo/fanfix/library/web/index.post.html b/src/be/nikiroo/fanfix/library/web/templates/index.post.html similarity index 100% rename from src/be/nikiroo/fanfix/library/web/index.post.html rename to src/be/nikiroo/fanfix/library/web/templates/index.post.html diff --git a/src/be/nikiroo/fanfix/library/web/index.pre.html b/src/be/nikiroo/fanfix/library/web/templates/index.pre.html similarity index 95% rename from src/be/nikiroo/fanfix/library/web/index.pre.html rename to src/be/nikiroo/fanfix/library/web/templates/index.pre.html index e4d9da8f..1e626aba 100644 --- a/src/be/nikiroo/fanfix/library/web/index.pre.html +++ b/src/be/nikiroo/fanfix/library/web/templates/index.pre.html @@ -40,9 +40,9 @@ --> - Fanfix + ${title} - +
diff --git a/src/be/nikiroo/fanfix/library/web/templates/package-info.java b/src/be/nikiroo/fanfix/library/web/templates/package-info.java new file mode 100644 index 00000000..5d4f3b2e --- /dev/null +++ b/src/be/nikiroo/fanfix/library/web/templates/package-info.java @@ -0,0 +1 @@ +package be.nikiroo.fanfix.library.web.templates; \ No newline at end of file diff --git a/src/be/nikiroo/fanfix/library/web/templates/viewer.desc.html b/src/be/nikiroo/fanfix/library/web/templates/viewer.desc.html new file mode 100644 index 00000000..21cddc45 --- /dev/null +++ b/src/be/nikiroo/fanfix/library/web/templates/viewer.desc.html @@ -0,0 +1,9 @@ +

${title}

+
+ + + + +${details}
+
+

Description

diff --git a/src/be/nikiroo/fanfix/library/web/templates/viewer.descline.html b/src/be/nikiroo/fanfix/library/web/templates/viewer.descline.html new file mode 100644 index 00000000..9d619879 --- /dev/null +++ b/src/be/nikiroo/fanfix/library/web/templates/viewer.descline.html @@ -0,0 +1 @@ + ${key}${value} diff --git a/src/be/nikiroo/fanfix/library/web/templates/viewer.image.html b/src/be/nikiroo/fanfix/library/web/templates/viewer.image.html new file mode 100644 index 00000000..24165162 --- /dev/null +++ b/src/be/nikiroo/fanfix/library/web/templates/viewer.image.html @@ -0,0 +1,6 @@ + + + diff --git a/src/be/nikiroo/fanfix/library/web/templates/viewer.link.html b/src/be/nikiroo/fanfix/library/web/templates/viewer.link.html new file mode 100644 index 00000000..5812c80a --- /dev/null +++ b/src/be/nikiroo/fanfix/library/web/templates/viewer.link.html @@ -0,0 +1 @@ + ${name} diff --git a/src/be/nikiroo/fanfix/library/web/templates/viewer.navbar.html b/src/be/nikiroo/fanfix/library/web/templates/viewer.navbar.html new file mode 100644 index 00000000..59f35be1 --- /dev/null +++ b/src/be/nikiroo/fanfix/library/web/templates/viewer.navbar.html @@ -0,0 +1,11 @@ + diff --git a/src/be/nikiroo/fanfix/library/web/templates/viewer.optionbar.button.html b/src/be/nikiroo/fanfix/library/web/templates/viewer.optionbar.button.html new file mode 100644 index 00000000..b85f9a9a --- /dev/null +++ b/src/be/nikiroo/fanfix/library/web/templates/viewer.optionbar.button.html @@ -0,0 +1 @@ + ${value} diff --git a/src/be/nikiroo/fanfix/library/web/templates/viewer.optionbar.html b/src/be/nikiroo/fanfix/library/web/templates/viewer.optionbar.html new file mode 100644 index 00000000..1d1eaec1 --- /dev/null +++ b/src/be/nikiroo/fanfix/library/web/templates/viewer.optionbar.html @@ -0,0 +1,2 @@ +
+${buttons}
diff --git a/src/be/nikiroo/fanfix/library/web/templates/viewer.text.html b/src/be/nikiroo/fanfix/library/web/templates/viewer.text.html new file mode 100644 index 00000000..4562ea47 --- /dev/null +++ b/src/be/nikiroo/fanfix/library/web/templates/viewer.text.html @@ -0,0 +1,4 @@ +
+${desc} + ${content} +
diff --git a/src/be/nikiroo/fanfix/reader/TextOutput.java b/src/be/nikiroo/fanfix/reader/TextOutput.java index 60b3a7f7..732fced7 100644 --- a/src/be/nikiroo/fanfix/reader/TextOutput.java +++ b/src/be/nikiroo/fanfix/reader/TextOutput.java @@ -145,4 +145,18 @@ public class TextOutput { } return builder.toString(); } + + /** + * Convert the paragraph into HTML3 code. + * + * @param para + * the {@link Paragraph} to convert + * + * @return HTML3 code tested with Java Swing + */ + public String convert(Paragraph para) { + Chapter fakeChapter = new Chapter(0, ""); + fakeChapter.setParagraphs(Arrays.asList(para)); + return convert(fakeChapter, false); + } }