Anonymous avatar Anonymous committed 45ae020

adding lexer for JBST markup with JavaScript code blocks

Comments (0)

Files changed (3)

pygments/lexers/web.py

 __all__ = ['HtmlLexer', 'XmlLexer', 'JavascriptLexer', 'CssLexer',
            'PhpLexer', 'ActionScriptLexer', 'XsltLexer', 'ActionScript3Lexer',
            'MxmlLexer', 'HaxeLexer', 'HamlLexer', 'SassLexer', 'ScssLexer',
-           'ObjectiveJLexer', 'CoffeeScriptLexer']
+           'ObjectiveJLexer', 'CoffeeScriptLexer', 'JbstLexer']
 
 
 class JavascriptLexer(RegexLexer):
             (r"'(\\\\|\\'|[^'])*'", String.Single),
         ]
     }
+
+class JbstLexer(RegexLexer):
+    """
+    Lexer for JsonML+Browser-Side Template (JSBT) markup with JavaScript code blocks.
+    http://jsonml.org/jbst/
+    """
+
+    name = 'JBST'
+    aliases = ['jbst', 'JsonML+BST']
+    filenames = ['*.jbst']
+    mimetypes = ['text/x-jbst']
+
+    flags = re.DOTALL
+
+    tokens = {
+        'root': [
+            (r'(<%[@=#!:]?)(.*?)(%>)', bygroups(Name.Tag, JavascriptLexer, Name.Tag)),
+            (r'(<%\$)(.*?)(:)(.*?)(%>)', bygroups(Name.Tag, Name.Function, Punctuation, String, Name.Tag)),
+            (r'(<%--)(.*?)(--%>)', bygroups(Name.Tag, Comment.Multiline, Name.Tag)),
+            (r'(<script.*?>)(.*?)(</script>)', bygroups(using(HtmlLexer),
+                                                        JavascriptLexer,
+                                                        using(HtmlLexer))),
+            (r'(.+?)(?=<)', using(HtmlLexer)),
+            (r'.+', using(HtmlLexer)),
+        ],
+    }

tests/examplefiles/jbst_example1.jbst

+<%@ Control Name="MyApp.MyJbstControl" Language="JavaScript" %>
+
+<script type="text/javascript">
+
+	/* initialization code block, executed only once as control is loaded */
+	this.generateValue = function() {
+		return new Date().toString();
+	};
+
+</script>
+
+<%!
+	/* initialization code block, executed only once as control is loaded */
+	/* alternate syntax to script block above */
+	this.myInitTime = this.generateValue();
+%>
+
+<%
+	/* data binding code block, executed each time as control is data bound */
+	this.myBindTime = this.generateValue();
+%>
+
+<%-- JBST Comment --%>
+<span style="color:red"><%= this.myBindTime /* data binding expression */ %></span>
+<span style="color:green"><%= this.myInitTime /* data binding expression */ %></span>
+
+<!-- HTML Comment -->
+<span style="color:blue"><%$ Resources: localizationKey %><%-- JBST globalization--%></span>

tests/examplefiles/jbst_example2.jbst

+<%@ Control Name="Foo.MyZebraList" Language="JavaScript" %>
+
+<script type="text/javascript">
+
+	/* private members ------------------------------------------ */
+
+	/*int*/ function digits(/*int*/ n) {
+		return (n < 10) ? '0' + n : n;
+	}
+
+	/* public members ------------------------------------------- */
+
+	// use the item index to alternate colors and highlight
+	/*string*/ this.zebraStripe = function(/*bool*/ selected, /*int*/ index, /*int*/ count) {
+		var css = [ "item" ];
+		if (index % 2 === 0) {
+			css.push("item-alt");
+		}
+		if (selected) {
+			css.push("item-selected");
+		}
+		return css.join(" ");
+	};
+
+	/*string*/ this.formatTime = function(/*Date*/ time) {
+		return time.getHours() + ':' + digits(time.getMinutes()) + ':' + digits(time.getSeconds());
+	};
+
+</script>
+
+<div class="example">
+	<h2><%= this.data.title %> as of <%= this.formatTime(this.data.timestamp) %>!</h2>
+	<p><%= this.data.description %></p>
+	<ul class="items" jbst:visible="<%= this.data.children.length > 0 %>">
+
+		<!-- anonymous inner template -->
+		<jbst:control data="<%= this.data.children %>">
+			<!-- populate list item for each item of the parent's children property -->
+			<li class="<%= Foo.MyZebraList.zebraStripe(this.data.selected, this.index, this.count) %>">
+				<%= this.data.label %> (<%= this.index+1 %> of <%= this.count %>)
+			</li>
+		</jbst:control>
+
+	</ul>
+</div>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.