Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's a fairly simple solution in JavaScript, tested via Node. Of course, you'll need to adjust the input and output methods.</p> <pre><code>var s = "\n\ --------------------------------\n\ I I\n\ I ------- ------- I\n\ I I I I I I\n\ I I A I I B I I\n\ I I I I I I\n\ I ------- ------- I\n\ I I\n\ I I\n\ --------------------------------\n\ "; var lines = s.split('\n'); var outer_box_top_re = /--+/g; var i; for (i=0; i&lt;lines.length; i++) { while ((res = outer_box_top_re.exec(lines[i])) != null) { L = res.index R = outer_box_top_re.lastIndex process_box(i, L, R) } } function process_box(T, L, R) { console.log('&lt;div top="' + T + '" left="' + L + '" right="' + R + '"&gt;') blank_out(T, L, R) var i = T; while (1) { i += 1; if (i &gt;= lines.length) { console.log('Fell off bottom of ascii-art without finding bottom of box'); process.exit(1); } var line = lines[i]; if (line[L] == 'I' &amp;&amp; line[R-1] == 'I') { // interior // Look for (the tops of) sub-boxes. // (between L+1 and R-2) var inner_box_top_re = /--+/g; // Inner and outer need to be separate so that // inner doesn't stomp on outer's lastIndex. inner_box_top_re.lastIndex = L+1; while ((res = inner_box_top_re.exec(lines[i])) != null) { sub_L = res.index; sub_R = inner_box_top_re.lastIndex; if (sub_L &gt; R-1) { break; } process_box(i, sub_L, sub_R); } // Look for any other content (i.e., a box label) content = lines[i].substring(L+1, R-1); if (content.search(/[^ ]/) != -1) { console.log(content); } blank_out(i, L, R); } else if (line.substring(L,R).match(/^-+$/)) { // bottom blank_out(i, L, R); break; } else { console.log("line " + i + " doesn't contain a valid continuation of the box"); process.exit(1) } } console.log('&lt;/div&gt;') } function blank_out(i, L, R) { lines[i] = ( lines[i].substring(0,L) + lines[i].substring(L,R).replace(/./g, ' ') + lines[i].substring(R) ); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload