Dave Jarvis' Repositories

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

Fix inline R code embedded within diagrams

AuthorDaveJarvis <email>
Date2022-05-24 23:43:38 GMT-0700
Commit3da99e9bf67fad0e18b4b75b3b996715a9815edd
Parent3892810
Delta29 lines added, 7 lines removed, 22-line increase
src/main/java/com/keenwrite/processors/r/RInlineEvaluator.java
private static final int PREFIX_LENGTH = PREFIX.length();
- private static final int SUFFIX_LENGTH = SUFFIX.length();
private final Processor<String> mProcessor;
+ private final ProcessorContext mContext;
/**
* Constructs an evaluator capable of executing R statements.
*/
public RInlineEvaluator( final ProcessorContext context ) {
mProcessor = new RVariableProcessor( IDENTITY, context );
+ mContext = context;
}
public String apply( final String text ) {
try {
- final var len = text.length();
- final var r = mProcessor.apply(
- text.substring( PREFIX_LENGTH, len - SUFFIX_LENGTH )
- );
+ final var buffer = new StringBuilder( text.length() );
- // Return the evaluated R expression for insertion back into the text.
- return Engine.eval( r );
+ int index = 0;
+ int began;
+ int ended;
+
+ RBootstrapController.init( mContext );
+
+ while( (began = text.indexOf( PREFIX, index )) >= 0 ) {
+ buffer.append( text, index, began );
+
+ ended = text.indexOf( SUFFIX, began + 1 );
+
+ if( ended > began ) {
+ final var r = mProcessor.apply(
+ text.substring( began + PREFIX_LENGTH, ended )
+ );
+
+ // Return the evaluated R expression for insertion back into the text.
+ buffer.append( Engine.eval( r ) );
+
+ index = ended + 1;
+ }
+ }
+
+ buffer.append( text.substring( index ) );
+
+ return buffer.toString();
} catch( final Exception ex ) {
clue( STATUS_PARSE_ERROR, ex.getMessage() );