prism-wpd.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. (function(){
  2. if (!self.Prism) {
  3. return;
  4. }
  5. if (Prism.languages.css) {
  6. Prism.languages.css.atrule.inside['atrule-id'] = /^@[\w-]+/;
  7. // check whether the selector is an advanced pattern before extending it
  8. if (Prism.languages.css.selector.pattern)
  9. {
  10. Prism.languages.css.selector.inside['pseudo-class'] = /:[\w-]+/;
  11. Prism.languages.css.selector.inside['pseudo-element'] = /::[\w-]+/;
  12. }
  13. else
  14. {
  15. Prism.languages.css.selector = {
  16. pattern: Prism.languages.css.selector,
  17. inside: {
  18. 'pseudo-class': /:[\w-]+/,
  19. 'pseudo-element': /::[\w-]+/
  20. }
  21. };
  22. }
  23. }
  24. if (Prism.languages.markup) {
  25. Prism.languages.markup.tag.inside.tag.inside['tag-id'] = /[\w-]+/;
  26. var Tags = {
  27. HTML: {
  28. 'a': 1, 'abbr': 1, 'acronym': 1, 'b': 1, 'basefont': 1, 'bdo': 1, 'big': 1, 'blink': 1, 'cite': 1, 'code': 1, 'dfn': 1, 'em': 1, 'kbd': 1, 'i': 1,
  29. 'rp': 1, 'rt': 1, 'ruby': 1, 's': 1, 'samp': 1, 'small': 1, 'spacer': 1, 'strike': 1, 'strong': 1, 'sub': 1, 'sup': 1, 'time': 1, 'tt': 1, 'u': 1,
  30. 'var': 1, 'wbr': 1, 'noframes': 1, 'summary': 1, 'command': 1, 'dt': 1, 'dd': 1, 'figure': 1, 'figcaption': 1, 'center': 1, 'section': 1, 'nav': 1,
  31. 'article': 1, 'aside': 1, 'hgroup': 1, 'header': 1, 'footer': 1, 'address': 1, 'noscript': 1, 'isIndex': 1, 'main': 1, 'mark': 1, 'marquee': 1,
  32. 'meter': 1, 'menu': 1
  33. },
  34. SVG: {
  35. 'animateColor': 1, 'animateMotion': 1, 'animateTransform': 1, 'glyph': 1, 'feBlend': 1, 'feColorMatrix': 1, 'feComponentTransfer': 1,
  36. 'feFuncR': 1, 'feFuncG': 1, 'feFuncB': 1, 'feFuncA': 1, 'feComposite': 1, 'feConvolveMatrix': 1, 'feDiffuseLighting': 1, 'feDisplacementMap': 1,
  37. 'feFlood': 1, 'feGaussianBlur': 1, 'feImage': 1, 'feMerge': 1, 'feMergeNode': 1, 'feMorphology': 1, 'feOffset': 1, 'feSpecularLighting': 1,
  38. 'feTile': 1, 'feTurbulence': 1, 'feDistantLight': 1, 'fePointLight': 1, 'feSpotLight': 1, 'linearGradient': 1, 'radialGradient': 1, 'altGlyph': 1,
  39. 'textPath': 1, 'tref': 1, 'altglyph': 1, 'textpath': 1, 'tref': 1, 'altglyphdef': 1, 'altglyphitem': 1, 'clipPath': 1, 'color-profile': 1, 'cursor': 1,
  40. 'font-face': 1, 'font-face-format': 1, 'font-face-name': 1, 'font-face-src': 1, 'font-face-uri': 1, 'foreignObject': 1, 'glyph': 1, 'glyphRef': 1,
  41. 'hkern': 1, 'vkern': 1,
  42. },
  43. MathML: {}
  44. }
  45. }
  46. var language;
  47. Prism.hooks.add('wrap', function(env) {
  48. if ((['tag-id'].indexOf(env.type) > -1
  49. || (env.type == 'property' && env.content.indexOf('-') != 0)
  50. || (env.type == 'atrule-id'&& env.content.indexOf('@-') != 0)
  51. || (env.type == 'pseudo-class'&& env.content.indexOf(':-') != 0)
  52. || (env.type == 'pseudo-element'&& env.content.indexOf('::-') != 0)
  53. || (env.type == 'attr-name' && env.content.indexOf('data-') != 0)
  54. ) && env.content.indexOf('<') === -1
  55. ) {
  56. var searchURL = 'w/index.php?fulltext&search=';
  57. env.tag = 'a';
  58. var href = 'http://docs.webplatform.org/';
  59. if (env.language == 'css') {
  60. href += 'wiki/css/'
  61. if (env.type == 'property') {
  62. href += 'properties/';
  63. }
  64. else if (env.type == 'atrule-id') {
  65. href += 'atrules/';
  66. }
  67. else if (env.type == 'pseudo-class') {
  68. href += 'selectors/pseudo-classes/';
  69. }
  70. else if (env.type == 'pseudo-element') {
  71. href += 'selectors/pseudo-elements/';
  72. }
  73. }
  74. else if (env.language == 'markup') {
  75. if (env.type == 'tag-id') {
  76. // Check language
  77. language = getLanguage(env.content) || language;
  78. if (language) {
  79. href += 'wiki/' + language + '/elements/';
  80. }
  81. else {
  82. href += searchURL;
  83. }
  84. }
  85. else if (env.type == 'attr-name') {
  86. if (language) {
  87. href += 'wiki/' + language + '/attributes/';
  88. }
  89. else {
  90. href += searchURL;
  91. }
  92. }
  93. }
  94. href += env.content;
  95. env.attributes.href = href;
  96. env.attributes.target = '_blank';
  97. }
  98. });
  99. function getLanguage(tag) {
  100. var tagL = tag.toLowerCase();
  101. if (Tags.HTML[tagL]) {
  102. return 'html';
  103. }
  104. else if (Tags.SVG[tag]) {
  105. return 'svg';
  106. }
  107. else if (Tags.MathML[tag]) {
  108. return 'mathml';
  109. }
  110. // Not in dictionary, perform check
  111. if (Tags.HTML[tagL] !== 0) {
  112. var htmlInterface = (document.createElement(tag).toString().match(/\[object HTML(.+)Element\]/) || [])[1];
  113. if (htmlInterface && htmlInterface != 'Unknown') {
  114. Tags.HTML[tagL] = 1;
  115. return 'html';
  116. }
  117. }
  118. Tags.HTML[tagL] = 0;
  119. if (Tags.SVG[tag] !== 0) {
  120. var svgInterface = (document.createElementNS('http://www.w3.org/2000/svg', tag).toString().match(/\[object SVG(.+)Element\]/) || [])[1];
  121. if (svgInterface && svgInterface != 'Unknown') {
  122. Tags.SVG[tag] = 1;
  123. return 'svg';
  124. }
  125. }
  126. Tags.SVG[tag] = 0;
  127. // Lame way to detect MathML, but browsers don’t expose interface names there :(
  128. if (Tags.MathML[tag] !== 0) {
  129. if (tag.indexOf('m') === 0) {
  130. Tags.MathML[tag] = 1;
  131. return 'mathml';
  132. }
  133. }
  134. Tags.MathML[tag] = 0;
  135. return null;
  136. }
  137. })();