| Author | DaveJarvis <email> |
|---|---|
| Date | 2021-03-29 21:10:49 GMT-0700 |
| Commit | 7b2f5115d57ae5d6246daeab2a3ff43d562bde47 |
| Parent | 7e75d92 |
| package com.keenwrite.processors; | ||
| +import com.keenwrite.typesetting.Typesetter; | ||
| + | ||
| import java.io.File; | ||
| +import java.io.IOException; | ||
| -import static com.keenwrite.util.ResourceWalker.canExecute; | ||
| +import static com.keenwrite.events.StatusEvent.clue; | ||
| +import static com.keenwrite.io.MediaType.APP_PDF; | ||
| +import static com.keenwrite.util.FileUtils.createTemporaryFile; | ||
| /** | ||
| * Responsible for using a typesetting engine to convert an XHTML document | ||
| * into a PDF file. | ||
| */ | ||
| public final class PdfProcessor extends ExecutorProcessor<String> { | ||
| - private static final String TYPESETTER = "context"; | ||
| + private static final Typesetter sTypesetter = new Typesetter(); | ||
| private final File mExportPath; | ||
| */ | ||
| public String apply( final String xhtml ) { | ||
| - if( canExecute( TYPESETTER ) ) { | ||
| - System.out.println( "CONTEXT IS CONFIGURED" ); | ||
| - System.out.println( "EXPORT AS: " + mExportPath ); | ||
| + try { | ||
| + final var document = createTemporaryFile( APP_PDF ); | ||
| + sTypesetter.typeset( document, mExportPath ); | ||
| + } catch( final IOException ex ) { | ||
| + clue( ex ); | ||
| } | ||
| } | ||
| + /** | ||
| + * Fully qualified file name to use when exporting (e.g., document.pdf). | ||
| + * | ||
| + * @return Full path to a file name. | ||
| + */ | ||
| public File getExportPath() { | ||
| return mExportPath; |
| import java.io.FileNotFoundException; | ||
| -import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.net.URL; | ||
| import java.nio.file.Path; | ||
| import java.util.regex.Pattern; | ||
| -import static com.keenwrite.Bootstrap.APP_TITLE_LOWERCASE; | ||
| import static com.keenwrite.events.StatusEvent.clue; | ||
| import static com.keenwrite.io.MediaType.IMAGE_SVG_XML; | ||
| -import static com.keenwrite.io.MediaTypeExtension.valueFrom; | ||
| import static com.keenwrite.preferences.WorkspaceKeys.KEY_IMAGES_DIR; | ||
| import static com.keenwrite.preferences.WorkspaceKeys.KEY_IMAGES_ORDER; | ||
| -import static java.io.File.createTempFile; | ||
| +import static com.keenwrite.util.FileUtils.createTemporaryFile; | ||
| import static java.lang.String.format; | ||
| import static java.nio.file.Files.copy; | ||
| private Workspace getWorkspace() { | ||
| return mContext.getWorkspace(); | ||
| - } | ||
| - | ||
| - private static Path createTemporaryFile( final MediaType media ) | ||
| - throws IOException { | ||
| - final var file = createTempFile( | ||
| - APP_TITLE_LOWERCASE, '.' + valueFrom( media ).getExtension() ); | ||
| - file.deleteOnExit(); | ||
| - return file.toPath(); | ||
| } | ||
| } | ||
| +/* Copyright 2020-2021 White Magic Software, Ltd. -- All rights reserved. */ | ||
| +package com.keenwrite.typesetting; | ||
| + | ||
| +import java.io.File; | ||
| +import java.nio.file.Path; | ||
| + | ||
| +import static com.keenwrite.util.FileUtils.canExecute; | ||
| + | ||
| +/** | ||
| + * Represents the executable responsible for typesetting text. This will | ||
| + * construct suitable command-line arguments to invoke the typesetting engine. | ||
| + */ | ||
| +public class Typesetter { | ||
| + private static final String TYPESETTER = "context"; | ||
| + | ||
| + public Typesetter() { | ||
| + } | ||
| + | ||
| + public boolean isInstalled() { | ||
| + return canExecute( TYPESETTER ); | ||
| + } | ||
| + | ||
| + /** | ||
| + * This will typeset the document using a new process. | ||
| + * | ||
| + * @param input The input document to typeset. | ||
| + * @param output Path to the finished typeset document. | ||
| + */ | ||
| + public void typeset( final Path input, final File output ) { | ||
| + | ||
| + } | ||
| +} | ||
| +package com.keenwrite.util; | ||
| + | ||
| +import com.keenwrite.io.MediaType; | ||
| + | ||
| +import java.io.IOException; | ||
| +import java.nio.file.Path; | ||
| +import java.nio.file.Paths; | ||
| +import java.util.stream.Stream; | ||
| + | ||
| +import static com.keenwrite.Bootstrap.APP_TITLE_LOWERCASE; | ||
| +import static com.keenwrite.io.MediaTypeExtension.valueFrom; | ||
| +import static java.io.File.createTempFile; | ||
| +import static java.io.File.pathSeparator; | ||
| +import static java.lang.System.getenv; | ||
| +import static java.nio.file.Files.isExecutable; | ||
| +import static java.util.regex.Pattern.quote; | ||
| + | ||
| +/** | ||
| + * Place for common file-related functionality. | ||
| + */ | ||
| +public class FileUtils { | ||
| + /** | ||
| + * For finding executable programs. | ||
| + */ | ||
| + private static final String[] EXTENSIONS = new String[] | ||
| + {"", ".com", ".exe", ".bat", ".cmd"}; | ||
| + | ||
| + private FileUtils() {} | ||
| + | ||
| + /** | ||
| + * Creates a temporary file for the given {@link MediaType}, which will be | ||
| + * deleted when the application exits. | ||
| + * | ||
| + * @param media The type of file to create (i.e., its extension). | ||
| + * @return The fully qualified path to a file. | ||
| + * @throws IOException Could not create the temporary file. | ||
| + */ | ||
| + public static Path createTemporaryFile( final MediaType media ) | ||
| + throws IOException { | ||
| + final var file = createTempFile( | ||
| + APP_TITLE_LOWERCASE, '.' + valueFrom( media ).getExtension() ); | ||
| + file.deleteOnExit(); | ||
| + return file.toPath(); | ||
| + } | ||
| + | ||
| + /** | ||
| + * Given the name of an executable (without an extension) file, this will | ||
| + * attempt to determine whether the executable is found in the PATH | ||
| + * environment variable. | ||
| + * | ||
| + * @param exe The executable file name to find. | ||
| + * @return {@code true} when the given file name references an executable | ||
| + * file located in the PATH environment variable. | ||
| + */ | ||
| + public static boolean canExecute( final String exe ) { | ||
| + final var paths = getenv( "PATH" ).split( quote( pathSeparator ) ); | ||
| + return Stream.of( paths ).map( Paths::get ).anyMatch( | ||
| + path -> { | ||
| + final var p = path.resolve( exe ); | ||
| + | ||
| + for( final var extension : EXTENSIONS ) { | ||
| + if( isExecutable( Path.of( p.toString() + extension ) ) ) { | ||
| + return true; | ||
| + } | ||
| + } | ||
| + | ||
| + return false; | ||
| + } | ||
| + ); | ||
| + } | ||
| +} | ||
| import java.nio.file.Paths; | ||
| import java.util.function.Consumer; | ||
| -import java.util.stream.Stream; | ||
| import static com.keenwrite.util.ProtocolScheme.JAR; | ||
| import static com.keenwrite.util.ProtocolScheme.valueFrom; | ||
| -import static java.io.File.pathSeparator; | ||
| -import static java.lang.System.getenv; | ||
| import static java.nio.file.FileSystems.getDefault; | ||
| import static java.nio.file.FileSystems.newFileSystem; | ||
| -import static java.nio.file.Files.isExecutable; | ||
| import static java.util.Collections.emptyMap; | ||
| -import static java.util.regex.Pattern.quote; | ||
| /** | ||
| * Responsible for finding file resources. | ||
| */ | ||
| public final class ResourceWalker { | ||
| - /** | ||
| - * For finding executable programs. | ||
| - */ | ||
| - private static final String[] EXTENSIONS = new String[] | ||
| - {"", ".com", ".exe", ".bat", ".cmd"}; | ||
| /** | ||
| } | ||
| } | ||
| - } | ||
| - | ||
| - /** | ||
| - * Given the name of an executable (without an extension) file, this will | ||
| - * attempt to determine whether the executable is found in the PATH | ||
| - * environment variable. | ||
| - * | ||
| - * @param exe The executable file name to find. | ||
| - * @return {@code true} when the given file name references an executable | ||
| - * file located in the PATH environment variable. | ||
| - */ | ||
| - public static boolean canExecute( final String exe ) { | ||
| - final var paths = getenv( "PATH" ).split( quote( pathSeparator ) ); | ||
| - return Stream.of( paths ).map( Paths::get ).anyMatch( | ||
| - path -> { | ||
| - final var p = path.resolve( exe ); | ||
| - var found = false; | ||
| - | ||
| - for( final var extension : EXTENSIONS ) { | ||
| - if( isExecutable( Path.of( p.toString() + extension ) ) ) { | ||
| - found = true; | ||
| - break; | ||
| - } | ||
| - } | ||
| - | ||
| - return found; | ||
| - } | ||
| - ); | ||
| } | ||
| } | ||
| Delta | 121 lines added, 55 lines removed, 66-line increase |
|---|