Dave Jarvis' Repositories

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

Add command-line R bootstrap, update word counting

AuthorDaveJarvis <email>
Date2023-03-10 23:07:40 GMT-0800
Commit43f00a9d3102e7053268dfa41f8460b7e5b24bff
Parent8a358d4
Delta61 lines added, 5 lines removed, 56-line increase
src/main/java/com/keenwrite/processors/r/RBootstrapController.java
-/* Copyright 2020-2021 White Magic Software, Ltd. -- All rights reserved. */
+/* Copyright 2020 White Magic Software, Ltd. -- All rights reserved.
+ *
+ * SPDX-License-Identifier: MIT
+ */
package com.keenwrite.processors.r;
final var dir = getRWorkingDirectory();
final var definitions = mDefinitions.get();
+
+ // A problem with the bootstrap script is likely caused by variables
+ // not being loaded. This implies that the R processor is being invoked
+ // too soon.
+ update(bootstrap, dir, definitions);
+ }
+ }
+
+ public static void update(
+ final String bootstrap,
+ final String workingDir,
+ final Map<String, String> definitions ) {
+
+ if( !bootstrap.isBlank() ) {
final var map = new HashMap<String, String>( definitions.size() + 1 );
definitions.forEach(
( k, v ) -> map.put( KEY_OPERATOR.apply( k ), escape( v ) )
);
map.put(
KEY_OPERATOR.apply( "application.r.working.directory" ),
- escape( dir )
+ escape( workingDir )
);
try {
Engine.eval( replace( bootstrap, map ) );
} catch( final Exception ex ) {
clue( ex );
- // A problem with the bootstrap script is likely caused by variables
- // not being loaded. This implies that the R processor is being invoked
- // too soon.
}
}
src/main/java/com/keenwrite/processors/RBootstrapProcessor.java
+/* Copyright 2023 White Magic Software, Ltd. -- All rights reserved.
+ *
+ * SPDX-License-Identifier: MIT
+ */
+package com.keenwrite.processors;
+
+import com.keenwrite.processors.r.RBootstrapController;
+
+public class RBootstrapProcessor extends ExecutorProcessor<String> {
+ private final Processor<String> mSuccessor;
+ private final ProcessorContext mContext;
+
+ public RBootstrapProcessor(
+ final Processor<String> successor,
+ final ProcessorContext context ) {
+ assert successor != null;
+ assert context != null;
+
+ mSuccessor = successor;
+ mContext = context;
+ }
+
+ /**
+ * Processes the given text document by replacing variables with their values.
+ *
+ * @param text The document text that includes variables that should be
+ * replaced with values when rendered as HTML.
+ * @return The text with all variables replaced.
+ */
+ @Override
+ public String apply( final String text ) {
+ assert text != null;
+
+ final var bootstrap = mContext.getRScript();
+ final var workingDir = mContext.getRWorkingDir().toString();
+ final var definitions = mContext.getDefinitions();
+
+ RBootstrapController.update(bootstrap, workingDir, definitions);
+
+ return mSuccessor.apply( text );
+ }
+}