Skip to content

Commit

Permalink
Support HTML blanks and HTML entities in image file names for preview (
Browse files Browse the repository at this point in the history
  • Loading branch information
ahus1 committed Sep 7, 2019
1 parent 82ccf16 commit a20b4eb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This document provides a high-level view of the changes introduced by release.
- support operation block macro in https://docs.spring.io/spring-restdocs/docs/current/reference/html5/[spring-restdoc] and auto-detect the snippets folder (#312)
- prepended config via plugin shouldn't add blank line that breaks document title (#325)
- upgrade to asciidoctorj-pdf:1.5.0-beta.4 (#325)
- Support HTML blanks and HTML entities in image file names for preview (#328)

=== 0.30.2 (preview, available from Github releases)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.MessageDigest;
Expand Down Expand Up @@ -188,6 +191,11 @@ private String prepareHtml(@NotNull String html) {
if (file.startsWith("image?")) {
continue;
}
try {
file = URLDecoder.decode(file, StandardCharsets.UTF_8.name()); // restore "%20" as " "
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
String tmpFile = findTempImageFile(file);
String md5;
String replacement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,12 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
Expand Down Expand Up @@ -531,17 +535,26 @@ private String prepareHtml(@NotNull String html) {
while (matcher.find()) {
final MatchResult matchResult = matcher.toMatchResult();
String file = matchResult.group(1);
try {
file = URLDecoder.decode(file, StandardCharsets.UTF_8.name()); // restore "%20" as " "
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
String tmpFile = findTempImageFile(file);
String md5;
String replacement;
if (tmpFile != null) {
md5 = calculateMd5(tmpFile, null);
tmpFile = tmpFile.replaceAll("\\\\", "/");
tmpFile = tmpFile.replaceAll(":", "%3A");
try {
tmpFile = URLEncoder.encode(tmpFile, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
if (JavaFxHtmlPanelProvider.isInitialized()) {
replacement = "<img src=\"localfile://" + md5 + "/" + tmpFile + "\"";
} else {
replacement = "<img src=\"file://" + tmpFile.replaceAll("%3A", ":") + "\"";
replacement = "<img src=\"file://" + tmpFile + "\"";
}
} else {
md5 = calculateMd5(file, base);
Expand Down

0 comments on commit a20b4eb

Please sign in to comment.