wxDiscode.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // HTML 支持的数学符号
  2. function strNumDiscode(str){
  3. str = str.replace(/∀/g, '∀');
  4. str = str.replace(/∂/g, '∂');
  5. str = str.replace(/&exists;/g, '∃');
  6. str = str.replace(/∅/g, '∅');
  7. str = str.replace(/∇/g, '∇');
  8. str = str.replace(/∈/g, '∈');
  9. str = str.replace(/∉/g, '∉');
  10. str = str.replace(/∋/g, '∋');
  11. str = str.replace(/∏/g, '∏');
  12. str = str.replace(/∑/g, '∑');
  13. str = str.replace(/−/g, '−');
  14. str = str.replace(/∗/g, '∗');
  15. str = str.replace(/√/g, '√');
  16. str = str.replace(/∝/g, '∝');
  17. str = str.replace(/∞/g, '∞');
  18. str = str.replace(/∠/g, '∠');
  19. str = str.replace(/∧/g, '∧');
  20. str = str.replace(/∨/g, '∨');
  21. str = str.replace(/∩/g, '∩');
  22. str = str.replace(/∩/g, '∪');
  23. str = str.replace(/∫/g, '∫');
  24. str = str.replace(/∴/g, '∴');
  25. str = str.replace(/∼/g, '∼');
  26. str = str.replace(/≅/g, '≅');
  27. str = str.replace(/≈/g, '≈');
  28. str = str.replace(/≠/g, '≠');
  29. str = str.replace(/≤/g, '≤');
  30. str = str.replace(/≥/g, '≥');
  31. str = str.replace(/⊂/g, '⊂');
  32. str = str.replace(/⊃/g, '⊃');
  33. str = str.replace(/⊄/g, '⊄');
  34. str = str.replace(/⊆/g, '⊆');
  35. str = str.replace(/⊇/g, '⊇');
  36. str = str.replace(/⊕/g, '⊕');
  37. str = str.replace(/⊗/g, '⊗');
  38. str = str.replace(/⊥/g, '⊥');
  39. str = str.replace(/⋅/g, '⋅');
  40. return str;
  41. }
  42. //HTML 支持的希腊字母
  43. function strGreeceDiscode(str){
  44. str = str.replace(/Α/g, 'Α');
  45. str = str.replace(/Β/g, 'Β');
  46. str = str.replace(/Γ/g, 'Γ');
  47. str = str.replace(/Δ/g, 'Δ');
  48. str = str.replace(/Ε/g, 'Ε');
  49. str = str.replace(/Ζ/g, 'Ζ');
  50. str = str.replace(/Η/g, 'Η');
  51. str = str.replace(/Θ/g, 'Θ');
  52. str = str.replace(/Ι/g, 'Ι');
  53. str = str.replace(/Κ/g, 'Κ');
  54. str = str.replace(/Λ/g, 'Λ');
  55. str = str.replace(/Μ/g, 'Μ');
  56. str = str.replace(/Ν/g, 'Ν');
  57. str = str.replace(/Ξ/g, 'Ν');
  58. str = str.replace(/Ο/g, 'Ο');
  59. str = str.replace(/Π/g, 'Π');
  60. str = str.replace(/Ρ/g, 'Ρ');
  61. str = str.replace(/Σ/g, 'Σ');
  62. str = str.replace(/Τ/g, 'Τ');
  63. str = str.replace(/Υ/g, 'Υ');
  64. str = str.replace(/Φ/g, 'Φ');
  65. str = str.replace(/Χ/g, 'Χ');
  66. str = str.replace(/Ψ/g, 'Ψ');
  67. str = str.replace(/Ω/g, 'Ω');
  68. str = str.replace(/α/g, 'α');
  69. str = str.replace(/β/g, 'β');
  70. str = str.replace(/γ/g, 'γ');
  71. str = str.replace(/δ/g, 'δ');
  72. str = str.replace(/ε/g, 'ε');
  73. str = str.replace(/ζ/g, 'ζ');
  74. str = str.replace(/η/g, 'η');
  75. str = str.replace(/θ/g, 'θ');
  76. str = str.replace(/ι/g, 'ι');
  77. str = str.replace(/κ/g, 'κ');
  78. str = str.replace(/λ/g, 'λ');
  79. str = str.replace(/μ/g, 'μ');
  80. str = str.replace(/ν/g, 'ν');
  81. str = str.replace(/ξ/g, 'ξ');
  82. str = str.replace(/ο/g, 'ο');
  83. str = str.replace(/π/g, 'π');
  84. str = str.replace(/ρ/g, 'ρ');
  85. str = str.replace(/ς/g, 'ς');
  86. str = str.replace(/σ/g, 'σ');
  87. str = str.replace(/τ/g, 'τ');
  88. str = str.replace(/υ/g, 'υ');
  89. str = str.replace(/φ/g, 'φ');
  90. str = str.replace(/χ/g, 'χ');
  91. str = str.replace(/ψ/g, 'ψ');
  92. str = str.replace(/ω/g, 'ω');
  93. str = str.replace(/ϑ/g, 'ϑ');
  94. str = str.replace(/ϒ/g, 'ϒ');
  95. str = str.replace(/ϖ/g, 'ϖ');
  96. str = str.replace(/·/g, '·');
  97. return str;
  98. }
  99. //
  100. function strcharacterDiscode(str){
  101. // 加入常用解析
  102. str = str.replace(/ /g, ' ');
  103. str = str.replace(/"/g, "'");
  104. str = str.replace(/&/g, '&');
  105. // str = str.replace(/</g, '‹');
  106. // str = str.replace(/>/g, '›');
  107. str = str.replace(/&lt;/g, '<');
  108. str = str.replace(/&gt;/g, '>');
  109. str = str.replace(/&#8226;/g, '•');
  110. return str;
  111. }
  112. // HTML 支持的其他实体
  113. function strOtherDiscode(str){
  114. str = str.replace(/&OElig;/g, 'Œ');
  115. str = str.replace(/&oelig;/g, 'œ');
  116. str = str.replace(/&Scaron;/g, 'Š');
  117. str = str.replace(/&scaron;/g, 'š');
  118. str = str.replace(/&Yuml;/g, 'Ÿ');
  119. str = str.replace(/&fnof;/g, 'ƒ');
  120. str = str.replace(/&circ;/g, 'ˆ');
  121. str = str.replace(/&tilde;/g, '˜');
  122. str = str.replace(/&ensp;/g, '');
  123. str = str.replace(/&emsp;/g, '');
  124. str = str.replace(/&thinsp;/g, '');
  125. str = str.replace(/&zwnj;/g, '');
  126. str = str.replace(/&zwj;/g, '');
  127. str = str.replace(/&lrm;/g, '');
  128. str = str.replace(/&rlm;/g, '');
  129. str = str.replace(/&ndash;/g, '–');
  130. str = str.replace(/&mdash;/g, '—');
  131. str = str.replace(/&lsquo;/g, '‘');
  132. str = str.replace(/&rsquo;/g, '’');
  133. str = str.replace(/&sbquo;/g, '‚');
  134. str = str.replace(/&ldquo;/g, '“');
  135. str = str.replace(/&rdquo;/g, '”');
  136. str = str.replace(/&bdquo;/g, '„');
  137. str = str.replace(/&dagger;/g, '†');
  138. str = str.replace(/&Dagger;/g, '‡');
  139. str = str.replace(/&bull;/g, '•');
  140. str = str.replace(/&hellip;/g, '…');
  141. str = str.replace(/&permil;/g, '‰');
  142. str = str.replace(/&prime;/g, '′');
  143. str = str.replace(/&Prime;/g, '″');
  144. str = str.replace(/&lsaquo;/g, '‹');
  145. str = str.replace(/&rsaquo;/g, '›');
  146. str = str.replace(/&oline;/g, '‾');
  147. str = str.replace(/&euro;/g, '€');
  148. str = str.replace(/&trade;/g, '™');
  149. str = str.replace(/&larr;/g, '←');
  150. str = str.replace(/&uarr;/g, '↑');
  151. str = str.replace(/&rarr;/g, '→');
  152. str = str.replace(/&darr;/g, '↓');
  153. str = str.replace(/&harr;/g, '↔');
  154. str = str.replace(/&crarr;/g, '↵');
  155. str = str.replace(/&lceil;/g, '⌈');
  156. str = str.replace(/&rceil;/g, '⌉');
  157. str = str.replace(/&lfloor;/g, '⌊');
  158. str = str.replace(/&rfloor;/g, '⌋');
  159. str = str.replace(/&loz;/g, '◊');
  160. str = str.replace(/&spades;/g, '♠');
  161. str = str.replace(/&clubs;/g, '♣');
  162. str = str.replace(/&hearts;/g, '♥');
  163. str = str.replace(/&diams;/g, '♦');
  164. str = str.replace(/&#39;/g, '\'');
  165. return str;
  166. }
  167. function strMoreDiscode(str){
  168. str = str.replace(/\r\n/g,"");
  169. str = str.replace(/\n/g,"");
  170. str = str.replace(/code/g,"wxxxcode-style");
  171. return str;
  172. }
  173. function strDiscode(str){
  174. str = strNumDiscode(str);
  175. str = strGreeceDiscode(str);
  176. str = strcharacterDiscode(str);
  177. str = strOtherDiscode(str);
  178. str = strMoreDiscode(str);
  179. return str;
  180. }
  181. function urlToHttpUrl(url,rep){
  182. var patt1 = new RegExp("^//");
  183. var result = patt1.test(url);
  184. if(result){
  185. url = rep+":"+url;
  186. }
  187. return url;
  188. }
  189. module.exports = {
  190. strDiscode:strDiscode,
  191. urlToHttpUrl:urlToHttpUrl
  192. }