Dave Jarvis' Repositories

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

Add settings for definition key delimiter tokens

AuthorDaveJarvis <email>
Date2020-09-09 23:28:21 GMT-0700
Commit8c384538a754bf7e912f379872d9537b4dfb0e7c
Parent9b9e159
Delta63 lines added, 76 lines removed, 13-line decrease
src/test/java/com/scrivenvar/definition/TreeItemInterpolatorTest.java
public class TreeItemInterpolatorTest {
- private final static String AUTHOR_FIRST = "FirstName";
- private final static String AUTHOR_LAST = "LastName";
- private final static String AUTHOR_ALL = "$root.name.first$ $root.name.last$";
+ private static final String AUTHOR_FIRST = "FirstName";
+ private static final String AUTHOR_LAST = "LastName";
+ private static final String AUTHOR_ALL = "$root.name.first$ $root.name.last$";
/**
src/main/java/com/scrivenvar/sigils/SigilOperator.java
package com.scrivenvar.sigils;
+import com.scrivenvar.Services;
+import com.scrivenvar.preferences.UserPreferences;
+import com.scrivenvar.service.Options;
+
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> {
+public abstract class SigilOperator implements UnaryOperator<String> {
+ private static final Options sOptions = Services.load( Options.class );
+
+ protected static UserPreferences getUserPreferences() {
+ return sOptions.getUserPreferences();
+ }
}
src/main/java/com/scrivenvar/sigils/YamlSigilOperator.java
package com.scrivenvar.sigils;
+import java.util.regex.Pattern;
+
+import static java.lang.String.format;
+import static java.util.regex.Pattern.compile;
+import static java.util.regex.Pattern.quote;
+
/**
- * Brackets variable names with dollar symbols.
+ * Brackets definition keys with token delimiters.
*/
-public class YamlSigilOperator implements SigilOperator {
+public class YamlSigilOperator extends SigilOperator {
+ public static final char KEY_SEPARATOR_DEF = '.';
+
+ private static final String mDelimiterBegan =
+ getUserPreferences().getDefDelimiterBegan();
+ private static final String mDelimiterEnded =
+ getUserPreferences().getDefDelimiterEnded();
+
+ /**
+ * Non-greedy match of key names delimited by definition tokens.
+ */
+ private static final String REGEX =
+ format( "(%s.*?%s)", quote( mDelimiterBegan ), quote( mDelimiterEnded ) );
+
+ /**
+ * Compiled regular expression for matching delimited references.
+ */
+ public static final Pattern REGEX_PATTERN = compile( REGEX );
+
/**
* 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.
+ * @param key Returned verbatim.
*/
@Override
- public String apply( final String variableName ) {
- assert variableName != null;
- return variableName;
+ public String apply( final String key ) {
+ return key;
}
/**
- * Sigilifies the given key.
+ * Adds delimiters to the given key.
*
- * @param key The key to adorn with YAML variable sigil characters.
- * @return The given key bracketed by dollar symbols.
+ * @param key The key to adorn with start and stop definition tokens.
+ * @return The given key bracketed by definition token symbols.
*/
public static String entoken( final String key ) {
assert key != null;
- return '$' + key + '$';
+ return mDelimiterBegan + key + mDelimiterEnded;
+ }
+
+ /**
+ * Removes start and stop definition key delimiters from the given key. This
+ * method does not check for delimiters, only that there are sufficient
+ * characters to remove from either end of the given key.
+ *
+ * @param key The key adorned with start and stop definition tokens.
+ * @return The given key with the delimiters removed.
+ */
+ public static String detoken( final String key ) {
+ final int beganLen = mDelimiterBegan.length();
+ final int endedLen = mDelimiterEnded.length();
+
+ return key.length() > beganLen + endedLen
+ ? key.substring( beganLen, key.length() - endedLen )
+ : key;
}
}
src/main/java/com/scrivenvar/editors/VariableNameDecoratorFactory.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.editors;
-
-import com.scrivenvar.AbstractFileFactory;
-import com.scrivenvar.sigils.RSigilOperator;
-import com.scrivenvar.sigils.SigilOperator;
-import com.scrivenvar.sigils.YamlSigilOperator;
-import java.nio.file.Path;
-
-/**
- * Responsible for creating a variable name decorator suited to a particular
- * file type.
- */
-public class VariableNameDecoratorFactory extends AbstractFileFactory {
-
- private VariableNameDecoratorFactory() {
- }
-
- public static SigilOperator newInstance( final Path path ) {
- final var factory = new VariableNameDecoratorFactory();
- final SigilOperator result;
-
- switch( factory.lookup( path ) ) {
- case RMARKDOWN:
- case RXML:
- result = new RSigilOperator();
- break;
-
- default:
- result = new YamlSigilOperator();
- break;
- }
-
- return result;
- }
-}