Dave Jarvis' Repositories

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

Rename decorators to sigil operators, variables to definitions

AuthorDaveJarvis <email>
Date2020-08-23 17:18:50 GMT-0700
Commit8367d5bb455cdad88b25d53f37e8ee54f73e44d9
Parentc407c8a
Delta188 lines added, 13 lines removed, 175-line increase
src/main/java/com/scrivenvar/sigils/RVariableDecorator.java
+/*
+ * Copyright 2020 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.scrivenvar.sigils;
+
+import com.scrivenvar.Services;
+import com.scrivenvar.preferences.UserPreferences;
+import com.scrivenvar.service.Options;
+
+/**
+ * Brackets variable names between {@link #PREFIX} and {@link #SUFFIX} sigils.
+ */
+public class RVariableDecorator implements SigilOperator {
+ private static final Options sOptions = Services.load( Options.class );
+
+ public static final String PREFIX = "`r#";
+ public static final char SUFFIX = '`';
+
+ private final String mDelimiterBegan =
+ getUserPreferences().getRDelimiterBegan();
+ private final String mDelimiterEnded =
+ getUserPreferences().getRDelimiterEnded();
+
+ /**
+ * Returns the given string R-escaping backticks prepended and appended. This
+ * is not null safe. Do not pass null into this method.
+ *
+ * @param variableName The string to decorate.
+ * @return "`r#" + delimiterBegan + variableName+ delimiterEnded + "`".
+ */
+ @Override
+ public String apply( String variableName ) {
+ assert variableName != null;
+
+ // Delete the $ $ sigils from Markdown variables.
+ if( variableName.length() > 1 ) {
+ variableName = variableName.substring( 1, variableName.length() - 1 );
+ }
+
+ return PREFIX
+ + mDelimiterBegan
+ + "v$"
+ + variableName.replace( '.', '$' )
+ + mDelimiterEnded
+ + SUFFIX;
+ }
+
+ private UserPreferences getUserPreferences() {
+ return sOptions.getUserPreferences();
+ }
+}
src/main/java/com/scrivenvar/sigils/SigilOperator.java
+/*
+ * Copyright 2020 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.scrivenvar.sigils;
+
+import java.util.function.UnaryOperator;
+
+/**
+ * Responsible for updating definition keys to use a machine-readable format
+ * corresponding to the type of file being edited. This changes a definition
+ * key name based on some criteria determined by the factory that creates
+ * implementations of this interface.
+ */
+public interface SigilOperator extends UnaryOperator<String> {
+}
src/main/java/com/scrivenvar/sigils/YamlVariableDecorator.java
+/*
+ * Copyright 2020 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.scrivenvar.sigils;
+
+/**
+ * Brackets variable names with dollar symbols.
+ */
+public class YamlVariableDecorator implements SigilOperator {
+
+ /**
+ * Returns the given {@link String} verbatim because variables in YAML
+ * documents and plain Markdown documents already have the appropriate
+ * tokenizable syntax wrapped around the text.
+ *
+ * @param variableName Returned verbatim.
+ */
+ @Override
+ public String apply( final String variableName ) {
+ assert variableName != null;
+ return variableName;
+ }
+
+ /**
+ * Sigilifies the given key.
+ *
+ * @param key The key to adorn with YAML variable sigil characters.
+ * @return The given key bracketed by dollar symbols.
+ */
+ public static String entoken( final String key ) {
+ assert key != null;
+ return '$' + key + '$';
+ }
+}
src/main/java/com/scrivenvar/processors/IdentityProcessor.java
*/
@Override
- public String process( final String t ) {
+ public String apply( final String t ) {
return "<pre>" + t + "</pre>";
}
src/main/java/com/scrivenvar/processors/InlineRProcessor.java
import static com.scrivenvar.Constants.STATUS_PARSE_ERROR;
import static com.scrivenvar.Messages.get;
-import static com.scrivenvar.decorators.RVariableDecorator.PREFIX;
-import static com.scrivenvar.decorators.RVariableDecorator.SUFFIX;
+import static com.scrivenvar.sigils.RVariableDecorator.PREFIX;
+import static com.scrivenvar.sigils.RVariableDecorator.SUFFIX;
import static com.scrivenvar.processors.text.TextReplacementFactory.replace;
import static java.lang.Math.min;
*/
@Override
- public String process( final String text ) {
+ public String apply( final String text ) {
getNotifier().clear();
src/main/java/com/scrivenvar/processors/Processor.java
package com.scrivenvar.processors;
+import java.util.function.UnaryOperator;
+
/**
* Responsible for processing documents from one known format to another.
+ * Processes the given content providing a transformation from one document
+ * format into another. For example, this could convert from XML to text using
+ * an XSLT processor, or from markdown to HTML.
*
* @param <T> The type of processor to create.
*/
-public interface Processor<T> {
+public interface Processor<T> extends UnaryOperator<T> {
/**
- * Processes the given content providing a transformation from one document
- * format into another. For example, this could convert from XML to text using
- * an XSLT processor, or from markdown to HTML.
+ * Removes the given processor from the chain, returning a new immutable
+ * chain equivalent to this chain, but without the given processor.
*
- * @param t The type of object to process.
- * @return The post-processed document, or null if processing should stop.
+ * @param processor The {@link Processor} to remove from the chain.
+ * @return A delegating processor chain starting from this processor
+ * onwards with the given processor removed from the chain.
*/
- T process( T t );
-
Processor<T> remove( Class<? extends Processor<T>> processor );
src/main/java/com/scrivenvar/processors/XmlProcessor.java
*/
@Override
- public String process( final String text ) {
+ public String apply( final String text ) {
try {
return text.isEmpty() ? text : transform( text );