Dave Jarvis' Repositories

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

Enhance processors with 'file export' ability

AuthorDaveJarvis <email>
Date2020-09-22 00:08:30 GMT-0700
Commit414341b7d9e83669926948d73b6cfc89ae07e945
Parent001108c
Delta146 lines added, 56 lines removed, 90-line increase
src/main/java/com/keenwrite/processors/HtmlPreProcessor.java
+/*
+ * Copyright 2017 White Magic Software, Ltd.
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * o Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * o Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package com.keenwrite.processors;
+
+/**
+ * This is the default processor used when an unknown filename extension is
+ * encountered. It processes the text by enclosing it in an HTML {@code <pre>}
+ * element.
+ */
+public class HtmlPreProcessor extends AbstractProcessor<String> {
+
+ /**
+ * Passes the link to the super constructor.
+ *
+ * @param successor The next processor in the chain to use for text
+ * processing.
+ */
+ public HtmlPreProcessor( final Processor<String> successor ) {
+ super( successor );
+ }
+
+ /**
+ * Returns the given string, modified with "pre" tags.
+ *
+ * @param t The string to return, enclosed in "pre" tags.
+ * @return The value of t wrapped in "pre" tags.
+ */
+ @Override
+ public String apply( final String t ) {
+ return "<pre>" + t + "</pre>";
+ }
+}
src/main/java/com/keenwrite/processors/IdentityProcessor.java
/**
- * This is the default processor used when an unknown filename extension is
- * encountered.
+ * Responsible for transforming a string into itself. This is typically used
+ * at the end of a processing chain when no more processing is required, such
+ * as when exporting files.
*/
public class IdentityProcessor extends AbstractProcessor<String> {
/**
- * Returns the given string, modified with "pre" tags.
+ * Returns the given string without modification.
*
- * @param t The string to return, enclosed in "pre" tags.
- * @return The value of t wrapped in "pre" tags.
+ * @param s The string to return.
+ * @return The value of s.
*/
@Override
- public String apply( final String t ) {
- return "<pre>" + t + "</pre>";
+ public String apply( final String s ) {
+ return s;
}
}
src/main/java/com/keenwrite/processors/ProcessorContext.java
}
- public HTMLPreviewPane getPreviewPane() {
+ HTMLPreviewPane getPreviewPane() {
return mPreviewPane;
}
- public Map<String, String> getResolvedMap() {
+ Map<String, String> getResolvedMap() {
return mResolvedMap;
}
- public Path getPath() {
+ Path getPath() {
return mPath;
}
- public FileType getFileType() {
+ FileType getFileType() {
return mFileType;
}
- public boolean isExportFormat( final ExportFormat format ) {
+ @SuppressWarnings("SameParameterValue")
+ boolean isExportFormat( final ExportFormat format ) {
return mExportFormat == format;
+ }
+
+ ExportFormat getExportFormat() {
+ return mExportFormat;
}
}
src/main/java/com/keenwrite/processors/ProcessorFactory.java
import com.keenwrite.AbstractFileFactory;
-import com.keenwrite.ExportFormat;
import com.keenwrite.preview.HTMLPreviewPane;
import com.keenwrite.processors.markdown.MarkdownProcessor;
import java.nio.file.Path;
import java.util.Map;
+
+import static com.keenwrite.ExportFormat.NONE;
/**
private Processor<String> createProcessor() {
- final ProcessorContext context = getContext();
- final Processor<String> processor;
+ final ProcessorContext context = getProcessorContext();
- if( context.isExportFormat( ExportFormat.NONE ) ) {
- processor = switch( context.getFileType() ) {
- case RMARKDOWN -> createRProcessor();
- case SOURCE -> createMarkdownDefinitionProcessor();
- case XML -> createXMLProcessor();
- case RXML -> createRXMLProcessor();
- default -> createIdentityProcessor();
- };
+ final Processor<String> successor;
+
+ if( context.isExportFormat( NONE ) ) {
+ successor = getCommonProcessor();
}
else {
- processor = null;
+ successor = switch( context.getExportFormat() ) {
+ case HTML_SVG -> createHtmlSvgProcessor();
+ case HTML_TEX -> createHtmlTexProcessor();
+ case MARKDOWN_PLAIN -> createMarkdownPlainProcessor();
+ case NONE -> null;
+ };
}
- return processor;
+ return switch( context.getFileType() ) {
+ case RMARKDOWN -> createRProcessor( successor );
+ case SOURCE -> createMarkdownDefinitionProcessor( successor );
+ case RXML -> createRXMLProcessor( successor );
+ case XML -> createXMLProcessor( successor );
+ default -> createHtmlPreProcessor();
+ };
}
}
- protected Processor<String> createIdentityProcessor() {
- final var hpp = createHTMLPreviewProcessor();
- return new IdentityProcessor( hpp );
+ private Processor<String> createHtmlPreProcessor(
+ final Processor<String> successor ) {
+ return new HtmlPreProcessor( successor );
}
- protected Processor<String> createDefinitionProcessor(
- final Processor<String> p ) {
- return new DefinitionProcessor( p, getResolvedMap() );
+ private Processor<String> createIdentityProcessor() {
+ return new IdentityProcessor( null );
}
- protected Processor<String> createMarkdownDefinitionProcessor() {
- final var tpc = getCommonProcessor();
- return createDefinitionProcessor( tpc );
+ private Processor<String> createHtmlPreProcessor() {
+ return createHtmlPreProcessor( createHTMLPreviewProcessor() );
}
- protected Processor<String> createXMLProcessor() {
- final var tpc = getCommonProcessor();
- final var xmlp = new XmlProcessor( tpc, getPath() );
- return createDefinitionProcessor( xmlp );
+ private Processor<String> createDefinitionProcessor(
+ final Processor<String> successor ) {
+ return new DefinitionProcessor( successor, getResolvedMap() );
}
- private Processor<String> createRProcessor() {
- final var tpc = getCommonProcessor();
- final var rp = new InlineRProcessor( tpc, getResolvedMap() );
+ private Processor<String> createRProcessor(
+ final Processor<String> successor ) {
+ final var rp = new InlineRProcessor( successor, getResolvedMap() );
return new RVariableProcessor( rp, getResolvedMap() );
}
- protected Processor<String> createRXMLProcessor() {
- final var tpc = getCommonProcessor();
- final var xmlp = new XmlProcessor( tpc, getPath() );
+ private Processor<String> createMarkdownDefinitionProcessor(
+ final Processor<String> successor ) {
+ return createDefinitionProcessor( successor );
+ }
+
+ protected Processor<String> createRXMLProcessor(
+ final Processor<String> successor ) {
+ final var xmlp = new XmlProcessor( successor, getPath() );
final var rp = new InlineRProcessor( xmlp, getResolvedMap() );
return new RVariableProcessor( rp, getResolvedMap() );
}
- private HTMLPreviewPane getPreviewPane() {
- return getContext().getPreviewPane();
+ private Processor<String> createXMLProcessor(
+ final Processor<String> successor ) {
+ final var xmlp = new XmlProcessor( successor, getPath() );
+ return createDefinitionProcessor( xmlp );
}
- /**
- * Returns the variable map of interpolated definitions.
- *
- * @return A map to help dereference variables.
- */
- private Map<String, String> getResolvedMap() {
- return getContext().getResolvedMap();
+ private Processor<String> createHtmlSvgProcessor() {
+ return new MarkdownProcessor( null, getPreviewPane().getPath() );
}
- private Path getPath() {
- return getContext().getPath();
+ private Processor<String> createHtmlTexProcessor() {
+ return new MarkdownProcessor( null, getPreviewPane().getPath() );
+ }
+
+ private Processor<String> createMarkdownPlainProcessor() {
+ return createIdentityProcessor();
}
}
- private ProcessorContext getContext() {
+ private ProcessorContext getProcessorContext() {
return mProcessorContext;
+ }
+
+ private HTMLPreviewPane getPreviewPane() {
+ return getProcessorContext().getPreviewPane();
+ }
+
+ /**
+ * Returns the variable map of interpolated definitions.
+ *
+ * @return A map to help dereference variables.
+ */
+ private Map<String, String> getResolvedMap() {
+ return getProcessorContext().getResolvedMap();
+ }
+
+ private Path getPath() {
+ return getProcessorContext().getPath();
}
}