diff --git a/mwparserfromhell/parser/tokenizer.c b/mwparserfromhell/parser/tokenizer.c index cc1b4dd..40ec723 100644 --- a/mwparserfromhell/parser/tokenizer.c +++ b/mwparserfromhell/parser/tokenizer.c @@ -759,11 +759,10 @@ Tokenizer_parse_heading(Tokenizer* self) if (BAD_ROUTE) { RESET_ROUTE(); self->head = reset + best - 1; - char text[best + 1]; - for (i = 0; i < best; i++) text[i] = *"="; - text[best] = *""; - if (Tokenizer_write_text_then_stack(self, text)) - return -1; + for (i = 0; i < best; i++) { + if (Tokenizer_write_text(self, *"=")) + return -1; + } self->global ^= GL_HEADING; return 0; } @@ -799,13 +798,12 @@ Tokenizer_parse_heading(Tokenizer* self) Py_DECREF(token); if (heading->level < best) { diff = best - heading->level; - char difftext[diff + 1]; - for (i = 0; i < diff; i++) difftext[i] = *"="; - difftext[diff] = *""; - if (Tokenizer_write_text_then_stack(self, difftext)) { - Py_DECREF(heading->title); - free(heading); - return -1; + for (i = 0; i < diff; i++) { + if (Tokenizer_write_text(self, *"=")) { + Py_DECREF(heading->title); + free(heading); + return -1; + } } } if (Tokenizer_write_all(self, heading->title)) { @@ -851,22 +849,20 @@ Tokenizer_handle_heading_end(Tokenizer* self) RESET_ROUTE(); if (level < best) { diff = best - level; - char difftext[diff + 1]; - for (i = 0; i < diff; i++) difftext[i] = *"="; - difftext[diff] = *""; - if (Tokenizer_write_text_then_stack(self, difftext)) - return NULL; + for (i = 0; i < diff; i++) { + if (Tokenizer_write_text(self, *"=")) + return NULL; + } } self->head = reset + best - 1; } else { - char text[best + 1]; - for (i = 0; i < best; i++) text[i] = *"="; - text[best] = *""; - if (Tokenizer_write_text_then_stack(self, text)) { - Py_DECREF(after->title); - free(after); - return NULL; + for (i = 0; i < best; i++) { + if (Tokenizer_write_text(self, *"=")) { + Py_DECREF(after->title); + free(after); + return NULL; + } } if (Tokenizer_write_all(self, after->title)) { Py_DECREF(after->title); diff --git a/mwparserfromhell/parser/tokenizer.py b/mwparserfromhell/parser/tokenizer.py index 5b0e976..455079a 100644 --- a/mwparserfromhell/parser/tokenizer.py +++ b/mwparserfromhell/parser/tokenizer.py @@ -339,14 +339,14 @@ class Tokenizer(object): current = int(log(self._context / contexts.HEADING_LEVEL_1, 2)) + 1 level = min(current, min(best, 6)) - try: + try: # Try to check for a heading closure after this one after, after_level = self._parse(self._context) except BadRoute: if level < best: self._write_text("=" * (best - level)) self._head = reset + best - 1 return self._pop(), level - else: + else: # Found another closure self._write_text("=" * best) self._write_all(after) return self._pop(), after_level