@@ -668,8 +668,8 @@ void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colo
668668void inline_display (WINDOW * window , const wchar_t * c , const int colors , int nocodebg ) {
669669 const static wchar_t * special = L"\\*_`![" ; // list of interpreted chars
670670 const wchar_t * i = c ; // iterator
671- const wchar_t * start_link_name , * start_url ;
672- int length_link_name , url_num ;
671+ const wchar_t * label_start , * label_end , * url_start , * url_end ;
672+ int label_length , url_length , url_num ;
673673 cstack_t * stack = cstack_init ();
674674
675675
@@ -728,47 +728,48 @@ void inline_display(WINDOW *window, const wchar_t *c, const int colors, int noco
728728 * i == L'\\' ) {
729729
730730 // url in pandoc style
731- if ((* i == L'[' && wcschr (i , L']' )) ||
732- (* i == L'!' && * (i + 1 ) == L'[' && wcschr (i , L']' ))) {
733-
734- if (* i == L'!' ) i ++ ;
735-
736- if (wcschr (i , L']' )[1 ] == L'(' && wcschr (i , L')' )) {
737- i ++ ;
731+ if ((* i == L'[' ) || (* i == L'!' && * (i + 1 ) && * (i + 1 ) == L'[' )) {
732+ label_start = (* i == L'!' ) ? i + 2 : i + 1 ;
733+ label_end = url_find_closing_bracket (label_start );
734+ url_start = NULL ;
735+ url_end = NULL ;
736+
737+ if (label_end && * (label_end + 1 ) && * (label_end + 1 ) == L'(' && * (label_end + 2 )) {
738+ url_start = label_end + 2 ;
739+ url_end = url_find_closing_parentheses (url_start );
740+ }
738741
742+ if (label_end && * (label_end + 1 ) == L'(' && url_end && * url_end == L')' ) {
739743 // turn higlighting and underlining on
740744 if (colors )
741745 wattron (window , COLOR_PAIR (CP_BLUE ));
742746 wattron (window , A_UNDERLINE );
743747
744- start_link_name = i ;
745-
746748 // print the content of the label
747749 // the label is printed as is
748- do {
750+ i = label_start ;
751+ while (i < label_end ) {
752+ if (* i == L'\\' && * (i + 1 )) {
753+ i ++ ;
754+ }
749755 waddnwstr (window , i , 1 );
750756 i ++ ;
751- } while (* i != L']' );
752-
753- length_link_name = i - 1 - start_link_name ;
757+ }
754758
755- i ++ ;
756- i ++ ;
757-
758- start_url = i ;
759-
760- while (* i != L')' ) i ++ ;
761-
762- url_num = url_add (start_link_name , length_link_name , start_url , i - start_url , 0 , 0 );
759+ label_length = label_end - label_start ;
760+ url_length = url_end - url_start ;
761+ url_num = url_add (label_start , label_length , url_start , url_length , 0 , 0 );
763762
764763 wprintw (window , " [%d]" , url_num );
765764
765+ i = url_end ;
766+
766767 // turn highlighting and underlining off
767768 wattroff (window , A_UNDERLINE );
768769 wattron (window , COLOR_PAIR (CP_WHITE ));
769770
770771 } else {
771- wprintw (window , "[" );
772+ waddnwstr (window , i , 1 );
772773 }
773774
774775 } else switch (* i ) {
@@ -786,6 +787,11 @@ void inline_display(WINDOW *window, const wchar_t *c, const int colors, int noco
786787 if (colors && !nocodebg )
787788 wattron (window , COLOR_PAIR (CP_BLACK ));
788789 break ;
790+ // broken pandoc url
791+ case L'!' :
792+ case L'[' :
793+ waddnwstr (window , i , 1 );
794+ break ;
789795 // do nothing for backslashes
790796 }
791797
0 commit comments