A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

2899 líneas
82 KiB

  1. /*
  2. Tokenizer for MWParserFromHell
  3. Copyright (C) 2012-2014 Ben Kurtovic <ben.kurtovic@gmail.com>
  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. Determine whether the given Py_UNICODE is a marker.
  23. */
  24. static int is_marker(Py_UNICODE this)
  25. {
  26. int i;
  27. for (i = 0; i < NUM_MARKERS; i++) {
  28. if (MARKERS[i] == this)
  29. return 1;
  30. }
  31. return 0;
  32. }
  33. /*
  34. Given a context, return the heading level encoded within it.
  35. */
  36. static int heading_level_from_context(int n)
  37. {
  38. int level;
  39. n /= LC_HEADING_LEVEL_1;
  40. for (level = 1; n > 1; n >>= 1)
  41. level++;
  42. return level;
  43. }
  44. /*
  45. Call the given function in definitions.py, using 'in1', 'in2', and 'in3' as
  46. parameters, and return its output as a bool.
  47. */
  48. static int call_def_func(const char* funcname, PyObject* in1, PyObject* in2,
  49. PyObject* in3)
  50. {
  51. PyObject* func = PyObject_GetAttrString(definitions, funcname);
  52. PyObject* result = PyObject_CallFunctionObjArgs(func, in1, in2, in3, NULL);
  53. int ans = (result == Py_True) ? 1 : 0;
  54. Py_DECREF(func);
  55. Py_DECREF(result);
  56. return ans;
  57. }
  58. /*
  59. Sanitize the name of a tag so it can be compared with others for equality.
  60. */
  61. static PyObject* strip_tag_name(PyObject* token)
  62. {
  63. PyObject *text, *rstripped, *lowered;
  64. text = PyObject_GetAttrString(token, "text");
  65. if (!text)
  66. return NULL;
  67. rstripped = PyObject_CallMethod(text, "rstrip", NULL);
  68. Py_DECREF(text);
  69. if (!rstripped)
  70. return NULL;
  71. lowered = PyObject_CallMethod(rstripped, "lower", NULL);
  72. Py_DECREF(rstripped);
  73. return lowered;
  74. }
  75. static Textbuffer* Textbuffer_new(void)
  76. {
  77. Textbuffer* buffer = malloc(sizeof(Textbuffer));
  78. if (!buffer) {
  79. PyErr_NoMemory();
  80. return NULL;
  81. }
  82. buffer->size = 0;
  83. buffer->data = malloc(sizeof(Py_UNICODE) * TEXTBUFFER_BLOCKSIZE);
  84. if (!buffer->data) {
  85. free(buffer);
  86. PyErr_NoMemory();
  87. return NULL;
  88. }
  89. buffer->prev = buffer->next = NULL;
  90. return buffer;
  91. }
  92. static void Textbuffer_dealloc(Textbuffer* self)
  93. {
  94. Textbuffer* next;
  95. while (self) {
  96. free(self->data);
  97. next = self->next;
  98. free(self);
  99. self = next;
  100. }
  101. }
  102. /*
  103. Write a Unicode codepoint to the given textbuffer.
  104. */
  105. static int Textbuffer_write(Textbuffer** this, Py_UNICODE code)
  106. {
  107. Textbuffer* self = *this;
  108. if (self->size == TEXTBUFFER_BLOCKSIZE) {
  109. Textbuffer* new = Textbuffer_new();
  110. if (!new)
  111. return -1;
  112. new->next = self;
  113. self->prev = new;
  114. *this = self = new;
  115. }
  116. self->data[self->size++] = code;
  117. return 0;
  118. }
  119. /*
  120. Return the contents of the textbuffer as a Python Unicode object.
  121. */
  122. static PyObject* Textbuffer_render(Textbuffer* self)
  123. {
  124. PyObject *result = PyUnicode_FromUnicode(self->data, self->size);
  125. PyObject *left, *concat;
  126. while (self->next) {
  127. self = self->next;
  128. left = PyUnicode_FromUnicode(self->data, self->size);
  129. concat = PyUnicode_Concat(left, result);
  130. Py_DECREF(left);
  131. Py_DECREF(result);
  132. result = concat;
  133. }
  134. return result;
  135. }
  136. static TagData* TagData_new(void)
  137. {
  138. TagData *self = malloc(sizeof(TagData));
  139. #define ALLOC_BUFFER(name) \
  140. name = Textbuffer_new(); \
  141. if (!name) { \
  142. TagData_dealloc(self); \
  143. return NULL; \
  144. }
  145. if (!self) {
  146. PyErr_NoMemory();
  147. return NULL;
  148. }
  149. self->context = TAG_NAME;
  150. ALLOC_BUFFER(self->pad_first)
  151. ALLOC_BUFFER(self->pad_before_eq)
  152. ALLOC_BUFFER(self->pad_after_eq)
  153. self->reset = 0;
  154. return self;
  155. }
  156. static void TagData_dealloc(TagData* self)
  157. {
  158. #define DEALLOC_BUFFER(name) \
  159. if (name) \
  160. Textbuffer_dealloc(name);
  161. DEALLOC_BUFFER(self->pad_first);
  162. DEALLOC_BUFFER(self->pad_before_eq);
  163. DEALLOC_BUFFER(self->pad_after_eq);
  164. free(self);
  165. }
  166. static int TagData_reset_buffers(TagData* self)
  167. {
  168. #define RESET_BUFFER(name) \
  169. Textbuffer_dealloc(name); \
  170. name = Textbuffer_new(); \
  171. if (!name) \
  172. return -1;
  173. RESET_BUFFER(self->pad_first)
  174. RESET_BUFFER(self->pad_before_eq)
  175. RESET_BUFFER(self->pad_after_eq)
  176. return 0;
  177. }
  178. static PyObject*
  179. Tokenizer_new(PyTypeObject* type, PyObject* args, PyObject* kwds)
  180. {
  181. Tokenizer* self = (Tokenizer*) type->tp_alloc(type, 0);
  182. return (PyObject*) self;
  183. }
  184. static void Tokenizer_dealloc(Tokenizer* self)
  185. {
  186. Stack *this = self->topstack, *next;
  187. Py_XDECREF(self->text);
  188. while (this) {
  189. Py_DECREF(this->stack);
  190. Textbuffer_dealloc(this->textbuffer);
  191. next = this->next;
  192. free(this);
  193. this = next;
  194. }
  195. Py_TYPE(self)->tp_free((PyObject*) self);
  196. }
  197. static int Tokenizer_init(Tokenizer* self, PyObject* args, PyObject* kwds)
  198. {
  199. static char* kwlist[] = {NULL};
  200. if (!PyArg_ParseTupleAndKeywords(args, kwds, "", kwlist))
  201. return -1;
  202. self->text = Py_None;
  203. Py_INCREF(Py_None);
  204. self->topstack = NULL;
  205. self->head = self->length = self->global = self->depth = self->cycles = 0;
  206. return 0;
  207. }
  208. /*
  209. Add a new token stack, context, and textbuffer to the list.
  210. */
  211. static int Tokenizer_push(Tokenizer* self, int context)
  212. {
  213. Stack* top = malloc(sizeof(Stack));
  214. if (!top) {
  215. PyErr_NoMemory();
  216. return -1;
  217. }
  218. top->stack = PyList_New(0);
  219. top->context = context;
  220. top->textbuffer = Textbuffer_new();
  221. if (!top->textbuffer)
  222. return -1;
  223. top->next = self->topstack;
  224. self->topstack = top;
  225. self->depth++;
  226. self->cycles++;
  227. return 0;
  228. }
  229. /*
  230. Push the textbuffer onto the stack as a Text node and clear it.
  231. */
  232. static int Tokenizer_push_textbuffer(Tokenizer* self)
  233. {
  234. PyObject *text, *kwargs, *token;
  235. Textbuffer* buffer = self->topstack->textbuffer;
  236. if (buffer->size == 0 && !buffer->next)
  237. return 0;
  238. text = Textbuffer_render(buffer);
  239. if (!text)
  240. return -1;
  241. kwargs = PyDict_New();
  242. if (!kwargs) {
  243. Py_DECREF(text);
  244. return -1;
  245. }
  246. PyDict_SetItemString(kwargs, "text", text);
  247. Py_DECREF(text);
  248. token = PyObject_Call(Text, NOARGS, kwargs);
  249. Py_DECREF(kwargs);
  250. if (!token)
  251. return -1;
  252. if (PyList_Append(self->topstack->stack, token)) {
  253. Py_DECREF(token);
  254. return -1;
  255. }
  256. Py_DECREF(token);
  257. Textbuffer_dealloc(buffer);
  258. self->topstack->textbuffer = Textbuffer_new();
  259. if (!self->topstack->textbuffer)
  260. return -1;
  261. return 0;
  262. }
  263. /*
  264. Pop and deallocate the top token stack/context/textbuffer.
  265. */
  266. static void Tokenizer_delete_top_of_stack(Tokenizer* self)
  267. {
  268. Stack* top = self->topstack;
  269. Py_DECREF(top->stack);
  270. Textbuffer_dealloc(top->textbuffer);
  271. self->topstack = top->next;
  272. free(top);
  273. self->depth--;
  274. }
  275. /*
  276. Pop the current stack/context/textbuffer, returing the stack.
  277. */
  278. static PyObject* Tokenizer_pop(Tokenizer* self)
  279. {
  280. PyObject* stack;
  281. if (Tokenizer_push_textbuffer(self))
  282. return NULL;
  283. stack = self->topstack->stack;
  284. Py_INCREF(stack);
  285. Tokenizer_delete_top_of_stack(self);
  286. return stack;
  287. }
  288. /*
  289. Pop the current stack/context/textbuffer, returing the stack. We will also
  290. replace the underlying stack's context with the current stack's.
  291. */
  292. static PyObject* Tokenizer_pop_keeping_context(Tokenizer* self)
  293. {
  294. PyObject* stack;
  295. int context;
  296. if (Tokenizer_push_textbuffer(self))
  297. return NULL;
  298. stack = self->topstack->stack;
  299. Py_INCREF(stack);
  300. context = self->topstack->context;
  301. Tokenizer_delete_top_of_stack(self);
  302. self->topstack->context = context;
  303. return stack;
  304. }
  305. /*
  306. Fail the current tokenization route. Discards the current
  307. stack/context/textbuffer and sets the BAD_ROUTE flag.
  308. */
  309. static void* Tokenizer_fail_route(Tokenizer* self)
  310. {
  311. int context = self->topstack->context;
  312. PyObject* stack = Tokenizer_pop(self);
  313. Py_XDECREF(stack);
  314. FAIL_ROUTE(context);
  315. return NULL;
  316. }
  317. /*
  318. Write a token to the current token stack.
  319. */
  320. static int Tokenizer_emit_token(Tokenizer* self, PyObject* token, int first)
  321. {
  322. PyObject* instance;
  323. if (Tokenizer_push_textbuffer(self))
  324. return -1;
  325. instance = PyObject_CallObject(token, NULL);
  326. if (!instance)
  327. return -1;
  328. if (first ? PyList_Insert(self->topstack->stack, 0, instance) :
  329. PyList_Append(self->topstack->stack, instance)) {
  330. Py_DECREF(instance);
  331. return -1;
  332. }
  333. Py_DECREF(instance);
  334. return 0;
  335. }
  336. /*
  337. Write a token to the current token stack, with kwargs. Steals a reference
  338. to kwargs.
  339. */
  340. static int Tokenizer_emit_token_kwargs(Tokenizer* self, PyObject* token,
  341. PyObject* kwargs, int first)
  342. {
  343. PyObject* instance;
  344. if (Tokenizer_push_textbuffer(self)) {
  345. Py_DECREF(kwargs);
  346. return -1;
  347. }
  348. instance = PyObject_Call(token, NOARGS, kwargs);
  349. if (!instance) {
  350. Py_DECREF(kwargs);
  351. return -1;
  352. }
  353. if (first ? PyList_Insert(self->topstack->stack, 0, instance):
  354. PyList_Append(self->topstack->stack, instance)) {
  355. Py_DECREF(instance);
  356. Py_DECREF(kwargs);
  357. return -1;
  358. }
  359. Py_DECREF(instance);
  360. Py_DECREF(kwargs);
  361. return 0;
  362. }
  363. /*
  364. Write a Unicode codepoint to the current textbuffer.
  365. */
  366. static int Tokenizer_emit_char(Tokenizer* self, Py_UNICODE code)
  367. {
  368. return Textbuffer_write(&(self->topstack->textbuffer), code);
  369. }
  370. /*
  371. Write a string of text to the current textbuffer.
  372. */
  373. static int Tokenizer_emit_text(Tokenizer* self, const char* text)
  374. {
  375. int i = 0;
  376. while (text[i]) {
  377. if (Tokenizer_emit_char(self, text[i]))
  378. return -1;
  379. i++;
  380. }
  381. return 0;
  382. }
  383. /*
  384. Write the contents of another textbuffer to the current textbuffer,
  385. deallocating it in the process.
  386. */
  387. static int
  388. Tokenizer_emit_textbuffer(Tokenizer* self, Textbuffer* buffer, int reverse)
  389. {
  390. Textbuffer *original = buffer;
  391. long i;
  392. if (reverse) {
  393. do {
  394. for (i = buffer->size - 1; i >= 0; i--) {
  395. if (Tokenizer_emit_char(self, buffer->data[i])) {
  396. Textbuffer_dealloc(original);
  397. return -1;
  398. }
  399. }
  400. } while ((buffer = buffer->next));
  401. }
  402. else {
  403. while (buffer->next)
  404. buffer = buffer->next;
  405. do {
  406. for (i = 0; i < buffer->size; i++) {
  407. if (Tokenizer_emit_char(self, buffer->data[i])) {
  408. Textbuffer_dealloc(original);
  409. return -1;
  410. }
  411. }
  412. } while ((buffer = buffer->prev));
  413. }
  414. Textbuffer_dealloc(original);
  415. return 0;
  416. }
  417. /*
  418. Write a series of tokens to the current stack at once.
  419. */
  420. static int Tokenizer_emit_all(Tokenizer* self, PyObject* tokenlist)
  421. {
  422. int pushed = 0;
  423. PyObject *stack, *token, *left, *right, *text;
  424. Textbuffer* buffer;
  425. Py_ssize_t size;
  426. if (PyList_GET_SIZE(tokenlist) > 0) {
  427. token = PyList_GET_ITEM(tokenlist, 0);
  428. switch (PyObject_IsInstance(token, Text)) {
  429. case 0:
  430. break;
  431. case 1: {
  432. pushed = 1;
  433. buffer = self->topstack->textbuffer;
  434. if (buffer->size == 0 && !buffer->next)
  435. break;
  436. left = Textbuffer_render(buffer);
  437. if (!left)
  438. return -1;
  439. right = PyObject_GetAttrString(token, "text");
  440. if (!right)
  441. return -1;
  442. text = PyUnicode_Concat(left, right);
  443. Py_DECREF(left);
  444. Py_DECREF(right);
  445. if (!text)
  446. return -1;
  447. if (PyObject_SetAttrString(token, "text", text)) {
  448. Py_DECREF(text);
  449. return -1;
  450. }
  451. Py_DECREF(text);
  452. Textbuffer_dealloc(buffer);
  453. self->topstack->textbuffer = Textbuffer_new();
  454. if (!self->topstack->textbuffer)
  455. return -1;
  456. break;
  457. }
  458. case -1:
  459. return -1;
  460. }
  461. }
  462. if (!pushed) {
  463. if (Tokenizer_push_textbuffer(self))
  464. return -1;
  465. }
  466. stack = self->topstack->stack;
  467. size = PyList_GET_SIZE(stack);
  468. if (PyList_SetSlice(stack, size, size, tokenlist))
  469. return -1;
  470. return 0;
  471. }
  472. /*
  473. Pop the current stack, write text, and then write the stack. 'text' is a
  474. NULL-terminated array of chars.
  475. */
  476. static int Tokenizer_emit_text_then_stack(Tokenizer* self, const char* text)
  477. {
  478. PyObject* stack = Tokenizer_pop(self);
  479. if (Tokenizer_emit_text(self, text)) {
  480. Py_DECREF(stack);
  481. return -1;
  482. }
  483. if (stack) {
  484. if (PyList_GET_SIZE(stack) > 0) {
  485. if (Tokenizer_emit_all(self, stack)) {
  486. Py_DECREF(stack);
  487. return -1;
  488. }
  489. }
  490. Py_DECREF(stack);
  491. }
  492. self->head--;
  493. return 0;
  494. }
  495. /*
  496. Read the value at a relative point in the wikicode, forwards.
  497. */
  498. static PyObject* Tokenizer_read(Tokenizer* self, Py_ssize_t delta)
  499. {
  500. Py_ssize_t index = self->head + delta;
  501. if (index >= self->length)
  502. return EMPTY;
  503. return PyList_GET_ITEM(self->text, index);
  504. }
  505. /*
  506. Read the value at a relative point in the wikicode, backwards.
  507. */
  508. static PyObject* Tokenizer_read_backwards(Tokenizer* self, Py_ssize_t delta)
  509. {
  510. Py_ssize_t index;
  511. if (delta > self->head)
  512. return EMPTY;
  513. index = self->head - delta;
  514. return PyList_GET_ITEM(self->text, index);
  515. }
  516. /*
  517. Parse a template at the head of the wikicode string.
  518. */
  519. static int Tokenizer_parse_template(Tokenizer* self)
  520. {
  521. PyObject *template;
  522. Py_ssize_t reset = self->head;
  523. template = Tokenizer_parse(self, LC_TEMPLATE_NAME, 1);
  524. if (BAD_ROUTE) {
  525. self->head = reset;
  526. return 0;
  527. }
  528. if (!template)
  529. return -1;
  530. if (Tokenizer_emit_first(self, TemplateOpen)) {
  531. Py_DECREF(template);
  532. return -1;
  533. }
  534. if (Tokenizer_emit_all(self, template)) {
  535. Py_DECREF(template);
  536. return -1;
  537. }
  538. Py_DECREF(template);
  539. if (Tokenizer_emit(self, TemplateClose))
  540. return -1;
  541. return 0;
  542. }
  543. /*
  544. Parse an argument at the head of the wikicode string.
  545. */
  546. static int Tokenizer_parse_argument(Tokenizer* self)
  547. {
  548. PyObject *argument;
  549. Py_ssize_t reset = self->head;
  550. argument = Tokenizer_parse(self, LC_ARGUMENT_NAME, 1);
  551. if (BAD_ROUTE) {
  552. self->head = reset;
  553. return 0;
  554. }
  555. if (!argument)
  556. return -1;
  557. if (Tokenizer_emit_first(self, ArgumentOpen)) {
  558. Py_DECREF(argument);
  559. return -1;
  560. }
  561. if (Tokenizer_emit_all(self, argument)) {
  562. Py_DECREF(argument);
  563. return -1;
  564. }
  565. Py_DECREF(argument);
  566. if (Tokenizer_emit(self, ArgumentClose))
  567. return -1;
  568. return 0;
  569. }
  570. /*
  571. Parse a template or argument at the head of the wikicode string.
  572. */
  573. static int Tokenizer_parse_template_or_argument(Tokenizer* self)
  574. {
  575. unsigned int braces = 2, i;
  576. PyObject *tokenlist;
  577. self->head += 2;
  578. while (Tokenizer_READ(self, 0) == '{' && braces < MAX_BRACES) {
  579. self->head++;
  580. braces++;
  581. }
  582. if (Tokenizer_push(self, 0))
  583. return -1;
  584. while (braces) {
  585. if (braces == 1) {
  586. if (Tokenizer_emit_text_then_stack(self, "{"))
  587. return -1;
  588. return 0;
  589. }
  590. if (braces == 2) {
  591. if (Tokenizer_parse_template(self))
  592. return -1;
  593. if (BAD_ROUTE) {
  594. RESET_ROUTE();
  595. if (Tokenizer_emit_text_then_stack(self, "{{"))
  596. return -1;
  597. return 0;
  598. }
  599. break;
  600. }
  601. if (Tokenizer_parse_argument(self))
  602. return -1;
  603. if (BAD_ROUTE) {
  604. RESET_ROUTE();
  605. if (Tokenizer_parse_template(self))
  606. return -1;
  607. if (BAD_ROUTE) {
  608. char text[MAX_BRACES + 1];
  609. RESET_ROUTE();
  610. for (i = 0; i < braces; i++) text[i] = '{';
  611. text[braces] = '\0';
  612. if (Tokenizer_emit_text_then_stack(self, text)) {
  613. Py_XDECREF(text);
  614. return -1;
  615. }
  616. Py_XDECREF(text);
  617. return 0;
  618. }
  619. else
  620. braces -= 2;
  621. }
  622. else
  623. braces -= 3;
  624. if (braces)
  625. self->head++;
  626. }
  627. tokenlist = Tokenizer_pop(self);
  628. if (!tokenlist)
  629. return -1;
  630. if (Tokenizer_emit_all(self, tokenlist)) {
  631. Py_DECREF(tokenlist);
  632. return -1;
  633. }
  634. Py_DECREF(tokenlist);
  635. if (self->topstack->context & LC_FAIL_NEXT)
  636. self->topstack->context ^= LC_FAIL_NEXT;
  637. return 0;
  638. }
  639. /*
  640. Handle a template parameter at the head of the string.
  641. */
  642. static int Tokenizer_handle_template_param(Tokenizer* self)
  643. {
  644. PyObject *stack;
  645. if (self->topstack->context & LC_TEMPLATE_NAME)
  646. self->topstack->context ^= LC_TEMPLATE_NAME;
  647. else if (self->topstack->context & LC_TEMPLATE_PARAM_VALUE)
  648. self->topstack->context ^= LC_TEMPLATE_PARAM_VALUE;
  649. if (self->topstack->context & LC_TEMPLATE_PARAM_KEY) {
  650. stack = Tokenizer_pop_keeping_context(self);
  651. if (!stack)
  652. return -1;
  653. if (Tokenizer_emit_all(self, stack)) {
  654. Py_DECREF(stack);
  655. return -1;
  656. }
  657. Py_DECREF(stack);
  658. }
  659. else
  660. self->topstack->context |= LC_TEMPLATE_PARAM_KEY;
  661. if (Tokenizer_emit(self, TemplateParamSeparator))
  662. return -1;
  663. if (Tokenizer_push(self, self->topstack->context))
  664. return -1;
  665. return 0;
  666. }
  667. /*
  668. Handle a template parameter's value at the head of the string.
  669. */
  670. static int Tokenizer_handle_template_param_value(Tokenizer* self)
  671. {
  672. PyObject *stack;
  673. stack = Tokenizer_pop_keeping_context(self);
  674. if (!stack)
  675. return -1;
  676. if (Tokenizer_emit_all(self, stack)) {
  677. Py_DECREF(stack);
  678. return -1;
  679. }
  680. Py_DECREF(stack);
  681. self->topstack->context ^= LC_TEMPLATE_PARAM_KEY;
  682. self->topstack->context |= LC_TEMPLATE_PARAM_VALUE;
  683. if (Tokenizer_emit(self, TemplateParamEquals))
  684. return -1;
  685. return 0;
  686. }
  687. /*
  688. Handle the end of a template at the head of the string.
  689. */
  690. static PyObject* Tokenizer_handle_template_end(Tokenizer* self)
  691. {
  692. PyObject* stack;
  693. if (self->topstack->context & LC_TEMPLATE_PARAM_KEY) {
  694. stack = Tokenizer_pop_keeping_context(self);
  695. if (!stack)
  696. return NULL;
  697. if (Tokenizer_emit_all(self, stack)) {
  698. Py_DECREF(stack);
  699. return NULL;
  700. }
  701. Py_DECREF(stack);
  702. }
  703. self->head++;
  704. stack = Tokenizer_pop(self);
  705. return stack;
  706. }
  707. /*
  708. Handle the separator between an argument's name and default.
  709. */
  710. static int Tokenizer_handle_argument_separator(Tokenizer* self)
  711. {
  712. self->topstack->context ^= LC_ARGUMENT_NAME;
  713. self->topstack->context |= LC_ARGUMENT_DEFAULT;
  714. if (Tokenizer_emit(self, ArgumentSeparator))
  715. return -1;
  716. return 0;
  717. }
  718. /*
  719. Handle the end of an argument at the head of the string.
  720. */
  721. static PyObject* Tokenizer_handle_argument_end(Tokenizer* self)
  722. {
  723. PyObject* stack = Tokenizer_pop(self);
  724. self->head += 2;
  725. return stack;
  726. }
  727. /*
  728. Parse an internal wikilink at the head of the wikicode string.
  729. */
  730. static int Tokenizer_parse_wikilink(Tokenizer* self)
  731. {
  732. Py_ssize_t reset;
  733. PyObject *wikilink;
  734. self->head += 2;
  735. reset = self->head - 1;
  736. wikilink = Tokenizer_parse(self, LC_WIKILINK_TITLE, 1);
  737. if (BAD_ROUTE) {
  738. RESET_ROUTE();
  739. self->head = reset;
  740. if (Tokenizer_emit_text(self, "[["))
  741. return -1;
  742. return 0;
  743. }
  744. if (!wikilink)
  745. return -1;
  746. if (Tokenizer_emit(self, WikilinkOpen)) {
  747. Py_DECREF(wikilink);
  748. return -1;
  749. }
  750. if (Tokenizer_emit_all(self, wikilink)) {
  751. Py_DECREF(wikilink);
  752. return -1;
  753. }
  754. Py_DECREF(wikilink);
  755. if (Tokenizer_emit(self, WikilinkClose))
  756. return -1;
  757. if (self->topstack->context & LC_FAIL_NEXT)
  758. self->topstack->context ^= LC_FAIL_NEXT;
  759. return 0;
  760. }
  761. /*
  762. Handle the separator between a wikilink's title and its text.
  763. */
  764. static int Tokenizer_handle_wikilink_separator(Tokenizer* self)
  765. {
  766. self->topstack->context ^= LC_WIKILINK_TITLE;
  767. self->topstack->context |= LC_WIKILINK_TEXT;
  768. if (Tokenizer_emit(self, WikilinkSeparator))
  769. return -1;
  770. return 0;
  771. }
  772. /*
  773. Handle the end of a wikilink at the head of the string.
  774. */
  775. static PyObject* Tokenizer_handle_wikilink_end(Tokenizer* self)
  776. {
  777. PyObject* stack = Tokenizer_pop(self);
  778. self->head += 1;
  779. return stack;
  780. }
  781. /*
  782. Parse the URI scheme of a bracket-enclosed external link.
  783. */
  784. static int Tokenizer_parse_bracketed_uri_scheme(Tokenizer* self)
  785. {
  786. static const char* valid = "abcdefghijklmnopqrstuvwxyz0123456789+.-";
  787. Textbuffer* buffer;
  788. PyObject* scheme;
  789. Py_UNICODE this;
  790. int slashes, i;
  791. if (Tokenizer_push(self, LC_EXT_LINK_URI))
  792. return -1;
  793. if (Tokenizer_READ(self, 0) == '/' && Tokenizer_READ(self, 1) == '/') {
  794. if (Tokenizer_emit_text(self, "//"))
  795. return -1;
  796. self->head += 2;
  797. }
  798. else {
  799. buffer = Textbuffer_new();
  800. if (!buffer)
  801. return -1;
  802. while ((this = Tokenizer_READ(self, 0))) {
  803. i = 0;
  804. while (1) {
  805. if (!valid[i])
  806. goto end_of_loop;
  807. if (this == valid[i])
  808. break;
  809. i++;
  810. }
  811. Textbuffer_write(&buffer, this);
  812. if (Tokenizer_emit_char(self, this)) {
  813. Textbuffer_dealloc(buffer);
  814. return -1;
  815. }
  816. self->head++;
  817. }
  818. end_of_loop:
  819. if (this != ':') {
  820. Textbuffer_dealloc(buffer);
  821. Tokenizer_fail_route(self);
  822. return 0;
  823. }
  824. if (Tokenizer_emit_char(self, ':')) {
  825. Textbuffer_dealloc(buffer);
  826. return -1;
  827. }
  828. self->head++;
  829. slashes = (Tokenizer_READ(self, 0) == '/' &&
  830. Tokenizer_READ(self, 1) == '/');
  831. if (slashes) {
  832. if (Tokenizer_emit_text(self, "//")) {
  833. Textbuffer_dealloc(buffer);
  834. return -1;
  835. }
  836. self->head += 2;
  837. }
  838. scheme = Textbuffer_render(buffer);
  839. Textbuffer_dealloc(buffer);
  840. if (!scheme)
  841. return -1;
  842. if (!IS_SCHEME(scheme, slashes, 0)) {
  843. Py_DECREF(scheme);
  844. Tokenizer_fail_route(self);
  845. return 0;
  846. }
  847. Py_DECREF(scheme);
  848. }
  849. return 0;
  850. }
  851. /*
  852. Parse the URI scheme of a free (no brackets) external link.
  853. */
  854. static int Tokenizer_parse_free_uri_scheme(Tokenizer* self)
  855. {
  856. static const char* valid = "abcdefghijklmnopqrstuvwxyz0123456789+.-";
  857. Textbuffer *scheme_buffer = Textbuffer_new(), *temp_buffer;
  858. PyObject *scheme;
  859. Py_UNICODE chunk;
  860. long i;
  861. int slashes, j;
  862. if (!scheme_buffer)
  863. return -1;
  864. // We have to backtrack through the textbuffer looking for our scheme since
  865. // it was just parsed as text:
  866. temp_buffer = self->topstack->textbuffer;
  867. while (temp_buffer) {
  868. for (i = temp_buffer->size - 1; i >= 0; i--) {
  869. chunk = temp_buffer->data[i];
  870. if (Py_UNICODE_ISSPACE(chunk) || is_marker(chunk))
  871. goto end_of_loop;
  872. j = 0;
  873. while (1) {
  874. if (!valid[j]) {
  875. Textbuffer_dealloc(scheme_buffer);
  876. FAIL_ROUTE(0);
  877. return 0;
  878. }
  879. if (chunk == valid[j])
  880. break;
  881. j++;
  882. }
  883. Textbuffer_write(&scheme_buffer, chunk);
  884. }
  885. temp_buffer = temp_buffer->next;
  886. }
  887. end_of_loop:
  888. scheme = Textbuffer_render(scheme_buffer);
  889. if (!scheme) {
  890. Textbuffer_dealloc(scheme_buffer);
  891. return -1;
  892. }
  893. slashes = (Tokenizer_READ(self, 0) == '/' &&
  894. Tokenizer_READ(self, 1) == '/');
  895. if (!IS_SCHEME(scheme, slashes, 1)) {
  896. Py_DECREF(scheme);
  897. Textbuffer_dealloc(scheme_buffer);
  898. FAIL_ROUTE(0);
  899. return 0;
  900. }
  901. Py_DECREF(scheme);
  902. if (Tokenizer_push(self, self->topstack->context | LC_EXT_LINK_URI)) {
  903. Textbuffer_dealloc(scheme_buffer);
  904. return -1;
  905. }
  906. if (Tokenizer_emit_textbuffer(self, scheme_buffer, 1))
  907. return -1;
  908. if (Tokenizer_emit_char(self, ':'))
  909. return -1;
  910. if (slashes) {
  911. if (Tokenizer_emit_text(self, "//"))
  912. return -1;
  913. self->head += 2;
  914. }
  915. return 0;
  916. }
  917. /*
  918. Handle text in a free external link, including trailing punctuation.
  919. */
  920. static int
  921. Tokenizer_handle_free_link_text(Tokenizer* self, int* parens,
  922. Textbuffer** tail, Py_UNICODE this)
  923. {
  924. #define PUSH_TAIL_BUFFER(tail, error) \
  925. if ((tail)->size || (tail)->next) { \
  926. if (Tokenizer_emit_textbuffer(self, tail, 0)) \
  927. return error; \
  928. tail = Textbuffer_new(); \
  929. if (!(tail)) \
  930. return error; \
  931. }
  932. if (this == '(' && !(*parens)) {
  933. *parens = 1;
  934. PUSH_TAIL_BUFFER(*tail, -1)
  935. }
  936. else if (this == ',' || this == ';' || this == '\\' || this == '.' ||
  937. this == ':' || this == '!' || this == '?' ||
  938. (!(*parens) && this == ')'))
  939. return Textbuffer_write(tail, this);
  940. else
  941. PUSH_TAIL_BUFFER(*tail, -1)
  942. return Tokenizer_emit_char(self, this);
  943. }
  944. /*
  945. Return whether the current head is the end of a free link.
  946. */
  947. static int
  948. Tokenizer_is_free_link(Tokenizer* self, Py_UNICODE this, Py_UNICODE next)
  949. {
  950. // Built from Tokenizer_parse()'s end sentinels:
  951. Py_UNICODE after = Tokenizer_READ(self, 2);
  952. int ctx = self->topstack->context;
  953. return (!this || this == '\n' || this == '[' || this == ']' ||
  954. this == '<' || this == '>' || (this == '\'' && next == '\'') ||
  955. (this == '|' && ctx & LC_TEMPLATE) ||
  956. (this == '=' && ctx & (LC_TEMPLATE_PARAM_KEY | LC_HEADING)) ||
  957. (this == '}' && next == '}' &&
  958. (ctx & LC_TEMPLATE || (after == '}' && ctx & LC_ARGUMENT))));
  959. }
  960. /*
  961. Really parse an external link.
  962. */
  963. static PyObject*
  964. Tokenizer_really_parse_external_link(Tokenizer* self, int brackets,
  965. Textbuffer** extra)
  966. {
  967. Py_UNICODE this, next;
  968. int parens = 0;
  969. if (brackets ? Tokenizer_parse_bracketed_uri_scheme(self) :
  970. Tokenizer_parse_free_uri_scheme(self))
  971. return NULL;
  972. if (BAD_ROUTE)
  973. return NULL;
  974. this = Tokenizer_READ(self, 0);
  975. if (!this || this == '\n' || this == ' ' || this == ']')
  976. return Tokenizer_fail_route(self);
  977. if (!brackets && this == '[')
  978. return Tokenizer_fail_route(self);
  979. while (1) {
  980. this = Tokenizer_READ(self, 0);
  981. next = Tokenizer_READ(self, 1);
  982. if (this == '&') {
  983. PUSH_TAIL_BUFFER(*extra, NULL)
  984. if (Tokenizer_parse_entity(self))
  985. return NULL;
  986. }
  987. else if (this == '<' && next == '!'
  988. && Tokenizer_READ(self, 2) == '-'
  989. && Tokenizer_READ(self, 3) == '-') {
  990. PUSH_TAIL_BUFFER(*extra, NULL)
  991. if (Tokenizer_parse_comment(self))
  992. return NULL;
  993. }
  994. else if (!brackets && Tokenizer_is_free_link(self, this, next)) {
  995. self->head--;
  996. return Tokenizer_pop(self);
  997. }
  998. else if (!this || this == '\n')
  999. return Tokenizer_fail_route(self);
  1000. else if (this == '{' && next == '{' && Tokenizer_CAN_RECURSE(self)) {
  1001. PUSH_TAIL_BUFFER(*extra, NULL)
  1002. if (Tokenizer_parse_template_or_argument(self))
  1003. return NULL;
  1004. }
  1005. else if (this == ']')
  1006. return Tokenizer_pop(self);
  1007. else if (this == ' ') {
  1008. if (brackets) {
  1009. if (Tokenizer_emit(self, ExternalLinkSeparator))
  1010. return NULL;
  1011. self->topstack->context ^= LC_EXT_LINK_URI;
  1012. self->topstack->context |= LC_EXT_LINK_TITLE;
  1013. self->head++;
  1014. return Tokenizer_parse(self, 0, 0);
  1015. }
  1016. if (Textbuffer_write(extra, ' '))
  1017. return NULL;
  1018. return Tokenizer_pop(self);
  1019. }
  1020. else if (!brackets) {
  1021. if (Tokenizer_handle_free_link_text(self, &parens, extra, this))
  1022. return NULL;
  1023. }
  1024. else {
  1025. if (Tokenizer_emit_char(self, this))
  1026. return NULL;
  1027. }
  1028. self->head++;
  1029. }
  1030. }
  1031. /*
  1032. Remove the URI scheme of a new external link from the textbuffer.
  1033. */
  1034. static int
  1035. Tokenizer_remove_uri_scheme_from_textbuffer(Tokenizer* self, PyObject* link)
  1036. {
  1037. PyObject *text = PyObject_GetAttrString(PyList_GET_ITEM(link, 0), "text"),
  1038. *split, *scheme;
  1039. Py_ssize_t length;
  1040. Textbuffer* temp;
  1041. if (!text)
  1042. return -1;
  1043. split = PyObject_CallMethod(text, "split", "si", ":", 1);
  1044. Py_DECREF(text);
  1045. if (!split)
  1046. return -1;
  1047. scheme = PyList_GET_ITEM(split, 0);
  1048. length = PyUnicode_GET_SIZE(scheme);
  1049. while (length) {
  1050. temp = self->topstack->textbuffer;
  1051. if (length <= temp->size) {
  1052. temp->size -= length;
  1053. break;
  1054. }
  1055. length -= temp->size;
  1056. self->topstack->textbuffer = temp->next;
  1057. free(temp->data);
  1058. free(temp);
  1059. }
  1060. Py_DECREF(split);
  1061. return 0;
  1062. }
  1063. /*
  1064. Parse an external link at the head of the wikicode string.
  1065. */
  1066. static int Tokenizer_parse_external_link(Tokenizer* self, int brackets)
  1067. {
  1068. #define INVALID_CONTEXT self->topstack->context & AGG_NO_EXT_LINKS
  1069. #define NOT_A_LINK \
  1070. if (!brackets && self->topstack->context & LC_DLTERM) \
  1071. return Tokenizer_handle_dl_term(self); \
  1072. return Tokenizer_emit_char(self, Tokenizer_READ(self, 0))
  1073. Py_ssize_t reset = self->head;
  1074. PyObject *link, *kwargs;
  1075. Textbuffer *extra = 0;
  1076. if (INVALID_CONTEXT || !(Tokenizer_CAN_RECURSE(self))) {
  1077. NOT_A_LINK;
  1078. }
  1079. extra = Textbuffer_new();
  1080. if (!extra)
  1081. return -1;
  1082. self->head++;
  1083. link = Tokenizer_really_parse_external_link(self, brackets, &extra);
  1084. if (BAD_ROUTE) {
  1085. RESET_ROUTE();
  1086. self->head = reset;
  1087. Textbuffer_dealloc(extra);
  1088. NOT_A_LINK;
  1089. }
  1090. if (!link) {
  1091. Textbuffer_dealloc(extra);
  1092. return -1;
  1093. }
  1094. if (!brackets) {
  1095. if (Tokenizer_remove_uri_scheme_from_textbuffer(self, link)) {
  1096. Textbuffer_dealloc(extra);
  1097. Py_DECREF(link);
  1098. return -1;
  1099. }
  1100. }
  1101. kwargs = PyDict_New();
  1102. if (!kwargs) {
  1103. Textbuffer_dealloc(extra);
  1104. Py_DECREF(link);
  1105. return -1;
  1106. }
  1107. PyDict_SetItemString(kwargs, "brackets", brackets ? Py_True : Py_False);
  1108. if (Tokenizer_emit_kwargs(self, ExternalLinkOpen, kwargs)) {
  1109. Textbuffer_dealloc(extra);
  1110. Py_DECREF(link);
  1111. return -1;
  1112. }
  1113. if (Tokenizer_emit_all(self, link)) {
  1114. Textbuffer_dealloc(extra);
  1115. Py_DECREF(link);
  1116. return -1;
  1117. }
  1118. Py_DECREF(link);
  1119. if (Tokenizer_emit(self, ExternalLinkClose)) {
  1120. Textbuffer_dealloc(extra);
  1121. return -1;
  1122. }
  1123. if (extra->size || extra->next)
  1124. return Tokenizer_emit_textbuffer(self, extra, 0);
  1125. Textbuffer_dealloc(extra);
  1126. return 0;
  1127. }
  1128. /*
  1129. Parse a section heading at the head of the wikicode string.
  1130. */
  1131. static int Tokenizer_parse_heading(Tokenizer* self)
  1132. {
  1133. Py_ssize_t reset = self->head;
  1134. int best = 1, i, context, diff;
  1135. HeadingData *heading;
  1136. PyObject *level, *kwargs;
  1137. self->global |= GL_HEADING;
  1138. self->head += 1;
  1139. while (Tokenizer_READ(self, 0) == '=') {
  1140. best++;
  1141. self->head++;
  1142. }
  1143. context = LC_HEADING_LEVEL_1 << (best > 5 ? 5 : best - 1);
  1144. heading = (HeadingData*) Tokenizer_parse(self, context, 1);
  1145. if (BAD_ROUTE) {
  1146. RESET_ROUTE();
  1147. self->head = reset + best - 1;
  1148. for (i = 0; i < best; i++) {
  1149. if (Tokenizer_emit_char(self, '='))
  1150. return -1;
  1151. }
  1152. self->global ^= GL_HEADING;
  1153. return 0;
  1154. }
  1155. level = NEW_INT_FUNC(heading->level);
  1156. if (!level) {
  1157. Py_DECREF(heading->title);
  1158. free(heading);
  1159. return -1;
  1160. }
  1161. kwargs = PyDict_New();
  1162. if (!kwargs) {
  1163. Py_DECREF(level);
  1164. Py_DECREF(heading->title);
  1165. free(heading);
  1166. return -1;
  1167. }
  1168. PyDict_SetItemString(kwargs, "level", level);
  1169. Py_DECREF(level);
  1170. if (Tokenizer_emit_kwargs(self, HeadingStart, kwargs)) {
  1171. Py_DECREF(heading->title);
  1172. free(heading);
  1173. return -1;
  1174. }
  1175. if (heading->level < best) {
  1176. diff = best - heading->level;
  1177. for (i = 0; i < diff; i++) {
  1178. if (Tokenizer_emit_char(self, '=')) {
  1179. Py_DECREF(heading->title);
  1180. free(heading);
  1181. return -1;
  1182. }
  1183. }
  1184. }
  1185. if (Tokenizer_emit_all(self, heading->title)) {
  1186. Py_DECREF(heading->title);
  1187. free(heading);
  1188. return -1;
  1189. }
  1190. Py_DECREF(heading->title);
  1191. free(heading);
  1192. if (Tokenizer_emit(self, HeadingEnd))
  1193. return -1;
  1194. self->global ^= GL_HEADING;
  1195. return 0;
  1196. }
  1197. /*
  1198. Handle the end of a section heading at the head of the string.
  1199. */
  1200. static HeadingData* Tokenizer_handle_heading_end(Tokenizer* self)
  1201. {
  1202. Py_ssize_t reset = self->head;
  1203. int best, i, current, level, diff;
  1204. HeadingData *after, *heading;
  1205. PyObject *stack;
  1206. self->head += 1;
  1207. best = 1;
  1208. while (Tokenizer_READ(self, 0) == '=') {
  1209. best++;
  1210. self->head++;
  1211. }
  1212. current = heading_level_from_context(self->topstack->context);
  1213. level = current > best ? (best > 6 ? 6 : best) :
  1214. (current > 6 ? 6 : current);
  1215. after = (HeadingData*) Tokenizer_parse(self, self->topstack->context, 1);
  1216. if (BAD_ROUTE) {
  1217. RESET_ROUTE();
  1218. if (level < best) {
  1219. diff = best - level;
  1220. for (i = 0; i < diff; i++) {
  1221. if (Tokenizer_emit_char(self, '='))
  1222. return NULL;
  1223. }
  1224. }
  1225. self->head = reset + best - 1;
  1226. }
  1227. else {
  1228. for (i = 0; i < best; i++) {
  1229. if (Tokenizer_emit_char(self, '=')) {
  1230. Py_DECREF(after->title);
  1231. free(after);
  1232. return NULL;
  1233. }
  1234. }
  1235. if (Tokenizer_emit_all(self, after->title)) {
  1236. Py_DECREF(after->title);
  1237. free(after);
  1238. return NULL;
  1239. }
  1240. Py_DECREF(after->title);
  1241. level = after->level;
  1242. free(after);
  1243. }
  1244. stack = Tokenizer_pop(self);
  1245. if (!stack)
  1246. return NULL;
  1247. heading = malloc(sizeof(HeadingData));
  1248. if (!heading) {
  1249. PyErr_NoMemory();
  1250. return NULL;
  1251. }
  1252. heading->title = stack;
  1253. heading->level = level;
  1254. return heading;
  1255. }
  1256. /*
  1257. Actually parse an HTML entity and ensure that it is valid.
  1258. */
  1259. static int Tokenizer_really_parse_entity(Tokenizer* self)
  1260. {
  1261. PyObject *kwargs, *textobj;
  1262. Py_UNICODE this;
  1263. int numeric, hexadecimal, i, j, zeroes, test;
  1264. char *valid, *text, *buffer, *def;
  1265. #define FAIL_ROUTE_AND_EXIT() { \
  1266. Tokenizer_fail_route(self); \
  1267. free(text); \
  1268. return 0; \
  1269. }
  1270. if (Tokenizer_emit(self, HTMLEntityStart))
  1271. return -1;
  1272. self->head++;
  1273. this = Tokenizer_READ(self, 0);
  1274. if (!this) {
  1275. Tokenizer_fail_route(self);
  1276. return 0;
  1277. }
  1278. if (this == '#') {
  1279. numeric = 1;
  1280. if (Tokenizer_emit(self, HTMLEntityNumeric))
  1281. return -1;
  1282. self->head++;
  1283. this = Tokenizer_READ(self, 0);
  1284. if (!this) {
  1285. Tokenizer_fail_route(self);
  1286. return 0;
  1287. }
  1288. if (this == 'x' || this == 'X') {
  1289. hexadecimal = 1;
  1290. kwargs = PyDict_New();
  1291. if (!kwargs)
  1292. return -1;
  1293. PyDict_SetItemString(kwargs, "char", Tokenizer_read(self, 0));
  1294. if (Tokenizer_emit_kwargs(self, HTMLEntityHex, kwargs))
  1295. return -1;
  1296. self->head++;
  1297. }
  1298. else
  1299. hexadecimal = 0;
  1300. }
  1301. else
  1302. numeric = hexadecimal = 0;
  1303. if (hexadecimal)
  1304. valid = HEXDIGITS;
  1305. else if (numeric)
  1306. valid = DIGITS;
  1307. else
  1308. valid = ALPHANUM;
  1309. text = calloc(MAX_ENTITY_SIZE, sizeof(char));
  1310. if (!text) {
  1311. PyErr_NoMemory();
  1312. return -1;
  1313. }
  1314. i = 0;
  1315. zeroes = 0;
  1316. while (1) {
  1317. this = Tokenizer_READ(self, 0);
  1318. if (this == ';') {
  1319. if (i == 0)
  1320. FAIL_ROUTE_AND_EXIT()
  1321. break;
  1322. }
  1323. if (i == 0 && this == '0') {
  1324. zeroes++;
  1325. self->head++;
  1326. continue;
  1327. }
  1328. if (i >= MAX_ENTITY_SIZE)
  1329. FAIL_ROUTE_AND_EXIT()
  1330. if (is_marker(this))
  1331. FAIL_ROUTE_AND_EXIT()
  1332. j = 0;
  1333. while (1) {
  1334. if (!valid[j])
  1335. FAIL_ROUTE_AND_EXIT()
  1336. if (this == valid[j])
  1337. break;
  1338. j++;
  1339. }
  1340. text[i] = (char) this;
  1341. self->head++;
  1342. i++;
  1343. }
  1344. if (numeric) {
  1345. sscanf(text, (hexadecimal ? "%x" : "%d"), &test);
  1346. if (test < 1 || test > 0x10FFFF)
  1347. FAIL_ROUTE_AND_EXIT()
  1348. }
  1349. else {
  1350. i = 0;
  1351. while (1) {
  1352. def = entitydefs[i];
  1353. if (!def) // We've reached the end of the defs without finding it
  1354. FAIL_ROUTE_AND_EXIT()
  1355. if (strcmp(text, def) == 0)
  1356. break;
  1357. i++;
  1358. }
  1359. }
  1360. if (zeroes) {
  1361. buffer = calloc(strlen(text) + zeroes + 1, sizeof(char));
  1362. if (!buffer) {
  1363. free(text);
  1364. PyErr_NoMemory();
  1365. return -1;
  1366. }
  1367. for (i = 0; i < zeroes; i++)
  1368. strcat(buffer, "0");
  1369. strcat(buffer, text);
  1370. free(text);
  1371. text = buffer;
  1372. }
  1373. textobj = PyUnicode_FromString(text);
  1374. if (!textobj) {
  1375. free(text);
  1376. return -1;
  1377. }
  1378. free(text);
  1379. kwargs = PyDict_New();
  1380. if (!kwargs) {
  1381. Py_DECREF(textobj);
  1382. return -1;
  1383. }
  1384. PyDict_SetItemString(kwargs, "text", textobj);
  1385. Py_DECREF(textobj);
  1386. if (Tokenizer_emit_kwargs(self, Text, kwargs))
  1387. return -1;
  1388. if (Tokenizer_emit(self, HTMLEntityEnd))
  1389. return -1;
  1390. return 0;
  1391. }
  1392. /*
  1393. Parse an HTML entity at the head of the wikicode string.
  1394. */
  1395. static int Tokenizer_parse_entity(Tokenizer* self)
  1396. {
  1397. Py_ssize_t reset = self->head;
  1398. PyObject *tokenlist;
  1399. if (Tokenizer_push(self, 0))
  1400. return -1;
  1401. if (Tokenizer_really_parse_entity(self))
  1402. return -1;
  1403. if (BAD_ROUTE) {
  1404. RESET_ROUTE();
  1405. self->head = reset;
  1406. if (Tokenizer_emit_char(self, '&'))
  1407. return -1;
  1408. return 0;
  1409. }
  1410. tokenlist = Tokenizer_pop(self);
  1411. if (!tokenlist)
  1412. return -1;
  1413. if (Tokenizer_emit_all(self, tokenlist)) {
  1414. Py_DECREF(tokenlist);
  1415. return -1;
  1416. }
  1417. Py_DECREF(tokenlist);
  1418. return 0;
  1419. }
  1420. /*
  1421. Parse an HTML comment at the head of the wikicode string.
  1422. */
  1423. static int Tokenizer_parse_comment(Tokenizer* self)
  1424. {
  1425. Py_ssize_t reset = self->head + 3;
  1426. PyObject *comment;
  1427. Py_UNICODE this;
  1428. self->head += 4;
  1429. if (Tokenizer_push(self, 0))
  1430. return -1;
  1431. while (1) {
  1432. this = Tokenizer_READ(self, 0);
  1433. if (!this) {
  1434. comment = Tokenizer_pop(self);
  1435. Py_XDECREF(comment);
  1436. self->head = reset;
  1437. return Tokenizer_emit_text(self, "<!--");
  1438. }
  1439. if (this == '-' && Tokenizer_READ(self, 1) == this &&
  1440. Tokenizer_READ(self, 2) == '>') {
  1441. if (Tokenizer_emit_first(self, CommentStart))
  1442. return -1;
  1443. if (Tokenizer_emit(self, CommentEnd))
  1444. return -1;
  1445. comment = Tokenizer_pop(self);
  1446. if (!comment)
  1447. return -1;
  1448. if (Tokenizer_emit_all(self, comment))
  1449. return -1;
  1450. Py_DECREF(comment);
  1451. self->head += 2;
  1452. return 0;
  1453. }
  1454. if (Tokenizer_emit_char(self, this))
  1455. return -1;
  1456. self->head++;
  1457. }
  1458. }
  1459. /*
  1460. Write a pending tag attribute from data to the stack.
  1461. */
  1462. static int Tokenizer_push_tag_buffer(Tokenizer* self, TagData* data)
  1463. {
  1464. PyObject *tokens, *kwargs, *pad_first, *pad_before_eq, *pad_after_eq;
  1465. if (data->context & TAG_QUOTED) {
  1466. if (Tokenizer_emit_first(self, TagAttrQuote))
  1467. return -1;
  1468. tokens = Tokenizer_pop(self);
  1469. if (!tokens)
  1470. return -1;
  1471. if (Tokenizer_emit_all(self, tokens)) {
  1472. Py_DECREF(tokens);
  1473. return -1;
  1474. }
  1475. Py_DECREF(tokens);
  1476. }
  1477. pad_first = Textbuffer_render(data->pad_first);
  1478. pad_before_eq = Textbuffer_render(data->pad_before_eq);
  1479. pad_after_eq = Textbuffer_render(data->pad_after_eq);
  1480. if (!pad_first || !pad_before_eq || !pad_after_eq)
  1481. return -1;
  1482. kwargs = PyDict_New();
  1483. if (!kwargs)
  1484. return -1;
  1485. PyDict_SetItemString(kwargs, "pad_first", pad_first);
  1486. PyDict_SetItemString(kwargs, "pad_before_eq", pad_before_eq);
  1487. PyDict_SetItemString(kwargs, "pad_after_eq", pad_after_eq);
  1488. Py_DECREF(pad_first);
  1489. Py_DECREF(pad_before_eq);
  1490. Py_DECREF(pad_after_eq);
  1491. if (Tokenizer_emit_first_kwargs(self, TagAttrStart, kwargs))
  1492. return -1;
  1493. tokens = Tokenizer_pop(self);
  1494. if (!tokens)
  1495. return -1;
  1496. if (Tokenizer_emit_all(self, tokens)) {
  1497. Py_DECREF(tokens);
  1498. return -1;
  1499. }
  1500. Py_DECREF(tokens);
  1501. if (TagData_reset_buffers(data))
  1502. return -1;
  1503. return 0;
  1504. }
  1505. /*
  1506. Handle whitespace inside of an HTML open tag.
  1507. */
  1508. static int
  1509. Tokenizer_handle_tag_space(Tokenizer* self, TagData* data, Py_UNICODE text)
  1510. {
  1511. int ctx = data->context;
  1512. int end_of_value = (ctx & TAG_ATTR_VALUE &&
  1513. !(ctx & (TAG_QUOTED | TAG_NOTE_QUOTE)));
  1514. if (end_of_value || (ctx & TAG_QUOTED && ctx & TAG_NOTE_SPACE)) {
  1515. if (Tokenizer_push_tag_buffer(self, data))
  1516. return -1;
  1517. data->context = TAG_ATTR_READY;
  1518. }
  1519. else if (ctx & TAG_NOTE_SPACE)
  1520. data->context = TAG_ATTR_READY;
  1521. else if (ctx & TAG_ATTR_NAME) {
  1522. data->context |= TAG_NOTE_EQUALS;
  1523. if (Textbuffer_write(&(data->pad_before_eq), text))
  1524. return -1;
  1525. }
  1526. if (ctx & TAG_QUOTED && !(ctx & TAG_NOTE_SPACE)) {
  1527. if (Tokenizer_emit_char(self, text))
  1528. return -1;
  1529. }
  1530. else if (data->context & TAG_ATTR_READY)
  1531. return Textbuffer_write(&(data->pad_first), text);
  1532. else if (data->context & TAG_ATTR_VALUE)
  1533. return Textbuffer_write(&(data->pad_after_eq), text);
  1534. return 0;
  1535. }
  1536. /*
  1537. Handle regular text inside of an HTML open tag.
  1538. */
  1539. static int Tokenizer_handle_tag_text(Tokenizer* self, Py_UNICODE text)
  1540. {
  1541. Py_UNICODE next = Tokenizer_READ(self, 1);
  1542. if (!is_marker(text) || !Tokenizer_CAN_RECURSE(self))
  1543. return Tokenizer_emit_char(self, text);
  1544. else if (text == next && next == '{')
  1545. return Tokenizer_parse_template_or_argument(self);
  1546. else if (text == next && next == '[')
  1547. return Tokenizer_parse_wikilink(self);
  1548. else if (text == '<')
  1549. return Tokenizer_parse_tag(self);
  1550. return Tokenizer_emit_char(self, text);
  1551. }
  1552. /*
  1553. Handle all sorts of text data inside of an HTML open tag.
  1554. */
  1555. static int
  1556. Tokenizer_handle_tag_data(Tokenizer* self, TagData* data, Py_UNICODE chunk)
  1557. {
  1558. PyObject *trash;
  1559. int first_time, escaped;
  1560. if (data->context & TAG_NAME) {
  1561. first_time = !(data->context & TAG_NOTE_SPACE);
  1562. if (is_marker(chunk) || (Py_UNICODE_ISSPACE(chunk) && first_time)) {
  1563. // Tags must start with text, not spaces
  1564. Tokenizer_fail_route(self);
  1565. return 0;
  1566. }
  1567. else if (first_time)
  1568. data->context |= TAG_NOTE_SPACE;
  1569. else if (Py_UNICODE_ISSPACE(chunk)) {
  1570. data->context = TAG_ATTR_READY;
  1571. return Tokenizer_handle_tag_space(self, data, chunk);
  1572. }
  1573. }
  1574. else if (Py_UNICODE_ISSPACE(chunk))
  1575. return Tokenizer_handle_tag_space(self, data, chunk);
  1576. else if (data->context & TAG_NOTE_SPACE) {
  1577. if (data->context & TAG_QUOTED) {
  1578. data->context = TAG_ATTR_VALUE;
  1579. trash = Tokenizer_pop(self);
  1580. Py_XDECREF(trash);
  1581. self->head = data->reset - 1; // Will be auto-incremented
  1582. }
  1583. else
  1584. Tokenizer_fail_route(self);
  1585. return 0;
  1586. }
  1587. else if (data->context & TAG_ATTR_READY) {
  1588. data->context = TAG_ATTR_NAME;
  1589. if (Tokenizer_push(self, LC_TAG_ATTR))
  1590. return -1;
  1591. }
  1592. else if (data->context & TAG_ATTR_NAME) {
  1593. if (chunk == '=') {
  1594. data->context = TAG_ATTR_VALUE | TAG_NOTE_QUOTE;
  1595. if (Tokenizer_emit(self, TagAttrEquals))
  1596. return -1;
  1597. return 0;
  1598. }
  1599. if (data->context & TAG_NOTE_EQUALS) {
  1600. if (Tokenizer_push_tag_buffer(self, data))
  1601. return -1;
  1602. data->context = TAG_ATTR_NAME;
  1603. if (Tokenizer_push(self, LC_TAG_ATTR))
  1604. return -1;
  1605. }
  1606. }
  1607. else if (data->context & TAG_ATTR_VALUE) {
  1608. escaped = (Tokenizer_READ_BACKWARDS(self, 1) == '\\' &&
  1609. Tokenizer_READ_BACKWARDS(self, 2) != '\\');
  1610. if (data->context & TAG_NOTE_QUOTE) {
  1611. data->context ^= TAG_NOTE_QUOTE;
  1612. if (chunk == '"' && !escaped) {
  1613. data->context |= TAG_QUOTED;
  1614. if (Tokenizer_push(self, self->topstack->context))
  1615. return -1;
  1616. data->reset = self->head;
  1617. return 0;
  1618. }
  1619. }
  1620. else if (data->context & TAG_QUOTED) {
  1621. if (chunk == '"' && !escaped) {
  1622. data->context |= TAG_NOTE_SPACE;
  1623. return 0;
  1624. }
  1625. }
  1626. }
  1627. return Tokenizer_handle_tag_text(self, chunk);
  1628. }
  1629. /*
  1630. Handle the closing of a open tag (<foo>).
  1631. */
  1632. static int
  1633. Tokenizer_handle_tag_close_open(Tokenizer* self, TagData* data, PyObject* cls)
  1634. {
  1635. PyObject *padding, *kwargs;
  1636. if (data->context & (TAG_ATTR_NAME | TAG_ATTR_VALUE)) {
  1637. if (Tokenizer_push_tag_buffer(self, data))
  1638. return -1;
  1639. }
  1640. padding = Textbuffer_render(data->pad_first);
  1641. if (!padding)
  1642. return -1;
  1643. kwargs = PyDict_New();
  1644. if (!kwargs) {
  1645. Py_DECREF(padding);
  1646. return -1;
  1647. }
  1648. PyDict_SetItemString(kwargs, "padding", padding);
  1649. Py_DECREF(padding);
  1650. if (Tokenizer_emit_kwargs(self, cls, kwargs))
  1651. return -1;
  1652. self->head++;
  1653. return 0;
  1654. }
  1655. /*
  1656. Handle the opening of a closing tag (</foo>).
  1657. */
  1658. static int Tokenizer_handle_tag_open_close(Tokenizer* self)
  1659. {
  1660. if (Tokenizer_emit(self, TagOpenClose))
  1661. return -1;
  1662. if (Tokenizer_push(self, LC_TAG_CLOSE))
  1663. return -1;
  1664. self->head++;
  1665. return 0;
  1666. }
  1667. /*
  1668. Handle the ending of a closing tag (</foo>).
  1669. */
  1670. static PyObject* Tokenizer_handle_tag_close_close(Tokenizer* self)
  1671. {
  1672. PyObject *closing, *first, *so, *sc;
  1673. int valid = 1;
  1674. closing = Tokenizer_pop(self);
  1675. if (!closing)
  1676. return NULL;
  1677. if (PyList_GET_SIZE(closing) != 1)
  1678. valid = 0;
  1679. else {
  1680. first = PyList_GET_ITEM(closing, 0);
  1681. switch (PyObject_IsInstance(first, Text)) {
  1682. case 0:
  1683. valid = 0;
  1684. break;
  1685. case 1: {
  1686. so = strip_tag_name(first);
  1687. sc = strip_tag_name(PyList_GET_ITEM(self->topstack->stack, 1));
  1688. if (so && sc) {
  1689. if (PyUnicode_Compare(so, sc))
  1690. valid = 0;
  1691. Py_DECREF(so);
  1692. Py_DECREF(sc);
  1693. break;
  1694. }
  1695. Py_XDECREF(so);
  1696. Py_XDECREF(sc);
  1697. }
  1698. case -1:
  1699. Py_DECREF(closing);
  1700. return NULL;
  1701. }
  1702. }
  1703. if (!valid) {
  1704. Py_DECREF(closing);
  1705. return Tokenizer_fail_route(self);
  1706. }
  1707. if (Tokenizer_emit_all(self, closing)) {
  1708. Py_DECREF(closing);
  1709. return NULL;
  1710. }
  1711. Py_DECREF(closing);
  1712. if (Tokenizer_emit(self, TagCloseClose))
  1713. return NULL;
  1714. return Tokenizer_pop(self);
  1715. }
  1716. /*
  1717. Handle the body of an HTML tag that is parser-blacklisted.
  1718. */
  1719. static PyObject* Tokenizer_handle_blacklisted_tag(Tokenizer* self)
  1720. {
  1721. Py_UNICODE this, next;
  1722. while (1) {
  1723. this = Tokenizer_READ(self, 0);
  1724. next = Tokenizer_READ(self, 1);
  1725. if (!this)
  1726. return Tokenizer_fail_route(self);
  1727. else if (this == '<' && next == '/') {
  1728. if (Tokenizer_handle_tag_open_close(self))
  1729. return NULL;
  1730. self->head++;
  1731. return Tokenizer_parse(self, 0, 0);
  1732. }
  1733. else if (this == '&') {
  1734. if (Tokenizer_parse_entity(self))
  1735. return NULL;
  1736. }
  1737. else if (Tokenizer_emit_char(self, this))
  1738. return NULL;
  1739. self->head++;
  1740. }
  1741. }
  1742. /*
  1743. Handle the end of an implicitly closing single-only HTML tag.
  1744. */
  1745. static PyObject* Tokenizer_handle_single_only_tag_end(Tokenizer* self)
  1746. {
  1747. PyObject *top, *padding, *kwargs;
  1748. top = PyObject_CallMethod(self->topstack->stack, "pop", NULL);
  1749. if (!top)
  1750. return NULL;
  1751. padding = PyObject_GetAttrString(top, "padding");
  1752. Py_DECREF(top);
  1753. if (!padding)
  1754. return NULL;
  1755. kwargs = PyDict_New();
  1756. if (!kwargs) {
  1757. Py_DECREF(padding);
  1758. return NULL;
  1759. }
  1760. PyDict_SetItemString(kwargs, "padding", padding);
  1761. PyDict_SetItemString(kwargs, "implicit", Py_True);
  1762. Py_DECREF(padding);
  1763. if (Tokenizer_emit_kwargs(self, TagCloseSelfclose, kwargs))
  1764. return NULL;
  1765. self->head--; // Offset displacement done by handle_tag_close_open
  1766. return Tokenizer_pop(self);
  1767. }
  1768. /*
  1769. Handle the stream end when inside a single-supporting HTML tag.
  1770. */
  1771. static PyObject* Tokenizer_handle_single_tag_end(Tokenizer* self)
  1772. {
  1773. PyObject *token = 0, *padding, *kwargs;
  1774. Py_ssize_t len, index;
  1775. int depth = 1, is_instance;
  1776. len = PyList_GET_SIZE(self->topstack->stack);
  1777. for (index = 2; index < len; index++) {
  1778. token = PyList_GET_ITEM(self->topstack->stack, index);
  1779. is_instance = PyObject_IsInstance(token, TagOpenOpen);
  1780. if (is_instance == -1)
  1781. return NULL;
  1782. else if (is_instance == 1)
  1783. depth++;
  1784. is_instance = PyObject_IsInstance(token, TagCloseOpen);
  1785. if (is_instance == -1)
  1786. return NULL;
  1787. else if (is_instance == 1) {
  1788. depth--;
  1789. if (depth == 0)
  1790. break;
  1791. }
  1792. }
  1793. if (!token || depth > 0)
  1794. return NULL;
  1795. padding = PyObject_GetAttrString(token, "padding");
  1796. if (!padding)
  1797. return NULL;
  1798. kwargs = PyDict_New();
  1799. if (!kwargs) {
  1800. Py_DECREF(padding);
  1801. return NULL;
  1802. }
  1803. PyDict_SetItemString(kwargs, "padding", padding);
  1804. PyDict_SetItemString(kwargs, "implicit", Py_True);
  1805. Py_DECREF(padding);
  1806. token = PyObject_Call(TagCloseSelfclose, NOARGS, kwargs);
  1807. Py_DECREF(kwargs);
  1808. if (!token)
  1809. return NULL;
  1810. if (PyList_SetItem(self->topstack->stack, index, token)) {
  1811. Py_DECREF(token);
  1812. return NULL;
  1813. }
  1814. return Tokenizer_pop(self);
  1815. }
  1816. /*
  1817. Actually parse an HTML tag, starting with the open (<foo>).
  1818. */
  1819. static PyObject* Tokenizer_really_parse_tag(Tokenizer* self)
  1820. {
  1821. TagData *data = TagData_new();
  1822. PyObject *token, *text, *trash;
  1823. Py_UNICODE this, next;
  1824. int can_exit;
  1825. if (!data)
  1826. return NULL;
  1827. if (Tokenizer_push(self, LC_TAG_OPEN)) {
  1828. TagData_dealloc(data);
  1829. return NULL;
  1830. }
  1831. if (Tokenizer_emit(self, TagOpenOpen)) {
  1832. TagData_dealloc(data);
  1833. return NULL;
  1834. }
  1835. while (1) {
  1836. this = Tokenizer_READ(self, 0);
  1837. next = Tokenizer_READ(self, 1);
  1838. can_exit = (!(data->context & (TAG_QUOTED | TAG_NAME)) ||
  1839. data->context & TAG_NOTE_SPACE);
  1840. if (!this) {
  1841. if (self->topstack->context & LC_TAG_ATTR) {
  1842. if (data->context & TAG_QUOTED) {
  1843. // Unclosed attribute quote: reset, don't die
  1844. data->context = TAG_ATTR_VALUE;
  1845. trash = Tokenizer_pop(self);
  1846. Py_XDECREF(trash);
  1847. self->head = data->reset;
  1848. continue;
  1849. }
  1850. trash = Tokenizer_pop(self);
  1851. Py_XDECREF(trash);
  1852. }
  1853. TagData_dealloc(data);
  1854. return Tokenizer_fail_route(self);
  1855. }
  1856. else if (this == '>' && can_exit) {
  1857. if (Tokenizer_handle_tag_close_open(self, data, TagCloseOpen)) {
  1858. TagData_dealloc(data);
  1859. return NULL;
  1860. }
  1861. TagData_dealloc(data);
  1862. self->topstack->context = LC_TAG_BODY;
  1863. token = PyList_GET_ITEM(self->topstack->stack, 1);
  1864. text = PyObject_GetAttrString(token, "text");
  1865. if (!text)
  1866. return NULL;
  1867. if (IS_SINGLE_ONLY(text)) {
  1868. Py_DECREF(text);
  1869. return Tokenizer_handle_single_only_tag_end(self);
  1870. }
  1871. if (IS_PARSABLE(text)) {
  1872. Py_DECREF(text);
  1873. return Tokenizer_parse(self, 0, 0);
  1874. }
  1875. Py_DECREF(text);
  1876. return Tokenizer_handle_blacklisted_tag(self);
  1877. }
  1878. else if (this == '/' && next == '>' && can_exit) {
  1879. if (Tokenizer_handle_tag_close_open(self, data,
  1880. TagCloseSelfclose)) {
  1881. TagData_dealloc(data);
  1882. return NULL;
  1883. }
  1884. TagData_dealloc(data);
  1885. return Tokenizer_pop(self);
  1886. }
  1887. else {
  1888. if (Tokenizer_handle_tag_data(self, data, this) || BAD_ROUTE) {
  1889. TagData_dealloc(data);
  1890. return NULL;
  1891. }
  1892. }
  1893. self->head++;
  1894. }
  1895. }
  1896. /*
  1897. Handle the (possible) start of an implicitly closing single tag.
  1898. */
  1899. static int Tokenizer_handle_invalid_tag_start(Tokenizer* self)
  1900. {
  1901. Py_ssize_t reset = self->head + 1, pos = 0;
  1902. Textbuffer* buf;
  1903. PyObject *name, *tag;
  1904. Py_UNICODE this;
  1905. self->head += 2;
  1906. buf = Textbuffer_new();
  1907. if (!buf)
  1908. return -1;
  1909. while (1) {
  1910. this = Tokenizer_READ(self, pos);
  1911. if (Py_UNICODE_ISSPACE(this) || is_marker(this)) {
  1912. name = Textbuffer_render(buf);
  1913. if (!name) {
  1914. Textbuffer_dealloc(buf);
  1915. return -1;
  1916. }
  1917. if (!IS_SINGLE_ONLY(name))
  1918. FAIL_ROUTE(0);
  1919. Py_DECREF(name);
  1920. break;
  1921. }
  1922. Textbuffer_write(&buf, this);
  1923. pos++;
  1924. }
  1925. Textbuffer_dealloc(buf);
  1926. if (!BAD_ROUTE)
  1927. tag = Tokenizer_really_parse_tag(self);
  1928. if (BAD_ROUTE) {
  1929. RESET_ROUTE();
  1930. self->head = reset;
  1931. return Tokenizer_emit_text(self, "</");
  1932. }
  1933. if (!tag)
  1934. return -1;
  1935. // Set invalid=True flag of TagOpenOpen
  1936. if (PyObject_SetAttrString(PyList_GET_ITEM(tag, 0), "invalid", Py_True))
  1937. return -1;
  1938. if (Tokenizer_emit_all(self, tag)) {
  1939. Py_DECREF(tag);
  1940. return -1;
  1941. }
  1942. Py_DECREF(tag);
  1943. return 0;
  1944. }
  1945. /*
  1946. Parse an HTML tag at the head of the wikicode string.
  1947. */
  1948. static int Tokenizer_parse_tag(Tokenizer* self)
  1949. {
  1950. Py_ssize_t reset = self->head;
  1951. PyObject* tag;
  1952. self->head++;
  1953. tag = Tokenizer_really_parse_tag(self);
  1954. if (BAD_ROUTE) {
  1955. RESET_ROUTE();
  1956. self->head = reset;
  1957. return Tokenizer_emit_char(self, '<');
  1958. }
  1959. if (!tag) {
  1960. return -1;
  1961. }
  1962. if (Tokenizer_emit_all(self, tag)) {
  1963. Py_DECREF(tag);
  1964. return -1;
  1965. }
  1966. Py_DECREF(tag);
  1967. return 0;
  1968. }
  1969. /*
  1970. Write the body of a tag and the tokens that should surround it.
  1971. */
  1972. static int Tokenizer_emit_style_tag(Tokenizer* self, const char* tag,
  1973. const char* ticks, PyObject* body)
  1974. {
  1975. PyObject *markup, *kwargs;
  1976. markup = PyUnicode_FromString(ticks);
  1977. if (!markup)
  1978. return -1;
  1979. kwargs = PyDict_New();
  1980. if (!kwargs) {
  1981. Py_DECREF(markup);
  1982. return -1;
  1983. }
  1984. PyDict_SetItemString(kwargs, "wiki_markup", markup);
  1985. Py_DECREF(markup);
  1986. if (Tokenizer_emit_kwargs(self, TagOpenOpen, kwargs))
  1987. return -1;
  1988. if (Tokenizer_emit_text(self, tag))
  1989. return -1;
  1990. if (Tokenizer_emit(self, TagCloseOpen))
  1991. return -1;
  1992. if (Tokenizer_emit_all(self, body))
  1993. return -1;
  1994. Py_DECREF(body);
  1995. if (Tokenizer_emit(self, TagOpenClose))
  1996. return -1;
  1997. if (Tokenizer_emit_text(self, tag))
  1998. return -1;
  1999. if (Tokenizer_emit(self, TagCloseClose))
  2000. return -1;
  2001. return 0;
  2002. }
  2003. /*
  2004. Parse wiki-style italics.
  2005. */
  2006. static int Tokenizer_parse_italics(Tokenizer* self)
  2007. {
  2008. Py_ssize_t reset = self->head;
  2009. int context;
  2010. PyObject *stack;
  2011. stack = Tokenizer_parse(self, LC_STYLE_ITALICS, 1);
  2012. if (BAD_ROUTE) {
  2013. RESET_ROUTE();
  2014. self->head = reset;
  2015. if (BAD_ROUTE_CONTEXT & LC_STYLE_PASS_AGAIN) {
  2016. context = LC_STYLE_ITALICS | LC_STYLE_SECOND_PASS;
  2017. stack = Tokenizer_parse(self, context, 1);
  2018. }
  2019. else
  2020. return Tokenizer_emit_text(self, "''");
  2021. }
  2022. if (!stack)
  2023. return -1;
  2024. return Tokenizer_emit_style_tag(self, "i", "''", stack);
  2025. }
  2026. /*
  2027. Parse wiki-style bold.
  2028. */
  2029. static int Tokenizer_parse_bold(Tokenizer* self)
  2030. {
  2031. Py_ssize_t reset = self->head;
  2032. PyObject *stack;
  2033. stack = Tokenizer_parse(self, LC_STYLE_BOLD, 1);
  2034. if (BAD_ROUTE) {
  2035. RESET_ROUTE();
  2036. self->head = reset;
  2037. if (self->topstack->context & LC_STYLE_SECOND_PASS)
  2038. return Tokenizer_emit_char(self, '\'') ? -1 : 1;
  2039. if (self->topstack->context & LC_STYLE_ITALICS) {
  2040. self->topstack->context |= LC_STYLE_PASS_AGAIN;
  2041. return Tokenizer_emit_text(self, "'''");
  2042. }
  2043. if (Tokenizer_emit_char(self, '\''))
  2044. return -1;
  2045. return Tokenizer_parse_italics(self);
  2046. }
  2047. if (!stack)
  2048. return -1;
  2049. return Tokenizer_emit_style_tag(self, "b", "'''", stack);
  2050. }
  2051. /*
  2052. Parse wiki-style italics and bold together (i.e., five ticks).
  2053. */
  2054. static int Tokenizer_parse_italics_and_bold(Tokenizer* self)
  2055. {
  2056. Py_ssize_t reset = self->head;
  2057. PyObject *stack, *stack2;
  2058. stack = Tokenizer_parse(self, LC_STYLE_BOLD, 1);
  2059. if (BAD_ROUTE) {
  2060. RESET_ROUTE();
  2061. self->head = reset;
  2062. stack = Tokenizer_parse(self, LC_STYLE_ITALICS, 1);
  2063. if (BAD_ROUTE) {
  2064. RESET_ROUTE();
  2065. self->head = reset;
  2066. return Tokenizer_emit_text(self, "'''''");
  2067. }
  2068. if (!stack)
  2069. return -1;
  2070. reset = self->head;
  2071. stack2 = Tokenizer_parse(self, LC_STYLE_BOLD, 1);
  2072. if (BAD_ROUTE) {
  2073. RESET_ROUTE();
  2074. self->head = reset;
  2075. if (Tokenizer_emit_text(self, "'''"))
  2076. return -1;
  2077. return Tokenizer_emit_style_tag(self, "i", "''", stack);
  2078. }
  2079. if (!stack2)
  2080. return -1;
  2081. if (Tokenizer_push(self, 0))
  2082. return -1;
  2083. if (Tokenizer_emit_style_tag(self, "i", "''", stack))
  2084. return -1;
  2085. if (Tokenizer_emit_all(self, stack2))
  2086. return -1;
  2087. Py_DECREF(stack2);
  2088. stack2 = Tokenizer_pop(self);
  2089. if (!stack2)
  2090. return -1;
  2091. return Tokenizer_emit_style_tag(self, "b", "'''", stack2);
  2092. }
  2093. if (!stack)
  2094. return -1;
  2095. reset = self->head;
  2096. stack2 = Tokenizer_parse(self, LC_STYLE_ITALICS, 1);
  2097. if (BAD_ROUTE) {
  2098. RESET_ROUTE();
  2099. self->head = reset;
  2100. if (Tokenizer_emit_text(self, "''"))
  2101. return -1;
  2102. return Tokenizer_emit_style_tag(self, "b", "'''", stack);
  2103. }
  2104. if (!stack2)
  2105. return -1;
  2106. if (Tokenizer_push(self, 0))
  2107. return -1;
  2108. if (Tokenizer_emit_style_tag(self, "b", "'''", stack))
  2109. return -1;
  2110. if (Tokenizer_emit_all(self, stack2))
  2111. return -1;
  2112. Py_DECREF(stack2);
  2113. stack2 = Tokenizer_pop(self);
  2114. if (!stack2)
  2115. return -1;
  2116. return Tokenizer_emit_style_tag(self, "i", "''", stack2);
  2117. }
  2118. /*
  2119. Parse wiki-style formatting (''/''' for italics/bold).
  2120. */
  2121. static PyObject* Tokenizer_parse_style(Tokenizer* self)
  2122. {
  2123. int context = self->topstack->context, ticks = 2, i;
  2124. self->head += 2;
  2125. while (Tokenizer_READ(self, 0) == '\'') {
  2126. self->head++;
  2127. ticks++;
  2128. }
  2129. if (ticks > 5) {
  2130. for (i = 0; i < ticks - 5; i++) {
  2131. if (Tokenizer_emit_char(self, '\''))
  2132. return NULL;
  2133. }
  2134. ticks = 5;
  2135. }
  2136. else if (ticks == 4) {
  2137. if (Tokenizer_emit_char(self, '\''))
  2138. return NULL;
  2139. ticks = 3;
  2140. }
  2141. if ((context & LC_STYLE_ITALICS && (ticks == 2 || ticks == 5)) ||
  2142. (context & LC_STYLE_BOLD && (ticks == 3 || ticks == 5))) {
  2143. if (ticks == 5)
  2144. self->head -= context & LC_STYLE_ITALICS ? 3 : 2;
  2145. return Tokenizer_pop(self);
  2146. }
  2147. if (!Tokenizer_CAN_RECURSE(self)) {
  2148. if (ticks == 3) {
  2149. if (context & LC_STYLE_SECOND_PASS) {
  2150. if (Tokenizer_emit_char(self, '\''))
  2151. return NULL;
  2152. return Tokenizer_pop(self);
  2153. }
  2154. if (context & LC_STYLE_ITALICS)
  2155. self->topstack->context |= LC_STYLE_PASS_AGAIN;
  2156. }
  2157. for (i = 0; i < ticks; i++) {
  2158. if (Tokenizer_emit_char(self, '\''))
  2159. return NULL;
  2160. }
  2161. }
  2162. else if (ticks == 2) {
  2163. if (Tokenizer_parse_italics(self))
  2164. return NULL;
  2165. }
  2166. else if (ticks == 3) {
  2167. switch (Tokenizer_parse_bold(self)) {
  2168. case 1:
  2169. return Tokenizer_pop(self);
  2170. case -1:
  2171. return NULL;
  2172. }
  2173. }
  2174. else {
  2175. if (Tokenizer_parse_italics_and_bold(self))
  2176. return NULL;
  2177. }
  2178. self->head--;
  2179. return Py_None;
  2180. }
  2181. /*
  2182. Handle a list marker at the head (#, *, ;, :).
  2183. */
  2184. static int Tokenizer_handle_list_marker(Tokenizer* self)
  2185. {
  2186. PyObject *markup = Tokenizer_read(self, 0), *kwargs;
  2187. Py_UNICODE code = *PyUnicode_AS_UNICODE(markup);
  2188. if (code == ';')
  2189. self->topstack->context |= LC_DLTERM;
  2190. kwargs = PyDict_New();
  2191. if (!kwargs)
  2192. return -1;
  2193. PyDict_SetItemString(kwargs, "wiki_markup", markup);
  2194. if (Tokenizer_emit_kwargs(self, TagOpenOpen, kwargs))
  2195. return -1;
  2196. if (Tokenizer_emit_text(self, GET_HTML_TAG(code)))
  2197. return -1;
  2198. if (Tokenizer_emit(self, TagCloseSelfclose))
  2199. return -1;
  2200. return 0;
  2201. }
  2202. /*
  2203. Handle a wiki-style list (#, *, ;, :).
  2204. */
  2205. static int Tokenizer_handle_list(Tokenizer* self)
  2206. {
  2207. Py_UNICODE marker = Tokenizer_READ(self, 1);
  2208. if (Tokenizer_handle_list_marker(self))
  2209. return -1;
  2210. while (marker == '#' || marker == '*' || marker == ';' ||
  2211. marker == ':') {
  2212. self->head++;
  2213. if (Tokenizer_handle_list_marker(self))
  2214. return -1;
  2215. marker = Tokenizer_READ(self, 1);
  2216. }
  2217. return 0;
  2218. }
  2219. /*
  2220. Handle a wiki-style horizontal rule (----) in the string.
  2221. */
  2222. static int Tokenizer_handle_hr(Tokenizer* self)
  2223. {
  2224. PyObject *markup, *kwargs;
  2225. Textbuffer *buffer = Textbuffer_new();
  2226. int i;
  2227. if (!buffer)
  2228. return -1;
  2229. self->head += 3;
  2230. for (i = 0; i < 4; i++) {
  2231. if (Textbuffer_write(&buffer, '-'))
  2232. return -1;
  2233. }
  2234. while (Tokenizer_READ(self, 1) == '-') {
  2235. if (Textbuffer_write(&buffer, '-'))
  2236. return -1;
  2237. self->head++;
  2238. }
  2239. markup = Textbuffer_render(buffer);
  2240. Textbuffer_dealloc(buffer);
  2241. if (!markup)
  2242. return -1;
  2243. kwargs = PyDict_New();
  2244. if (!kwargs)
  2245. return -1;
  2246. PyDict_SetItemString(kwargs, "wiki_markup", markup);
  2247. Py_DECREF(markup);
  2248. if (Tokenizer_emit_kwargs(self, TagOpenOpen, kwargs))
  2249. return -1;
  2250. if (Tokenizer_emit_text(self, "hr"))
  2251. return -1;
  2252. if (Tokenizer_emit(self, TagCloseSelfclose))
  2253. return -1;
  2254. return 0;
  2255. }
  2256. /*
  2257. Handle the term in a description list ('foo' in ';foo:bar').
  2258. */
  2259. static int Tokenizer_handle_dl_term(Tokenizer* self)
  2260. {
  2261. self->topstack->context ^= LC_DLTERM;
  2262. if (Tokenizer_READ(self, 0) == ':')
  2263. return Tokenizer_handle_list_marker(self);
  2264. return Tokenizer_emit_char(self, '\n');
  2265. }
  2266. /*
  2267. Handle the end of the stream of wikitext.
  2268. */
  2269. static PyObject* Tokenizer_handle_end(Tokenizer* self, int context)
  2270. {
  2271. PyObject *token, *text, *trash;
  2272. int single;
  2273. if (context & AGG_FAIL) {
  2274. if (context & LC_TAG_BODY) {
  2275. token = PyList_GET_ITEM(self->topstack->stack, 1);
  2276. text = PyObject_GetAttrString(token, "text");
  2277. if (!text)
  2278. return NULL;
  2279. single = IS_SINGLE(text);
  2280. Py_DECREF(text);
  2281. if (single)
  2282. return Tokenizer_handle_single_tag_end(self);
  2283. }
  2284. else if (context & AGG_DOUBLE) {
  2285. trash = Tokenizer_pop(self);
  2286. Py_XDECREF(trash);
  2287. }
  2288. return Tokenizer_fail_route(self);
  2289. }
  2290. return Tokenizer_pop(self);
  2291. }
  2292. /*
  2293. Make sure we are not trying to write an invalid character. Return 0 if
  2294. everything is safe, or -1 if the route must be failed.
  2295. */
  2296. static int Tokenizer_verify_safe(Tokenizer* self, int context, Py_UNICODE data)
  2297. {
  2298. if (context & LC_FAIL_NEXT)
  2299. return -1;
  2300. if (context & LC_WIKILINK_TITLE) {
  2301. if (data == ']' || data == '{')
  2302. self->topstack->context |= LC_FAIL_NEXT;
  2303. else if (data == '\n' || data == '[' || data == '}')
  2304. return -1;
  2305. return 0;
  2306. }
  2307. if (context & LC_EXT_LINK_TITLE)
  2308. return (data == '\n') ? -1 : 0;
  2309. if (context & LC_TAG_CLOSE)
  2310. return (data == '<') ? -1 : 0;
  2311. if (context & LC_TEMPLATE_NAME) {
  2312. if (data == '{' || data == '}' || data == '[') {
  2313. self->topstack->context |= LC_FAIL_NEXT;
  2314. return 0;
  2315. }
  2316. if (data == ']') {
  2317. return -1;
  2318. }
  2319. if (data == '|')
  2320. return 0;
  2321. if (context & LC_HAS_TEXT) {
  2322. if (context & LC_FAIL_ON_TEXT) {
  2323. if (!Py_UNICODE_ISSPACE(data))
  2324. return -1;
  2325. }
  2326. else {
  2327. if (data == '\n')
  2328. self->topstack->context |= LC_FAIL_ON_TEXT;
  2329. }
  2330. }
  2331. else if (!Py_UNICODE_ISSPACE(data))
  2332. self->topstack->context |= LC_HAS_TEXT;
  2333. }
  2334. else {
  2335. if (context & LC_FAIL_ON_EQUALS) {
  2336. if (data == '=') {
  2337. return -1;
  2338. }
  2339. }
  2340. else if (context & LC_FAIL_ON_LBRACE) {
  2341. if (data == '{' || (Tokenizer_READ(self, -1) == '{' &&
  2342. Tokenizer_READ(self, -2) == '{')) {
  2343. if (context & LC_TEMPLATE)
  2344. self->topstack->context |= LC_FAIL_ON_EQUALS;
  2345. else
  2346. self->topstack->context |= LC_FAIL_NEXT;
  2347. return 0;
  2348. }
  2349. self->topstack->context ^= LC_FAIL_ON_LBRACE;
  2350. }
  2351. else if (context & LC_FAIL_ON_RBRACE) {
  2352. if (data == '}') {
  2353. if (context & LC_TEMPLATE)
  2354. self->topstack->context |= LC_FAIL_ON_EQUALS;
  2355. else
  2356. self->topstack->context |= LC_FAIL_NEXT;
  2357. return 0;
  2358. }
  2359. self->topstack->context ^= LC_FAIL_ON_RBRACE;
  2360. }
  2361. else if (data == '{')
  2362. self->topstack->context |= LC_FAIL_ON_LBRACE;
  2363. else if (data == '}')
  2364. self->topstack->context |= LC_FAIL_ON_RBRACE;
  2365. }
  2366. return 0;
  2367. }
  2368. /*
  2369. Parse the wikicode string, using context for when to stop. If push is true,
  2370. we will push a new context, otherwise we won't and context will be ignored.
  2371. */
  2372. static PyObject* Tokenizer_parse(Tokenizer* self, int context, int push)
  2373. {
  2374. int this_context;
  2375. Py_UNICODE this, next, next_next, last;
  2376. PyObject* temp;
  2377. if (push) {
  2378. if (Tokenizer_push(self, context))
  2379. return NULL;
  2380. }
  2381. while (1) {
  2382. this = Tokenizer_READ(self, 0);
  2383. this_context = self->topstack->context;
  2384. if (this_context & AGG_UNSAFE) {
  2385. if (Tokenizer_verify_safe(self, this_context, this) < 0) {
  2386. if (this_context & AGG_DOUBLE) {
  2387. temp = Tokenizer_pop(self);
  2388. Py_XDECREF(temp);
  2389. }
  2390. return Tokenizer_fail_route(self);
  2391. }
  2392. }
  2393. if (!is_marker(this)) {
  2394. if (Tokenizer_emit_char(self, this))
  2395. return NULL;
  2396. self->head++;
  2397. continue;
  2398. }
  2399. if (!this)
  2400. return Tokenizer_handle_end(self, this_context);
  2401. next = Tokenizer_READ(self, 1);
  2402. last = Tokenizer_READ_BACKWARDS(self, 1);
  2403. if (this == next && next == '{') {
  2404. if (Tokenizer_CAN_RECURSE(self)) {
  2405. if (Tokenizer_parse_template_or_argument(self))
  2406. return NULL;
  2407. }
  2408. else if (Tokenizer_emit_char(self, this))
  2409. return NULL;
  2410. }
  2411. else if (this == '|' && this_context & LC_TEMPLATE) {
  2412. if (Tokenizer_handle_template_param(self))
  2413. return NULL;
  2414. }
  2415. else if (this == '=' && this_context & LC_TEMPLATE_PARAM_KEY) {
  2416. if (Tokenizer_handle_template_param_value(self))
  2417. return NULL;
  2418. }
  2419. else if (this == next && next == '}' && this_context & LC_TEMPLATE)
  2420. return Tokenizer_handle_template_end(self);
  2421. else if (this == '|' && this_context & LC_ARGUMENT_NAME) {
  2422. if (Tokenizer_handle_argument_separator(self))
  2423. return NULL;
  2424. }
  2425. else if (this == next && next == '}' && this_context & LC_ARGUMENT) {
  2426. if (Tokenizer_READ(self, 2) == '}') {
  2427. return Tokenizer_handle_argument_end(self);
  2428. }
  2429. if (Tokenizer_emit_char(self, this))
  2430. return NULL;
  2431. }
  2432. else if (this == next && next == '[' && Tokenizer_CAN_RECURSE(self)) {
  2433. if (!(this_context & AGG_NO_WIKILINKS)) {
  2434. if (Tokenizer_parse_wikilink(self))
  2435. return NULL;
  2436. }
  2437. else if (Tokenizer_emit_char(self, this))
  2438. return NULL;
  2439. }
  2440. else if (this == '|' && this_context & LC_WIKILINK_TITLE) {
  2441. if (Tokenizer_handle_wikilink_separator(self))
  2442. return NULL;
  2443. }
  2444. else if (this == next && next == ']' && this_context & LC_WIKILINK)
  2445. return Tokenizer_handle_wikilink_end(self);
  2446. else if (this == '[') {
  2447. if (Tokenizer_parse_external_link(self, 1))
  2448. return NULL;
  2449. }
  2450. else if (this == ':' && !is_marker(last)) {
  2451. if (Tokenizer_parse_external_link(self, 0))
  2452. return NULL;
  2453. }
  2454. else if (this == ']' && this_context & LC_EXT_LINK_TITLE)
  2455. return Tokenizer_pop(self);
  2456. else if (this == '=' && !(self->global & GL_HEADING)) {
  2457. if (!last || last == '\n') {
  2458. if (Tokenizer_parse_heading(self))
  2459. return NULL;
  2460. }
  2461. else if (Tokenizer_emit_char(self, this))
  2462. return NULL;
  2463. }
  2464. else if (this == '=' && this_context & LC_HEADING)
  2465. return (PyObject*) Tokenizer_handle_heading_end(self);
  2466. else if (this == '\n' && this_context & LC_HEADING)
  2467. return Tokenizer_fail_route(self);
  2468. else if (this == '&') {
  2469. if (Tokenizer_parse_entity(self))
  2470. return NULL;
  2471. }
  2472. else if (this == '<' && next == '!') {
  2473. next_next = Tokenizer_READ(self, 2);
  2474. if (next_next == Tokenizer_READ(self, 3) && next_next == '-') {
  2475. if (Tokenizer_parse_comment(self))
  2476. return NULL;
  2477. }
  2478. else if (Tokenizer_emit_char(self, this))
  2479. return NULL;
  2480. }
  2481. else if (this == '<' && next == '/' && Tokenizer_READ(self, 2)) {
  2482. if (this_context & LC_TAG_BODY ?
  2483. Tokenizer_handle_tag_open_close(self) :
  2484. Tokenizer_handle_invalid_tag_start(self))
  2485. return NULL;
  2486. }
  2487. else if (this == '<' && !(this_context & LC_TAG_CLOSE)) {
  2488. if (Tokenizer_CAN_RECURSE(self)) {
  2489. if (Tokenizer_parse_tag(self))
  2490. return NULL;
  2491. }
  2492. else if (Tokenizer_emit_char(self, this))
  2493. return NULL;
  2494. }
  2495. else if (this == '>' && this_context & LC_TAG_CLOSE)
  2496. return Tokenizer_handle_tag_close_close(self);
  2497. else if (this == next && next == '\'' && !self->skip_style_tags) {
  2498. temp = Tokenizer_parse_style(self);
  2499. if (temp != Py_None)
  2500. return temp;
  2501. }
  2502. else if (!last || last == '\n') {
  2503. if (this == '#' || this == '*' || this == ';' || this == ':') {
  2504. if (Tokenizer_handle_list(self))
  2505. return NULL;
  2506. }
  2507. else if (this == '-' && this == next &&
  2508. this == Tokenizer_READ(self, 2) &&
  2509. this == Tokenizer_READ(self, 3)) {
  2510. if (Tokenizer_handle_hr(self))
  2511. return NULL;
  2512. }
  2513. else if (Tokenizer_emit_char(self, this))
  2514. return NULL;
  2515. }
  2516. else if ((this == '\n' || this == ':') && this_context & LC_DLTERM) {
  2517. if (Tokenizer_handle_dl_term(self))
  2518. return NULL;
  2519. }
  2520. else if (Tokenizer_emit_char(self, this))
  2521. return NULL;
  2522. self->head++;
  2523. }
  2524. }
  2525. /*
  2526. Build a list of tokens from a string of wikicode and return it.
  2527. */
  2528. static PyObject* Tokenizer_tokenize(Tokenizer* self, PyObject* args)
  2529. {
  2530. PyObject *text, *temp, *tokens;
  2531. int context = 0, skip_style_tags = 0;
  2532. if (PyArg_ParseTuple(args, "U|ii", &text, &context, &skip_style_tags)) {
  2533. Py_XDECREF(self->text);
  2534. self->text = PySequence_Fast(text, "expected a sequence");
  2535. }
  2536. else {
  2537. const char* encoded;
  2538. Py_ssize_t size;
  2539. /* Failed to parse a Unicode object; try a string instead. */
  2540. PyErr_Clear();
  2541. if (!PyArg_ParseTuple(args, "s#|ii", &encoded, &size, &context,
  2542. &skip_style_tags))
  2543. return NULL;
  2544. temp = PyUnicode_FromStringAndSize(encoded, size);
  2545. if (!text)
  2546. return NULL;
  2547. Py_XDECREF(self->text);
  2548. text = PySequence_Fast(temp, "expected a sequence");
  2549. Py_XDECREF(temp);
  2550. self->text = text;
  2551. }
  2552. self->head = self->global = self->depth = self->cycles = 0;
  2553. self->length = PyList_GET_SIZE(self->text);
  2554. self->skip_style_tags = skip_style_tags;
  2555. tokens = Tokenizer_parse(self, context, 1);
  2556. if (!tokens && !PyErr_Occurred()) {
  2557. if (!ParserError) {
  2558. if (load_exceptions())
  2559. return NULL;
  2560. }
  2561. if (BAD_ROUTE) {
  2562. RESET_ROUTE();
  2563. PyErr_SetString(ParserError, "C tokenizer exited with BAD_ROUTE");
  2564. }
  2565. else
  2566. PyErr_SetString(ParserError, "C tokenizer exited unexpectedly");
  2567. return NULL;
  2568. }
  2569. return tokens;
  2570. }
  2571. static int load_entities(void)
  2572. {
  2573. PyObject *tempmod, *defmap, *deflist;
  2574. unsigned numdefs, i;
  2575. #ifdef IS_PY3K
  2576. PyObject *string;
  2577. #endif
  2578. tempmod = PyImport_ImportModule(ENTITYDEFS_MODULE);
  2579. if (!tempmod)
  2580. return -1;
  2581. defmap = PyObject_GetAttrString(tempmod, "entitydefs");
  2582. if (!defmap)
  2583. return -1;
  2584. Py_DECREF(tempmod);
  2585. deflist = PyDict_Keys(defmap);
  2586. if (!deflist)
  2587. return -1;
  2588. Py_DECREF(defmap);
  2589. numdefs = (unsigned) PyList_GET_SIZE(defmap);
  2590. entitydefs = calloc(numdefs + 1, sizeof(char*));
  2591. if (!entitydefs)
  2592. return -1;
  2593. for (i = 0; i < numdefs; i++) {
  2594. #ifdef IS_PY3K
  2595. string = PyUnicode_AsASCIIString(PyList_GET_ITEM(deflist, i));
  2596. if (!string)
  2597. return -1;
  2598. entitydefs[i] = PyBytes_AsString(string);
  2599. #else
  2600. entitydefs[i] = PyBytes_AsString(PyList_GET_ITEM(deflist, i));
  2601. #endif
  2602. if (!entitydefs[i])
  2603. return -1;
  2604. }
  2605. Py_DECREF(deflist);
  2606. return 0;
  2607. }
  2608. static int load_tokens(void)
  2609. {
  2610. PyObject *tempmod, *tokens,
  2611. *globals = PyEval_GetGlobals(),
  2612. *locals = PyEval_GetLocals(),
  2613. *fromlist = PyList_New(1),
  2614. *modname = IMPORT_NAME_FUNC("tokens");
  2615. char *name = "mwparserfromhell.parser";
  2616. if (!fromlist || !modname)
  2617. return -1;
  2618. PyList_SET_ITEM(fromlist, 0, modname);
  2619. tempmod = PyImport_ImportModuleLevel(name, globals, locals, fromlist, 0);
  2620. Py_DECREF(fromlist);
  2621. if (!tempmod)
  2622. return -1;
  2623. tokens = PyObject_GetAttrString(tempmod, "tokens");
  2624. Py_DECREF(tempmod);
  2625. Text = PyObject_GetAttrString(tokens, "Text");
  2626. TemplateOpen = PyObject_GetAttrString(tokens, "TemplateOpen");
  2627. TemplateParamSeparator = PyObject_GetAttrString(tokens,
  2628. "TemplateParamSeparator");
  2629. TemplateParamEquals = PyObject_GetAttrString(tokens,
  2630. "TemplateParamEquals");
  2631. TemplateClose = PyObject_GetAttrString(tokens, "TemplateClose");
  2632. ArgumentOpen = PyObject_GetAttrString(tokens, "ArgumentOpen");
  2633. ArgumentSeparator = PyObject_GetAttrString(tokens, "ArgumentSeparator");
  2634. ArgumentClose = PyObject_GetAttrString(tokens, "ArgumentClose");
  2635. WikilinkOpen = PyObject_GetAttrString(tokens, "WikilinkOpen");
  2636. WikilinkSeparator = PyObject_GetAttrString(tokens, "WikilinkSeparator");
  2637. WikilinkClose = PyObject_GetAttrString(tokens, "WikilinkClose");
  2638. ExternalLinkOpen = PyObject_GetAttrString(tokens, "ExternalLinkOpen");
  2639. ExternalLinkSeparator = PyObject_GetAttrString(tokens,
  2640. "ExternalLinkSeparator");
  2641. ExternalLinkClose = PyObject_GetAttrString(tokens, "ExternalLinkClose");
  2642. HTMLEntityStart = PyObject_GetAttrString(tokens, "HTMLEntityStart");
  2643. HTMLEntityNumeric = PyObject_GetAttrString(tokens, "HTMLEntityNumeric");
  2644. HTMLEntityHex = PyObject_GetAttrString(tokens, "HTMLEntityHex");
  2645. HTMLEntityEnd = PyObject_GetAttrString(tokens, "HTMLEntityEnd");
  2646. HeadingStart = PyObject_GetAttrString(tokens, "HeadingStart");
  2647. HeadingEnd = PyObject_GetAttrString(tokens, "HeadingEnd");
  2648. CommentStart = PyObject_GetAttrString(tokens, "CommentStart");
  2649. CommentEnd = PyObject_GetAttrString(tokens, "CommentEnd");
  2650. TagOpenOpen = PyObject_GetAttrString(tokens, "TagOpenOpen");
  2651. TagAttrStart = PyObject_GetAttrString(tokens, "TagAttrStart");
  2652. TagAttrEquals = PyObject_GetAttrString(tokens, "TagAttrEquals");
  2653. TagAttrQuote = PyObject_GetAttrString(tokens, "TagAttrQuote");
  2654. TagCloseOpen = PyObject_GetAttrString(tokens, "TagCloseOpen");
  2655. TagCloseSelfclose = PyObject_GetAttrString(tokens, "TagCloseSelfclose");
  2656. TagOpenClose = PyObject_GetAttrString(tokens, "TagOpenClose");
  2657. TagCloseClose = PyObject_GetAttrString(tokens, "TagCloseClose");
  2658. Py_DECREF(tokens);
  2659. return 0;
  2660. }
  2661. static int load_defs(void)
  2662. {
  2663. PyObject *tempmod,
  2664. *globals = PyEval_GetGlobals(),
  2665. *locals = PyEval_GetLocals(),
  2666. *fromlist = PyList_New(1),
  2667. *modname = IMPORT_NAME_FUNC("definitions");
  2668. char *name = "mwparserfromhell";
  2669. if (!fromlist || !modname)
  2670. return -1;
  2671. PyList_SET_ITEM(fromlist, 0, modname);
  2672. tempmod = PyImport_ImportModuleLevel(name, globals, locals, fromlist, 0);
  2673. Py_DECREF(fromlist);
  2674. if (!tempmod)
  2675. return -1;
  2676. definitions = PyObject_GetAttrString(tempmod, "definitions");
  2677. Py_DECREF(tempmod);
  2678. return 0;
  2679. }
  2680. static int load_exceptions(void)
  2681. {
  2682. PyObject *tempmod, *parsermod,
  2683. *globals = PyEval_GetGlobals(),
  2684. *locals = PyEval_GetLocals(),
  2685. *fromlist = PyList_New(1),
  2686. *modname = IMPORT_NAME_FUNC("parser");
  2687. char *name = "mwparserfromhell";
  2688. if (!fromlist || !modname)
  2689. return -1;
  2690. PyList_SET_ITEM(fromlist, 0, modname);
  2691. tempmod = PyImport_ImportModuleLevel(name, globals, locals, fromlist, 0);
  2692. Py_DECREF(fromlist);
  2693. if (!tempmod)
  2694. return -1;
  2695. parsermod = PyObject_GetAttrString(tempmod, "parser");
  2696. Py_DECREF(tempmod);
  2697. ParserError = PyObject_GetAttrString(parsermod, "ParserError");
  2698. Py_DECREF(parsermod);
  2699. return 0;
  2700. }
  2701. PyMODINIT_FUNC INIT_FUNC_NAME(void)
  2702. {
  2703. PyObject *module;
  2704. TokenizerType.tp_new = PyType_GenericNew;
  2705. if (PyType_Ready(&TokenizerType) < 0)
  2706. INIT_ERROR;
  2707. module = CREATE_MODULE;
  2708. if (!module)
  2709. INIT_ERROR;
  2710. Py_INCREF(&TokenizerType);
  2711. PyModule_AddObject(module, "CTokenizer", (PyObject*) &TokenizerType);
  2712. Py_INCREF(Py_True);
  2713. PyDict_SetItemString(TokenizerType.tp_dict, "USES_C", Py_True);
  2714. EMPTY = PyUnicode_FromString("");
  2715. NOARGS = PyTuple_New(0);
  2716. if (!EMPTY || !NOARGS || load_entities() || load_tokens() || load_defs())
  2717. INIT_ERROR;
  2718. #ifdef IS_PY3K
  2719. return module;
  2720. #endif
  2721. }