A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

2626 lines
73 KiB

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