Dave Jarvis' Repositories

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

Refactors code

AuthorDaveJarvis <email>
Date2023-11-25 20:41:43 GMT-0800
Commit2c7bd04cae82531f252b6da268c30aed89db6b13
Parenta737885
src/main/java/com/keenwrite/processors/markdown/extensions/captions/CaptionExtension.java
package com.keenwrite.processors.markdown.extensions.captions;
-import com.keenwrite.processors.markdown.extensions.HtmlRendererAdapter;
-import com.vladsch.flexmark.html.HtmlRenderer;
-import com.vladsch.flexmark.parser.InlineParser;
-import com.vladsch.flexmark.parser.Parser;
-import com.vladsch.flexmark.parser.Parser.ParserExtension;
+import com.keenwrite.processors.markdown.extensions.common.MarkdownExtension;
+import com.vladsch.flexmark.html.renderer.NodeRendererFactory;
+import com.vladsch.flexmark.parser.Parser.Builder;
import com.vladsch.flexmark.parser.block.*;
-import com.vladsch.flexmark.util.ast.Block;
import com.vladsch.flexmark.util.data.DataHolder;
-import com.vladsch.flexmark.util.data.MutableDataHolder;
-import com.vladsch.flexmark.util.sequence.BasedSequence;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Set;
/**
* Responsible for parsing and rendering {@link CaptionBlock} instances.
*/
-public class CaptionExtension extends HtmlRendererAdapter
- implements ParserExtension {
-
+public final class CaptionExtension extends MarkdownExtension {
/**
- * @see #create()
+ * Use {@link #create()}.
*/
private CaptionExtension() {}
/**
- * Returns an instance of extension that can be added to the
- * {@link Parser.Builder} and {@link HtmlRenderer.Builder} extensions.
+ * Returns a new {@link CaptionExtension}.
*
* @return An extension capable of parsing caption syntax.
*/
public static CaptionExtension create() {
return new CaptionExtension();
}
@Override
- public void extend( final Parser.Builder builder ) {
+ public void extend( final Builder builder ) {
builder.customBlockParserFactory( new CaptionBlockParserFactory() );
- }
-
- @Override
- public void extend(
- @NotNull final HtmlRenderer.Builder builder,
- @NotNull final String rendererType ) {
- if( "HTML".equalsIgnoreCase( rendererType ) ) {
- builder.nodeRendererFactory( new CaptionNodeRenderer.Factory() );
- }
}
@Override
- public void parserOptions( final MutableDataHolder options ) {}
-
- private static class CaptionParser extends AbstractBlockParser {
- private final CaptionBlock mBlock;
-
- private CaptionParser( final BasedSequence text ) {
- assert text != null;
- assert text.isNotEmpty();
- assert text.length() > 2;
-
- final var caption = text.subSequence( 2 );
-
- mBlock = new CaptionBlock( caption.trim() );
- }
-
- private static boolean canParse( final BasedSequence text ) {
- return text.length() > 3 &&
- text.charAt( 0 ) == ':' &&
- text.charAt( 1 ) == ':' &&
- text.charAt( 2 ) != ':';
- }
-
- @Override
- public Block getBlock() {
- return mBlock;
- }
-
- @Override
- public BlockContinue tryContinue( final ParserState state ) {
- return BlockContinue.none();
- }
-
- @Override
- public void parseInlines( final InlineParser inlineParser ) {
- assert inlineParser != null;
-
- mBlock.parse( inlineParser );
- }
-
- @Override
- public void closeBlock( final ParserState state ) {}
+ protected NodeRendererFactory createNodeRendererFactory() {
+ return new CaptionNodeRenderer.Factory();
}
src/main/java/com/keenwrite/processors/markdown/extensions/captions/CaptionParser.java
+package com.keenwrite.processors.markdown.extensions.captions;
+
+import com.vladsch.flexmark.parser.InlineParser;
+import com.vladsch.flexmark.parser.block.AbstractBlockParser;
+import com.vladsch.flexmark.parser.block.BlockContinue;
+import com.vladsch.flexmark.parser.block.ParserState;
+import com.vladsch.flexmark.util.ast.Block;
+import com.vladsch.flexmark.util.sequence.BasedSequence;
+
+class CaptionParser extends AbstractBlockParser {
+ private final CaptionBlock mBlock;
+
+ CaptionParser( final BasedSequence text ) {
+ assert text != null;
+ assert text.isNotEmpty();
+ assert text.length() > 2;
+
+ final var caption = text.subSequence( 2 );
+
+ mBlock = new CaptionBlock( caption.trim() );
+ }
+
+ static boolean canParse( final BasedSequence text ) {
+ return text.length() > 3 &&
+ text.charAt( 0 ) == ':' &&
+ text.charAt( 1 ) == ':' &&
+ text.charAt( 2 ) != ':';
+ }
+
+ @Override
+ public Block getBlock() {
+ return mBlock;
+ }
+
+ @Override
+ public BlockContinue tryContinue( final ParserState state ) {
+ return BlockContinue.none();
+ }
+
+ @Override
+ public void parseInlines( final InlineParser inlineParser ) {
+ assert inlineParser != null;
+
+ mBlock.parse( inlineParser );
+ }
+
+ @Override
+ public void closeBlock( final ParserState state ) {}
+}
src/main/java/com/keenwrite/processors/markdown/extensions/common/MarkdownExtension.java
+/* Copyright 2023 White Magic Software, Ltd. -- All rights reserved.
+ *
+ * SPDX-License-Identifier: MIT
+ */
+package com.keenwrite.processors.markdown.extensions.common;
+
+import com.keenwrite.processors.markdown.extensions.HtmlRendererAdapter;
+import com.vladsch.flexmark.html.HtmlRenderer.Builder;
+import com.vladsch.flexmark.html.renderer.NodeRenderer;
+import com.vladsch.flexmark.html.renderer.NodeRendererFactory;
+import com.vladsch.flexmark.parser.Parser;
+import com.vladsch.flexmark.util.data.MutableDataHolder;
+import org.jetbrains.annotations.NotNull;
+
+public abstract class MarkdownExtension extends HtmlRendererAdapter
+ implements Parser.ParserExtension {
+
+ /**
+ * Implemented by subclasses to create the {@link NodeRendererFactory} capable
+ * of converting nodes created by an extension into HTML elements.
+ *
+ * @return The {@link NodeRendererFactory} for producing {@link NodeRenderer}
+ * instances.
+ */
+ protected abstract NodeRendererFactory createNodeRendererFactory();
+
+ /**
+ * Adds an extension for HTML document export types.
+ *
+ * @param builder The document builder.
+ * @param rendererType Indicates the document type to be built.
+ */
+ @Override
+ public void extend(
+ @NotNull final Builder builder,
+ @NotNull final String rendererType ) {
+ if( "HTML".equalsIgnoreCase( rendererType ) ) {
+ builder.nodeRendererFactory( createNodeRendererFactory() );
+ }
+ }
+
+ @Override
+ public void parserOptions( final MutableDataHolder options ) {}
+}
src/main/java/com/keenwrite/processors/markdown/extensions/fences/FencedDivExtension.java
package com.keenwrite.processors.markdown.extensions.fences;
-import com.keenwrite.processors.markdown.extensions.HtmlRendererAdapter;
-import com.vladsch.flexmark.html.HtmlRenderer;
-import com.vladsch.flexmark.parser.Parser;
+import com.keenwrite.processors.markdown.extensions.common.MarkdownExtension;
+import com.vladsch.flexmark.html.renderer.NodeRendererFactory;
+import com.vladsch.flexmark.parser.Parser.Builder;
import com.vladsch.flexmark.parser.block.*;
import com.vladsch.flexmark.util.ast.Block;
import com.vladsch.flexmark.util.data.DataHolder;
-import com.vladsch.flexmark.util.data.MutableDataHolder;
import com.vladsch.flexmark.util.html.Attribute;
import com.vladsch.flexmark.util.html.AttributeImpl;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Set;
import java.util.regex.Pattern;
-import static com.vladsch.flexmark.parser.Parser.ParserExtension;
-import static java.util.regex.Pattern.*;
+import static java.util.regex.Pattern.UNICODE_CHARACTER_CLASS;
+import static java.util.regex.Pattern.compile;
/**
* </p>
*/
-public class FencedDivExtension extends HtmlRendererAdapter
- implements ParserExtension {
+public class FencedDivExtension extends MarkdownExtension {
/**
* Matches any number of colons at start of line. This will match both the
public static FencedDivExtension create() {
return new FencedDivExtension();
- }
-
- @Override
- public void parserOptions( final MutableDataHolder options ) {
}
@Override
- public void extend( final Parser.Builder builder ) {
+ public void extend( final Builder builder ) {
builder.customBlockParserFactory( new Factory() );
}
- /**
- * Creates a renderer that can generate HTML div elements.
- *
- * @param builder The document builder.
- * @param rendererType Indicates the document type to be built.
- */
@Override
- public void extend( @NotNull final HtmlRenderer.Builder builder,
- @NotNull final String rendererType ) {
- if( "HTML".equalsIgnoreCase( rendererType ) ) {
- builder.nodeRendererFactory( new FencedDivRenderer.Factory() );
- }
+ protected NodeRendererFactory createNodeRendererFactory() {
+ return new FencedDivRenderer.Factory();
}
src/main/java/com/keenwrite/processors/markdown/extensions/references/BasedSequenceNameParser.java
import java.util.regex.Pattern;
-import static java.util.regex.Pattern.UNICODE_CHARACTER_CLASS;
-import static java.util.regex.Pattern.compile;
-
class BasedSequenceNameParser extends BasedSequenceParser {
private static final String REGEX = STR. "#\{ REGEX_INNER }" ;
- private static final Pattern PATTERN = compile(
- REGEX, UNICODE_CHARACTER_CLASS
- );
+ private static final Pattern PATTERN = asPattern( REGEX );
private BasedSequenceNameParser( final String text ) {
src/main/java/com/keenwrite/processors/markdown/extensions/references/BasedSequenceParser.java
import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import static java.util.regex.Pattern.UNICODE_CHARACTER_CLASS;
abstract class BasedSequenceParser {
mIdName = null;
}
+ }
+
+ static Pattern asPattern( final String regex ) {
+ return Pattern.compile( regex, UNICODE_CHARACTER_CLASS );
}
src/main/java/com/keenwrite/processors/markdown/extensions/references/BasedSequenceXrefParser.java
import static com.keenwrite.processors.markdown.extensions.EmptyNode.EMPTY_NODE;
-import static java.util.regex.Pattern.UNICODE_CHARACTER_CLASS;
-import static java.util.regex.Pattern.compile;
class BasedSequenceXrefParser extends BasedSequenceParser {
private static final String REGEX = STR. "\\[@\{ REGEX_INNER }]" ;
- private static final Pattern PATTERN = compile(
- REGEX, UNICODE_CHARACTER_CLASS
- );
+ private static final Pattern PATTERN = asPattern( REGEX );
- BasedSequenceXrefParser( final String text ) {
+ private BasedSequenceXrefParser( final String text ) {
super( text );
}
src/main/java/com/keenwrite/processors/markdown/extensions/references/CrossReferenceExtension.java
package com.keenwrite.processors.markdown.extensions.references;
-import com.keenwrite.processors.markdown.extensions.HtmlRendererAdapter;
-import com.vladsch.flexmark.html.HtmlRenderer;
-import com.vladsch.flexmark.parser.Parser;
-import com.vladsch.flexmark.parser.Parser.ParserExtension;
-import com.vladsch.flexmark.util.data.MutableDataHolder;
-import org.jetbrains.annotations.NotNull;
+import com.keenwrite.processors.markdown.extensions.common.MarkdownExtension;
+import com.vladsch.flexmark.html.renderer.NodeRendererFactory;
+import com.vladsch.flexmark.parser.Parser.Builder;
/**
* Responsible for processing {@code {@type:id}} anchors and their corresponding
* {@code [@type:id]} cross-references.
*/
-public class CrossReferenceExtension extends HtmlRendererAdapter
- implements ParserExtension {
+public final class CrossReferenceExtension extends MarkdownExtension {
+ /**
+ * Use {@link #create()}.
+ */
+ private CrossReferenceExtension() {}
/**
- * Returns a new instance of {@link CrossReferenceExtension}. This is here
- * for consistency with the other extensions.
+ * Returns a new {@link CrossReferenceExtension}.
*
- * @return A new {@link CrossReferenceExtension} instance.
+ * @return An extension capable of parsing cross-reference syntax.
*/
public static CrossReferenceExtension create() {
return new CrossReferenceExtension();
}
@Override
- public void extend( final Parser.Builder builder ) {
+ public void extend( final Builder builder ) {
builder.linkRefProcessorFactory( new AnchorXrefProcessorFactory() );
builder.customDelimiterProcessor( new AnchorNameDelimiterProcessor() );
}
@Override
- public void extend( @NotNull final HtmlRenderer.Builder builder,
- @NotNull final String rendererType ) {
- if( "HTML".equalsIgnoreCase( rendererType ) ) {
- builder.nodeRendererFactory( new CrossReferencesNodeRenderer.Factory() );
- }
+ protected NodeRendererFactory createNodeRendererFactory() {
+ return new CrossReferencesNodeRenderer.Factory();
}
-
- @Override
- public void parserOptions( final MutableDataHolder options ) {}
}
src/main/java/com/keenwrite/processors/markdown/extensions/tex/TexExtension.java
import com.keenwrite.ExportFormat;
import com.keenwrite.processors.ProcessorContext;
-import com.keenwrite.processors.markdown.extensions.HtmlRendererAdapter;
-import com.keenwrite.processors.markdown.extensions.tex.TexNodeRenderer.Factory;
-import com.vladsch.flexmark.html.HtmlRenderer;
+import com.keenwrite.processors.markdown.extensions.common.MarkdownExtension;
+import com.keenwrite.processors.markdown.extensions.tex.TexNodeRenderer.TexNodeRendererFactory;
+import com.vladsch.flexmark.html.renderer.NodeRendererFactory;
import com.vladsch.flexmark.parser.Parser;
-import com.vladsch.flexmark.util.data.MutableDataHolder;
-import org.jetbrains.annotations.NotNull;
import java.util.function.Function;
-
-import static com.vladsch.flexmark.parser.Parser.ParserExtension;
/**
* performance.
*/
-public class TexExtension extends HtmlRendererAdapter
- implements ParserExtension {
-
+public class TexExtension extends MarkdownExtension {
/**
* Responsible for pre-parsing the input.
private TexExtension(
- final Function<String, String> evaluator, final ProcessorContext context ) {
+ final Function<String, String> evaluator,
+ final ProcessorContext context ) {
mEvaluator = evaluator;
mExportFormat = context.getExportFormat();
/**
- * Adds the TeX extension for HTML document export types.
- *
- * @param builder The document builder.
- * @param rendererType Indicates the document type to be built.
+ * Creates the TeX {@link NodeRendererFactory} for HTML document export types.
*/
@Override
- public void extend( @NotNull final HtmlRenderer.Builder builder,
- @NotNull final String rendererType ) {
- if( "HTML".equalsIgnoreCase( rendererType ) ) {
- builder.nodeRendererFactory( new Factory( mExportFormat, mEvaluator ) );
- }
+ public NodeRendererFactory createNodeRendererFactory() {
+ return new TexNodeRendererFactory( mExportFormat, mEvaluator );
}
@Override
public void extend( final Parser.Builder builder ) {
builder.customDelimiterProcessor( new TexInlineDelimiterProcessor() );
}
-
- @Override
- public void parserOptions( final MutableDataHolder options ) {}
}
src/main/java/com/keenwrite/processors/markdown/extensions/tex/TexNodeRenderer.java
);
- public static class Factory implements NodeRendererFactory {
+ public static class TexNodeRendererFactory implements NodeRendererFactory {
private final RendererFacade mNodeRenderer;
- public Factory(
+ public TexNodeRendererFactory(
final ExportFormat exportFormat,
final Function<String, String> evaluator ) {
Delta145 lines added, 146 lines removed, 1-line decrease