/** * Reads at most {@code limit} bytes from {@code inputFile} and returns it as a byte array. * * @throws IOException if there was an error. */ public static byte[] readContentWithLimit(Path inputFile, int limit) throws IOException { Preconditions.checkArgument(limit >= 0, "limit needs to be >=0, but it is %s", limit); ByteSource byteSource = asByteSource(inputFile); byte[] buffer = new byte[limit]; try (InputStream inputStream = byteSource.openBufferedStream()) { int read = ByteStreams.read(inputStream, buffer, 0, limit); return Arrays.copyOf(buffer, read); } }
private static Map<String, String> content_for( Set<String> relevant_files, Map<String, Vertabrae> file_names) { Map<String, String> result = new HashMap<>(); for (String file : relevant_files) { String index = file.substring(file.length() - 1); Vertabrae vertabrae = file_names.get(file); ByteSource bs = Files.asByteSource(vertabrae.path().toFile()); String html; try { html = md.process(bs.openBufferedStream()); result.put(index, html); } catch (IOException e) { e.printStackTrace(); } } return result; }
private static void generate_markup( Path abs_path_from, Path abs_path_to, Map<String, Vertabrae> file_names) throws IOException { Function<Vertabrae, String> title = Vertabrae::title; Map<String, String> toc = Maps.transformValues(file_names, title); Set<String> files = file_names.keySet(); Set<String> filtered_files = files.stream().filter(file -> !file.contains("_")).collect(Collectors.toSet()); for (String key : filtered_files) { Vertabrae vertabrae = file_names.get(key); ByteSource bs = Files.asByteSource(vertabrae.path().toFile()); String html = md.process(bs.openBufferedStream()); Set<String> relevant_files = files.stream().filter(file -> file.contains(key + "_")).collect(Collectors.toSet()); String template = pick_template(abs_path_from, vertabrae.file_name()); VelocityContext velocity_context = new VelocityContext(); velocity_context.put("name", vertabrae.title()); velocity_context.put("content", html); velocity_context.put("pages", toc); Map<String, String> content_files = content_for(relevant_files, file_names); for (String index : content_files.keySet()) { velocity_context.put("content_" + index, content_files.get(index)); } System.out.println("[SPINE] Template: " + template); String page = merge(template, velocity_context); File to_file = new File(abs_path_to + File.separator + vertabrae.file_name() + ".html"); Files.write(page, to_file, Charsets.UTF_8); } }