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.
 
 
 
 

194 lines
4.3 KiB

  1. /*
  2. Copyright (C) 2012-2020 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 "definitions.h"
  20. /*
  21. This file should be kept up to date with mwparserfromhell/definitions.py.
  22. See the Python version for data sources.
  23. */
  24. // clang-format off
  25. static const char *URI_SCHEMES[] = {
  26. "bitcoin",
  27. "ftp",
  28. "ftps",
  29. "geo",
  30. "git",
  31. "gopher",
  32. "http",
  33. "https",
  34. "irc",
  35. "ircs",
  36. "magnet",
  37. "mailto",
  38. "mms",
  39. "news",
  40. "nntp",
  41. "redis",
  42. "sftp",
  43. "sip",
  44. "sips",
  45. "sms",
  46. "ssh",
  47. "svn",
  48. "tel",
  49. "telnet",
  50. "urn",
  51. "worldwind",
  52. "xmpp",
  53. NULL,
  54. };
  55. static const char *URI_SCHEMES_AUTHORITY_OPTIONAL[] = {
  56. "bitcoin",
  57. "geo",
  58. "magnet",
  59. "mailto",
  60. "news",
  61. "sip",
  62. "sips",
  63. "sms",
  64. "tel",
  65. "urn",
  66. "xmpp",
  67. NULL,
  68. };
  69. static const char *PARSER_BLACKLIST[] = {
  70. "categorytree",
  71. "ce",
  72. "chem",
  73. "gallery",
  74. "graph",
  75. "hiero",
  76. "imagemap",
  77. "inputbox",
  78. "math",
  79. "nowiki",
  80. "pre",
  81. "score",
  82. "section",
  83. "source",
  84. "syntaxhighlight",
  85. "templatedata",
  86. "timeline",
  87. NULL,
  88. };
  89. // clang-format on
  90. static const char *SINGLE[] = {
  91. "br", "wbr", "hr", "meta", "link", "img", "li", "dt", "dd", "th", "td", "tr", NULL};
  92. static const char *SINGLE_ONLY[] = {"br", "wbr", "hr", "meta", "link", "img", NULL};
  93. /*
  94. Convert a PyUnicodeObject to a lowercase ASCII char* array and store it in
  95. the second argument. The caller must free the return value when finished.
  96. If the return value is NULL, the conversion failed and *string is not set.
  97. */
  98. static PyObject *
  99. unicode_to_lcase_ascii(PyObject *input, const char **string)
  100. {
  101. PyObject *lower = PyObject_CallMethod(input, "lower", NULL), *bytes;
  102. if (!lower) {
  103. return NULL;
  104. }
  105. bytes = PyUnicode_AsASCIIString(lower);
  106. Py_DECREF(lower);
  107. if (!bytes) {
  108. if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) {
  109. PyErr_Clear();
  110. }
  111. return NULL;
  112. }
  113. *string = PyBytes_AS_STRING(bytes);
  114. return bytes;
  115. }
  116. /*
  117. Return whether a PyUnicodeObject is in a list of lowercase ASCII strings.
  118. */
  119. static int
  120. unicode_in_string_list(PyObject *input, const char **list)
  121. {
  122. const char *string;
  123. PyObject *temp = unicode_to_lcase_ascii(input, &string);
  124. int retval = 0;
  125. if (!temp) {
  126. return 0;
  127. }
  128. while (*list) {
  129. if (!strcmp(*(list++), string)) {
  130. retval = 1;
  131. goto end;
  132. }
  133. }
  134. end:
  135. Py_DECREF(temp);
  136. return retval;
  137. }
  138. /*
  139. Return if the given tag's contents should be passed to the parser.
  140. */
  141. int
  142. is_parsable(PyObject *tag)
  143. {
  144. return !unicode_in_string_list(tag, PARSER_BLACKLIST);
  145. }
  146. /*
  147. Return whether or not the given tag can exist without a close tag.
  148. */
  149. int
  150. is_single(PyObject *tag)
  151. {
  152. return unicode_in_string_list(tag, SINGLE);
  153. }
  154. /*
  155. Return whether or not the given tag must exist without a close tag.
  156. */
  157. int
  158. is_single_only(PyObject *tag)
  159. {
  160. return unicode_in_string_list(tag, SINGLE_ONLY);
  161. }
  162. /*
  163. Return whether the given scheme is valid for external links.
  164. */
  165. int
  166. is_scheme(PyObject *scheme, int slashes)
  167. {
  168. if (slashes) {
  169. return unicode_in_string_list(scheme, URI_SCHEMES);
  170. } else {
  171. return unicode_in_string_list(scheme, URI_SCHEMES_AUTHORITY_OPTIONAL);
  172. }
  173. }