119548cef579a328676614e9707b9e4cc06b4a8a0ba99f4199f7a64dd98477e451598b6203eb8c4e03e4f4e1449875b275395f74a5e50e0e95b9d761aff616 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*jshint node:true */
  2. /*
  3. The MIT License (MIT)
  4. Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
  5. Permission is hereby granted, free of charge, to any person
  6. obtaining a copy of this software and associated documentation files
  7. (the "Software"), to deal in the Software without restriction,
  8. including without limitation the rights to use, copy, modify, merge,
  9. publish, distribute, sublicense, and/or sell copies of the Software,
  10. and to permit persons to whom the Software is furnished to do so,
  11. subject to the following conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  18. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  19. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. SOFTWARE.
  22. */
  23. 'use strict';
  24. var Pattern = require('./pattern').Pattern;
  25. var template_names = {
  26. django: false,
  27. erb: false,
  28. handlebars: false,
  29. php: false
  30. };
  31. // This lets templates appear anywhere we would do a readUntil
  32. // The cost is higher but it is pay to play.
  33. function TemplatablePattern(input_scanner, parent) {
  34. Pattern.call(this, input_scanner, parent);
  35. this.__template_pattern = null;
  36. this._disabled = Object.assign({}, template_names);
  37. this._excluded = Object.assign({}, template_names);
  38. if (parent) {
  39. this.__template_pattern = this._input.get_regexp(parent.__template_pattern);
  40. this._excluded = Object.assign(this._excluded, parent._excluded);
  41. this._disabled = Object.assign(this._disabled, parent._disabled);
  42. }
  43. var pattern = new Pattern(input_scanner);
  44. this.__patterns = {
  45. handlebars_comment: pattern.starting_with(/{{!--/).until_after(/--}}/),
  46. handlebars_unescaped: pattern.starting_with(/{{{/).until_after(/}}}/),
  47. handlebars: pattern.starting_with(/{{/).until_after(/}}/),
  48. php: pattern.starting_with(/<\?(?:[=]|php)/).until_after(/\?>/),
  49. erb: pattern.starting_with(/<%[^%]/).until_after(/[^%]%>/),
  50. // django coflicts with handlebars a bit.
  51. django: pattern.starting_with(/{%/).until_after(/%}/),
  52. django_value: pattern.starting_with(/{{/).until_after(/}}/),
  53. django_comment: pattern.starting_with(/{#/).until_after(/#}/)
  54. };
  55. }
  56. TemplatablePattern.prototype = new Pattern();
  57. TemplatablePattern.prototype._create = function() {
  58. return new TemplatablePattern(this._input, this);
  59. };
  60. TemplatablePattern.prototype._update = function() {
  61. this.__set_templated_pattern();
  62. };
  63. TemplatablePattern.prototype.disable = function(language) {
  64. var result = this._create();
  65. result._disabled[language] = true;
  66. result._update();
  67. return result;
  68. };
  69. TemplatablePattern.prototype.read_options = function(options) {
  70. var result = this._create();
  71. for (var language in template_names) {
  72. result._disabled[language] = options.templating.indexOf(language) === -1;
  73. }
  74. result._update();
  75. return result;
  76. };
  77. TemplatablePattern.prototype.exclude = function(language) {
  78. var result = this._create();
  79. result._excluded[language] = true;
  80. result._update();
  81. return result;
  82. };
  83. TemplatablePattern.prototype.read = function() {
  84. var result = '';
  85. if (this._match_pattern) {
  86. result = this._input.read(this._starting_pattern);
  87. } else {
  88. result = this._input.read(this._starting_pattern, this.__template_pattern);
  89. }
  90. var next = this._read_template();
  91. while (next) {
  92. if (this._match_pattern) {
  93. next += this._input.read(this._match_pattern);
  94. } else {
  95. next += this._input.readUntil(this.__template_pattern);
  96. }
  97. result += next;
  98. next = this._read_template();
  99. }
  100. if (this._until_after) {
  101. result += this._input.readUntilAfter(this._until_pattern);
  102. }
  103. return result;
  104. };
  105. TemplatablePattern.prototype.__set_templated_pattern = function() {
  106. var items = [];
  107. if (!this._disabled.php) {
  108. items.push(this.__patterns.php._starting_pattern.source);
  109. }
  110. if (!this._disabled.handlebars) {
  111. items.push(this.__patterns.handlebars._starting_pattern.source);
  112. }
  113. if (!this._disabled.erb) {
  114. items.push(this.__patterns.erb._starting_pattern.source);
  115. }
  116. if (!this._disabled.django) {
  117. items.push(this.__patterns.django._starting_pattern.source);
  118. items.push(this.__patterns.django_value._starting_pattern.source);
  119. items.push(this.__patterns.django_comment._starting_pattern.source);
  120. }
  121. if (this._until_pattern) {
  122. items.push(this._until_pattern.source);
  123. }
  124. this.__template_pattern = this._input.get_regexp('(?:' + items.join('|') + ')');
  125. };
  126. TemplatablePattern.prototype._read_template = function() {
  127. var resulting_string = '';
  128. var c = this._input.peek();
  129. if (c === '<') {
  130. var peek1 = this._input.peek(1);
  131. //if we're in a comment, do something special
  132. // We treat all comments as literals, even more than preformatted tags
  133. // we just look for the appropriate close tag
  134. if (!this._disabled.php && !this._excluded.php && peek1 === '?') {
  135. resulting_string = resulting_string ||
  136. this.__patterns.php.read();
  137. }
  138. if (!this._disabled.erb && !this._excluded.erb && peek1 === '%') {
  139. resulting_string = resulting_string ||
  140. this.__patterns.erb.read();
  141. }
  142. } else if (c === '{') {
  143. if (!this._disabled.handlebars && !this._excluded.handlebars) {
  144. resulting_string = resulting_string ||
  145. this.__patterns.handlebars_comment.read();
  146. resulting_string = resulting_string ||
  147. this.__patterns.handlebars_unescaped.read();
  148. resulting_string = resulting_string ||
  149. this.__patterns.handlebars.read();
  150. }
  151. if (!this._disabled.django) {
  152. // django coflicts with handlebars a bit.
  153. if (!this._excluded.django && !this._excluded.handlebars) {
  154. resulting_string = resulting_string ||
  155. this.__patterns.django_value.read();
  156. }
  157. if (!this._excluded.django) {
  158. resulting_string = resulting_string ||
  159. this.__patterns.django_comment.read();
  160. resulting_string = resulting_string ||
  161. this.__patterns.django.read();
  162. }
  163. }
  164. }
  165. return resulting_string;
  166. };
  167. module.exports.TemplatablePattern = TemplatablePattern;