From a20b4eba244d9f272db08fb8e8f4039c635d0675 Mon Sep 17 00:00:00 2001 From: Alexander Schwartz Date: Sat, 7 Sep 2019 13:37:32 +0200 Subject: [PATCH] Support HTML blanks and HTML entities in image file names for preview (#328) --- CHANGELOG.adoc | 1 + .../intellij/editor/browser/BrowserPanel.java | 8 ++++++++ .../intellij/editor/javafx/JavaFxHtmlPanel.java | 17 +++++++++++++++-- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index f3c0fc60b..698e5d329 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -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) diff --git a/src/main/java/org/asciidoc/intellij/editor/browser/BrowserPanel.java b/src/main/java/org/asciidoc/intellij/editor/browser/BrowserPanel.java index 3e895816a..d4afb07c7 100644 --- a/src/main/java/org/asciidoc/intellij/editor/browser/BrowserPanel.java +++ b/src/main/java/org/asciidoc/intellij/editor/browser/BrowserPanel.java @@ -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; @@ -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; diff --git a/src/main/java/org/asciidoc/intellij/editor/javafx/JavaFxHtmlPanel.java b/src/main/java/org/asciidoc/intellij/editor/javafx/JavaFxHtmlPanel.java index 93e5c40b4..90bab49be 100644 --- a/src/main/java/org/asciidoc/intellij/editor/javafx/JavaFxHtmlPanel.java +++ b/src/main/java/org/asciidoc/intellij/editor/javafx/JavaFxHtmlPanel.java @@ -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; @@ -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 = "