@javax.annotation.Nonnull @Override public String file(final CharSequence data, final CharSequence caption) { return file(data, ++MarkdownNotebookOutput.excerptNumber + ".txt", caption); }
/** * Get notebook output. * * @param s the s * @return the notebook output */ public static NotebookOutput get(final String s) { return get(new File(s), true); }
@javax.annotation.Nonnull @Override public String png(@Nullable final BufferedImage rawImage, final CharSequence caption) { if (null == rawImage) return ""; @Nonnull final File file = pngFile(rawImage, new File(getResourceDir(), getName() + "." + ++MarkdownNotebookOutput.imageNumber + ".png")); return anchor(anchorId()) + "![" + caption + "](etc/" + file.getName() + ")"; }
final boolean autobrowse ) throws FileNotFoundException { this.setName(reportFile.getName()); root = reportFile.getAbsoluteFile().getParentFile(); root.mkdirs(); setCurrentHome(root.toURI()); setArchiveHome(root.toURI()); primaryOut = new PrintStream(new FileOutputStream(new File(root, getName().toString()))); FileNanoHTTPD httpd = httpPort <= 0 ? null : new FileNanoHTTPD(root, httpPort); if (null != httpd) httpd.addGET("", "text/html", out -> { try { write(); try (FileInputStream input = new FileInputStream(new File(getRoot(), getName() + ".html"))) { IOUtils.copy(input, out); if (null != httpd) httpd.addGET("pdf", "application/pdf", out -> { try { write(); try (FileInputStream input = new FileInputStream(new File(getRoot(), getName() + ".pdf"))) { IOUtils.copy(input, out); printStream.print("Closing..."); try { close(); printStream.print("Done"); } catch (IOException e) { System.exit(0); });
@Override public <T> T subreport(String subreportName, Function<NotebookOutput, T> fn) { if (null == subreportName) return subreport("", fn); String reportName = getName() + subreportName; MarkdownNotebookOutput outer = this; try { File root = getRoot(); File subreportFile = new File(root, reportName); MarkdownNotebookOutput subreport = new MarkdownNotebookOutput(subreportFile, -1, false) { try { try { outer.p("Subreport: %s %s %s %s", stripPrefixes(URLDecoder.decode(subreportName, "UTF-8"), "_", "/", "-", " ", "."), outer.link(subreportFile, "markdown"), outer.link(new File(root, reportName + ".html"), "html"), outer.link(new File(root, reportName + ".pdf"), "pdf") ); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); getHttpd().addGET(reportName + ".html", "text/html", out -> { try { subreport.write(); try (FileInputStream input = new FileInputStream(new File(root, subreport.getName() + ".html"))) { IOUtils.copy(input, out); getHttpd().addGET(reportName + ".pdf", "application/pdf", out -> { try { subreport.write(); try (FileInputStream input = new FileInputStream(new File(root, subreport.getName() + ".pdf"))) {
@javax.annotation.Nonnull @Override public String jpg(@Nullable final BufferedImage rawImage, final CharSequence caption) { if (null == rawImage) return ""; @Nonnull final File file = jpgFile(rawImage, new File(getResourceDir(), getName() + "." + ++MarkdownNotebookOutput.imageNumber + ".jpg")); return anchor(anchorId()) + "![" + caption + "](etc/" + file.getName() + ")"; }
out(anchor(anchorId()) + "Code from [%s:%s](%s#L%s) executed in %.2f seconds (%.3f gc): ", callingFrame.getFileName(), callingFrame.getLineNumber(), CodeUtil.codeUrl(callingFrame), callingFrame.getLineNumber(), result.obj.seconds(), result.obj.gc_seconds() ); out("```java"); out(" " + sourceCode.replaceAll("\n", "\n ")); out("```"); CharSequence summary = summarize(result.log, maxLog).replaceAll("\n", "\n ").replaceAll(" ~", ""); out(anchor(anchorId()) + "Logging: "); out("```"); out(" " + summary); out("```"); out(""); out(anchor(anchorId()) + "Returns: \n"); String str; boolean escape; escape = true;// } else if (eval instanceof Component) { str = png(Util.toImage((Component) eval), "Result"); escape = false; } else if (eval instanceof BufferedImage) { str = png((BufferedImage) eval, "Result"); escape = false; } else if (eval instanceof TableOutput) {
/** * Gets log. * * @param path the report location * @return the log */ @Nonnull public static NotebookOutput getLog(final File path) { try { StackTraceElement callingFrame = Thread.currentThread().getStackTrace()[2]; String methodName = callingFrame.getMethodName(); path.getParentFile().mkdirs(); return new MarkdownNotebookOutput(new File(path, methodName), TestSettings.INSTANCE.autobrowse); } catch (FileNotFoundException e) { throw new RuntimeException(e); } }
/** * Run. * * @param fn the fn * @param logPath the log path */ public void run(@Nonnull Consumer<NotebookOutput> fn, @Nonnull CharSequence... logPath) { try (@Nonnull NotebookOutput log = getLog(logPath)) { NotebookOutput.concat(this::printHeader, MarkdownNotebookOutput.wrapFrontmatter(fn)).accept(log); } catch (IOException e) { throw new RuntimeException(e); } }
/** * Gets exception string. * * @param e the e * @return the exception string */ @Nonnull public static CharSequence getExceptionString(Throwable e) { if (e instanceof RuntimeException && e.getCause() != null && e.getCause() != e) return getExceptionString(e.getCause()); if (e.getCause() != null && e.getCause() != e) return e.getClass().getSimpleName() + " / " + getExceptionString(e.getCause()); return e.getClass().getSimpleName(); }
@Override public void out(@javax.annotation.Nonnull final CharSequence fmt, final Object... args) { @javax.annotation.Nonnull final String msg = format(fmt, args); markdownData.add(msg); primaryOut.println(msg); log.info(msg); }
final boolean autobrowse ) throws FileNotFoundException { this.setName(reportFile.getName()); root = reportFile.getAbsoluteFile().getParentFile(); root.mkdirs(); setCurrentHome(root.toURI()); setArchiveHome(root.toURI()); primaryOut = new PrintStream(new FileOutputStream(new File(root, getName().toString()))); FileNanoHTTPD httpd = httpPort <= 0 ? null : new FileNanoHTTPD(root, httpPort); if (null != httpd) httpd.addGET("", "text/html", out -> { try { write(); try (FileInputStream input = new FileInputStream(new File(getRoot(), getName() + ".html"))) { IOUtils.copy(input, out); if (null != httpd) httpd.addGET("pdf", "application/pdf", out -> { try { write(); try (FileInputStream input = new FileInputStream(new File(getRoot(), getName() + ".pdf"))) { IOUtils.copy(input, out); printStream.print("Closing..."); try { close(); printStream.print("Done"); } catch (IOException e) { System.exit(0); });
@Override public <T> T subreport(String subreportName, Function<NotebookOutput, T> fn) { String reportName = getName() + subreportName; MarkdownNotebookOutput outer = this; try { File root = getRoot(); File subreportFile = new File(root, reportName); MarkdownNotebookOutput subreport = new MarkdownNotebookOutput(subreportFile, -1, false) { try { try { outer.p("Subreport: %s %s %s %s", stripPrefixes(URLDecoder.decode(subreportName, "UTF-8"), "_", "/", "-", " ", "."), outer.link(subreportFile, "markdown"), outer.link(new File(root, reportName + ".html"), "html"), outer.link(new File(root, reportName + ".pdf"), "pdf") ); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); getHttpd().addGET(reportName + ".html", "text/html", out -> { try { subreport.write(); try (FileInputStream input = new FileInputStream(new File(root, subreport.getName() + ".html"))) { IOUtils.copy(input, out); getHttpd().addGET(reportName + ".pdf", "application/pdf", out -> { try { subreport.write(); try (FileInputStream input = new FileInputStream(new File(root, subreport.getName() + ".pdf"))) { IOUtils.copy(input, out);
@javax.annotation.Nonnull @Override public String png(@Nullable final BufferedImage rawImage, final CharSequence caption) { if (null == rawImage) return ""; @Nonnull final File file = pngFile(rawImage, new File(getResourceDir(), getName() + "." + ++MarkdownNotebookOutput.imageNumber + ".png")); return anchor(anchorId()) + "![" + caption + "](etc/" + file.getName() + ")"; }
@javax.annotation.Nonnull @Override public String jpg(@Nullable final BufferedImage rawImage, final CharSequence caption) { if (null == rawImage) return ""; @Nonnull final File file = jpgFile(rawImage, new File(getResourceDir(), getName() + "." + ++MarkdownNotebookOutput.imageNumber + ".jpg")); return anchor(anchorId()) + "![" + caption + "](etc/" + file.getName() + ")"; }
out(anchor(anchorId()) + "Code from [%s:%s](%s#L%s) executed in %.2f seconds (%.3f gc): ", callingFrame.getFileName(), callingFrame.getLineNumber(), CodeUtil.codeUrl(callingFrame), callingFrame.getLineNumber(), result.obj.seconds(), result.obj.gc_seconds() ); out("```java"); out(" " + sourceCode.replaceAll("\n", "\n ")); out("```"); CharSequence summary = summarize(result.log, maxLog).replaceAll("\n", "\n ").replaceAll(" ~", ""); out(anchor(anchorId()) + "Logging: "); out("```"); out(" " + summary); out("```"); out(""); out(anchor(anchorId()) + "Returns: \n"); String str; boolean escape; escape = true;// } else if (eval instanceof Component) { str = png(Util.toImage((Component) eval), "Result"); escape = false; } else if (eval instanceof BufferedImage) { str = png((BufferedImage) eval, "Result"); escape = false; } else if (eval instanceof TableOutput) {
/** * Get markdown notebook output. * * @param path the path * @param autobrowse the autobrowse * @return the markdown notebook output */ public static NotebookOutput get(File path, final boolean autobrowse) { try { StackTraceElement callingFrame = Thread.currentThread().getStackTrace()[2]; String methodName = callingFrame.getMethodName(); path.getAbsoluteFile().getParentFile().mkdirs(); return new MarkdownNotebookOutput(new File(path, methodName), autobrowse); } catch (FileNotFoundException e) { throw new RuntimeException(e); } }