| Author | DaveJarvis <email> |
|---|---|
| Date | 2021-03-08 18:59:39 GMT-0800 |
| Commit | 6d4b12a884a313d1ae55e8fedd490467bdcdde9e |
| Parent | bfef24b |
| import com.keenwrite.util.ProtocolScheme; | ||
| -import javax.imageio.ImageIO; | ||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.net.URL; | ||
| -import java.nio.file.Files; | ||
| +import java.nio.file.Path; | ||
| import static com.keenwrite.Bootstrap.APP_TITLE_LOWERCASE; | ||
| import static com.keenwrite.events.StatusEvent.clue; | ||
| import static com.keenwrite.io.MediaTypeExtension.valueFrom; | ||
| -import static com.keenwrite.preview.SvgRasterizer.rasterize; | ||
| +import static com.keenwrite.preferences.WorkspaceKeys.KEY_IMAGES_DIR; | ||
| +import static com.keenwrite.util.ProtocolScheme.isRemote; | ||
| import static java.io.File.createTempFile; | ||
| +import static java.nio.file.Files.copy; | ||
| import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; | ||
| import static org.jsoup.Jsoup.parse; | ||
| for( final var img : doc.getElementsByTag( "img" ) ) { | ||
| - final var src = img.absUrl( "src" ); | ||
| + final var src = img.attr( "src" ); | ||
| try { | ||
| - final var url = new URL( src ); | ||
| - final var protocol = ProtocolScheme.valueFrom( url ); | ||
| + final var protocol = ProtocolScheme.getProtocol( src ); | ||
| + final File imageFile; | ||
| if( protocol.isRemote() ) { | ||
| + final var url = new URL( src ); | ||
| final var conn = url.openConnection(); | ||
| conn.setUseCaches( false ); | ||
| final var type = conn.getContentType(); | ||
| final var media = MediaType.valueFrom( type ); | ||
| try( final var in = conn.getInputStream() ) { | ||
| - File imageFile; | ||
| - | ||
| - if( media == MediaType.IMAGE_SVG_XML ) { | ||
| - // Rasterize. | ||
| - final var image = rasterize( in, 300f ); | ||
| - final var mt = MediaType.IMAGE_PNG; | ||
| - imageFile = createTemporaryFile( mt ); | ||
| - ImageIO.write( image, mt.getSubtype(), imageFile ); | ||
| - } | ||
| - else { | ||
| - // Download into temporary directory. | ||
| - imageFile = createTemporaryFile( media ); | ||
| - Files.copy( in, imageFile.toPath(), REPLACE_EXISTING ); | ||
| - } | ||
| - | ||
| - img.attr( "src", imageFile.getAbsolutePath() ); | ||
| + imageFile = createTemporaryFile( media ); | ||
| + copy( in, imageFile.toPath(), REPLACE_EXISTING ); | ||
| } | ||
| + } | ||
| + else { | ||
| + imageFile = Path.of( getImagePath(), src ).toFile(); | ||
| } | ||
| + | ||
| + img.attr( "src", imageFile.getAbsolutePath() ); | ||
| } catch( final Exception ex ) { | ||
| clue( ex ); | ||
| } | ||
| } | ||
| return doc.html(); | ||
| + } | ||
| + | ||
| + private String getImagePath() { | ||
| + return mWorkspace.fileProperty( KEY_IMAGES_DIR ).get().toString(); | ||
| } | ||
| import java.time.Instant; | ||
| import java.time.format.DateTimeFormatter; | ||
| -import java.util.Comparator; | ||
| import java.util.Locale; | ||
| import static java.time.ZoneId.systemDefault; | ||
| import static java.time.format.DateTimeFormatter.ofPattern; | ||
| -import static java.util.Comparator.*; | ||
| +import static java.util.Comparator.comparing; | ||
| import static javafx.collections.FXCollections.observableArrayList; | ||
| import static javafx.scene.control.TableView.CONSTRAINED_RESIZE_POLICY; | ||
| +import static javafx.scene.input.KeyCode.ENTER; | ||
| import static javafx.scene.layout.Priority.ALWAYS; | ||
| import static org.apache.commons.io.FilenameUtils.getExtension; | ||
| mDirectory.addListener( ( c, o, n ) -> { | ||
| if( n != null ) { field.setText( n.getAbsolutePath() ); } | ||
| + } ); | ||
| + | ||
| + field.setOnKeyPressed( event -> { | ||
| + if( event.getCode() == ENTER ) { | ||
| + mDirectory.set( new File( field.getText() ) ); | ||
| + } | ||
| } ); | ||
| * Determines the protocol scheme for a given {@link URL}. | ||
| * | ||
| - * @param url A {@link URL} that contains a protocol scheme. | ||
| + * @param url The {@link URL} containing a protocol scheme. | ||
| * @return {@link #UNKNOWN} if the protocol is unrecognized, otherwise a | ||
| * valid value from this enumeration. | ||
| */ | ||
| public static ProtocolScheme valueFrom( final URL url ) { | ||
| return valueFrom( url.getProtocol() ); | ||
| + } | ||
| + | ||
| + /** | ||
| + * Answers whether the given {@link URL} points to a remote resource. | ||
| + * | ||
| + * @param url The {@link URL} containing a protocol scheme. | ||
| + * @return {@link true} if the protocol must be fetched via HTTP or FTP. | ||
| + */ | ||
| + public static boolean isRemote( final URL url ) { | ||
| + return valueFrom( url ).isRemote(); | ||
| } | ||
| * Answers whether the given protocol represents a remote resource. | ||
| * | ||
| - * @return {@code true} the protocol is HTTP(S) or FTP. | ||
| + * @return {@code true} the protocol is HTTP or FTP. | ||
| */ | ||
| public boolean isRemote() { | ||
| \startxmlsetups xml:img | ||
| - \placefigure[none]{}{% | ||
| - \externalfigure[\xmlatt{#1}{src}] | ||
| - } | ||
| + \placefigure{}{\externalfigure[\xmlatt{#1}{src}]} | ||
| \stopxmlsetups | ||
| \stop | ||
| \stopxmlsetups | ||
| - | ||
| -\xmltexentity{ldquo}{\symbol[leftquotation]{}} | ||
| -\xmltexentity{rdquo}{\symbol[rightquotation]{}} | ||
| -\xmltexentity{rsquo}{\quotesingle{}} | ||
| -\xmltexentity{mdash}{\emdash{}} | ||
| -\xmltexentity{ndash}{\endash{}} | ||
| -\xmltexentity{hellip}{\dots{}} | ||
| \setupexternalfigures[ | ||
| - %order={svg,pdf,png}, | ||
| - %directory={images}, | ||
| - %maxwidth=\makeupwidth, | ||
| + order={svg,pdf,png}, | ||
| + maxwidth=\makeupwidth, | ||
| width=\makeupwidth, | ||
| ] | ||
| -% Force images to flow exactly where they fall in the text. | ||
| -\setupfloat[figure][default=force] | ||
| +% Force images to flow exactly where they fall in the text, captionlessly. | ||
| +\setupfloat[figure][default={force,none}] | ||
| % Indent the paragraph following each image. |
| Delta | 44 lines added, 41 lines removed, 3-line increase |
|---|