A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

1513 řádky
41 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. static PyObject*
  33. Tokenizer_new(PyTypeObject* type, PyObject* args, PyObject* kwds)
  34. {
  35. Tokenizer* self = (Tokenizer*) type->tp_alloc(type, 0);
  36. return (PyObject*) self;
  37. }
  38. static struct Textbuffer*
  39. Textbuffer_new(void)
  40. {
  41. struct Textbuffer* buffer = malloc(sizeof(struct Textbuffer));
  42. if (!buffer) {
  43. PyErr_NoMemory();
  44. return NULL;
  45. }
  46. buffer->size = 0;
  47. buffer->data = malloc(sizeof(Py_UNICODE) * TEXTBUFFER_BLOCKSIZE);
  48. if (!buffer->data) {
  49. free(buffer);
  50. PyErr_NoMemory();
  51. return NULL;
  52. }
  53. buffer->next = NULL;
  54. return buffer;
  55. }
  56. static void
  57. Tokenizer_dealloc(Tokenizer* self)
  58. {
  59. struct Stack *this = self->topstack, *next;
  60. Py_XDECREF(self->text);
  61. while (this) {
  62. Py_DECREF(this->stack);
  63. Textbuffer_dealloc(this->textbuffer);
  64. next = this->next;
  65. free(this);
  66. this = next;
  67. }
  68. self->ob_type->tp_free((PyObject*) self);
  69. }
  70. static void
  71. Textbuffer_dealloc(struct Textbuffer* this)
  72. {
  73. struct Textbuffer* next;
  74. while (this) {
  75. free(this->data);
  76. next = this->next;
  77. free(this);
  78. this = next;
  79. }
  80. }
  81. static int
  82. Tokenizer_init(Tokenizer* self, PyObject* args, PyObject* kwds)
  83. {
  84. static char* kwlist[] = {NULL};
  85. if (!PyArg_ParseTupleAndKeywords(args, kwds, "", kwlist))
  86. return -1;
  87. self->text = Py_None;
  88. Py_INCREF(Py_None);
  89. self->topstack = NULL;
  90. self->head = 0;
  91. self->length = 0;
  92. self->global = 0;
  93. return 0;
  94. }
  95. /*
  96. Add a new token stack, context, and textbuffer to the list.
  97. */
  98. static int
  99. Tokenizer_push(Tokenizer* self, int context)
  100. {
  101. struct Stack* top = malloc(sizeof(struct Stack));
  102. if (!top) {
  103. PyErr_NoMemory();
  104. return -1;
  105. }
  106. top->stack = PyList_New(0);
  107. top->context = context;
  108. top->textbuffer = Textbuffer_new();
  109. if (!top->textbuffer)
  110. return -1;
  111. top->next = self->topstack;
  112. self->topstack = top;
  113. self->depth++;
  114. self->cycles++;
  115. return 0;
  116. }
  117. /*
  118. Return the contents of the textbuffer as a Python Unicode object.
  119. */
  120. static PyObject*
  121. Textbuffer_render(struct Textbuffer* self)
  122. {
  123. PyObject *result = PyUnicode_FromUnicode(self->data, self->size);
  124. PyObject *left, *concat;
  125. while (self->next) {
  126. self = self->next;
  127. left = PyUnicode_FromUnicode(self->data, self->size);
  128. concat = PyUnicode_Concat(left, result);
  129. Py_DECREF(left);
  130. Py_DECREF(result);
  131. result = concat;
  132. }
  133. return result;
  134. }
  135. /*
  136. Push the textbuffer onto the stack as a Text node and clear it.
  137. */
  138. static int
  139. Tokenizer_push_textbuffer(Tokenizer* self)
  140. {
  141. PyObject *text, *kwargs, *token;
  142. struct Textbuffer* buffer = self->topstack->textbuffer;
  143. if (buffer->size == 0 && !buffer->next)
  144. return 0;
  145. text = Textbuffer_render(buffer);
  146. if (!text)
  147. return -1;
  148. kwargs = PyDict_New();
  149. if (!kwargs) {
  150. Py_DECREF(text);
  151. return -1;
  152. }
  153. PyDict_SetItemString(kwargs, "text", text);
  154. Py_DECREF(text);
  155. token = PyObject_Call(Text, NOARGS, kwargs);
  156. Py_DECREF(kwargs);
  157. if (!token)
  158. return -1;
  159. if (PyList_Append(self->topstack->stack, token)) {
  160. Py_DECREF(token);
  161. return -1;
  162. }
  163. Py_DECREF(token);
  164. Textbuffer_dealloc(buffer);
  165. self->topstack->textbuffer = Textbuffer_new();
  166. if (!self->topstack->textbuffer)
  167. return -1;
  168. return 0;
  169. }
  170. /*
  171. Pop and deallocate the top token stack/context/textbuffer.
  172. */
  173. static void
  174. Tokenizer_delete_top_of_stack(Tokenizer* self)
  175. {
  176. struct Stack* top = self->topstack;
  177. Py_DECREF(top->stack);
  178. Textbuffer_dealloc(top->textbuffer);
  179. self->topstack = top->next;
  180. free(top);
  181. self->depth--;
  182. }
  183. /*
  184. Pop the current stack/context/textbuffer, returing the stack.
  185. */
  186. static PyObject*
  187. Tokenizer_pop(Tokenizer* self)
  188. {
  189. PyObject* stack;
  190. if (Tokenizer_push_textbuffer(self))
  191. return NULL;
  192. stack = self->topstack->stack;
  193. Py_INCREF(stack);
  194. Tokenizer_delete_top_of_stack(self);
  195. return stack;
  196. }
  197. /*
  198. Pop the current stack/context/textbuffer, returing the stack. We will also
  199. replace the underlying stack's context with the current stack's.
  200. */
  201. static PyObject*
  202. Tokenizer_pop_keeping_context(Tokenizer* self)
  203. {
  204. PyObject* stack;
  205. int context;
  206. if (Tokenizer_push_textbuffer(self))
  207. return NULL;
  208. stack = self->topstack->stack;
  209. Py_INCREF(stack);
  210. context = self->topstack->context;
  211. Tokenizer_delete_top_of_stack(self);
  212. self->topstack->context = context;
  213. return stack;
  214. }
  215. /*
  216. Fail the current tokenization route. Discards the current
  217. stack/context/textbuffer and raises a BadRoute exception.
  218. */
  219. static void*
  220. Tokenizer_fail_route(Tokenizer* self)
  221. {
  222. PyObject* stack = Tokenizer_pop(self);
  223. Py_XDECREF(stack);
  224. FAIL_ROUTE();
  225. return NULL;
  226. }
  227. /*
  228. Write a token to the end of the current token stack.
  229. */
  230. static int
  231. Tokenizer_write(Tokenizer* self, PyObject* token)
  232. {
  233. if (Tokenizer_push_textbuffer(self))
  234. return -1;
  235. if (PyList_Append(self->topstack->stack, token))
  236. return -1;
  237. return 0;
  238. }
  239. /*
  240. Write a token to the beginning of the current token stack.
  241. */
  242. static int
  243. Tokenizer_write_first(Tokenizer* self, PyObject* token)
  244. {
  245. if (Tokenizer_push_textbuffer(self))
  246. return -1;
  247. if (PyList_Insert(self->topstack->stack, 0, token))
  248. return -1;
  249. return 0;
  250. }
  251. /*
  252. Write text to the current textbuffer.
  253. */
  254. static int
  255. Tokenizer_write_text(Tokenizer* self, Py_UNICODE text)
  256. {
  257. struct Textbuffer* buf = self->topstack->textbuffer;
  258. if (buf->size == TEXTBUFFER_BLOCKSIZE) {
  259. struct Textbuffer* new = Textbuffer_new();
  260. if (!new)
  261. return -1;
  262. new->next = buf;
  263. self->topstack->textbuffer = new;
  264. buf = new;
  265. }
  266. buf->data[buf->size] = text;
  267. buf->size++;
  268. return 0;
  269. }
  270. /*
  271. Write a series of tokens to the current stack at once.
  272. */
  273. static int
  274. Tokenizer_write_all(Tokenizer* self, PyObject* tokenlist)
  275. {
  276. int pushed = 0;
  277. PyObject *stack, *token, *left, *right, *text;
  278. struct Textbuffer* buffer;
  279. Py_ssize_t size;
  280. if (PyList_GET_SIZE(tokenlist) > 0) {
  281. token = PyList_GET_ITEM(tokenlist, 0);
  282. switch (PyObject_IsInstance(token, Text)) {
  283. case 0:
  284. break;
  285. case 1: {
  286. pushed = 1;
  287. buffer = self->topstack->textbuffer;
  288. if (buffer->size == 0 && !buffer->next)
  289. break;
  290. left = Textbuffer_render(buffer);
  291. if (!left)
  292. return -1;
  293. right = PyObject_GetAttrString(token, "text");
  294. if (!right)
  295. return -1;
  296. text = PyUnicode_Concat(left, right);
  297. Py_DECREF(left);
  298. Py_DECREF(right);
  299. if (!text)
  300. return -1;
  301. if (PyObject_SetAttrString(token, "text", text)) {
  302. Py_DECREF(text);
  303. return -1;
  304. }
  305. Py_DECREF(text);
  306. Textbuffer_dealloc(buffer);
  307. self->topstack->textbuffer = Textbuffer_new();
  308. if (!self->topstack->textbuffer)
  309. return -1;
  310. break;
  311. }
  312. case -1:
  313. return -1;
  314. }
  315. }
  316. if (!pushed) {
  317. if (Tokenizer_push_textbuffer(self))
  318. return -1;
  319. }
  320. stack = self->topstack->stack;
  321. size = PyList_GET_SIZE(stack);
  322. if (PyList_SetSlice(stack, size, size, tokenlist))
  323. return -1;
  324. return 0;
  325. }
  326. /*
  327. Pop the current stack, write text, and then write the stack. 'text' is a
  328. NULL-terminated array of chars.
  329. */
  330. static int
  331. Tokenizer_write_text_then_stack(Tokenizer* self, const char* text)
  332. {
  333. PyObject* stack = Tokenizer_pop(self);
  334. int i = 0;
  335. while (1) {
  336. if (!text[i])
  337. break;
  338. if (Tokenizer_write_text(self, (Py_UNICODE) text[i])) {
  339. Py_XDECREF(stack);
  340. return -1;
  341. }
  342. i++;
  343. }
  344. if (stack) {
  345. if (PyList_GET_SIZE(stack) > 0) {
  346. if (Tokenizer_write_all(self, stack)) {
  347. Py_DECREF(stack);
  348. return -1;
  349. }
  350. }
  351. Py_DECREF(stack);
  352. }
  353. self->head--;
  354. return 0;
  355. }
  356. /*
  357. Read the value at a relative point in the wikicode, forwards.
  358. */
  359. static PyObject*
  360. Tokenizer_read(Tokenizer* self, Py_ssize_t delta)
  361. {
  362. Py_ssize_t index = self->head + delta;
  363. if (index >= self->length)
  364. return EMPTY;
  365. return PyList_GET_ITEM(self->text, index);
  366. }
  367. /*
  368. Read the value at a relative point in the wikicode, backwards.
  369. */
  370. static PyObject*
  371. Tokenizer_read_backwards(Tokenizer* self, Py_ssize_t delta)
  372. {
  373. Py_ssize_t index;
  374. if (delta > self->head)
  375. return EMPTY;
  376. index = self->head - delta;
  377. return PyList_GET_ITEM(self->text, index);
  378. }
  379. /*
  380. Parse a template or argument at the head of the wikicode string.
  381. */
  382. static int
  383. Tokenizer_parse_template_or_argument(Tokenizer* self)
  384. {
  385. unsigned int braces = 2, i;
  386. PyObject *tokenlist;
  387. self->head += 2;
  388. while (Tokenizer_READ(self, 0) == *"{" && braces < MAX_BRACES) {
  389. self->head++;
  390. braces++;
  391. }
  392. if (Tokenizer_push(self, 0))
  393. return -1;
  394. while (braces) {
  395. if (braces == 1) {
  396. if (Tokenizer_write_text_then_stack(self, "{"))
  397. return -1;
  398. return 0;
  399. }
  400. if (braces == 2) {
  401. if (Tokenizer_parse_template(self))
  402. return -1;
  403. if (BAD_ROUTE) {
  404. RESET_ROUTE();
  405. if (Tokenizer_write_text_then_stack(self, "{{"))
  406. return -1;
  407. return 0;
  408. }
  409. break;
  410. }
  411. if (Tokenizer_parse_argument(self))
  412. return -1;
  413. if (BAD_ROUTE) {
  414. RESET_ROUTE();
  415. if (Tokenizer_parse_template(self))
  416. return -1;
  417. if (BAD_ROUTE) {
  418. char text[MAX_BRACES + 1];
  419. RESET_ROUTE();
  420. for (i = 0; i < braces; i++) text[i] = *"{";
  421. text[braces] = *"";
  422. if (Tokenizer_write_text_then_stack(self, text)) {
  423. Py_XDECREF(text);
  424. return -1;
  425. }
  426. Py_XDECREF(text);
  427. return 0;
  428. }
  429. else
  430. braces -= 2;
  431. }
  432. else
  433. braces -= 3;
  434. if (braces)
  435. self->head++;
  436. }
  437. tokenlist = Tokenizer_pop(self);
  438. if (!tokenlist)
  439. return -1;
  440. if (Tokenizer_write_all(self, tokenlist)) {
  441. Py_DECREF(tokenlist);
  442. return -1;
  443. }
  444. Py_DECREF(tokenlist);
  445. return 0;
  446. }
  447. /*
  448. Parse a template at the head of the wikicode string.
  449. */
  450. static int
  451. Tokenizer_parse_template(Tokenizer* self)
  452. {
  453. PyObject *template, *token;
  454. Py_ssize_t reset = self->head;
  455. template = Tokenizer_parse(self, LC_TEMPLATE_NAME);
  456. if (BAD_ROUTE) {
  457. self->head = reset;
  458. return 0;
  459. }
  460. if (!template)
  461. return -1;
  462. token = PyObject_CallObject(TemplateOpen, NULL);
  463. if (!token) {
  464. Py_DECREF(template);
  465. return -1;
  466. }
  467. if (Tokenizer_write_first(self, token)) {
  468. Py_DECREF(token);
  469. Py_DECREF(template);
  470. return -1;
  471. }
  472. Py_DECREF(token);
  473. if (Tokenizer_write_all(self, template)) {
  474. Py_DECREF(template);
  475. return -1;
  476. }
  477. Py_DECREF(template);
  478. token = PyObject_CallObject(TemplateClose, NULL);
  479. if (!token)
  480. return -1;
  481. if (Tokenizer_write(self, token)) {
  482. Py_DECREF(token);
  483. return -1;
  484. }
  485. Py_DECREF(token);
  486. return 0;
  487. }
  488. /*
  489. Parse an argument at the head of the wikicode string.
  490. */
  491. static int
  492. Tokenizer_parse_argument(Tokenizer* self)
  493. {
  494. PyObject *argument, *token;
  495. Py_ssize_t reset = self->head;
  496. argument = Tokenizer_parse(self, LC_ARGUMENT_NAME);
  497. if (BAD_ROUTE) {
  498. self->head = reset;
  499. return 0;
  500. }
  501. if (!argument)
  502. return -1;
  503. token = PyObject_CallObject(ArgumentOpen, NULL);
  504. if (!token) {
  505. Py_DECREF(argument);
  506. return -1;
  507. }
  508. if (Tokenizer_write_first(self, token)) {
  509. Py_DECREF(token);
  510. Py_DECREF(argument);
  511. return -1;
  512. }
  513. Py_DECREF(token);
  514. if (Tokenizer_write_all(self, argument)) {
  515. Py_DECREF(argument);
  516. return -1;
  517. }
  518. Py_DECREF(argument);
  519. token = PyObject_CallObject(ArgumentClose, NULL);
  520. if (!token)
  521. return -1;
  522. if (Tokenizer_write(self, token)) {
  523. Py_DECREF(token);
  524. return -1;
  525. }
  526. Py_DECREF(token);
  527. return 0;
  528. }
  529. /*
  530. Handle a template parameter at the head of the string.
  531. */
  532. static int
  533. Tokenizer_handle_template_param(Tokenizer* self)
  534. {
  535. PyObject *stack, *token;
  536. if (self->topstack->context & LC_TEMPLATE_NAME)
  537. self->topstack->context ^= LC_TEMPLATE_NAME;
  538. else if (self->topstack->context & LC_TEMPLATE_PARAM_VALUE)
  539. self->topstack->context ^= LC_TEMPLATE_PARAM_VALUE;
  540. if (self->topstack->context & LC_TEMPLATE_PARAM_KEY) {
  541. stack = Tokenizer_pop_keeping_context(self);
  542. if (!stack)
  543. return -1;
  544. if (Tokenizer_write_all(self, stack)) {
  545. Py_DECREF(stack);
  546. return -1;
  547. }
  548. Py_DECREF(stack);
  549. }
  550. else
  551. self->topstack->context |= LC_TEMPLATE_PARAM_KEY;
  552. token = PyObject_CallObject(TemplateParamSeparator, NULL);
  553. if (!token)
  554. return -1;
  555. if (Tokenizer_write(self, token)) {
  556. Py_DECREF(token);
  557. return -1;
  558. }
  559. Py_DECREF(token);
  560. if (Tokenizer_push(self, self->topstack->context))
  561. return -1;
  562. return 0;
  563. }
  564. /*
  565. Handle a template parameter's value at the head of the string.
  566. */
  567. static int
  568. Tokenizer_handle_template_param_value(Tokenizer* self)
  569. {
  570. PyObject *stack, *token;
  571. stack = Tokenizer_pop_keeping_context(self);
  572. if (!stack)
  573. return -1;
  574. if (Tokenizer_write_all(self, stack)) {
  575. Py_DECREF(stack);
  576. return -1;
  577. }
  578. Py_DECREF(stack);
  579. self->topstack->context ^= LC_TEMPLATE_PARAM_KEY;
  580. self->topstack->context |= LC_TEMPLATE_PARAM_VALUE;
  581. token = PyObject_CallObject(TemplateParamEquals, NULL);
  582. if (!token)
  583. return -1;
  584. if (Tokenizer_write(self, token)) {
  585. Py_DECREF(token);
  586. return -1;
  587. }
  588. Py_DECREF(token);
  589. return 0;
  590. }
  591. /*
  592. Handle the end of a template at the head of the string.
  593. */
  594. static PyObject*
  595. Tokenizer_handle_template_end(Tokenizer* self)
  596. {
  597. PyObject* stack;
  598. if (self->topstack->context & LC_TEMPLATE_PARAM_KEY) {
  599. stack = Tokenizer_pop_keeping_context(self);
  600. if (!stack)
  601. return NULL;
  602. if (Tokenizer_write_all(self, stack)) {
  603. Py_DECREF(stack);
  604. return NULL;
  605. }
  606. Py_DECREF(stack);
  607. }
  608. self->head++;
  609. stack = Tokenizer_pop(self);
  610. return stack;
  611. }
  612. /*
  613. Handle the separator between an argument's name and default.
  614. */
  615. static int
  616. Tokenizer_handle_argument_separator(Tokenizer* self)
  617. {
  618. PyObject* token;
  619. self->topstack->context ^= LC_ARGUMENT_NAME;
  620. self->topstack->context |= LC_ARGUMENT_DEFAULT;
  621. token = PyObject_CallObject(ArgumentSeparator, NULL);
  622. if (!token)
  623. return -1;
  624. if (Tokenizer_write(self, token)) {
  625. Py_DECREF(token);
  626. return -1;
  627. }
  628. Py_DECREF(token);
  629. return 0;
  630. }
  631. /*
  632. Handle the end of an argument at the head of the string.
  633. */
  634. static PyObject*
  635. Tokenizer_handle_argument_end(Tokenizer* self)
  636. {
  637. PyObject* stack = Tokenizer_pop(self);
  638. self->head += 2;
  639. return stack;
  640. }
  641. /*
  642. Parse an internal wikilink at the head of the wikicode string.
  643. */
  644. static int
  645. Tokenizer_parse_wikilink(Tokenizer* self)
  646. {
  647. Py_ssize_t reset;
  648. PyObject *wikilink, *token;
  649. int i;
  650. self->head += 2;
  651. reset = self->head - 1;
  652. wikilink = Tokenizer_parse(self, LC_WIKILINK_TITLE);
  653. if (BAD_ROUTE) {
  654. RESET_ROUTE();
  655. self->head = reset;
  656. for (i = 0; i < 2; i++) {
  657. if (Tokenizer_write_text(self, *"["))
  658. return -1;
  659. }
  660. return 0;
  661. }
  662. if (!wikilink)
  663. return -1;
  664. token = PyObject_CallObject(WikilinkOpen, NULL);
  665. if (!token) {
  666. Py_DECREF(wikilink);
  667. return -1;
  668. }
  669. if (Tokenizer_write(self, token)) {
  670. Py_DECREF(token);
  671. Py_DECREF(wikilink);
  672. return -1;
  673. }
  674. Py_DECREF(token);
  675. if (Tokenizer_write_all(self, wikilink)) {
  676. Py_DECREF(wikilink);
  677. return -1;
  678. }
  679. Py_DECREF(wikilink);
  680. token = PyObject_CallObject(WikilinkClose, NULL);
  681. if (!token)
  682. return -1;
  683. if (Tokenizer_write(self, token)) {
  684. Py_DECREF(token);
  685. return -1;
  686. }
  687. Py_DECREF(token);
  688. return 0;
  689. }
  690. /*
  691. Handle the separator between a wikilink's title and its text.
  692. */
  693. static int
  694. Tokenizer_handle_wikilink_separator(Tokenizer* self)
  695. {
  696. PyObject* token;
  697. self->topstack->context ^= LC_WIKILINK_TITLE;
  698. self->topstack->context |= LC_WIKILINK_TEXT;
  699. token = PyObject_CallObject(WikilinkSeparator, NULL);
  700. if (!token)
  701. return -1;
  702. if (Tokenizer_write(self, token)) {
  703. Py_DECREF(token);
  704. return -1;
  705. }
  706. Py_DECREF(token);
  707. return 0;
  708. }
  709. /*
  710. Handle the end of a wikilink at the head of the string.
  711. */
  712. static PyObject*
  713. Tokenizer_handle_wikilink_end(Tokenizer* self)
  714. {
  715. PyObject* stack = Tokenizer_pop(self);
  716. self->head += 1;
  717. return stack;
  718. }
  719. /*
  720. Parse a section heading at the head of the wikicode string.
  721. */
  722. static int
  723. Tokenizer_parse_heading(Tokenizer* self)
  724. {
  725. Py_ssize_t reset = self->head;
  726. int best = 1, i, context, diff;
  727. HeadingData *heading;
  728. PyObject *level, *kwargs, *token;
  729. self->global |= GL_HEADING;
  730. self->head += 1;
  731. while (Tokenizer_READ(self, 0) == *"=") {
  732. best++;
  733. self->head++;
  734. }
  735. context = LC_HEADING_LEVEL_1 << (best > 5 ? 5 : best - 1);
  736. heading = (HeadingData*) Tokenizer_parse(self, context);
  737. if (BAD_ROUTE) {
  738. RESET_ROUTE();
  739. self->head = reset + best - 1;
  740. for (i = 0; i < best; i++) {
  741. if (Tokenizer_write_text(self, *"="))
  742. return -1;
  743. }
  744. self->global ^= GL_HEADING;
  745. return 0;
  746. }
  747. level = PyInt_FromSsize_t(heading->level);
  748. if (!level) {
  749. Py_DECREF(heading->title);
  750. free(heading);
  751. return -1;
  752. }
  753. kwargs = PyDict_New();
  754. if (!kwargs) {
  755. Py_DECREF(level);
  756. Py_DECREF(heading->title);
  757. free(heading);
  758. return -1;
  759. }
  760. PyDict_SetItemString(kwargs, "level", level);
  761. Py_DECREF(level);
  762. token = PyObject_Call(HeadingStart, NOARGS, kwargs);
  763. Py_DECREF(kwargs);
  764. if (!token) {
  765. Py_DECREF(heading->title);
  766. free(heading);
  767. return -1;
  768. }
  769. if (Tokenizer_write(self, token)) {
  770. Py_DECREF(token);
  771. Py_DECREF(heading->title);
  772. free(heading);
  773. return -1;
  774. }
  775. Py_DECREF(token);
  776. if (heading->level < best) {
  777. diff = best - heading->level;
  778. for (i = 0; i < diff; i++) {
  779. if (Tokenizer_write_text(self, *"=")) {
  780. Py_DECREF(heading->title);
  781. free(heading);
  782. return -1;
  783. }
  784. }
  785. }
  786. if (Tokenizer_write_all(self, heading->title)) {
  787. Py_DECREF(heading->title);
  788. free(heading);
  789. return -1;
  790. }
  791. Py_DECREF(heading->title);
  792. free(heading);
  793. token = PyObject_CallObject(HeadingEnd, NULL);
  794. if (!token)
  795. return -1;
  796. if (Tokenizer_write(self, token)) {
  797. Py_DECREF(token);
  798. return -1;
  799. }
  800. Py_DECREF(token);
  801. self->global ^= GL_HEADING;
  802. return 0;
  803. }
  804. /*
  805. Handle the end of a section heading at the head of the string.
  806. */
  807. static HeadingData*
  808. Tokenizer_handle_heading_end(Tokenizer* self)
  809. {
  810. Py_ssize_t reset = self->head, best;
  811. int i, current, level, diff;
  812. HeadingData *after, *heading;
  813. PyObject *stack;
  814. self->head += 1;
  815. best = 1;
  816. while (Tokenizer_READ(self, 0) == *"=") {
  817. best++;
  818. self->head++;
  819. }
  820. current = heading_level_from_context(self->topstack->context);
  821. level = current > best ? (best > 6 ? 6 : best) :
  822. (current > 6 ? 6 : current);
  823. after = (HeadingData*) Tokenizer_parse(self, self->topstack->context);
  824. if (BAD_ROUTE) {
  825. RESET_ROUTE();
  826. if (level < best) {
  827. diff = best - level;
  828. for (i = 0; i < diff; i++) {
  829. if (Tokenizer_write_text(self, *"="))
  830. return NULL;
  831. }
  832. }
  833. self->head = reset + best - 1;
  834. }
  835. else {
  836. for (i = 0; i < best; i++) {
  837. if (Tokenizer_write_text(self, *"=")) {
  838. Py_DECREF(after->title);
  839. free(after);
  840. return NULL;
  841. }
  842. }
  843. if (Tokenizer_write_all(self, after->title)) {
  844. Py_DECREF(after->title);
  845. free(after);
  846. return NULL;
  847. }
  848. Py_DECREF(after->title);
  849. level = after->level;
  850. free(after);
  851. }
  852. stack = Tokenizer_pop(self);
  853. if (!stack)
  854. return NULL;
  855. heading = malloc(sizeof(HeadingData));
  856. if (!heading) {
  857. PyErr_NoMemory();
  858. return NULL;
  859. }
  860. heading->title = stack;
  861. heading->level = level;
  862. return heading;
  863. }
  864. /*
  865. Actually parse an HTML entity and ensure that it is valid.
  866. */
  867. static int
  868. Tokenizer_really_parse_entity(Tokenizer* self)
  869. {
  870. PyObject *token, *kwargs, *textobj;
  871. Py_UNICODE this;
  872. int numeric, hexadecimal, i, j, zeroes, test;
  873. char *valid, *text, *buffer, *def;
  874. #define FAIL_ROUTE_AND_EXIT() { \
  875. Tokenizer_fail_route(self); \
  876. free(text); \
  877. return 0; \
  878. }
  879. token = PyObject_CallObject(HTMLEntityStart, NULL);
  880. if (!token)
  881. return -1;
  882. if (Tokenizer_write(self, token)) {
  883. Py_DECREF(token);
  884. return -1;
  885. }
  886. Py_DECREF(token);
  887. self->head++;
  888. this = Tokenizer_READ(self, 0);
  889. if (this == *"") {
  890. Tokenizer_fail_route(self);
  891. return 0;
  892. }
  893. if (this == *"#") {
  894. numeric = 1;
  895. token = PyObject_CallObject(HTMLEntityNumeric, NULL);
  896. if (!token)
  897. return -1;
  898. if (Tokenizer_write(self, token)) {
  899. Py_DECREF(token);
  900. return -1;
  901. }
  902. Py_DECREF(token);
  903. self->head++;
  904. this = Tokenizer_READ(self, 0);
  905. if (this == *"") {
  906. Tokenizer_fail_route(self);
  907. return 0;
  908. }
  909. if (this == *"x" || this == *"X") {
  910. hexadecimal = 1;
  911. kwargs = PyDict_New();
  912. if (!kwargs)
  913. return -1;
  914. PyDict_SetItemString(kwargs, "char", Tokenizer_read(self, 0));
  915. token = PyObject_Call(HTMLEntityHex, NOARGS, kwargs);
  916. Py_DECREF(kwargs);
  917. if (!token)
  918. return -1;
  919. if (Tokenizer_write(self, token)) {
  920. Py_DECREF(token);
  921. return -1;
  922. }
  923. Py_DECREF(token);
  924. self->head++;
  925. }
  926. else
  927. hexadecimal = 0;
  928. }
  929. else
  930. numeric = hexadecimal = 0;
  931. if (hexadecimal)
  932. valid = HEXDIGITS;
  933. else if (numeric)
  934. valid = DIGITS;
  935. else
  936. valid = ALPHANUM;
  937. text = calloc(MAX_ENTITY_SIZE, sizeof(char));
  938. if (!text) {
  939. PyErr_NoMemory();
  940. return -1;
  941. }
  942. i = 0;
  943. zeroes = 0;
  944. while (1) {
  945. this = Tokenizer_READ(self, 0);
  946. if (this == *";") {
  947. if (i == 0)
  948. FAIL_ROUTE_AND_EXIT()
  949. break;
  950. }
  951. if (i == 0 && this == *"0") {
  952. zeroes++;
  953. self->head++;
  954. continue;
  955. }
  956. if (i >= 8)
  957. FAIL_ROUTE_AND_EXIT()
  958. for (j = 0; j < NUM_MARKERS; j++) {
  959. if (this == *MARKERS[j])
  960. FAIL_ROUTE_AND_EXIT()
  961. }
  962. j = 0;
  963. while (1) {
  964. if (!valid[j])
  965. FAIL_ROUTE_AND_EXIT()
  966. if (this == valid[j])
  967. break;
  968. j++;
  969. }
  970. text[i] = this;
  971. self->head++;
  972. i++;
  973. }
  974. if (numeric) {
  975. sscanf(text, (hexadecimal ? "%x" : "%d"), &test);
  976. if (test < 1 || test > 0x10FFFF)
  977. FAIL_ROUTE_AND_EXIT()
  978. }
  979. else {
  980. i = 0;
  981. while (1) {
  982. def = entitydefs[i];
  983. if (!def) // We've reached the end of the defs without finding it
  984. FAIL_ROUTE_AND_EXIT()
  985. if (strcmp(text, def) == 0)
  986. break;
  987. i++;
  988. }
  989. }
  990. if (zeroes) {
  991. buffer = calloc(strlen(text) + zeroes + 1, sizeof(char));
  992. if (!buffer) {
  993. free(text);
  994. PyErr_NoMemory();
  995. return -1;
  996. }
  997. for (i = 0; i < zeroes; i++)
  998. strcat(buffer, "0");
  999. strcat(buffer, text);
  1000. free(text);
  1001. text = buffer;
  1002. }
  1003. textobj = PyUnicode_FromString(text);
  1004. if (!textobj) {
  1005. free(text);
  1006. return -1;
  1007. }
  1008. free(text);
  1009. kwargs = PyDict_New();
  1010. if (!kwargs) {
  1011. Py_DECREF(textobj);
  1012. return -1;
  1013. }
  1014. PyDict_SetItemString(kwargs, "text", textobj);
  1015. Py_DECREF(textobj);
  1016. token = PyObject_Call(Text, NOARGS, kwargs);
  1017. Py_DECREF(kwargs);
  1018. if (!token)
  1019. return -1;
  1020. if (Tokenizer_write(self, token)) {
  1021. Py_DECREF(token);
  1022. return -1;
  1023. }
  1024. Py_DECREF(token);
  1025. token = PyObject_CallObject(HTMLEntityEnd, NULL);
  1026. if (!token)
  1027. return -1;
  1028. if (Tokenizer_write(self, token)) {
  1029. Py_DECREF(token);
  1030. return -1;
  1031. }
  1032. Py_DECREF(token);
  1033. return 0;
  1034. }
  1035. /*
  1036. Parse an HTML entity at the head of the wikicode string.
  1037. */
  1038. static int
  1039. Tokenizer_parse_entity(Tokenizer* self)
  1040. {
  1041. Py_ssize_t reset = self->head;
  1042. PyObject *tokenlist;
  1043. if (Tokenizer_push(self, 0))
  1044. return -1;
  1045. if (Tokenizer_really_parse_entity(self))
  1046. return -1;
  1047. if (BAD_ROUTE) {
  1048. RESET_ROUTE();
  1049. self->head = reset;
  1050. if (Tokenizer_write_text(self, *"&"))
  1051. return -1;
  1052. return 0;
  1053. }
  1054. tokenlist = Tokenizer_pop(self);
  1055. if (!tokenlist)
  1056. return -1;
  1057. if (Tokenizer_write_all(self, tokenlist)) {
  1058. Py_DECREF(tokenlist);
  1059. return -1;
  1060. }
  1061. Py_DECREF(tokenlist);
  1062. return 0;
  1063. }
  1064. /*
  1065. Parse an HTML comment at the head of the wikicode string.
  1066. */
  1067. static int
  1068. Tokenizer_parse_comment(Tokenizer* self)
  1069. {
  1070. Py_ssize_t reset = self->head + 3;
  1071. PyObject *token, *comment;
  1072. int i;
  1073. self->head += 4;
  1074. comment = Tokenizer_parse(self, LC_COMMENT);
  1075. if (BAD_ROUTE) {
  1076. const char* text = "<!--";
  1077. RESET_ROUTE();
  1078. self->head = reset;
  1079. i = 0;
  1080. while (1) {
  1081. if (!text[i])
  1082. return 0;
  1083. if (Tokenizer_write_text(self, (Py_UNICODE) text[i])) {
  1084. Py_XDECREF(text);
  1085. return -1;
  1086. }
  1087. i++;
  1088. }
  1089. return 0;
  1090. }
  1091. if (!comment)
  1092. return -1;
  1093. token = PyObject_CallObject(CommentStart, NULL);
  1094. if (!token) {
  1095. Py_DECREF(comment);
  1096. return -1;
  1097. }
  1098. if (Tokenizer_write(self, token)) {
  1099. Py_DECREF(token);
  1100. Py_DECREF(comment);
  1101. return -1;
  1102. }
  1103. Py_DECREF(token);
  1104. if (Tokenizer_write_all(self, comment)) {
  1105. Py_DECREF(comment);
  1106. return -1;
  1107. }
  1108. Py_DECREF(comment);
  1109. token = PyObject_CallObject(CommentEnd, NULL);
  1110. if (!token)
  1111. return -1;
  1112. if (Tokenizer_write(self, token)) {
  1113. Py_DECREF(token);
  1114. return -1;
  1115. }
  1116. Py_DECREF(token);
  1117. self->head += 2;
  1118. return 0;
  1119. }
  1120. /*
  1121. Make sure we are not trying to write an invalid character. Return 0 if
  1122. everything is safe, or -1 if the route must be failed.
  1123. */
  1124. static int
  1125. Tokenizer_verify_safe(Tokenizer* self, int context, Py_UNICODE data)
  1126. {
  1127. if (context & LC_FAIL_NEXT) {
  1128. return -1;
  1129. }
  1130. if (context & LC_WIKILINK_TITLE) {
  1131. if (data == *"]" || data == *"{")
  1132. self->topstack->context |= LC_FAIL_NEXT;
  1133. else if (data == *"\n" || data == *"[" || data == *"}")
  1134. return -1;
  1135. return 0;
  1136. }
  1137. if (context & LC_TEMPLATE_NAME) {
  1138. if (data == *"{" || data == *"}" || data == *"[") {
  1139. self->topstack->context |= LC_FAIL_NEXT;
  1140. return 0;
  1141. }
  1142. if (data == *"]") {
  1143. return -1;
  1144. }
  1145. if (data == *"|")
  1146. return 0;
  1147. if (context & LC_HAS_TEXT) {
  1148. if (context & LC_FAIL_ON_TEXT) {
  1149. if (!Py_UNICODE_ISSPACE(data))
  1150. return -1;
  1151. }
  1152. else {
  1153. if (data == *"\n")
  1154. self->topstack->context |= LC_FAIL_ON_TEXT;
  1155. }
  1156. }
  1157. else if (!Py_UNICODE_ISSPACE(data))
  1158. self->topstack->context |= LC_HAS_TEXT;
  1159. }
  1160. else {
  1161. if (context & LC_FAIL_ON_EQUALS) {
  1162. if (data == *"=") {
  1163. return -1;
  1164. }
  1165. }
  1166. else if (context & LC_FAIL_ON_LBRACE) {
  1167. if (data == *"{" || (Tokenizer_READ(self, -1) == *"{" &&
  1168. Tokenizer_READ(self, -2) == *"{")) {
  1169. if (context & LC_TEMPLATE)
  1170. self->topstack->context |= LC_FAIL_ON_EQUALS;
  1171. else
  1172. self->topstack->context |= LC_FAIL_NEXT;
  1173. return 0;
  1174. }
  1175. self->topstack->context ^= LC_FAIL_ON_LBRACE;
  1176. }
  1177. else if (context & LC_FAIL_ON_RBRACE) {
  1178. if (data == *"}") {
  1179. if (context & LC_TEMPLATE)
  1180. self->topstack->context |= LC_FAIL_ON_EQUALS;
  1181. else
  1182. self->topstack->context |= LC_FAIL_NEXT;
  1183. return 0;
  1184. }
  1185. self->topstack->context ^= LC_FAIL_ON_RBRACE;
  1186. }
  1187. else if (data == *"{")
  1188. self->topstack->context |= LC_FAIL_ON_LBRACE;
  1189. else if (data == *"}")
  1190. self->topstack->context |= LC_FAIL_ON_RBRACE;
  1191. }
  1192. return 0;
  1193. }
  1194. /*
  1195. Parse the wikicode string, using context for when to stop.
  1196. */
  1197. static PyObject*
  1198. Tokenizer_parse(Tokenizer* self, int context)
  1199. {
  1200. static int fail_contexts = (LC_TEMPLATE | LC_ARGUMENT | LC_WIKILINK |
  1201. LC_HEADING | LC_COMMENT);
  1202. static int unsafe_contexts = (LC_TEMPLATE_NAME | LC_WIKILINK_TITLE |
  1203. LC_TEMPLATE_PARAM_KEY | LC_ARGUMENT_NAME);
  1204. int this_context, is_marker, i;
  1205. Py_UNICODE this, next, next_next, last;
  1206. PyObject *trash;
  1207. if (Tokenizer_push(self, context))
  1208. return NULL;
  1209. while (1) {
  1210. this = Tokenizer_READ(self, 0);
  1211. this_context = self->topstack->context;
  1212. if (this_context & unsafe_contexts) {
  1213. if (Tokenizer_verify_safe(self, this_context, this) < 0) {
  1214. if (this_context & LC_TEMPLATE_PARAM_KEY) {
  1215. trash = Tokenizer_pop(self);
  1216. Py_XDECREF(trash);
  1217. }
  1218. Tokenizer_fail_route(self);
  1219. return NULL;
  1220. }
  1221. }
  1222. is_marker = 0;
  1223. for (i = 0; i < NUM_MARKERS; i++) {
  1224. if (*MARKERS[i] == this) {
  1225. is_marker = 1;
  1226. break;
  1227. }
  1228. }
  1229. if (!is_marker) {
  1230. Tokenizer_write_text(self, this);
  1231. self->head++;
  1232. continue;
  1233. }
  1234. if (this == *"") {
  1235. if (this_context & LC_TEMPLATE_PARAM_KEY) {
  1236. trash = Tokenizer_pop(self);
  1237. Py_XDECREF(trash);
  1238. }
  1239. if (this_context & fail_contexts)
  1240. return Tokenizer_fail_route(self);
  1241. return Tokenizer_pop(self);
  1242. }
  1243. next = Tokenizer_READ(self, 1);
  1244. if (this_context & LC_COMMENT) {
  1245. if (this == next && next == *"-") {
  1246. if (Tokenizer_READ(self, 2) == *">")
  1247. return Tokenizer_pop(self);
  1248. }
  1249. Tokenizer_write_text(self, this);
  1250. }
  1251. else if (this == next && next == *"{") {
  1252. if (Tokenizer_CAN_RECURSE(self)) {
  1253. if (Tokenizer_parse_template_or_argument(self))
  1254. return NULL;
  1255. if (self->topstack->context & LC_FAIL_NEXT)
  1256. self->topstack->context ^= LC_FAIL_NEXT;
  1257. }
  1258. else
  1259. Tokenizer_write_text(self, this);
  1260. }
  1261. else if (this == *"|" && this_context & LC_TEMPLATE) {
  1262. if (Tokenizer_handle_template_param(self))
  1263. return NULL;
  1264. }
  1265. else if (this == *"=" && this_context & LC_TEMPLATE_PARAM_KEY) {
  1266. if (Tokenizer_handle_template_param_value(self))
  1267. return NULL;
  1268. }
  1269. else if (this == next && next == *"}" && this_context & LC_TEMPLATE)
  1270. return Tokenizer_handle_template_end(self);
  1271. else if (this == *"|" && this_context & LC_ARGUMENT_NAME) {
  1272. if (Tokenizer_handle_argument_separator(self))
  1273. return NULL;
  1274. }
  1275. else if (this == next && next == *"}" && this_context & LC_ARGUMENT) {
  1276. if (Tokenizer_READ(self, 2) == *"}") {
  1277. return Tokenizer_handle_argument_end(self);
  1278. }
  1279. Tokenizer_write_text(self, this);
  1280. }
  1281. else if (this == next && next == *"[") {
  1282. if (!(this_context & LC_WIKILINK_TITLE) &&
  1283. Tokenizer_CAN_RECURSE(self)) {
  1284. if (Tokenizer_parse_wikilink(self))
  1285. return NULL;
  1286. if (self->topstack->context & LC_FAIL_NEXT)
  1287. self->topstack->context ^= LC_FAIL_NEXT;
  1288. }
  1289. else
  1290. Tokenizer_write_text(self, this);
  1291. }
  1292. else if (this == *"|" && this_context & LC_WIKILINK_TITLE) {
  1293. if (Tokenizer_handle_wikilink_separator(self))
  1294. return NULL;
  1295. }
  1296. else if (this == next && next == *"]" && this_context & LC_WIKILINK)
  1297. return Tokenizer_handle_wikilink_end(self);
  1298. else if (this == *"=" && !(self->global & GL_HEADING)) {
  1299. last = *PyUnicode_AS_UNICODE(Tokenizer_read_backwards(self, 1));
  1300. if (last == *"\n" || last == *"") {
  1301. if (Tokenizer_parse_heading(self))
  1302. return NULL;
  1303. }
  1304. else
  1305. Tokenizer_write_text(self, this);
  1306. }
  1307. else if (this == *"=" && this_context & LC_HEADING)
  1308. return (PyObject*) Tokenizer_handle_heading_end(self);
  1309. else if (this == *"\n" && this_context & LC_HEADING)
  1310. return Tokenizer_fail_route(self);
  1311. else if (this == *"&") {
  1312. if (Tokenizer_parse_entity(self))
  1313. return NULL;
  1314. }
  1315. else if (this == *"<" && next == *"!") {
  1316. next_next = Tokenizer_READ(self, 2);
  1317. if (next_next == Tokenizer_READ(self, 3) && next_next == *"-") {
  1318. if (Tokenizer_parse_comment(self))
  1319. return NULL;
  1320. }
  1321. else
  1322. Tokenizer_write_text(self, this);
  1323. }
  1324. else
  1325. Tokenizer_write_text(self, this);
  1326. self->head++;
  1327. }
  1328. }
  1329. /*
  1330. Build a list of tokens from a string of wikicode and return it.
  1331. */
  1332. static PyObject*
  1333. Tokenizer_tokenize(Tokenizer* self, PyObject* args)
  1334. {
  1335. PyObject *text, *temp;
  1336. if (!PyArg_ParseTuple(args, "U", &text)) {
  1337. const char* encoded;
  1338. Py_ssize_t size;
  1339. /* Failed to parse a Unicode object; try a string instead. */
  1340. PyErr_Clear();
  1341. if (!PyArg_ParseTuple(args, "s#", &encoded, &size))
  1342. return NULL;
  1343. temp = PyUnicode_FromStringAndSize(encoded, size);
  1344. if (!text)
  1345. return NULL;
  1346. Py_XDECREF(self->text);
  1347. text = PySequence_Fast(temp, "expected a sequence");
  1348. Py_XDECREF(temp);
  1349. self->text = text;
  1350. }
  1351. else {
  1352. Py_XDECREF(self->text);
  1353. self->text = PySequence_Fast(text, "expected a sequence");
  1354. }
  1355. self->length = PyList_GET_SIZE(self->text);
  1356. return Tokenizer_parse(self, 0);
  1357. }
  1358. PyMODINIT_FUNC
  1359. init_tokenizer(void)
  1360. {
  1361. PyObject *module, *tempmod, *defmap, *deflist, *globals, *locals,
  1362. *fromlist, *modname;
  1363. unsigned numdefs, i;
  1364. char *name;
  1365. TokenizerType.tp_new = PyType_GenericNew;
  1366. if (PyType_Ready(&TokenizerType) < 0)
  1367. return;
  1368. module = Py_InitModule("_tokenizer", module_methods);
  1369. Py_INCREF(&TokenizerType);
  1370. PyModule_AddObject(module, "CTokenizer", (PyObject*) &TokenizerType);
  1371. Py_INCREF(Py_True);
  1372. PyDict_SetItemString(TokenizerType.tp_dict, "USES_C", Py_True);
  1373. tempmod = PyImport_ImportModule("htmlentitydefs");
  1374. if (!tempmod)
  1375. return;
  1376. defmap = PyObject_GetAttrString(tempmod, "entitydefs");
  1377. if (!defmap)
  1378. return;
  1379. Py_DECREF(tempmod);
  1380. deflist = PyDict_Keys(defmap);
  1381. if (!deflist)
  1382. return;
  1383. Py_DECREF(defmap);
  1384. numdefs = (unsigned) PyList_GET_SIZE(defmap);
  1385. entitydefs = calloc(numdefs + 1, sizeof(char*));
  1386. for (i = 0; i < numdefs; i++)
  1387. entitydefs[i] = PyBytes_AsString(PyList_GET_ITEM(deflist, i));
  1388. Py_DECREF(deflist);
  1389. EMPTY = PyUnicode_FromString("");
  1390. NOARGS = PyTuple_New(0);
  1391. name = "mwparserfromhell.parser";
  1392. globals = PyEval_GetGlobals();
  1393. locals = PyEval_GetLocals();
  1394. fromlist = PyList_New(1);
  1395. if (!fromlist)
  1396. return;
  1397. modname = PyBytes_FromString("tokens");
  1398. if (!modname)
  1399. return;
  1400. PyList_SET_ITEM(fromlist, 0, modname);
  1401. tempmod = PyImport_ImportModuleLevel(name, globals, locals, fromlist, 0);
  1402. Py_DECREF(fromlist);
  1403. if (!tempmod)
  1404. return;
  1405. tokens = PyObject_GetAttrString(tempmod, "tokens");
  1406. Py_DECREF(tempmod);
  1407. Text = PyObject_GetAttrString(tokens, "Text");
  1408. TemplateOpen = PyObject_GetAttrString(tokens, "TemplateOpen");
  1409. TemplateParamSeparator = PyObject_GetAttrString(tokens,
  1410. "TemplateParamSeparator");
  1411. TemplateParamEquals = PyObject_GetAttrString(tokens,
  1412. "TemplateParamEquals");
  1413. TemplateClose = PyObject_GetAttrString(tokens, "TemplateClose");
  1414. ArgumentOpen = PyObject_GetAttrString(tokens, "ArgumentOpen");
  1415. ArgumentSeparator = PyObject_GetAttrString(tokens, "ArgumentSeparator");
  1416. ArgumentClose = PyObject_GetAttrString(tokens, "ArgumentClose");
  1417. WikilinkOpen = PyObject_GetAttrString(tokens, "WikilinkOpen");
  1418. WikilinkSeparator = PyObject_GetAttrString(tokens, "WikilinkSeparator");
  1419. WikilinkClose = PyObject_GetAttrString(tokens, "WikilinkClose");
  1420. HTMLEntityStart = PyObject_GetAttrString(tokens, "HTMLEntityStart");
  1421. HTMLEntityNumeric = PyObject_GetAttrString(tokens, "HTMLEntityNumeric");
  1422. HTMLEntityHex = PyObject_GetAttrString(tokens, "HTMLEntityHex");
  1423. HTMLEntityEnd = PyObject_GetAttrString(tokens, "HTMLEntityEnd");
  1424. HeadingStart = PyObject_GetAttrString(tokens, "HeadingStart");
  1425. HeadingEnd = PyObject_GetAttrString(tokens, "HeadingEnd");
  1426. CommentStart = PyObject_GetAttrString(tokens, "CommentStart");
  1427. CommentEnd = PyObject_GetAttrString(tokens, "CommentEnd");
  1428. TagOpenOpen = PyObject_GetAttrString(tokens, "TagOpenOpen");
  1429. TagAttrStart = PyObject_GetAttrString(tokens, "TagAttrStart");
  1430. TagAttrEquals = PyObject_GetAttrString(tokens, "TagAttrEquals");
  1431. TagAttrQuote = PyObject_GetAttrString(tokens, "TagAttrQuote");
  1432. TagCloseOpen = PyObject_GetAttrString(tokens, "TagCloseOpen");
  1433. TagCloseSelfclose = PyObject_GetAttrString(tokens, "TagCloseSelfclose");
  1434. TagOpenClose = PyObject_GetAttrString(tokens, "TagOpenClose");
  1435. TagCloseClose = PyObject_GetAttrString(tokens, "TagCloseClose");
  1436. }