@@ -14,8 +14,6 @@ if (hpList === undefined) hpList = {};
1414export default class HParsons extends RunestoneBase {
1515 constructor ( opts ) {
1616 super ( opts ) ;
17- // copied from activecode
18- var suffStart ;
1917 // getting settings
2018 var orig = $ ( opts . orig ) . find ( "textarea" ) [ 0 ] ;
2119 this . reuse = $ ( orig ) . data ( "reuse" ) ? true : false ;
@@ -37,16 +35,7 @@ export default class HParsons extends RunestoneBase {
3735 this . loadButton = null ;
3836 this . outerDiv = null ;
3937 this . controlDiv = null ;
40- let prefixEnd = this . code . indexOf ( "^^^^" ) ;
41- if ( prefixEnd > - 1 ) {
42- this . prefix = this . code . substring ( 0 , prefixEnd ) ;
43- this . code = this . code . substring ( prefixEnd + 5 ) ;
44- }
45- suffStart = this . code . indexOf ( "--unittest--" ) ;
46- if ( suffStart > - 1 ) {
47- this . suffix = this . code . substring ( suffStart + 5 ) ;
48- this . code = this . code . substring ( 0 , suffStart ) ;
49- }
38+ this . processContent ( this . code )
5039
5140 // Change to factory when more execution based feedback is included
5241 if ( this . isBlockGrading ) {
@@ -73,6 +62,26 @@ export default class HParsons extends RunestoneBase {
7362 this . feedbackController . init ( ) ;
7463 }
7564
65+ processContent ( code ) {
66+ // todo: add errors when blocks are nonexistent (maybe in python)?
67+ this . originalBlocks = this . processSingleContent ( code , '--blocks--' ) . split ( '\n' ) . slice ( 1 , - 1 ) ;
68+ this . unittest = this . processSingleContent ( code , '--unittest--' ) ;
69+ }
70+
71+ processSingleContent ( code , delimitier ) {
72+ let index = code . indexOf ( delimitier ) ;
73+ if ( index > - 1 ) {
74+ let content = code . substring ( index + delimitier . length ) ;
75+ let endIndex = content . indexOf ( "\n--" ) ;
76+ content =
77+ endIndex > - 1
78+ ? content . substring ( 0 , endIndex + 1 )
79+ : content ;
80+ return content ;
81+ }
82+ return undefined ;
83+ }
84+
7685 // copied from activecode, already modified to add parsons
7786 createEditor ( ) {
7887 this . outerDiv = document . createElement ( "div" ) ;
@@ -82,24 +91,12 @@ export default class HParsons extends RunestoneBase {
8291 this . logHorizontalParsonsEvent ( ev . detail ) ;
8392 this . feedbackController . clearFeedback ( ) ;
8493 } ) ;
85- let blocks = [ ] ;
86- let blockIndex = this . code . indexOf ( "--blocks--" ) ;
87- if ( blockIndex > - 1 ) {
88- let blocksString = this . code . substring ( blockIndex + 10 ) ;
89- let endIndex = blocksString . indexOf ( "\n--" ) ;
90- blocksString =
91- endIndex > - 1
92- ? blocksString . substring ( 0 , endIndex )
93- : blocksString ;
94- blocks = blocksString . split ( "\n" ) ;
95- }
96- this . originalBlocks = blocks . slice ( 1 , - 1 ) ;
9794 const props = {
9895 selector : `#${ this . divid } -container` ,
9996 id : `${ this . divid } -hparsons` ,
10097 reuse : this . reuse ,
10198 randomize : this . randomize ,
102- parsonsBlocks : blocks . slice ( 1 , - 1 ) ,
99+ parsonsBlocks : [ ... this . originalBlocks ] ,
103100 language : this . language
104101 }
105102 InitMicroParsons ( props ) ;
0 commit comments