A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

2196 рядки
59 KiB

  1. /*
  2. Tokenizer for MWParserFromHell
  3. Copyright (C) 2012-2013 Ben Kurtovic <ben.kurtovic@verizon.net>
  4. Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. this software and associated documentation files (the "Software"), to deal in
  6. the Software without restriction, including without limitation the rights to
  7. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  8. of the Software, and to permit persons to whom the Software is furnished to do
  9. so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. #include "tokenizer.h"
  21. /*
  22. Given a context, return the heading level encoded within it.
  23. */
  24. static int heading_level_from_context(int n)
  25. {
  26. int level;
  27. n /= LC_HEADING_LEVEL_1;
  28. for (level = 1; n > 1; n >>= 1)
  29. level++;
  30. return level;
  31. }
  32. /*
  33. Call the given function in tag_defs, using 'tag' as a parameter, and return
  34. its output as a bool.
  35. */
  36. static int
  37. call_tag_def_func(const char* funcname, PyObject* tag)
  38. {
  39. PyObject* func = PyObject_GetAttrString(tag_defs, funcname);
  40. PyObject* result = PyObject_CallFunctionObjArgs(func, tag, NULL);
  41. int ans = (result == Py_True) ? 1 : 0;
  42. Py_DECREF(func);
  43. Py_DECREF(result);
  44. return ans;
  45. }
  46. /*
  47. Sanitize the name of a tag so it can be compared with others for equality.
  48. */
  49. static PyObject*
  50. strip_tag_name(PyObject* token)
  51. {
  52. PyObject *text, *rstripped, *lowered;
  53. text = PyObject_GetAttrString(token, "text");
  54. if (!text)
  55. return NULL;
  56. rstripped = PyObject_CallMethod(text, "rstrip", NULL);
  57. Py_DECREF(text);
  58. if (!rstripped)
  59. return NULL;
  60. lowered = PyObject_CallMethod(rstripped, "rstrip", NULL);
  61. Py_DECREF(rstripped);
  62. return lowered;
  63. }
  64. static Textbuffer*
  65. Textbuffer_new(void)
  66. {
  67. Textbuffer* buffer = malloc(sizeof(Textbuffer));
  68. if (!buffer) {
  69. PyErr_NoMemory();
  70. return NULL;
  71. }
  72. buffer->size = 0;
  73. buffer->data = malloc(sizeof(Py_UNICODE) * TEXTBUFFER_BLOCKSIZE);
  74. if (!buffer->data) {
  75. free(buffer);
  76. PyErr_NoMemory();
  77. return NULL;
  78. }
  79. buffer->next = NULL;
  80. return buffer;
  81. }
  82. static void
  83. Textbuffer_dealloc(Textbuffer* self)
  84. {
  85. Textbuffer* next;
  86. while (self) {
  87. free(self->data);
  88. next = self->next;
  89. free(self);
  90. self = next;
  91. }
  92. }
  93. /*
  94. Write text to the given textbuffer.
  95. */
  96. static int
  97. Textbuffer_write(Textbuffer** this, Py_UNICODE text)
  98. {
  99. Textbuffer* self = *this;
  100. if (self->size == TEXTBUFFER_BLOCKSIZE) {
  101. Textbuffer* new = Textbuffer_new();
  102. if (!new)
  103. return -1;
  104. new->next = self;
  105. *this = self = new;
  106. }
  107. self->data[self->size] = text;
  108. self->size++;
  109. return 0;
  110. }
  111. /*
  112. Return the contents of the textbuffer as a Python Unicode object.
  113. */
  114. static PyObject*
  115. Textbuffer_render(Textbuffer* self)
  116. {
  117. PyObject *result = PyUnicode_FromUnicode(self->data, self->size);
  118. PyObject *left, *concat;
  119. while (self->next) {
  120. self = self->next;
  121. left = PyUnicode_FromUnicode(self->data, self->size);
  122. concat = PyUnicode_Concat(left, result);
  123. Py_DECREF(left);
  124. Py_DECREF(result);
  125. result = concat;
  126. }
  127. return result;
  128. }
  129. static PyObject*
  130. Tokenizer_new(PyTypeObject* type, PyObject* args, PyObject* kwds)
  131. {
  132. Tokenizer* self = (Tokenizer*) type->tp_alloc(type, 0);
  133. return (PyObject*) self;
  134. }
  135. static void
  136. Tokenizer_dealloc(Tokenizer* self)
  137. {
  138. Stack *this = self->topstack, *next;
  139. Py_XDECREF(self->text);
  140. while (this) {
  141. Py_DECREF(this->stack);
  142. Textbuffer_dealloc(this->textbuffer);
  143. next = this->next;
  144. free(this);
  145. this = next;
  146. }
  147. self->ob_type->tp_free((PyObject*) self);
  148. }
  149. static int
  150. Tokenizer_init(Tokenizer* self, PyObject* args, PyObject* kwds)
  151. {
  152. static char* kwlist[] = {NULL};
  153. if (!PyArg_ParseTupleAndKeywords(args, kwds, "", kwlist))
  154. return -1;
  155. self->text = Py_None;
  156. Py_INCREF(Py_None);
  157. self->topstack = NULL;
  158. self->head = 0;
  159. self->length = 0;
  160. self->global = 0;
  161. return 0;
  162. }
  163. /*
  164. Add a new token stack, context, and textbuffer to the list.
  165. */
  166. static int
  167. Tokenizer_push(Tokenizer* self, int context)
  168. {
  169. Stack* top = malloc(sizeof(Stack));
  170. if (!top) {
  171. PyErr_NoMemory();
  172. return -1;
  173. }
  174. top->stack = PyList_New(0);
  175. top->context = context;
  176. top->textbuffer = Textbuffer_new();
  177. if (!top->textbuffer)
  178. return -1;
  179. top->next = self->topstack;
  180. self->topstack = top;
  181. self->depth++;
  182. self->cycles++;
  183. return 0;
  184. }
  185. /*
  186. Push the textbuffer onto the stack as a Text node and clear it.
  187. */
  188. static int
  189. Tokenizer_push_textbuffer(Tokenizer* self)
  190. {
  191. PyObject *text, *kwargs, *token;
  192. Textbuffer* buffer = self->topstack->textbuffer;
  193. if (buffer->size == 0 && !buffer->next)
  194. return 0;
  195. text = Textbuffer_render(buffer);
  196. if (!text)
  197. return -1;
  198. kwargs = PyDict_New();
  199. if (!kwargs) {
  200. Py_DECREF(text);
  201. return -1;
  202. }
  203. PyDict_SetItemString(kwargs, "text", text);
  204. Py_DECREF(text);
  205. token = PyObject_Call(Text, NOARGS, kwargs);
  206. Py_DECREF(kwargs);
  207. if (!token)
  208. return -1;
  209. if (PyList_Append(self->topstack->stack, token)) {
  210. Py_DECREF(token);
  211. return -1;
  212. }
  213. Py_DECREF(token);
  214. Textbuffer_dealloc(buffer);
  215. self->topstack->textbuffer = Textbuffer_new();
  216. if (!self->topstack->textbuffer)
  217. return -1;
  218. return 0;
  219. }
  220. /*
  221. Pop and deallocate the top token stack/context/textbuffer.
  222. */
  223. static void
  224. Tokenizer_delete_top_of_stack(Tokenizer* self)
  225. {
  226. Stack* top = self->topstack;
  227. Py_DECREF(top->stack);
  228. Textbuffer_dealloc(top->textbuffer);
  229. self->topstack = top->next;
  230. free(top);
  231. self->depth--;
  232. }
  233. /*
  234. Pop the current stack/context/textbuffer, returing the stack.
  235. */
  236. static PyObject*
  237. Tokenizer_pop(Tokenizer* self)
  238. {
  239. PyObject* stack;
  240. if (Tokenizer_push_textbuffer(self))
  241. return NULL;
  242. stack = self->topstack->stack;
  243. Py_INCREF(stack);
  244. Tokenizer_delete_top_of_stack(self);
  245. return stack;
  246. }
  247. /*
  248. Pop the current stack/context/textbuffer, returing the stack. We will also
  249. replace the underlying stack's context with the current stack's.
  250. */
  251. static PyObject*
  252. Tokenizer_pop_keeping_context(Tokenizer* self)
  253. {
  254. PyObject* stack;
  255. int context;
  256. if (Tokenizer_push_textbuffer(self))
  257. return NULL;
  258. stack = self->topstack->stack;
  259. Py_INCREF(stack);
  260. context = self->topstack->context;
  261. Tokenizer_delete_top_of_stack(self);
  262. self->topstack->context = context;
  263. return stack;
  264. }
  265. /*
  266. Fail the current tokenization route. Discards the current
  267. stack/context/textbuffer and raises a BadRoute exception.
  268. */
  269. static void*
  270. Tokenizer_fail_route(Tokenizer* self)
  271. {
  272. PyObject* stack = Tokenizer_pop(self);
  273. Py_XDECREF(stack);
  274. FAIL_ROUTE();
  275. return NULL;
  276. }
  277. /*
  278. Write a token to the end of the current token stack.
  279. */
  280. static int
  281. Tokenizer_emit(Tokenizer* self, PyObject* token)
  282. {
  283. if (Tokenizer_push_textbuffer(self))
  284. return -1;
  285. if (PyList_Append(self->topstack->stack, token))
  286. return -1;
  287. return 0;
  288. }
  289. /*
  290. Write a token to the beginning of the current token stack.
  291. */
  292. static int
  293. Tokenizer_emit_first(Tokenizer* self, PyObject* token)
  294. {
  295. if (Tokenizer_push_textbuffer(self))
  296. return -1;
  297. if (PyList_Insert(self->topstack->stack, 0, token))
  298. return -1;
  299. return 0;
  300. }
  301. /*
  302. Write text to the current textbuffer.
  303. */
  304. static int
  305. Tokenizer_emit_text(Tokenizer* self, Py_UNICODE text)
  306. {
  307. return Textbuffer_write(&(self->topstack->textbuffer), text);
  308. }
  309. /*
  310. Write a series of tokens to the current stack at once.
  311. */
  312. static int
  313. Tokenizer_emit_all(Tokenizer* self, PyObject* tokenlist)
  314. {
  315. int pushed = 0;
  316. PyObject *stack, *token, *left, *right, *text;
  317. Textbuffer* buffer;
  318. Py_ssize_t size;
  319. if (PyList_GET_SIZE(tokenlist) > 0) {
  320. token = PyList_GET_ITEM(tokenlist, 0);
  321. switch (PyObject_IsInstance(token, Text)) {
  322. case 0:
  323. break;
  324. case 1: {
  325. pushed = 1;
  326. buffer = self->topstack->textbuffer;
  327. if (buffer->size == 0 && !buffer->next)
  328. break;
  329. left = Textbuffer_render(buffer);
  330. if (!left)
  331. return -1;
  332. right = PyObject_GetAttrString(token, "text");
  333. if (!right)
  334. return -1;
  335. text = PyUnicode_Concat(left, right);
  336. Py_DECREF(left);
  337. Py_DECREF(right);
  338. if (!text)
  339. return -1;
  340. if (PyObject_SetAttrString(token, "text", text)) {
  341. Py_DECREF(text);
  342. return -1;
  343. }
  344. Py_DECREF(text);
  345. Textbuffer_dealloc(buffer);
  346. self->topstack->textbuffer = Textbuffer_new();
  347. if (!self->topstack->textbuffer)
  348. return -1;
  349. break;
  350. }
  351. case -1:
  352. return -1;
  353. }
  354. }
  355. if (!pushed) {
  356. if (Tokenizer_push_textbuffer(self))
  357. return -1;
  358. }
  359. stack = self->topstack->stack;
  360. size = PyList_GET_SIZE(stack);
  361. if (PyList_SetSlice(stack, size, size, tokenlist))
  362. return -1;
  363. return 0;
  364. }
  365. /*
  366. Pop the current stack, write text, and then write the stack. 'text' is a
  367. NULL-terminated array of chars.
  368. */
  369. static int
  370. Tokenizer_emit_text_then_stack(Tokenizer* self, const char* text)
  371. {
  372. PyObject* stack = Tokenizer_pop(self);
  373. int i = 0;
  374. while (1) {
  375. if (!text[i])
  376. break;
  377. if (Tokenizer_emit_text(self, (Py_UNICODE) text[i])) {
  378. Py_XDECREF(stack);
  379. return -1;
  380. }
  381. i++;
  382. }
  383. if (stack) {
  384. if (PyList_GET_SIZE(stack) > 0) {
  385. if (Tokenizer_emit_all(self, stack)) {
  386. Py_DECREF(stack);
  387. return -1;
  388. }
  389. }
  390. Py_DECREF(stack);
  391. }
  392. self->head--;
  393. return 0;
  394. }
  395. /*
  396. Read the value at a relative point in the wikicode, forwards.
  397. */
  398. static PyObject*
  399. Tokenizer_read(Tokenizer* self, Py_ssize_t delta)
  400. {
  401. Py_ssize_t index = self->head + delta;
  402. if (index >= self->length)
  403. return EMPTY;
  404. return PyList_GET_ITEM(self->text, index);
  405. }
  406. /*
  407. Read the value at a relative point in the wikicode, backwards.
  408. */
  409. static PyObject*
  410. Tokenizer_read_backwards(Tokenizer* self, Py_ssize_t delta)
  411. {
  412. Py_ssize_t index;
  413. if (delta > self->head)
  414. return EMPTY;
  415. index = self->head - delta;
  416. return PyList_GET_ITEM(self->text, index);
  417. }
  418. /*
  419. Parse a template or argument at the head of the wikicode string.
  420. */
  421. static int
  422. Tokenizer_parse_template_or_argument(Tokenizer* self)
  423. {
  424. unsigned int braces = 2, i;
  425. PyObject *tokenlist;
  426. self->head += 2;
  427. while (Tokenizer_READ(self, 0) == *"{" && braces < MAX_BRACES) {
  428. self->head++;
  429. braces++;
  430. }
  431. if (Tokenizer_push(self, 0))
  432. return -1;
  433. while (braces) {
  434. if (braces == 1) {
  435. if (Tokenizer_emit_text_then_stack(self, "{"))
  436. return -1;
  437. return 0;
  438. }
  439. if (braces == 2) {
  440. if (Tokenizer_parse_template(self))
  441. return -1;
  442. if (BAD_ROUTE) {
  443. RESET_ROUTE();
  444. if (Tokenizer_emit_text_then_stack(self, "{{"))
  445. return -1;
  446. return 0;
  447. }
  448. break;
  449. }
  450. if (Tokenizer_parse_argument(self))
  451. return -1;
  452. if (BAD_ROUTE) {
  453. RESET_ROUTE();
  454. if (Tokenizer_parse_template(self))
  455. return -1;
  456. if (BAD_ROUTE) {
  457. char text[MAX_BRACES + 1];
  458. RESET_ROUTE();
  459. for (i = 0; i < braces; i++) text[i] = *"{";
  460. text[braces] = *"";
  461. if (Tokenizer_emit_text_then_stack(self, text)) {
  462. Py_XDECREF(text);
  463. return -1;
  464. }
  465. Py_XDECREF(text);
  466. return 0;
  467. }
  468. else
  469. braces -= 2;
  470. }
  471. else
  472. braces -= 3;
  473. if (braces)
  474. self->head++;
  475. }
  476. tokenlist = Tokenizer_pop(self);
  477. if (!tokenlist)
  478. return -1;
  479. if (Tokenizer_emit_all(self, tokenlist)) {
  480. Py_DECREF(tokenlist);
  481. return -1;
  482. }
  483. Py_DECREF(tokenlist);
  484. if (self->topstack->context & LC_FAIL_NEXT)
  485. self->topstack->context ^= LC_FAIL_NEXT;
  486. return 0;
  487. }
  488. /*
  489. Parse a template at the head of the wikicode string.
  490. */
  491. static int
  492. Tokenizer_parse_template(Tokenizer* self)
  493. {
  494. PyObject *template, *token;
  495. Py_ssize_t reset = self->head;
  496. template = Tokenizer_parse(self, LC_TEMPLATE_NAME, 1);
  497. if (BAD_ROUTE) {
  498. self->head = reset;
  499. return 0;
  500. }
  501. if (!template)
  502. return -1;
  503. token = PyObject_CallObject(TemplateOpen, NULL);
  504. if (!token) {
  505. Py_DECREF(template);
  506. return -1;
  507. }
  508. if (Tokenizer_emit_first(self, token)) {
  509. Py_DECREF(token);
  510. Py_DECREF(template);
  511. return -1;
  512. }
  513. Py_DECREF(token);
  514. if (Tokenizer_emit_all(self, template)) {
  515. Py_DECREF(template);
  516. return -1;
  517. }
  518. Py_DECREF(template);
  519. token = PyObject_CallObject(TemplateClose, NULL);
  520. if (!token)
  521. return -1;
  522. if (Tokenizer_emit(self, token)) {
  523. Py_DECREF(token);
  524. return -1;
  525. }
  526. Py_DECREF(token);
  527. return 0;
  528. }
  529. /*
  530. Parse an argument at the head of the wikicode string.
  531. */
  532. static int
  533. Tokenizer_parse_argument(Tokenizer* self)
  534. {
  535. PyObject *argument, *token;
  536. Py_ssize_t reset = self->head;
  537. argument = Tokenizer_parse(self, LC_ARGUMENT_NAME, 1);
  538. if (BAD_ROUTE) {
  539. self->head = reset;
  540. return 0;
  541. }
  542. if (!argument)
  543. return -1;
  544. token = PyObject_CallObject(ArgumentOpen, NULL);
  545. if (!token) {
  546. Py_DECREF(argument);
  547. return -1;
  548. }
  549. if (Tokenizer_emit_first(self, token)) {
  550. Py_DECREF(token);
  551. Py_DECREF(argument);
  552. return -1;
  553. }
  554. Py_DECREF(token);
  555. if (Tokenizer_emit_all(self, argument)) {
  556. Py_DECREF(argument);
  557. return -1;
  558. }
  559. Py_DECREF(argument);
  560. token = PyObject_CallObject(ArgumentClose, NULL);
  561. if (!token)
  562. return -1;
  563. if (Tokenizer_emit(self, token)) {
  564. Py_DECREF(token);
  565. return -1;
  566. }
  567. Py_DECREF(token);
  568. return 0;
  569. }
  570. /*
  571. Handle a template parameter at the head of the string.
  572. */
  573. static int
  574. Tokenizer_handle_template_param(Tokenizer* self)
  575. {
  576. PyObject *stack, *token;
  577. if (self->topstack->context & LC_TEMPLATE_NAME)
  578. self->topstack->context ^= LC_TEMPLATE_NAME;
  579. else if (self->topstack->context & LC_TEMPLATE_PARAM_VALUE)
  580. self->topstack->context ^= LC_TEMPLATE_PARAM_VALUE;
  581. if (self->topstack->context & LC_TEMPLATE_PARAM_KEY) {
  582. stack = Tokenizer_pop_keeping_context(self);
  583. if (!stack)
  584. return -1;
  585. if (Tokenizer_emit_all(self, stack)) {
  586. Py_DECREF(stack);
  587. return -1;
  588. }
  589. Py_DECREF(stack);
  590. }
  591. else
  592. self->topstack->context |= LC_TEMPLATE_PARAM_KEY;
  593. token = PyObject_CallObject(TemplateParamSeparator, NULL);
  594. if (!token)
  595. return -1;
  596. if (Tokenizer_emit(self, token)) {
  597. Py_DECREF(token);
  598. return -1;
  599. }
  600. Py_DECREF(token);
  601. if (Tokenizer_push(self, self->topstack->context))
  602. return -1;
  603. return 0;
  604. }
  605. /*
  606. Handle a template parameter's value at the head of the string.
  607. */
  608. static int
  609. Tokenizer_handle_template_param_value(Tokenizer* self)
  610. {
  611. PyObject *stack, *token;
  612. stack = Tokenizer_pop_keeping_context(self);
  613. if (!stack)
  614. return -1;
  615. if (Tokenizer_emit_all(self, stack)) {
  616. Py_DECREF(stack);
  617. return -1;
  618. }
  619. Py_DECREF(stack);
  620. self->topstack->context ^= LC_TEMPLATE_PARAM_KEY;
  621. self->topstack->context |= LC_TEMPLATE_PARAM_VALUE;
  622. token = PyObject_CallObject(TemplateParamEquals, NULL);
  623. if (!token)
  624. return -1;
  625. if (Tokenizer_emit(self, token)) {
  626. Py_DECREF(token);
  627. return -1;
  628. }
  629. Py_DECREF(token);
  630. return 0;
  631. }
  632. /*
  633. Handle the end of a template at the head of the string.
  634. */
  635. static PyObject*
  636. Tokenizer_handle_template_end(Tokenizer* self)
  637. {
  638. PyObject* stack;
  639. if (self->topstack->context & LC_TEMPLATE_PARAM_KEY) {
  640. stack = Tokenizer_pop_keeping_context(self);
  641. if (!stack)
  642. return NULL;
  643. if (Tokenizer_emit_all(self, stack)) {
  644. Py_DECREF(stack);
  645. return NULL;
  646. }
  647. Py_DECREF(stack);
  648. }
  649. self->head++;
  650. stack = Tokenizer_pop(self);
  651. return stack;
  652. }
  653. /*
  654. Handle the separator between an argument's name and default.
  655. */
  656. static int
  657. Tokenizer_handle_argument_separator(Tokenizer* self)
  658. {
  659. PyObject* token;
  660. self->topstack->context ^= LC_ARGUMENT_NAME;
  661. self->topstack->context |= LC_ARGUMENT_DEFAULT;
  662. token = PyObject_CallObject(ArgumentSeparator, NULL);
  663. if (!token)
  664. return -1;
  665. if (Tokenizer_emit(self, token)) {
  666. Py_DECREF(token);
  667. return -1;
  668. }
  669. Py_DECREF(token);
  670. return 0;
  671. }
  672. /*
  673. Handle the end of an argument at the head of the string.
  674. */
  675. static PyObject*
  676. Tokenizer_handle_argument_end(Tokenizer* self)
  677. {
  678. PyObject* stack = Tokenizer_pop(self);
  679. self->head += 2;
  680. return stack;
  681. }
  682. /*
  683. Parse an internal wikilink at the head of the wikicode string.
  684. */
  685. static int
  686. Tokenizer_parse_wikilink(Tokenizer* self)
  687. {
  688. Py_ssize_t reset;
  689. PyObject *wikilink, *token;
  690. int i;
  691. self->head += 2;
  692. reset = self->head - 1;
  693. wikilink = Tokenizer_parse(self, LC_WIKILINK_TITLE, 1);
  694. if (BAD_ROUTE) {
  695. RESET_ROUTE();
  696. self->head = reset;
  697. for (i = 0; i < 2; i++) {
  698. if (Tokenizer_emit_text(self, *"["))
  699. return -1;
  700. }
  701. return 0;
  702. }
  703. if (!wikilink)
  704. return -1;
  705. token = PyObject_CallObject(WikilinkOpen, NULL);
  706. if (!token) {
  707. Py_DECREF(wikilink);
  708. return -1;
  709. }
  710. if (Tokenizer_emit(self, token)) {
  711. Py_DECREF(token);
  712. Py_DECREF(wikilink);
  713. return -1;
  714. }
  715. Py_DECREF(token);
  716. if (Tokenizer_emit_all(self, wikilink)) {
  717. Py_DECREF(wikilink);
  718. return -1;
  719. }
  720. Py_DECREF(wikilink);
  721. token = PyObject_CallObject(WikilinkClose, NULL);
  722. if (!token)
  723. return -1;
  724. if (Tokenizer_emit(self, token)) {
  725. Py_DECREF(token);
  726. return -1;
  727. }
  728. Py_DECREF(token);
  729. if (self->topstack->context & LC_FAIL_NEXT)
  730. self->topstack->context ^= LC_FAIL_NEXT;
  731. return 0;
  732. }
  733. /*
  734. Handle the separator between a wikilink's title and its text.
  735. */
  736. static int
  737. Tokenizer_handle_wikilink_separator(Tokenizer* self)
  738. {
  739. PyObject* token;
  740. self->topstack->context ^= LC_WIKILINK_TITLE;
  741. self->topstack->context |= LC_WIKILINK_TEXT;
  742. token = PyObject_CallObject(WikilinkSeparator, NULL);
  743. if (!token)
  744. return -1;
  745. if (Tokenizer_emit(self, token)) {
  746. Py_DECREF(token);
  747. return -1;
  748. }
  749. Py_DECREF(token);
  750. return 0;
  751. }
  752. /*
  753. Handle the end of a wikilink at the head of the string.
  754. */
  755. static PyObject*
  756. Tokenizer_handle_wikilink_end(Tokenizer* self)
  757. {
  758. PyObject* stack = Tokenizer_pop(self);
  759. self->head += 1;
  760. return stack;
  761. }
  762. /*
  763. Parse a section heading at the head of the wikicode string.
  764. */
  765. static int
  766. Tokenizer_parse_heading(Tokenizer* self)
  767. {
  768. Py_ssize_t reset = self->head;
  769. int best = 1, i, context, diff;
  770. HeadingData *heading;
  771. PyObject *level, *kwargs, *token;
  772. self->global |= GL_HEADING;
  773. self->head += 1;
  774. while (Tokenizer_READ(self, 0) == *"=") {
  775. best++;
  776. self->head++;
  777. }
  778. context = LC_HEADING_LEVEL_1 << (best > 5 ? 5 : best - 1);
  779. heading = (HeadingData*) Tokenizer_parse(self, context, 1);
  780. if (BAD_ROUTE) {
  781. RESET_ROUTE();
  782. self->head = reset + best - 1;
  783. for (i = 0; i < best; i++) {
  784. if (Tokenizer_emit_text(self, *"="))
  785. return -1;
  786. }
  787. self->global ^= GL_HEADING;
  788. return 0;
  789. }
  790. level = PyInt_FromSsize_t(heading->level);
  791. if (!level) {
  792. Py_DECREF(heading->title);
  793. free(heading);
  794. return -1;
  795. }
  796. kwargs = PyDict_New();
  797. if (!kwargs) {
  798. Py_DECREF(level);
  799. Py_DECREF(heading->title);
  800. free(heading);
  801. return -1;
  802. }
  803. PyDict_SetItemString(kwargs, "level", level);
  804. Py_DECREF(level);
  805. token = PyObject_Call(HeadingStart, NOARGS, kwargs);
  806. Py_DECREF(kwargs);
  807. if (!token) {
  808. Py_DECREF(heading->title);
  809. free(heading);
  810. return -1;
  811. }
  812. if (Tokenizer_emit(self, token)) {
  813. Py_DECREF(token);
  814. Py_DECREF(heading->title);
  815. free(heading);
  816. return -1;
  817. }
  818. Py_DECREF(token);
  819. if (heading->level < best) {
  820. diff = best - heading->level;
  821. for (i = 0; i < diff; i++) {
  822. if (Tokenizer_emit_text(self, *"=")) {
  823. Py_DECREF(heading->title);
  824. free(heading);
  825. return -1;
  826. }
  827. }
  828. }
  829. if (Tokenizer_emit_all(self, heading->title)) {
  830. Py_DECREF(heading->title);
  831. free(heading);
  832. return -1;
  833. }
  834. Py_DECREF(heading->title);
  835. free(heading);
  836. token = PyObject_CallObject(HeadingEnd, NULL);
  837. if (!token)
  838. return -1;
  839. if (Tokenizer_emit(self, token)) {
  840. Py_DECREF(token);
  841. return -1;
  842. }
  843. Py_DECREF(token);
  844. self->global ^= GL_HEADING;
  845. return 0;
  846. }
  847. /*
  848. Handle the end of a section heading at the head of the string.
  849. */
  850. static HeadingData*
  851. Tokenizer_handle_heading_end(Tokenizer* self)
  852. {
  853. Py_ssize_t reset = self->head, best;
  854. int i, current, level, diff;
  855. HeadingData *after, *heading;
  856. PyObject *stack;
  857. self->head += 1;
  858. best = 1;
  859. while (Tokenizer_READ(self, 0) == *"=") {
  860. best++;
  861. self->head++;
  862. }
  863. current = heading_level_from_context(self->topstack->context);
  864. level = current > best ? (best > 6 ? 6 : best) :
  865. (current > 6 ? 6 : current);
  866. after = (HeadingData*) Tokenizer_parse(self, self->topstack->context, 1);
  867. if (BAD_ROUTE) {
  868. RESET_ROUTE();
  869. if (level < best) {
  870. diff = best - level;
  871. for (i = 0; i < diff; i++) {
  872. if (Tokenizer_emit_text(self, *"="))
  873. return NULL;
  874. }
  875. }
  876. self->head = reset + best - 1;
  877. }
  878. else {
  879. for (i = 0; i < best; i++) {
  880. if (Tokenizer_emit_text(self, *"=")) {
  881. Py_DECREF(after->title);
  882. free(after);
  883. return NULL;
  884. }
  885. }
  886. if (Tokenizer_emit_all(self, after->title)) {
  887. Py_DECREF(after->title);
  888. free(after);
  889. return NULL;
  890. }
  891. Py_DECREF(after->title);
  892. level = after->level;
  893. free(after);
  894. }
  895. stack = Tokenizer_pop(self);
  896. if (!stack)
  897. return NULL;
  898. heading = malloc(sizeof(HeadingData));
  899. if (!heading) {
  900. PyErr_NoMemory();
  901. return NULL;
  902. }
  903. heading->title = stack;
  904. heading->level = level;
  905. return heading;
  906. }
  907. /*
  908. Actually parse an HTML entity and ensure that it is valid.
  909. */
  910. static int
  911. Tokenizer_really_parse_entity(Tokenizer* self)
  912. {
  913. PyObject *token, *kwargs, *textobj;
  914. Py_UNICODE this;
  915. int numeric, hexadecimal, i, j, zeroes, test;
  916. char *valid, *text, *buffer, *def;
  917. #define FAIL_ROUTE_AND_EXIT() { \
  918. Tokenizer_fail_route(self); \
  919. free(text); \
  920. return 0; \
  921. }
  922. token = PyObject_CallObject(HTMLEntityStart, NULL);
  923. if (!token)
  924. return -1;
  925. if (Tokenizer_emit(self, token)) {
  926. Py_DECREF(token);
  927. return -1;
  928. }
  929. Py_DECREF(token);
  930. self->head++;
  931. this = Tokenizer_READ(self, 0);
  932. if (this == *"") {
  933. Tokenizer_fail_route(self);
  934. return 0;
  935. }
  936. if (this == *"#") {
  937. numeric = 1;
  938. token = PyObject_CallObject(HTMLEntityNumeric, NULL);
  939. if (!token)
  940. return -1;
  941. if (Tokenizer_emit(self, token)) {
  942. Py_DECREF(token);
  943. return -1;
  944. }
  945. Py_DECREF(token);
  946. self->head++;
  947. this = Tokenizer_READ(self, 0);
  948. if (this == *"") {
  949. Tokenizer_fail_route(self);
  950. return 0;
  951. }
  952. if (this == *"x" || this == *"X") {
  953. hexadecimal = 1;
  954. kwargs = PyDict_New();
  955. if (!kwargs)
  956. return -1;
  957. PyDict_SetItemString(kwargs, "char", Tokenizer_read(self, 0));
  958. token = PyObject_Call(HTMLEntityHex, NOARGS, kwargs);
  959. Py_DECREF(kwargs);
  960. if (!token)
  961. return -1;
  962. if (Tokenizer_emit(self, token)) {
  963. Py_DECREF(token);
  964. return -1;
  965. }
  966. Py_DECREF(token);
  967. self->head++;
  968. }
  969. else
  970. hexadecimal = 0;
  971. }
  972. else
  973. numeric = hexadecimal = 0;
  974. if (hexadecimal)
  975. valid = HEXDIGITS;
  976. else if (numeric)
  977. valid = DIGITS;
  978. else
  979. valid = ALPHANUM;
  980. text = calloc(MAX_ENTITY_SIZE, sizeof(char));
  981. if (!text) {
  982. PyErr_NoMemory();
  983. return -1;
  984. }
  985. i = 0;
  986. zeroes = 0;
  987. while (1) {
  988. this = Tokenizer_READ(self, 0);
  989. if (this == *";") {
  990. if (i == 0)
  991. FAIL_ROUTE_AND_EXIT()
  992. break;
  993. }
  994. if (i == 0 && this == *"0") {
  995. zeroes++;
  996. self->head++;
  997. continue;
  998. }
  999. if (i >= 8)
  1000. FAIL_ROUTE_AND_EXIT()
  1001. for (j = 0; j < NUM_MARKERS; j++) {
  1002. if (this == *MARKERS[j])
  1003. FAIL_ROUTE_AND_EXIT()
  1004. }
  1005. j = 0;
  1006. while (1) {
  1007. if (!valid[j])
  1008. FAIL_ROUTE_AND_EXIT()
  1009. if (this == valid[j])
  1010. break;
  1011. j++;
  1012. }
  1013. text[i] = this;
  1014. self->head++;
  1015. i++;
  1016. }
  1017. if (numeric) {
  1018. sscanf(text, (hexadecimal ? "%x" : "%d"), &test);
  1019. if (test < 1 || test > 0x10FFFF)
  1020. FAIL_ROUTE_AND_EXIT()
  1021. }
  1022. else {
  1023. i = 0;
  1024. while (1) {
  1025. def = entitydefs[i];
  1026. if (!def) // We've reached the end of the defs without finding it
  1027. FAIL_ROUTE_AND_EXIT()
  1028. if (strcmp(text, def) == 0)
  1029. break;
  1030. i++;
  1031. }
  1032. }
  1033. if (zeroes) {
  1034. buffer = calloc(strlen(text) + zeroes + 1, sizeof(char));
  1035. if (!buffer) {
  1036. free(text);
  1037. PyErr_NoMemory();
  1038. return -1;
  1039. }
  1040. for (i = 0; i < zeroes; i++)
  1041. strcat(buffer, "0");
  1042. strcat(buffer, text);
  1043. free(text);
  1044. text = buffer;
  1045. }
  1046. textobj = PyUnicode_FromString(text);
  1047. if (!textobj) {
  1048. free(text);
  1049. return -1;
  1050. }
  1051. free(text);
  1052. kwargs = PyDict_New();
  1053. if (!kwargs) {
  1054. Py_DECREF(textobj);
  1055. return -1;
  1056. }
  1057. PyDict_SetItemString(kwargs, "text", textobj);
  1058. Py_DECREF(textobj);
  1059. token = PyObject_Call(Text, NOARGS, kwargs);
  1060. Py_DECREF(kwargs);
  1061. if (!token)
  1062. return -1;
  1063. if (Tokenizer_emit(self, token)) {
  1064. Py_DECREF(token);
  1065. return -1;
  1066. }
  1067. Py_DECREF(token);
  1068. token = PyObject_CallObject(HTMLEntityEnd, NULL);
  1069. if (!token)
  1070. return -1;
  1071. if (Tokenizer_emit(self, token)) {
  1072. Py_DECREF(token);
  1073. return -1;
  1074. }
  1075. Py_DECREF(token);
  1076. return 0;
  1077. }
  1078. /*
  1079. Parse an HTML entity at the head of the wikicode string.
  1080. */
  1081. static int
  1082. Tokenizer_parse_entity(Tokenizer* self)
  1083. {
  1084. Py_ssize_t reset = self->head;
  1085. PyObject *tokenlist;
  1086. if (Tokenizer_push(self, 0))
  1087. return -1;
  1088. if (Tokenizer_really_parse_entity(self))
  1089. return -1;
  1090. if (BAD_ROUTE) {
  1091. RESET_ROUTE();
  1092. self->head = reset;
  1093. if (Tokenizer_emit_text(self, *"&"))
  1094. return -1;
  1095. return 0;
  1096. }
  1097. tokenlist = Tokenizer_pop(self);
  1098. if (!tokenlist)
  1099. return -1;
  1100. if (Tokenizer_emit_all(self, tokenlist)) {
  1101. Py_DECREF(tokenlist);
  1102. return -1;
  1103. }
  1104. Py_DECREF(tokenlist);
  1105. return 0;
  1106. }
  1107. /*
  1108. Parse an HTML comment at the head of the wikicode string.
  1109. */
  1110. static int
  1111. Tokenizer_parse_comment(Tokenizer* self)
  1112. {
  1113. Py_ssize_t reset = self->head + 3;
  1114. PyObject *token, *comment;
  1115. int i;
  1116. self->head += 4;
  1117. comment = Tokenizer_parse(self, LC_COMMENT, 1);
  1118. if (BAD_ROUTE) {
  1119. const char* text = "<!--";
  1120. RESET_ROUTE();
  1121. self->head = reset;
  1122. i = 0;
  1123. while (1) {
  1124. if (!text[i])
  1125. return 0;
  1126. if (Tokenizer_emit_text(self, (Py_UNICODE) text[i])) {
  1127. Py_XDECREF(text);
  1128. return -1;
  1129. }
  1130. i++;
  1131. }
  1132. return 0;
  1133. }
  1134. if (!comment)
  1135. return -1;
  1136. token = PyObject_CallObject(CommentStart, NULL);
  1137. if (!token) {
  1138. Py_DECREF(comment);
  1139. return -1;
  1140. }
  1141. if (Tokenizer_emit(self, token)) {
  1142. Py_DECREF(token);
  1143. Py_DECREF(comment);
  1144. return -1;
  1145. }
  1146. Py_DECREF(token);
  1147. if (Tokenizer_emit_all(self, comment)) {
  1148. Py_DECREF(comment);
  1149. return -1;
  1150. }
  1151. Py_DECREF(comment);
  1152. token = PyObject_CallObject(CommentEnd, NULL);
  1153. if (!token)
  1154. return -1;
  1155. if (Tokenizer_emit(self, token)) {
  1156. Py_DECREF(token);
  1157. return -1;
  1158. }
  1159. Py_DECREF(token);
  1160. self->head += 2;
  1161. return 0;
  1162. }
  1163. /*
  1164. Parse an HTML tag at the head of the wikicode string.
  1165. */
  1166. static int
  1167. Tokenizer_parse_tag(Tokenizer* self)
  1168. {
  1169. Py_ssize_t reset = self->head;
  1170. PyObject* tag;
  1171. self->head++;
  1172. tag = Tokenizer_really_parse_tag(self);
  1173. if (BAD_ROUTE) {
  1174. self->head = reset;
  1175. return Tokenizer_emit_text(self, *"<");
  1176. }
  1177. if (!tag) {
  1178. return -1;
  1179. }
  1180. Tokenizer_emit_all(self, tag);
  1181. Py_DECREF(tag);
  1182. return 0;
  1183. }
  1184. /*
  1185. Actually parse an HTML tag, starting with the open (<foo>).
  1186. */
  1187. static PyObject*
  1188. Tokenizer_really_parse_tag(Tokenizer* self)
  1189. {
  1190. TagOpenData *data = malloc(sizeof(TagOpenData));
  1191. PyObject *token, *text, *trash;
  1192. Py_UNICODE this, next;
  1193. int can_exit;
  1194. if (!data) {
  1195. PyErr_NoMemory();
  1196. return NULL;
  1197. }
  1198. data->pad_first = Textbuffer_new();
  1199. data->pad_before_eq = Textbuffer_new();
  1200. data->pad_after_eq = Textbuffer_new();
  1201. if (!data->pad_first || !data->pad_before_eq || !data->pad_after_eq) {
  1202. free(data);
  1203. return NULL;
  1204. }
  1205. if (Tokenizer_push(self, LC_TAG_OPEN)) {
  1206. free(data);
  1207. return NULL;
  1208. }
  1209. token = PyObject_CallObject(TagOpenOpen, NULL);
  1210. if (!token) {
  1211. free(data);
  1212. return NULL;
  1213. }
  1214. if (Tokenizer_emit(self, token)) {
  1215. Py_DECREF(token);
  1216. free(data);
  1217. return NULL;
  1218. }
  1219. Py_DECREF(token);
  1220. while (1) {
  1221. this = Tokenizer_READ(self, 0);
  1222. next = Tokenizer_READ(self, 1);
  1223. can_exit = (!(data->context & (TAG_QUOTED | TAG_NAME)) ||
  1224. data->context & TAG_NOTE_SPACE);
  1225. if (this == *"") {
  1226. if (self->topstack->context & LC_TAG_ATTR) {
  1227. if (data->context & TAG_QUOTED) {
  1228. // Unclosed attribute quote: reset, don't die
  1229. data->context = TAG_ATTR_VALUE;
  1230. trash = Tokenizer_pop(self);
  1231. Py_XDECREF(trash);
  1232. self->head = data->reset;
  1233. continue;
  1234. }
  1235. trash = Tokenizer_pop(self);
  1236. Py_XDECREF(trash);
  1237. }
  1238. free(data);
  1239. return Tokenizer_fail_route(self);
  1240. }
  1241. else if (this == *">" && can_exit) {
  1242. if (Tokenizer_handle_tag_close_open(self, data, TagCloseOpen)) {
  1243. free(data);
  1244. return NULL;
  1245. }
  1246. free(data);
  1247. self->topstack->context = LC_TAG_BODY;
  1248. token = PyList_GET_ITEM(self->topstack->stack, 1);
  1249. text = PyObject_GetAttrString(token, "text");
  1250. if (!text)
  1251. return NULL;
  1252. if (IS_SINGLE_ONLY(text)) {
  1253. Py_DECREF(text);
  1254. return Tokenizer_handle_single_only_tag_end(self);
  1255. }
  1256. if (IS_PARSABLE(text)) {
  1257. Py_DECREF(text);
  1258. return Tokenizer_parse(self, 0, 0);
  1259. }
  1260. Py_DECREF(text);
  1261. return Tokenizer_handle_blacklisted_tag(self);
  1262. }
  1263. else if (this == *"/" && next == *">" && can_exit) {
  1264. if (Tokenizer_handle_tag_close_open(self, data, TagCloseSelfclose)) {
  1265. free(data);
  1266. return NULL;
  1267. }
  1268. free(data);
  1269. return Tokenizer_pop(self);
  1270. }
  1271. else {
  1272. if (Tokenizer_handle_tag_data(self, data, this) || BAD_ROUTE) {
  1273. free(data);
  1274. return NULL;
  1275. }
  1276. }
  1277. self->head++;
  1278. }
  1279. }
  1280. /*
  1281. Write a pending tag attribute from data to the stack.
  1282. */
  1283. static int
  1284. Tokenizer_push_tag_buffer(Tokenizer* self, TagOpenData* data)
  1285. {
  1286. PyObject *token, *tokens, *kwargs, *pad_first, *pad_before_eq,
  1287. *pad_after_eq;
  1288. if (data->context & TAG_QUOTED) {
  1289. token = PyObject_CallObject(TagAttrQuote, NULL);
  1290. if (!token)
  1291. return -1;
  1292. if (Tokenizer_emit_first(self, token)) {
  1293. Py_DECREF(token);
  1294. return -1;
  1295. }
  1296. Py_DECREF(token);
  1297. tokens = Tokenizer_pop(self);
  1298. if (!tokens)
  1299. return -1;
  1300. if (Tokenizer_emit_all(self, tokens)) {
  1301. Py_DECREF(tokens);
  1302. return -1;
  1303. }
  1304. Py_DECREF(tokens);
  1305. }
  1306. pad_first = Textbuffer_render(data->pad_first);
  1307. pad_before_eq = Textbuffer_render(data->pad_before_eq);
  1308. pad_after_eq = Textbuffer_render(data->pad_after_eq);
  1309. if (!pad_first || !pad_before_eq || !pad_after_eq)
  1310. return -1;
  1311. kwargs = PyDict_New();
  1312. if (!kwargs)
  1313. return -1;
  1314. PyDict_SetItemString(kwargs, "pad_first", pad_first);
  1315. PyDict_SetItemString(kwargs, "pad_before_eq", pad_before_eq);
  1316. PyDict_SetItemString(kwargs, "pad_after_eq", pad_after_eq);
  1317. Py_DECREF(pad_first);
  1318. Py_DECREF(pad_before_eq);
  1319. Py_DECREF(pad_after_eq);
  1320. token = PyObject_Call(TagAttrStart, NOARGS, kwargs);
  1321. Py_DECREF(kwargs);
  1322. if (!token)
  1323. return -1;
  1324. if (Tokenizer_emit_first(self, token)) {
  1325. Py_DECREF(token);
  1326. return -1;
  1327. }
  1328. Py_DECREF(token);
  1329. tokens = Tokenizer_pop(self);
  1330. if (!tokens)
  1331. return -1;
  1332. if (Tokenizer_emit_all(self, tokens)) {
  1333. Py_DECREF(tokens);
  1334. return -1;
  1335. }
  1336. Py_DECREF(tokens);
  1337. Textbuffer_dealloc(data->pad_first);
  1338. Textbuffer_dealloc(data->pad_before_eq);
  1339. Textbuffer_dealloc(data->pad_after_eq);
  1340. data->pad_first = Textbuffer_new();
  1341. data->pad_before_eq = Textbuffer_new();
  1342. data->pad_after_eq = Textbuffer_new();
  1343. if (!data->pad_first || !data->pad_before_eq || !data->pad_after_eq)
  1344. return -1;
  1345. return 0;
  1346. }
  1347. /*
  1348. Handle all sorts of text data inside of an HTML open tag.
  1349. */
  1350. static int
  1351. Tokenizer_handle_tag_data(Tokenizer* self, TagOpenData* data, Py_UNICODE chunk)
  1352. {
  1353. PyObject *trash, *token;
  1354. int first_time, i, is_marker = 0, escaped;
  1355. if (data->context & TAG_NAME) {
  1356. first_time = !(data->context & TAG_NOTE_SPACE);
  1357. for (i = 0; i < NUM_MARKERS; i++) {
  1358. if (*MARKERS[i] == chunk) {
  1359. is_marker = 1;
  1360. break;
  1361. }
  1362. }
  1363. if (is_marker || (Py_UNICODE_ISSPACE(chunk) && first_time)) {
  1364. // Tags must start with text, not spaces
  1365. Tokenizer_fail_route(self);
  1366. return 0;
  1367. }
  1368. else if (first_time)
  1369. data->context |= TAG_NOTE_SPACE;
  1370. else if (Py_UNICODE_ISSPACE(chunk))
  1371. data->context = TAG_ATTR_READY;
  1372. }
  1373. else if (Py_UNICODE_ISSPACE(chunk))
  1374. return Tokenizer_handle_tag_space(self, data, chunk);
  1375. else if (data->context & TAG_NOTE_SPACE) {
  1376. if (data->context & TAG_QUOTED) {
  1377. data->context = TAG_ATTR_VALUE;
  1378. trash = Tokenizer_pop(self);
  1379. Py_XDECREF(trash);
  1380. self->head = data->reset - 1; // Will be auto-incremented
  1381. }
  1382. else
  1383. Tokenizer_fail_route(self);
  1384. return 0;
  1385. }
  1386. else if (data->context & TAG_ATTR_READY) {
  1387. data->context = TAG_ATTR_NAME;
  1388. if (Tokenizer_push(self, LC_TAG_ATTR))
  1389. return -1;
  1390. }
  1391. else if (data->context & TAG_ATTR_NAME) {
  1392. if (chunk == *"=") {
  1393. data->context = TAG_ATTR_VALUE | TAG_NOTE_QUOTE;
  1394. token = PyObject_CallObject(TagAttrEquals, NULL);
  1395. if (!token)
  1396. return -1;
  1397. if (Tokenizer_emit(self, token)) {
  1398. Py_DECREF(token);
  1399. return -1;
  1400. }
  1401. Py_DECREF(token);
  1402. return 0;
  1403. }
  1404. if (data->context & TAG_NOTE_EQUALS) {
  1405. if (Tokenizer_push_tag_buffer(self, data))
  1406. return -1;
  1407. data->context = TAG_ATTR_NAME;
  1408. if (Tokenizer_push(self, LC_TAG_ATTR))
  1409. return -1;
  1410. }
  1411. }
  1412. else if (data->context & TAG_ATTR_VALUE) {
  1413. escaped = (Tokenizer_READ_BACKWARDS(self, 1) == *"\\" &&
  1414. Tokenizer_READ_BACKWARDS(self, 2) != *"\\");
  1415. if (data->context & TAG_NOTE_QUOTE) {
  1416. data->context ^= TAG_NOTE_QUOTE;
  1417. if (chunk == *"\"" && !escaped) {
  1418. data->context |= TAG_QUOTED;
  1419. if (Tokenizer_push(self, self->topstack->context))
  1420. return -1;
  1421. data->reset = self->head;
  1422. return 0;
  1423. }
  1424. }
  1425. else if (data->context & TAG_QUOTED) {
  1426. if (chunk == *"\"" && !escaped) {
  1427. data->context |= TAG_NOTE_SPACE;
  1428. return 0;
  1429. }
  1430. }
  1431. }
  1432. return Tokenizer_handle_tag_text(self, chunk);
  1433. }
  1434. /*
  1435. Handle whitespace inside of an HTML open tag.
  1436. */
  1437. static int
  1438. Tokenizer_handle_tag_space(Tokenizer* self, TagOpenData* data, Py_UNICODE text)
  1439. {
  1440. int ctx = data->context;
  1441. int end_of_value = (ctx & TAG_ATTR_VALUE &&
  1442. !(ctx & (TAG_QUOTED | TAG_NOTE_QUOTE)));
  1443. if (end_of_value || (ctx & TAG_QUOTED && ctx & TAG_NOTE_SPACE)) {
  1444. if (Tokenizer_push_tag_buffer(self, data))
  1445. return -1;
  1446. data->context = TAG_ATTR_READY;
  1447. }
  1448. else if (ctx & TAG_NOTE_SPACE)
  1449. data->context = TAG_ATTR_READY;
  1450. else if (ctx & TAG_ATTR_NAME) {
  1451. data->context |= TAG_NOTE_EQUALS;
  1452. if (Textbuffer_write(&(data->pad_before_eq), text))
  1453. return -1;
  1454. }
  1455. if (ctx & TAG_QUOTED && !(ctx & TAG_NOTE_SPACE)) {
  1456. if (Tokenizer_emit_text(self, text))
  1457. return -1;
  1458. }
  1459. else if (data->context & TAG_ATTR_READY)
  1460. return Textbuffer_write(&(data->pad_first), text);
  1461. else if (data->context & TAG_ATTR_VALUE)
  1462. return Textbuffer_write(&(data->pad_after_eq), text);
  1463. return 0;
  1464. }
  1465. /*
  1466. Handle regular text inside of an HTML open tag.
  1467. */
  1468. static int
  1469. Tokenizer_handle_tag_text(Tokenizer* self, Py_UNICODE text)
  1470. {
  1471. Py_UNICODE next = Tokenizer_READ(self, 1);
  1472. int i, is_marker = 0;
  1473. for (i = 0; i < NUM_MARKERS; i++) {
  1474. if (*MARKERS[i] == text) {
  1475. is_marker = 1;
  1476. break;
  1477. }
  1478. }
  1479. if (!is_marker || !Tokenizer_CAN_RECURSE(self))
  1480. return Tokenizer_emit_text(self, text);
  1481. else if (text == next && next == *"{")
  1482. return Tokenizer_parse_template_or_argument(self);
  1483. else if (text == next && next == *"[")
  1484. return Tokenizer_parse_wikilink(self);
  1485. else if (text == *"<")
  1486. return Tokenizer_parse_tag(self);
  1487. return Tokenizer_emit_text(self, text);
  1488. }
  1489. /*
  1490. Handle the body of an HTML tag that is parser-blacklisted.
  1491. */
  1492. static PyObject*
  1493. Tokenizer_handle_blacklisted_tag(Tokenizer* self)
  1494. {
  1495. Py_UNICODE this, next;
  1496. while (1) {
  1497. this = Tokenizer_READ(self, 0);
  1498. next = Tokenizer_READ(self, 1);
  1499. self->head++;
  1500. if (this == *"")
  1501. return Tokenizer_fail_route(self);
  1502. else if (this == *"<" && next == *"/") {
  1503. if (Tokenizer_handle_tag_open_close(self))
  1504. return NULL;
  1505. return Tokenizer_parse(self, 0, 0);
  1506. }
  1507. if (Tokenizer_emit_text(self, this))
  1508. return NULL;
  1509. }
  1510. }
  1511. /*
  1512. Handle the closing of a open tag (<foo>).
  1513. */
  1514. static int
  1515. Tokenizer_handle_tag_close_open(Tokenizer* self, TagOpenData* data,
  1516. PyObject* token)
  1517. {
  1518. PyObject *padding, *kwargs, *tok;
  1519. if (data->context & (TAG_ATTR_NAME | TAG_ATTR_VALUE)) {
  1520. if (Tokenizer_push_tag_buffer(self, data))
  1521. return -1;
  1522. }
  1523. padding = Textbuffer_render(data->pad_first);
  1524. if (!padding)
  1525. return -1;
  1526. kwargs = PyDict_New();
  1527. if (!kwargs) {
  1528. Py_DECREF(padding);
  1529. return -1;
  1530. }
  1531. PyDict_SetItemString(kwargs, "padding", padding);
  1532. Py_DECREF(padding);
  1533. tok = PyObject_Call(token, NOARGS, kwargs);
  1534. Py_DECREF(kwargs);
  1535. if (!tok)
  1536. return -1;
  1537. if (Tokenizer_emit(self, tok)) {
  1538. Py_DECREF(tok);
  1539. return -1;
  1540. }
  1541. Py_DECREF(tok);
  1542. self->head++;
  1543. return 0;
  1544. }
  1545. /*
  1546. Handle the opening of a closing tag (</foo>).
  1547. */
  1548. static int
  1549. Tokenizer_handle_tag_open_close(Tokenizer* self)
  1550. {
  1551. PyObject* token;
  1552. token = PyObject_CallObject(TagOpenClose, NULL);
  1553. if (!token)
  1554. return -1;
  1555. if (Tokenizer_emit(self, token)) {
  1556. Py_DECREF(token);
  1557. return -1;
  1558. }
  1559. Py_DECREF(token);
  1560. if (Tokenizer_push(self, LC_TAG_CLOSE))
  1561. return -1;
  1562. self->head++;
  1563. return 0;
  1564. }
  1565. /*
  1566. Handle the ending of a closing tag (</foo>).
  1567. */
  1568. static PyObject*
  1569. Tokenizer_handle_tag_close_close(Tokenizer* self)
  1570. {
  1571. PyObject *closing, *first, *so, *sc, *token;
  1572. int valid = 1;
  1573. closing = Tokenizer_pop(self);
  1574. if (!closing)
  1575. return NULL;
  1576. if (PyList_GET_SIZE(closing) != 1)
  1577. valid = 0;
  1578. else {
  1579. first = PyList_GET_ITEM(closing, 0);
  1580. switch (PyObject_IsInstance(first, Text)) {
  1581. case 0:
  1582. valid = 0;
  1583. break;
  1584. case 1: {
  1585. so = strip_tag_name(first);
  1586. sc = strip_tag_name(PyList_GET_ITEM(self->topstack->stack, 1));
  1587. if (so && sc) {
  1588. if (PyUnicode_Compare(so, sc))
  1589. valid = 0;
  1590. Py_DECREF(so);
  1591. Py_DECREF(sc);
  1592. break;
  1593. }
  1594. Py_XDECREF(so);
  1595. Py_XDECREF(sc);
  1596. }
  1597. case -1:
  1598. Py_DECREF(closing);
  1599. return NULL;
  1600. }
  1601. }
  1602. if (!valid) {
  1603. Py_DECREF(closing);
  1604. return Tokenizer_fail_route(self);
  1605. }
  1606. if (Tokenizer_emit_all(self, closing)) {
  1607. Py_DECREF(closing);
  1608. return NULL;
  1609. }
  1610. Py_DECREF(closing);
  1611. token = PyObject_CallObject(TagCloseClose, NULL);
  1612. if (!token)
  1613. return NULL;
  1614. if (Tokenizer_emit(self, token)) {
  1615. Py_DECREF(token);
  1616. return NULL;
  1617. }
  1618. Py_DECREF(token);
  1619. return Tokenizer_pop(self);
  1620. }
  1621. /*
  1622. Handle the (possible) start of an implicitly closing single tag.
  1623. */
  1624. static int
  1625. Tokenizer_handle_invalid_tag_start(Tokenizer* self)
  1626. {
  1627. Py_ssize_t reset = self->head + 1, pos = 0;
  1628. Textbuffer* buf;
  1629. PyObject *name, *tag;
  1630. Py_UNICODE this;
  1631. int is_marker, i;
  1632. self->head += 2;
  1633. buf = Textbuffer_new();
  1634. if (!buf)
  1635. return -1;
  1636. while (1) {
  1637. this = Tokenizer_READ(self, pos);
  1638. is_marker = 0;
  1639. for (i = 0; i < NUM_MARKERS; i++) {
  1640. if (*MARKERS[i] == this) {
  1641. is_marker = 1;
  1642. break;
  1643. }
  1644. }
  1645. if (is_marker) {
  1646. name = Textbuffer_render(buf);
  1647. if (!name) {
  1648. Textbuffer_dealloc(buf);
  1649. return -1;
  1650. }
  1651. if (!IS_SINGLE_ONLY(name))
  1652. FAIL_ROUTE();
  1653. break;
  1654. }
  1655. pos++;
  1656. }
  1657. if (!BAD_ROUTE) {
  1658. tag = Tokenizer_really_parse_tag(self);
  1659. if (!tag)
  1660. return -1;
  1661. }
  1662. if (BAD_ROUTE) {
  1663. self->head = reset;
  1664. return (Tokenizer_emit_text(self, *"<") ||
  1665. Tokenizer_emit_text(self, *"/"));
  1666. }
  1667. // Set invalid=True flag of TagOpenOpen
  1668. if (PyObject_SetAttrString(PyList_GET_ITEM(tag, 0), "invalid", Py_True))
  1669. return -1;
  1670. return Tokenizer_emit_all(self, tag);
  1671. }
  1672. /*
  1673. Handle the end of an implicitly closing single-only HTML tag.
  1674. */
  1675. static PyObject*
  1676. Tokenizer_handle_single_only_tag_end(Tokenizer* self)
  1677. {
  1678. return NULL;
  1679. }
  1680. /*
  1681. Handle the stream end when inside a single-supporting HTML tag.
  1682. */
  1683. static PyObject*
  1684. Tokenizer_handle_single_tag_end(Tokenizer* self)
  1685. {
  1686. return NULL;
  1687. }
  1688. /*
  1689. Handle the end of the stream of wikitext.
  1690. */
  1691. static PyObject*
  1692. Tokenizer_handle_end(Tokenizer* self, int context)
  1693. {
  1694. static int fail_contexts = (LC_TEMPLATE | LC_ARGUMENT | LC_WIKILINK |
  1695. LC_HEADING | LC_COMMENT);
  1696. static int double_fail = (LC_TEMPLATE_PARAM_KEY | LC_TAG_CLOSE);
  1697. PyObject *token, *text, *trash;
  1698. int single;
  1699. if (context & fail_contexts) {
  1700. if (context & LC_TAG_BODY) {
  1701. token = PyList_GET_ITEM(self->topstack->stack, 1);
  1702. text = PyObject_GetAttrString(token, "text");
  1703. if (!text)
  1704. return NULL;
  1705. single = IS_SINGLE(text);
  1706. Py_DECREF(text);
  1707. if (single)
  1708. return Tokenizer_handle_single_tag_end(self);
  1709. }
  1710. else if (context & double_fail) {
  1711. trash = Tokenizer_pop(self);
  1712. Py_XDECREF(trash);
  1713. }
  1714. return Tokenizer_fail_route(self);
  1715. }
  1716. return Tokenizer_pop(self);
  1717. }
  1718. /*
  1719. Make sure we are not trying to write an invalid character. Return 0 if
  1720. everything is safe, or -1 if the route must be failed.
  1721. */
  1722. static int
  1723. Tokenizer_verify_safe(Tokenizer* self, int context, Py_UNICODE data)
  1724. {
  1725. if (context & LC_FAIL_NEXT) {
  1726. return -1;
  1727. }
  1728. if (context & LC_WIKILINK_TITLE) {
  1729. if (data == *"]" || data == *"{")
  1730. self->topstack->context |= LC_FAIL_NEXT;
  1731. else if (data == *"\n" || data == *"[" || data == *"}")
  1732. return -1;
  1733. return 0;
  1734. }
  1735. if (context & LC_TAG_CLOSE) {
  1736. if (data == *"<")
  1737. return -1;
  1738. return 0;
  1739. }
  1740. if (context & LC_TEMPLATE_NAME) {
  1741. if (data == *"{" || data == *"}" || data == *"[") {
  1742. self->topstack->context |= LC_FAIL_NEXT;
  1743. return 0;
  1744. }
  1745. if (data == *"]") {
  1746. return -1;
  1747. }
  1748. if (data == *"|")
  1749. return 0;
  1750. if (context & LC_HAS_TEXT) {
  1751. if (context & LC_FAIL_ON_TEXT) {
  1752. if (!Py_UNICODE_ISSPACE(data))
  1753. return -1;
  1754. }
  1755. else {
  1756. if (data == *"\n")
  1757. self->topstack->context |= LC_FAIL_ON_TEXT;
  1758. }
  1759. }
  1760. else if (!Py_UNICODE_ISSPACE(data))
  1761. self->topstack->context |= LC_HAS_TEXT;
  1762. }
  1763. else {
  1764. if (context & LC_FAIL_ON_EQUALS) {
  1765. if (data == *"=") {
  1766. return -1;
  1767. }
  1768. }
  1769. else if (context & LC_FAIL_ON_LBRACE) {
  1770. if (data == *"{" || (Tokenizer_READ(self, -1) == *"{" &&
  1771. Tokenizer_READ(self, -2) == *"{")) {
  1772. if (context & LC_TEMPLATE)
  1773. self->topstack->context |= LC_FAIL_ON_EQUALS;
  1774. else
  1775. self->topstack->context |= LC_FAIL_NEXT;
  1776. return 0;
  1777. }
  1778. self->topstack->context ^= LC_FAIL_ON_LBRACE;
  1779. }
  1780. else if (context & LC_FAIL_ON_RBRACE) {
  1781. if (data == *"}") {
  1782. if (context & LC_TEMPLATE)
  1783. self->topstack->context |= LC_FAIL_ON_EQUALS;
  1784. else
  1785. self->topstack->context |= LC_FAIL_NEXT;
  1786. return 0;
  1787. }
  1788. self->topstack->context ^= LC_FAIL_ON_RBRACE;
  1789. }
  1790. else if (data == *"{")
  1791. self->topstack->context |= LC_FAIL_ON_LBRACE;
  1792. else if (data == *"}")
  1793. self->topstack->context |= LC_FAIL_ON_RBRACE;
  1794. }
  1795. return 0;
  1796. }
  1797. /*
  1798. Parse the wikicode string, using context for when to stop. If push is true,
  1799. we will push a new context, otherwise we won't and context will be ignored.
  1800. */
  1801. static PyObject*
  1802. Tokenizer_parse(Tokenizer* self, int context, int push)
  1803. {
  1804. static int unsafe_contexts = (LC_TEMPLATE_NAME | LC_WIKILINK_TITLE |
  1805. LC_TEMPLATE_PARAM_KEY | LC_ARGUMENT_NAME);
  1806. static int double_unsafe = (LC_TEMPLATE_PARAM_KEY | LC_TAG_CLOSE);
  1807. int this_context, is_marker, i;
  1808. Py_UNICODE this, next, next_next, last;
  1809. PyObject* trash;
  1810. if (push) {
  1811. if (Tokenizer_push(self, context))
  1812. return NULL;
  1813. }
  1814. while (1) {
  1815. this = Tokenizer_READ(self, 0);
  1816. this_context = self->topstack->context;
  1817. if (this_context & unsafe_contexts) {
  1818. if (Tokenizer_verify_safe(self, this_context, this) < 0) {
  1819. if (this_context & double_unsafe) {
  1820. trash = Tokenizer_pop(self);
  1821. Py_XDECREF(trash);
  1822. }
  1823. return Tokenizer_fail_route(self);
  1824. }
  1825. }
  1826. is_marker = 0;
  1827. for (i = 0; i < NUM_MARKERS; i++) {
  1828. if (*MARKERS[i] == this) {
  1829. is_marker = 1;
  1830. break;
  1831. }
  1832. }
  1833. if (!is_marker) {
  1834. if (Tokenizer_emit_text(self, this))
  1835. return NULL;
  1836. self->head++;
  1837. continue;
  1838. }
  1839. if (this == *"")
  1840. return Tokenizer_handle_end(self, this_context);
  1841. next = Tokenizer_READ(self, 1);
  1842. if (this_context & LC_COMMENT) {
  1843. if (this == next && next == *"-") {
  1844. if (Tokenizer_READ(self, 2) == *">")
  1845. return Tokenizer_pop(self);
  1846. }
  1847. if (Tokenizer_emit_text(self, this))
  1848. return NULL;
  1849. }
  1850. else if (this == next && next == *"{") {
  1851. if (Tokenizer_CAN_RECURSE(self)) {
  1852. if (Tokenizer_parse_template_or_argument(self))
  1853. return NULL;
  1854. }
  1855. else if (Tokenizer_emit_text(self, this))
  1856. return NULL;
  1857. }
  1858. else if (this == *"|" && this_context & LC_TEMPLATE) {
  1859. if (Tokenizer_handle_template_param(self))
  1860. return NULL;
  1861. }
  1862. else if (this == *"=" && this_context & LC_TEMPLATE_PARAM_KEY) {
  1863. if (Tokenizer_handle_template_param_value(self))
  1864. return NULL;
  1865. }
  1866. else if (this == next && next == *"}" && this_context & LC_TEMPLATE)
  1867. return Tokenizer_handle_template_end(self);
  1868. else if (this == *"|" && this_context & LC_ARGUMENT_NAME) {
  1869. if (Tokenizer_handle_argument_separator(self))
  1870. return NULL;
  1871. }
  1872. else if (this == next && next == *"}" && this_context & LC_ARGUMENT) {
  1873. if (Tokenizer_READ(self, 2) == *"}") {
  1874. return Tokenizer_handle_argument_end(self);
  1875. }
  1876. if (Tokenizer_emit_text(self, this))
  1877. return NULL;
  1878. }
  1879. else if (this == next && next == *"[") {
  1880. if (!(this_context & LC_WIKILINK_TITLE) &&
  1881. Tokenizer_CAN_RECURSE(self)) {
  1882. if (Tokenizer_parse_wikilink(self))
  1883. return NULL;
  1884. }
  1885. else if (Tokenizer_emit_text(self, this))
  1886. return NULL;
  1887. }
  1888. else if (this == *"|" && this_context & LC_WIKILINK_TITLE) {
  1889. if (Tokenizer_handle_wikilink_separator(self))
  1890. return NULL;
  1891. }
  1892. else if (this == next && next == *"]" && this_context & LC_WIKILINK)
  1893. return Tokenizer_handle_wikilink_end(self);
  1894. else if (this == *"=" && !(self->global & GL_HEADING)) {
  1895. last = Tokenizer_READ_BACKWARDS(self, 1);
  1896. if (last == *"\n" || last == *"") {
  1897. if (Tokenizer_parse_heading(self))
  1898. return NULL;
  1899. }
  1900. else if (Tokenizer_emit_text(self, this))
  1901. return NULL;
  1902. }
  1903. else if (this == *"=" && this_context & LC_HEADING)
  1904. return (PyObject*) Tokenizer_handle_heading_end(self);
  1905. else if (this == *"\n" && this_context & LC_HEADING)
  1906. return Tokenizer_fail_route(self);
  1907. else if (this == *"&") {
  1908. if (Tokenizer_parse_entity(self))
  1909. return NULL;
  1910. }
  1911. else if (this == *"<" && next == *"!") {
  1912. next_next = Tokenizer_READ(self, 2);
  1913. if (next_next == Tokenizer_READ(self, 3) && next_next == *"-") {
  1914. if (Tokenizer_parse_comment(self))
  1915. return NULL;
  1916. }
  1917. else if (Tokenizer_emit_text(self, this))
  1918. return NULL;
  1919. }
  1920. else if (this == *"<" && next == *"/" &&
  1921. Tokenizer_READ(self, 2) != *"") {
  1922. if (this_context & LC_TAG_BODY) {
  1923. if (Tokenizer_handle_tag_open_close(self))
  1924. return NULL;
  1925. }
  1926. else {
  1927. if (Tokenizer_handle_invalid_tag_start(self))
  1928. return NULL;
  1929. }
  1930. }
  1931. else if (this == *"<") {
  1932. if (!(this_context & LC_TAG_CLOSE) &&
  1933. Tokenizer_CAN_RECURSE(self)) {
  1934. if (Tokenizer_parse_tag(self))
  1935. return NULL;
  1936. }
  1937. else if (Tokenizer_emit_text(self, this))
  1938. return NULL;
  1939. }
  1940. else if (this == *">" && this_context & LC_TAG_CLOSE)
  1941. return Tokenizer_handle_tag_close_close(self);
  1942. else if (Tokenizer_emit_text(self, this))
  1943. return NULL;
  1944. self->head++;
  1945. }
  1946. }
  1947. /*
  1948. Build a list of tokens from a string of wikicode and return it.
  1949. */
  1950. static PyObject*
  1951. Tokenizer_tokenize(Tokenizer* self, PyObject* args)
  1952. {
  1953. PyObject *text, *temp;
  1954. if (!PyArg_ParseTuple(args, "U", &text)) {
  1955. const char* encoded;
  1956. Py_ssize_t size;
  1957. /* Failed to parse a Unicode object; try a string instead. */
  1958. PyErr_Clear();
  1959. if (!PyArg_ParseTuple(args, "s#", &encoded, &size))
  1960. return NULL;
  1961. temp = PyUnicode_FromStringAndSize(encoded, size);
  1962. if (!text)
  1963. return NULL;
  1964. Py_XDECREF(self->text);
  1965. text = PySequence_Fast(temp, "expected a sequence");
  1966. Py_XDECREF(temp);
  1967. self->text = text;
  1968. }
  1969. else {
  1970. Py_XDECREF(self->text);
  1971. self->text = PySequence_Fast(text, "expected a sequence");
  1972. }
  1973. self->length = PyList_GET_SIZE(self->text);
  1974. return Tokenizer_parse(self, 0, 1);
  1975. }
  1976. static void
  1977. load_entitydefs(void)
  1978. {
  1979. PyObject *tempmod, *defmap, *deflist;
  1980. unsigned numdefs, i;
  1981. tempmod = PyImport_ImportModule("htmlentitydefs");
  1982. if (!tempmod)
  1983. return;
  1984. defmap = PyObject_GetAttrString(tempmod, "entitydefs");
  1985. if (!defmap)
  1986. return;
  1987. Py_DECREF(tempmod);
  1988. deflist = PyDict_Keys(defmap);
  1989. if (!deflist)
  1990. return;
  1991. Py_DECREF(defmap);
  1992. numdefs = (unsigned) PyList_GET_SIZE(defmap);
  1993. entitydefs = calloc(numdefs + 1, sizeof(char*));
  1994. for (i = 0; i < numdefs; i++)
  1995. entitydefs[i] = PyBytes_AsString(PyList_GET_ITEM(deflist, i));
  1996. Py_DECREF(deflist);
  1997. }
  1998. static void
  1999. load_tokens(void)
  2000. {
  2001. PyObject *tempmod, *tokens,
  2002. *globals = PyEval_GetGlobals(),
  2003. *locals = PyEval_GetLocals(),
  2004. *fromlist = PyList_New(1),
  2005. *modname = PyBytes_FromString("tokens");
  2006. char *name = "mwparserfromhell.parser";
  2007. if (!fromlist || !modname)
  2008. return;
  2009. PyList_SET_ITEM(fromlist, 0, modname);
  2010. tempmod = PyImport_ImportModuleLevel(name, globals, locals, fromlist, 0);
  2011. Py_DECREF(fromlist);
  2012. if (!tempmod)
  2013. return;
  2014. tokens = PyObject_GetAttrString(tempmod, "tokens");
  2015. Py_DECREF(tempmod);
  2016. Text = PyObject_GetAttrString(tokens, "Text");
  2017. TemplateOpen = PyObject_GetAttrString(tokens, "TemplateOpen");
  2018. TemplateParamSeparator = PyObject_GetAttrString(tokens,
  2019. "TemplateParamSeparator");
  2020. TemplateParamEquals = PyObject_GetAttrString(tokens,
  2021. "TemplateParamEquals");
  2022. TemplateClose = PyObject_GetAttrString(tokens, "TemplateClose");
  2023. ArgumentOpen = PyObject_GetAttrString(tokens, "ArgumentOpen");
  2024. ArgumentSeparator = PyObject_GetAttrString(tokens, "ArgumentSeparator");
  2025. ArgumentClose = PyObject_GetAttrString(tokens, "ArgumentClose");
  2026. WikilinkOpen = PyObject_GetAttrString(tokens, "WikilinkOpen");
  2027. WikilinkSeparator = PyObject_GetAttrString(tokens, "WikilinkSeparator");
  2028. WikilinkClose = PyObject_GetAttrString(tokens, "WikilinkClose");
  2029. HTMLEntityStart = PyObject_GetAttrString(tokens, "HTMLEntityStart");
  2030. HTMLEntityNumeric = PyObject_GetAttrString(tokens, "HTMLEntityNumeric");
  2031. HTMLEntityHex = PyObject_GetAttrString(tokens, "HTMLEntityHex");
  2032. HTMLEntityEnd = PyObject_GetAttrString(tokens, "HTMLEntityEnd");
  2033. HeadingStart = PyObject_GetAttrString(tokens, "HeadingStart");
  2034. HeadingEnd = PyObject_GetAttrString(tokens, "HeadingEnd");
  2035. CommentStart = PyObject_GetAttrString(tokens, "CommentStart");
  2036. CommentEnd = PyObject_GetAttrString(tokens, "CommentEnd");
  2037. TagOpenOpen = PyObject_GetAttrString(tokens, "TagOpenOpen");
  2038. TagAttrStart = PyObject_GetAttrString(tokens, "TagAttrStart");
  2039. TagAttrEquals = PyObject_GetAttrString(tokens, "TagAttrEquals");
  2040. TagAttrQuote = PyObject_GetAttrString(tokens, "TagAttrQuote");
  2041. TagCloseOpen = PyObject_GetAttrString(tokens, "TagCloseOpen");
  2042. TagCloseSelfclose = PyObject_GetAttrString(tokens, "TagCloseSelfclose");
  2043. TagOpenClose = PyObject_GetAttrString(tokens, "TagOpenClose");
  2044. TagCloseClose = PyObject_GetAttrString(tokens, "TagCloseClose");
  2045. Py_DECREF(tokens);
  2046. }
  2047. static void
  2048. load_tag_defs(void)
  2049. {
  2050. PyObject *tempmod,
  2051. *globals = PyEval_GetGlobals(),
  2052. *locals = PyEval_GetLocals(),
  2053. *fromlist = PyList_New(1),
  2054. *modname = PyBytes_FromString("tag_defs");
  2055. char *name = "mwparserfromhell";
  2056. if (!fromlist || !modname)
  2057. return;
  2058. PyList_SET_ITEM(fromlist, 0, modname);
  2059. tempmod = PyImport_ImportModuleLevel(name, globals, locals, fromlist, 0);
  2060. Py_DECREF(fromlist);
  2061. if (!tempmod)
  2062. return;
  2063. tag_defs = PyObject_GetAttrString(tempmod, "tag_defs");
  2064. Py_DECREF(tempmod);
  2065. }
  2066. PyMODINIT_FUNC
  2067. init_tokenizer(void)
  2068. {
  2069. PyObject *module;
  2070. TokenizerType.tp_new = PyType_GenericNew;
  2071. if (PyType_Ready(&TokenizerType) < 0)
  2072. return;
  2073. module = Py_InitModule("_tokenizer", module_methods);
  2074. Py_INCREF(&TokenizerType);
  2075. PyModule_AddObject(module, "CTokenizer", (PyObject*) &TokenizerType);
  2076. Py_INCREF(Py_True);
  2077. PyDict_SetItemString(TokenizerType.tp_dict, "USES_C", Py_True);
  2078. EMPTY = PyUnicode_FromString("");
  2079. NOARGS = PyTuple_New(0);
  2080. load_entitydefs();
  2081. load_tokens();
  2082. load_tag_defs();
  2083. }