Skip to content

Commit 342db73

Browse files
committed
fixed pandoc link parsing
closes #133
1 parent b65d515 commit 342db73

File tree

3 files changed

+64
-24
lines changed

3 files changed

+64
-24
lines changed

include/url.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,7 @@ void url_purge(void);
4040
void url_dump(void);
4141
int url_count_inline(const wchar_t *line);
4242
int url_len_inline(const wchar_t *value);
43+
wchar_t* url_find_closing_bracket(const wchar_t *start);
44+
wchar_t *url_find_closing_parentheses(const wchar_t *start);
4345

4446
#endif // !defined( URL_H )

src/url.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,35 @@ int url_len_inline(const wchar_t *value) {
210210

211211
return count;
212212
}
213+
214+
wchar_t * url_find_closing_bracket(const wchar_t *start) {
215+
if (!start || *start == L'\0') return NULL;
216+
int depth = 1;
217+
const wchar_t *p = start;
218+
if (*p == L'[') p++;
219+
while (*p) {
220+
if (*p == L'[') {
221+
depth++;
222+
} else if (*p == L']') {
223+
depth--;
224+
if (depth == 0) return (wchar_t *)p;
225+
}
226+
p++;
227+
}
228+
return NULL;
229+
}
230+
231+
wchar_t *url_find_closing_parentheses(const wchar_t *start) {
232+
if (!start || *start == L'\0') return NULL;
233+
const wchar_t *p = start;
234+
while (*p) {
235+
if (*p == L'\\' && *(p + 1)) {
236+
p += 2;
237+
} else if (*p == L')') {
238+
return (wchar_t *)p;
239+
} else {
240+
p++;
241+
}
242+
}
243+
return NULL;
244+
}

src/viewer.c

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -668,8 +668,8 @@ void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colo
668668
void 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

Comments
 (0)