Browse Source

Fix handling of sections headers with equal signs (closes #20)

tags/v0.2
Ben Kurtovic 12 years ago
parent
commit
11cf5def75
2 changed files with 22 additions and 26 deletions
  1. +20
    -24
      mwparserfromhell/parser/tokenizer.c
  2. +2
    -2
      mwparserfromhell/parser/tokenizer.py

+ 20
- 24
mwparserfromhell/parser/tokenizer.c View File

@@ -759,11 +759,10 @@ Tokenizer_parse_heading(Tokenizer* self)
if (BAD_ROUTE) { if (BAD_ROUTE) {
RESET_ROUTE(); RESET_ROUTE();
self->head = reset + best - 1; 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; self->global ^= GL_HEADING;
return 0; return 0;
} }
@@ -799,13 +798,12 @@ Tokenizer_parse_heading(Tokenizer* self)
Py_DECREF(token); Py_DECREF(token);
if (heading->level < best) { if (heading->level < best) {
diff = best - heading->level; 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)) { if (Tokenizer_write_all(self, heading->title)) {
@@ -851,22 +849,20 @@ Tokenizer_handle_heading_end(Tokenizer* self)
RESET_ROUTE(); RESET_ROUTE();
if (level < best) { if (level < best) {
diff = best - level; 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; self->head = reset + best - 1;
} }
else { 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)) { if (Tokenizer_write_all(self, after->title)) {
Py_DECREF(after->title); Py_DECREF(after->title);


+ 2
- 2
mwparserfromhell/parser/tokenizer.py View File

@@ -339,14 +339,14 @@ class Tokenizer(object):
current = int(log(self._context / contexts.HEADING_LEVEL_1, 2)) + 1 current = int(log(self._context / contexts.HEADING_LEVEL_1, 2)) + 1
level = min(current, min(best, 6)) 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) after, after_level = self._parse(self._context)
except BadRoute: except BadRoute:
if level < best: if level < best:
self._write_text("=" * (best - level)) self._write_text("=" * (best - level))
self._head = reset + best - 1 self._head = reset + best - 1
return self._pop(), level return self._pop(), level
else:
else: # Found another closure
self._write_text("=" * best) self._write_text("=" * best)
self._write_all(after) self._write_all(after)
return self._pop(), after_level return self._pop(), after_level


Loading…
Cancel
Save