Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/keenwrite.git

Provide caret supplier to decouple processor context from text editor

AuthorDaveJarvis <email>
Date2021-12-15 23:15:29 GMT-0800
Commit73f301770248aa30dd046e7adda8808e182e7510
Parentce316ab
Delta35 lines added, 91 lines removed, 56-line decrease
src/test/java/com/keenwrite/processors/markdown/ImageLinkExtensionTest.java
import com.keenwrite.Caret;
import com.keenwrite.preferences.Workspace;
-import com.keenwrite.preview.HtmlPreview;
import com.keenwrite.processors.Processor;
import com.keenwrite.processors.ProcessorContext;
import com.keenwrite.processors.markdown.extensions.ImageLinkExtension;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
-import javafx.beans.property.SimpleObjectProperty;
import javafx.stage.Stage;
import org.junit.jupiter.api.Test;
import java.util.Map;
-import static com.keenwrite.ExportFormat.NONE;
+import static com.keenwrite.ExportFormat.XHTML_TEX;
import static com.keenwrite.constants.Constants.DOCUMENT_DEFAULT;
import static java.lang.String.format;
addUri( "https://" + URI_WEB );
}
-
- private HtmlPreview mPreview;
@Start
@SuppressWarnings( "unused" )
private void start( final Stage stage ) {
- mPreview = new HtmlPreview( sWorkspace );
}
* Creates a new {@link ProcessorContext} for the given file name path.
*
- * @param documentPath Fully qualified path to the file name.
+ * @param inputPath Fully qualified path to the file name.
* @return A context used for creating new {@link Processor} instances.
*/
- private ProcessorContext createProcessorContext( final Path documentPath ) {
- return new ProcessorContext(
- mPreview,
- new SimpleObjectProperty<>(),
- documentPath,
- null,
- NONE,
- sWorkspace,
- Caret.builder().build()
- );
+ private ProcessorContext createProcessorContext( final Path inputPath ) {
+ return ProcessorContext
+ .builder()
+ .with( ProcessorContext.Mutator::setInputPath, inputPath )
+ .with( ProcessorContext.Mutator::setExportFormat, XHTML_TEX )
+ .with( ProcessorContext.Mutator::setWorkspace, sWorkspace )
+ .with( ProcessorContext.Mutator::setCaret, () -> Caret.builder().build() )
+ .build();
}
src/main/java/com/keenwrite/processors/ProcessorContext.java
import java.util.Map;
import java.util.concurrent.Callable;
+import java.util.function.Supplier;
import static com.keenwrite.AbstractFileFactory.lookup;
private Path mOutputPath;
private ExportFormat mExportFormat;
- private Callable<Map<String, String>> mDefinitions;
- private Caret mCaret;
+ private Supplier<Map<String, String>> mDefinitions;
+ private Supplier<Caret> mCaret;
private Workspace mWorkspace;
*/
public void setDefinitions(
- final Callable<Map<String, String>> definitions ) {
+ final Supplier<Map<String, String>> definitions ) {
mDefinitions = definitions;
}
- public void setCaret( final Caret caret ) {
+ /**
+ * Sets the source for deriving the {@link Caret}. Typically, this is
+ * the text editor that has focus.
+ *
+ * @param caret The source for the currently active caret.
+ */
+ public void setCaret( final Supplier<Caret> caret ) {
mCaret = caret;
}
public static GenericBuilder<Mutator, ProcessorContext> builder() {
return GenericBuilder.of( Mutator::new, ProcessorContext::new );
- }
-
- /**
- * @param inputPath Path to the document to process.
- * @param outputPath Fully qualified filename to use when exporting.
- * @param format Indicate configuration options for export format.
- * @param definitions Source for fully expanded interpolated strings.
- * @param workspace Persistent user preferences settings.
- * @param caret Location of the caret in the edited document,
- * which is used to synchronize the scrollbars.
- * @return A context that may be used for processing documents.
- */
- public static ProcessorContext create(
- final Path inputPath,
- final Path outputPath,
- final ExportFormat format,
- final Callable<Map<String, String>> definitions,
- final Workspace workspace,
- final Caret caret ) {
- return ProcessorContext
- .builder()
- .with( Mutator::setInputPath, inputPath )
- .with( Mutator::setOutputPath, outputPath )
- .with( Mutator::setExportFormat, format )
- .with( Mutator::setDefinitions, definitions )
- .with( Mutator::setWorkspace, workspace )
- .with( Mutator::setCaret, caret )
- .build();
}
final Path inputPath,
final ExportFormat format ) {
- return builder()
- .with( Mutator::setInputPath, inputPath )
- .with( Mutator::setExportFormat, format )
- .build();
- }
-
- /**
- * @param inputPath Path to the document to process.
- * @param outputPath Fully qualified filename to use when exporting.
- * @param format Indicate configuration options for export format.
- * @return A context that may be used for processing documents.
- */
- public static ProcessorContext create(
- final Path inputPath, final Path outputPath, final ExportFormat format ) {
return builder()
.with( Mutator::setInputPath, inputPath )
- .with( Mutator::setOutputPath, outputPath )
.with( Mutator::setExportFormat, format )
.build();
*/
Map<String, String> getResolvedMap() {
- try {
- return mMutator.mDefinitions.call();
- } catch( final Exception ex ) {
- // If this happens, it is a programming error because the definitions
- // list must always return a valid map of variable names to values.
- throw new RuntimeException( ex );
- }
+ return mMutator.mDefinitions.get();
}
* @return Caret position in the document.
*/
- public Caret getCaret() {
+ public Supplier<Caret> getCaret() {
return mMutator.mCaret;
}
*/
public Path getBaseDir() {
- final var path = getDocumentPath().toAbsolutePath().getParent();
+ final var path = getInputPath().toAbsolutePath().getParent();
return path == null ? DEFAULT_DIRECTORY : path;
}
- public Path getDocumentPath() {
+ public Path getInputPath() {
return mMutator.mInputPath;
}
FileType getFileType() {
- return lookup( getDocumentPath() );
+ return lookup( getInputPath() );
}
src/main/java/com/keenwrite/processors/ProcessorFactory.java
import com.keenwrite.AbstractFileFactory;
-import com.keenwrite.preview.HtmlPreview;
import com.keenwrite.processors.markdown.MarkdownProcessor;
private ProcessorFactory() {
+ }
+
+ public static Processor<String> createProcessors(
+ final ProcessorContext context ) {
+ return createProcessors( context, null );
}
*/
public static Processor<String> createProcessors(
- final ProcessorContext context, final HtmlPreview preview ) {
+ final ProcessorContext context, final Processor<String> preview ) {
return ProcessorFactory.createProcessor( context, preview );
}
/**
* Constructs processors that chain various processing operations on a
* document to generate a transformed version of the source document.
*
* @param context Parameters needed to construct various processors.
+ * @param preview The processor to use when no export format is specified.
*/
private static Processor<String> createProcessor(
- final ProcessorContext context, final HtmlPreview preview ) {
+ final ProcessorContext context, final Processor<String> preview ) {
// If the content is not to be exported, then the successor processor
// is one that parses Markdown into HTML and passes the string to the
// math (such as using the JavaScript-based KaTeX engine).
final var successor = switch( context.getExportFormat() ) {
- case NONE -> createHtmlPreviewProcessor( preview );
+ case NONE -> preview;
case XHTML_TEX -> createXhtmlProcessor( context );
case APPLICATION_PDF -> createPdfProcessor( context );
final ProcessorContext ignored ) {
return IDENTITY;
- }
-
- /**
- * Instantiates a new {@link Processor} that passes an incoming HTML
- * string to a user interface widget that can render HTML as a web page.
- *
- * @return An instance of {@link Processor} that forwards HTML for display.
- */
- private static Processor<String> createHtmlPreviewProcessor(
- final HtmlPreview preview ) {
- return new HtmlPreviewProcessor( preview );
}
-
/**
* Instantiates a {@link Processor} responsible for parsing Markdown and