| @@ -462,6 +462,11 @@ Tokenizer_parse_wikilink(Tokenizer *self) | |||||
| static int | static int | ||||
| Tokenizer_handle_wikilink_separator(Tokenizer *self) | Tokenizer_handle_wikilink_separator(Tokenizer *self) | ||||
| { | { | ||||
| if (!PyList_GET_SIZE(self->topstack->stack) && | |||||
| !self->topstack->textbuffer->length) { | |||||
| Tokenizer_fail_route(self); | |||||
| return -1; | |||||
| } | |||||
| self->topstack->context ^= LC_WIKILINK_TITLE; | self->topstack->context ^= LC_WIKILINK_TITLE; | ||||
| self->topstack->context |= LC_WIKILINK_TEXT; | self->topstack->context |= LC_WIKILINK_TEXT; | ||||
| if (Tokenizer_emit(self, WikilinkSeparator)) { | if (Tokenizer_emit(self, WikilinkSeparator)) { | ||||
| @@ -476,7 +481,13 @@ Tokenizer_handle_wikilink_separator(Tokenizer *self) | |||||
| static PyObject * | static PyObject * | ||||
| Tokenizer_handle_wikilink_end(Tokenizer *self) | Tokenizer_handle_wikilink_end(Tokenizer *self) | ||||
| { | { | ||||
| PyObject *stack = Tokenizer_pop(self); | |||||
| PyObject *stack; | |||||
| if ((self->topstack->context & LC_WIKILINK_TITLE) && | |||||
| !PyList_GET_SIZE(self->topstack->stack) && | |||||
| !self->topstack->textbuffer->length) { | |||||
| return Tokenizer_fail_route(self); | |||||
| } | |||||
| stack = Tokenizer_pop(self); | |||||
| self->head += 1; | self->head += 1; | ||||
| return stack; | return stack; | ||||
| } | } | ||||
| @@ -408,12 +408,20 @@ class Tokenizer: | |||||
| def _handle_wikilink_separator(self): | def _handle_wikilink_separator(self): | ||||
| """Handle the separator between a wikilink's title and its text.""" | """Handle the separator between a wikilink's title and its text.""" | ||||
| if not self._stack and not self._textbuffer: | |||||
| self._fail_route() | |||||
| self._context ^= contexts.WIKILINK_TITLE | self._context ^= contexts.WIKILINK_TITLE | ||||
| self._context |= contexts.WIKILINK_TEXT | self._context |= contexts.WIKILINK_TEXT | ||||
| self._emit(tokens.WikilinkSeparator()) | self._emit(tokens.WikilinkSeparator()) | ||||
| def _handle_wikilink_end(self): | def _handle_wikilink_end(self): | ||||
| """Handle the end of a wikilink at the head of the string.""" | """Handle the end of a wikilink at the head of the string.""" | ||||
| if ( | |||||
| self._context & contexts.WIKILINK_TITLE | |||||
| and not self._stack | |||||
| and not self._textbuffer | |||||
| ): | |||||
| self._fail_route() | |||||
| self._head += 1 | self._head += 1 | ||||
| return self._pop() | return self._pop() | ||||
| @@ -641,3 +641,18 @@ def test_get_tree(): | |||||
| + "{{\n\t\t\tbaz\n\t }}\n\t| spam\n\t= eggs\n}}" | + "{{\n\t\t\tbaz\n\t }}\n\t| spam\n\t= eggs\n}}" | ||||
| ) | ) | ||||
| assert expected.expandtabs(4) == code.get_tree() | assert expected.expandtabs(4) == code.get_tree() | ||||
| @pytest.mark.parametrize("text", ("[[]]", "[[|]]", "[[|||]]", "before [[]] after")) | |||||
| def test_empty_wikilink_titles_are_text(text): | |||||
| code = parse(text) | |||||
| assert str(code) == text | |||||
| assert code.filter_wikilinks() == [] | |||||
| assert code.strip_code() == text | |||||
| @pytest.mark.parametrize("text", ("[[{{echo|}}]]", "[[{{!}}]]", "[[{{{title|}}}]]")) | |||||
| def test_dynamic_wikilink_titles_are_preserved(text): | |||||
| code = parse(text) | |||||
| assert str(code) == text | |||||
| assert len(code.filter_wikilinks()) == 1 | |||||
| @@ -1,14 +1,14 @@ | |||||
| name: blank | name: blank | ||||
| label: wikilink with no content | label: wikilink with no content | ||||
| input: "[[]]" | input: "[[]]" | ||||
| output: [WikilinkOpen(), WikilinkClose()] | |||||
| output: [Text(text="[[]]")] | |||||
| --- | --- | ||||
| name: blank_with_text | name: blank_with_text | ||||
| label: wikilink with no content but a pipe | label: wikilink with no content but a pipe | ||||
| input: "[[|]]" | input: "[[|]]" | ||||
| output: [WikilinkOpen(), WikilinkSeparator(), WikilinkClose()] | |||||
| output: [Text(text="[[|]]")] | |||||
| --- | --- | ||||
| @@ -29,7 +29,7 @@ output: [WikilinkOpen(), Text(text="foo"), WikilinkSeparator(), Text(text="bar") | |||||
| name: blank_with_multiple_texts | name: blank_with_multiple_texts | ||||
| label: no content, multiple pipes | label: no content, multiple pipes | ||||
| input: "[[|||]]" | input: "[[|||]]" | ||||
| output: [WikilinkOpen(), WikilinkSeparator(), Text(text="||"), WikilinkClose()] | |||||
| output: [Text(text="[[|||]]")] | |||||
| --- | --- | ||||
| @@ -205,3 +205,17 @@ name: incomplete_preserve_previous | |||||
| label: incomplete wikilinks: a valid wikilink followed by an invalid one | label: incomplete wikilinks: a valid wikilink followed by an invalid one | ||||
| input: "[[foo]] [[bar" | input: "[[foo]] [[bar" | ||||
| output: [WikilinkOpen(), Text(text="foo"), WikilinkClose(), Text(text=" [[bar")] | output: [WikilinkOpen(), Text(text="foo"), WikilinkClose(), Text(text=" [[bar")] | ||||
| --- | |||||
| name: empty_title_followed_by_link | |||||
| label: rejecting an empty title does not prevent a subsequent valid link | |||||
| input: "[[]][[valid]]" | |||||
| output: [Text(text="[[]]"), WikilinkOpen(), Text(text="valid"), WikilinkClose()] | |||||
| --- | |||||
| name: template_title_may_expand_empty | |||||
| label: templates in titles must be preserved without attempting expansion | |||||
| input: "[[{{echo|}}]]" | |||||
| output: [WikilinkOpen(), TemplateOpen(), Text(text="echo"), TemplateParamSeparator(), TemplateClose(), WikilinkClose()] | |||||