Skip to content

Commit

Permalink
Extract methods
Browse files Browse the repository at this point in the history
  • Loading branch information
marcphilipp committed Dec 30, 2024
1 parent b543739 commit 0ed0d93
Showing 1 changed file with 72 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,72 +189,89 @@ private static Optional<Section> createAttachmentsSection(Context context) {
var type = child.getLocalName();
var section = Section.builder().title(capitalize(type));
if (matches(File.ELEMENT, child)) {
var attributes = KeyValuePairs.builder();
getAttributeValue(child, File.TIME).ifPresent(section::metaInfo);
var mediaType = getAttributeValue(child, File.MEDIA_TYPE);
getAttributeValue(child, File.PATH).ifPresent(rawPath -> {
var originalPath = context.sourceXmlFile().getParent().resolve(rawPath).toAbsolutePath();
var path = tryRelativize(context.targetHtmlFile().getParent(), originalPath);
var filename = path.getFileName().toString();
filename = filename.substring(Math.max(filename.lastIndexOf('/'), filename.lastIndexOf('\\')) + 1);
attributes.putContent("Filename", filename);
attributes.putContent("Path", "link:" + path);
if (Files.isRegularFile(originalPath)) {
var resolvedMediaType = mediaType.or(() -> {
try {
return Optional.ofNullable(Files.probeContentType(path));
}
catch (IOException e) {
return Optional.empty();
}
}).orElse(null);
if (resolvedMediaType != null) {
if (SUPPORTED_IMAGE_MEDIA_TYPES.contains(resolvedMediaType)) {
section.addBlock(Image.builder().content(path.toString()).altText(filename).build());
}
else if (SUPPORTED_TEXT_MEDIA_TYPES.stream().anyMatch(resolvedMediaType::startsWith)) {
try {
var charset = parseCharset(resolvedMediaType);
var content = Files.readString(originalPath, charset);
section.addBlock(PreFormattedOutput.builder().content(content).build());
}
catch (IOException ignore) {
}
}
}
}
});
mediaType.ifPresent(it -> attributes.putContent("Media type", it));
section.addBlock(attributes.build());
addFileAttachment(context, child, section);
}
else if (matches(Data.ELEMENT, child)) {
var attributes = KeyValuePairs.builder();
getAttributeValue(child, Data.TIME).ifPresent(section::metaInfo);
findChildren(child, Data.Entry.ELEMENT).forEach(entry -> {
getAttributeValue(entry, Data.Entry.KEY).ifPresent(key -> {
var value = entry.getTextContent();
attributes.putContent(key, value);
});
});
section.addBlock(attributes.build());
addDataAttachment(child, section);
}
else if (matches(Output.ELEMENT, child)) {
getAttributeValue(child, Output.SOURCE) //
.map(source -> switch (source) {
case "stdout" -> "Standard output";
case "stderr" -> "Standard error";
default -> "%s (%s)".formatted("Output", source);
}) //
.ifPresent(section::title);
getAttributeValue(child, Output.TIME).ifPresent(section::metaInfo);
section.addBlock(PreFormattedOutput.builder().content(child.getTextContent()).build());
addOutputAttachment(child, section);
}
return section.build();
}).forEach(subsections::addContent);

return Optional.of(Section.builder().title("Attachments").order(30).addBlock(subsections.build()).build());
}

private static void addFileAttachment(Context context, Node child, Section.Builder section) {
var attributes = KeyValuePairs.builder();
getAttributeValue(child, File.TIME).ifPresent(section::metaInfo);
var mediaType = getAttributeValue(child, File.MEDIA_TYPE);
getAttributeValue(child, File.PATH).ifPresent(rawPath -> {
var originalPath = context.sourceXmlFile().getParent().resolve(rawPath).toAbsolutePath();
var path = tryRelativize(context.targetHtmlFile().getParent(), originalPath);
var filename = path.getFileName().toString();
filename = filename.substring(Math.max(filename.lastIndexOf('/'), filename.lastIndexOf('\\')) + 1);
attributes.putContent("Filename", filename);
attributes.putContent("Path", "link:" + path);
if (Files.isRegularFile(originalPath)) {
var resolvedMediaType = mediaType.or(() -> {
try {
return Optional.ofNullable(Files.probeContentType(path));
}
catch (IOException e) {
return Optional.empty();
}
}).orElse(null);
if (resolvedMediaType != null) {
addInlineFileBlock(section, resolvedMediaType, path, filename, originalPath);
}
}
});
mediaType.ifPresent(it -> attributes.putContent("Media type", it));
section.addBlock(attributes.build());
}

private static void addInlineFileBlock(Section.Builder section, String resolvedMediaType, Path path,
String filename, Path originalPath) {
if (SUPPORTED_IMAGE_MEDIA_TYPES.contains(resolvedMediaType)) {
section.addBlock(Image.builder().content(path.toString()).altText(filename).build());
}
else if (SUPPORTED_TEXT_MEDIA_TYPES.stream().anyMatch(resolvedMediaType::startsWith)) {
try {
var charset = parseCharset(resolvedMediaType);
var content = Files.readString(originalPath, charset);
section.addBlock(PreFormattedOutput.builder().content(content).build());
}
catch (IOException ignore) {
}
}
}

private static void addDataAttachment(Node child, Section.Builder section) {
var attributes = KeyValuePairs.builder();
getAttributeValue(child, Data.TIME).ifPresent(section::metaInfo);
findChildren(child, Data.Entry.ELEMENT).forEach(entry -> {
getAttributeValue(entry, Data.Entry.KEY).ifPresent(key -> {
var value = entry.getTextContent();
attributes.putContent(key, value);
});
});
section.addBlock(attributes.build());
}

private static void addOutputAttachment(Node child, Section.Builder section) {
getAttributeValue(child, Output.SOURCE) //
.map(source -> switch (source) {
case "stdout" -> "Standard output";
case "stderr" -> "Standard error";
default -> "%s (%s)".formatted("Output", source);
}) //
.ifPresent(section::title);
getAttributeValue(child, Output.TIME).ifPresent(section::metaInfo);
section.addBlock(PreFormattedOutput.builder().content(child.getTextContent()).build());
}

private static Charset parseCharset(String resolvedMediaType) {
var mediaTypeParts = resolvedMediaType.split(";");
for (var part : mediaTypeParts) {
Expand Down

0 comments on commit 0ed0d93

Please sign in to comment.