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.
 
 
 
 

2815 lines
83 KiB

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