4
4
BarelyML.cpp
5
5
Created: 5 Oct 2023
6
6
Author: Fritz Menzer
7
- Version: 0.2
7
+ Version: 0.2.1
8
8
9
9
==============================================================================
10
10
Copyright (C) 2023 Fritz Menzer
@@ -114,11 +114,11 @@ void BarelyMLDisplay::resized()
114
114
int h = margin;
115
115
for (int i=0 ; i<blocks.size (); i++) {
116
116
int bh;
117
- bh = blocks[i]->getHeightRequired (getWidth ()-2 *margin);
117
+ bh = blocks[i]->getHeightRequired (getWidth ()-2 *margin)+ 5 ; // just to be on the safe side
118
118
if (blocks[i]->canExtendBeyondMargin ()) {
119
119
blocks[i]->setBounds (0 ,h,getWidth (),bh);
120
120
} else {
121
- blocks[i]->setBounds (margin,h,getWidth ()-2 *margin,bh);
121
+ blocks[i]->setBounds (margin,h,getWidth ()-2 *margin,bh+ 10 );
122
122
}
123
123
h += bh;
124
124
}
@@ -190,14 +190,17 @@ void BarelyMLDisplay::setMarkupString(String s) {
190
190
li++; // ...and go to next line.
191
191
} else { // otherwise we assume that we have a text block
192
192
StringArray blines; // set up text block lines
193
+ bool blockEnd = false ;
193
194
while (!ListItem::isListItem (line) && // while line is not part of a list...
194
195
!TableBlock::isTableLine (line) && // ...nor a table...
195
196
!AdmonitionBlock::isAdmonitionLine (line) && // ...nor an admonition...
196
197
!ImageBlock::isImageLine (line) && // ...nor an image...
197
198
!Block::containsLink (line) && // ...and doesn't contain a link...
198
- li<lines.size ()) { // ...and we're not done yet...
199
+ li<lines.size () && !blockEnd) { // ...and we're not done yet...
199
200
blines.add (line); // ...add line to text block lines...
200
- line = lines[++li]; // ...and read next line.
201
+ blockEnd = line.isEmpty (); // ...and set up shouldEndBlock...
202
+ line = lines[++li]; // ...read next line...
203
+ blockEnd &= line.isNotEmpty (); // ...and finish shouldEndBloc...
201
204
}
202
205
TextBlock* b = new TextBlock (); // set up a new text block object...
203
206
b->setColours (&colours); // ...set its colours...
@@ -237,7 +240,7 @@ String BarelyMLDisplay::convertFromMarkdown(String md) {
237
240
String address = line.substring (idx2+2 , idx3);
238
241
line = line.substring (0 , idx1) + " {{" + address + " }}" + line.substring (idx3+2 );
239
242
}
240
- // replace links
243
+ // replace links with labels
241
244
while (line.contains (" [" ) &&
242
245
line.fromFirstOccurrenceOf (" [" , false , false ).contains (" ](" ) &&
243
246
line.fromLastOccurrenceOf (" ](" , false , false ).contains (" )" )) {
@@ -247,7 +250,20 @@ String BarelyMLDisplay::convertFromMarkdown(String md) {
247
250
int idx3 = line.indexOf (idx2+2 , " )" );
248
251
String text = line.substring (idx1+1 , idx2);
249
252
String address = line.substring (idx2+2 , idx3);
250
- line = line.substring (0 , idx1) + " [[" + address + " |" + text + " ]]" + line.substring (idx3+2 );
253
+ line = line.substring (0 , idx1) + " [[" + address + " |" + text + " ]]" + line.substring (idx3+1 );
254
+ }
255
+ // replace links without labels
256
+ while (line.contains (" <" ) &&
257
+ line.fromFirstOccurrenceOf (" <" , false , false ).contains (" >" ) && (
258
+ line.fromFirstOccurrenceOf (" <" , false , false ).startsWith (" http://" ) ||
259
+ line.fromFirstOccurrenceOf (" <" , false , false ).startsWith (" https://" ) ||
260
+ line.fromFirstOccurrenceOf (" <" , false , false ).startsWith (" mailto:" ))
261
+ ) {
262
+ // replace links
263
+ int idx1 = line.indexOf (" <" );
264
+ int idx2 = line.indexOf (idx1+1 , " >" );
265
+ String address = line.substring (idx1+1 , idx2);
266
+ line = line.substring (0 , idx1) + " [[" + address + " ]]" + line.substring (idx2+1 );
251
267
}
252
268
// when in a table, skip lines which look like this : | --- | --- |
253
269
if (!lastLineWasTable || !(line.containsOnly (" | -\t " ) && line.isNotEmpty ())) {
@@ -261,7 +277,7 @@ String BarelyMLDisplay::convertFromMarkdown(String md) {
261
277
} else {
262
278
lastLineWasTable = false ; // ...otherwise, keep also track.
263
279
}
264
- bml += line + " \n " ;
280
+ bml += line + (li<lines. size ()- 1 ? " \n " : " " ) ;
265
281
}
266
282
}
267
283
// replace bold and italic markers
@@ -274,11 +290,60 @@ String BarelyMLDisplay::convertFromMarkdown(String md) {
274
290
}
275
291
276
292
String BarelyMLDisplay::convertToMarkdown (String bml) {
277
- // NOTE: for now, all this method does is convert a BarelyML string
278
- // to something that convertFromMarkdown will turn into a
279
- // BarelyML string again.
293
+ StringArray lines;
294
+ lines.addLines (bml);
295
+ String md;
296
+
297
+ bool isTable = false ;
298
+ for (int li=0 ; li<lines.size (); li++) {
299
+ String line = lines[li];
300
+
301
+ // replace table headers
302
+ if (line.startsWith (" ^" ) && !isTable) {
303
+ isTable = true ;
304
+ line = line.replace (" ^" , " |" );
305
+ // count columns
306
+ String tmp = line.substring (1 );
307
+ Array<int > colWidths;
308
+ while (tmp.contains (" |" )) {
309
+ colWidths.add (tmp.indexOf (" |" ));
310
+ tmp = tmp.fromFirstOccurrenceOf (" |" , false , false );
311
+ }
312
+ if (!colWidths.isEmpty ()) {
313
+ line += " \n |" ;
314
+ for (int i=0 ; i<colWidths.size (); i++) {
315
+ int nhyphen = jmax (3 ,colWidths[i]-2 );
316
+ line += " " ;
317
+ while (nhyphen>0 ) {
318
+ line += " -" ;
319
+ nhyphen--;
320
+ }
321
+ line += " |" ;
322
+ }
323
+ }
324
+ }
325
+ if (line.startsWith (" ^" ) | line.startsWith (" |" )) {
326
+ isTable = true ;
327
+ } else {
328
+ isTable = false ;
329
+ }
330
+
331
+ // replace links
332
+ while (line.contains (" [[" ) && line.fromFirstOccurrenceOf (" [[" , false , false ).contains (" ]]" )) {
333
+ int idx1 = line.indexOf (" [[" );
334
+ int idx2 = line.indexOf (idx1, " ]]" );
335
+ String link = line.substring (idx1+2 , idx2);
336
+ if (link.contains (" |" )) {
337
+ line = line.substring (0 , idx1) + " [" + link.fromFirstOccurrenceOf (" |" , false , false ) + " ](" + link.upToFirstOccurrenceOf (" |" , false , false ) + " )" + line.substring (idx2+2 );
338
+ } else {
339
+ line = line.substring (0 , idx1) + " <" + link + " >" + line.substring (idx2+2 );
340
+ }
341
+ }
280
342
281
- String md = bml;
343
+ // add line
344
+ md += line + (li<lines.size ()-1 ?" \n " :" " );
345
+ }
346
+
282
347
// replace bold markers
283
348
md = md.replace (" *" , " **" );
284
349
return md;
@@ -325,7 +390,7 @@ String BarelyMLDisplay::convertFromDokuWiki(String dw) {
325
390
if (line.startsWith (" * " )) { line = " - " + line.substring (10 ); }
326
391
if (line.startsWith (" * " )) { line = " - " + line.substring (12 ); }
327
392
// add line
328
- bml += line + " \n " ;
393
+ bml += line + (li<lines. size ()- 1 ? " \n " : " " ) ;
329
394
}
330
395
331
396
// save the URLs
@@ -348,10 +413,6 @@ String BarelyMLDisplay::convertFromDokuWiki(String dw) {
348
413
}
349
414
350
415
String BarelyMLDisplay::convertToDokuWiki (String bml) {
351
- // NOTE: for now, all this method does is convert a BarelyML string
352
- // to something that convertFromDokuWiki will turn into a
353
- // BarelyML string again.
354
-
355
416
StringArray lines;
356
417
lines.addLines (bml);
357
418
String dw;
@@ -383,7 +444,7 @@ String BarelyMLDisplay::convertToDokuWiki(String bml) {
383
444
if ( line.substring (0 , didx).containsOnly (" 0123456789" )) { line = " - " + line.substring (didx+2 ); }
384
445
}
385
446
// add line
386
- dw += line + " \n " ;
447
+ dw += line + (li<lines. size ()- 1 ? " \n " : " " ) ;
387
448
}
388
449
389
450
// replace color markers (supporting a subset of the "color" plugin syntax)
@@ -493,6 +554,10 @@ String BarelyMLDisplay::convertFromAsciiDoc(String ad) {
493
554
int idx2 = line.indexOf (idx1, " " );
494
555
if (idx2<0 ) { idx2 = line.indexOf (idx1, " \t " ); }
495
556
if (idx2<0 ) { idx2 = line.length (); }
557
+ // needed for cases like this: [JUCE Forum] (space in label)
558
+ if (line.substring (idx1, idx2).contains (" [" )) {
559
+ idx2 = jmax (idx2, line.indexOf (idx1, " ]" )+1 );
560
+ }
496
561
String link = line.substring (idx1, idx2);
497
562
if (link.contains (" [" ) && link.endsWith (" ]" )) {
498
563
int lidx = link.indexOf (" [" );
@@ -506,7 +571,7 @@ String BarelyMLDisplay::convertFromAsciiDoc(String ad) {
506
571
507
572
// add line
508
573
if (!skipLine) {
509
- bml += line + " \n " ;
574
+ bml += line + (li<lines. size ()- 1 ? " \n " : " " ) ;
510
575
}
511
576
}
512
577
@@ -526,10 +591,6 @@ String BarelyMLDisplay::convertFromAsciiDoc(String ad) {
526
591
}
527
592
528
593
String BarelyMLDisplay::convertToAsciiDoc (String bml) {
529
- // NOTE: for now, all this method does is convert a BarelyML string
530
- // to something that convertFromAsciiDoc will turn into a
531
- // BarelyML string again.
532
-
533
594
StringArray lines;
534
595
lines.addLines (bml);
535
596
String ad;
@@ -594,7 +655,7 @@ String BarelyMLDisplay::convertToAsciiDoc(String bml) {
594
655
if (line.startsWith (" HINT: " )) { line = " TIP: " + line.substring (6 ); }
595
656
596
657
// add line
597
- ad += line + " \n " ;
658
+ ad += line + (li<lines. size ()- 1 ? " \n " : " " ) ;
598
659
}
599
660
600
661
// replace color markers (named colors only)
@@ -673,9 +734,10 @@ void BarelyMLDisplay::Block::mouseUp(const MouseEvent& event) {
673
734
}
674
735
}
675
736
676
- AttributedString BarelyMLDisplay::Block::parsePureText (const StringArray& lines, Font font)
737
+ AttributedString BarelyMLDisplay::Block::parsePureText (const StringArray& lines, Font font, bool addNewline )
677
738
{
678
739
AttributedString attributedString;
740
+
679
741
String currentLine;
680
742
currentColour = defaultColour;
681
743
@@ -686,23 +748,23 @@ AttributedString BarelyMLDisplay::Block::parsePureText(const StringArray& lines,
686
748
{
687
749
if (line.startsWith (" ##### " ))
688
750
{
689
- attributedString.append (line.substring (6 ), font.boldened ().withHeight (font.getHeight ()*1 .1f ), defaultColour );
751
+ attributedString.append (parsePureText ( line.substring (6 ), font.boldened ().withHeight (font.getHeight ()*1 .1f ),false ) );
690
752
}
691
753
else if (line.startsWith (" #### " ))
692
754
{
693
- attributedString.append (line.substring (5 ), font.boldened ().withHeight (font.getHeight ()*1 .25f ), defaultColour );
755
+ attributedString.append (parsePureText ( line.substring (5 ), font.boldened ().withHeight (font.getHeight ()*1 .25f ),false ) );
694
756
}
695
757
else if (line.startsWith (" ### " ))
696
758
{
697
- attributedString.append (line.substring (4 ), font.boldened ().withHeight (font.getHeight ()*1 .42f ), defaultColour );
759
+ attributedString.append (parsePureText ( line.substring (4 ), font.boldened ().withHeight (font.getHeight ()*1 .42f ),false ) );
698
760
}
699
761
else if (line.startsWith (" ## " ))
700
762
{
701
- attributedString.append (line.substring (3 ), font.boldened ().withHeight (font.getHeight ()*1 .7f ), defaultColour );
763
+ attributedString.append (parsePureText ( line.substring (3 ), font.boldened ().withHeight (font.getHeight ()*1 .7f ),false ) );
702
764
}
703
765
else if (line.startsWith (" # " ))
704
766
{
705
- attributedString.append (line.substring (2 ), font.boldened ().withHeight (font.getHeight ()*2 .1f ), defaultColour );
767
+ attributedString.append (parsePureText ( line.substring (2 ), font.boldened ().withHeight (font.getHeight ()*2 .1f ),false ) );
706
768
}
707
769
else
708
770
{
@@ -778,7 +840,9 @@ AttributedString BarelyMLDisplay::Block::parsePureText(const StringArray& lines,
778
840
}
779
841
}
780
842
781
- attributedString.append (" \n " , font, defaultColour);
843
+ if (addNewline) {
844
+ attributedString.append (" \n " , font, defaultColour);
845
+ }
782
846
}
783
847
return attributedString;
784
848
}
@@ -796,8 +860,7 @@ float BarelyMLDisplay::TextBlock::getHeightRequired(float width) {
796
860
}
797
861
798
862
void BarelyMLDisplay::TextBlock::paint (juce::Graphics& g) {
799
- // g.fillAll(Colours::lightblue); // clear the background
800
- attributedString.draw (g, getLocalBounds ().toFloat ().expanded (0 , 1 .f ));
863
+ attributedString.draw (g, getLocalBounds ().toFloat ());
801
864
}
802
865
803
866
// MARK: - Admonition Block
@@ -942,7 +1005,6 @@ void BarelyMLDisplay::TableBlock::resized() {
942
1005
}
943
1006
944
1007
void BarelyMLDisplay::TableBlock::Table::paint (juce::Graphics& g) {
945
- // g.fillAll(Colours::lightpink); // clear the background
946
1008
float y = 0 .f ; // Y coordinate of cell's top left corner
947
1009
for (int i=0 ; i<cells.size (); i++) {
948
1010
float x = leftmargin; // X coordinate of cell's top left corner
0 commit comments