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.
 
 
 
 

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