Commit 3527ccc790b5849db904b4e5ff9921ce8e4aa2e6

Daniel G. Taylor 2013-06-01T09:24:18

Add support for raw HTTP request and response syntax highlighting; add examples for these on the examples pages.

diff --git a/components.js b/components.js
index 0be9079..c64b8dd 100644
--- a/components.js
+++ b/components.js
@@ -95,6 +95,9 @@ var components = {
 		'groovy': {
 			title: 'Groovy',
 			require: 'clike'
+		},
+		'http': {
+			title: 'HTTP',
 		}
 	}
 };
\ No newline at end of file
diff --git a/components/prism-http.js b/components/prism-http.js
new file mode 100644
index 0000000..c47dba6
--- /dev/null
+++ b/components/prism-http.js
@@ -0,0 +1,44 @@
+Prism.languages.http = {
+    'request-line': {
+        pattern: /^(POST|GET|PUT|DELETE|OPTIONS)\b\shttps?:\/\/\S+\sHTTP\/[0-9.]+/g,
+        inside: {
+            // HTTP Verb
+            property: /^\b(POST|GET|PUT|DELETE|OPTIONS)\b/g,
+            // Path or query argument
+            'attr-name': /:\w+/g
+        }
+    },
+    'response-status': {
+        pattern: /^HTTP\/1.[01] [0-9]+.*/g,
+        inside: {
+            // Status, e.g. 200 OK
+            property: /[0-9]+[A-Z\s-]+$/g
+        }
+    },
+    // HTTP header name
+    keyword: /^[\w-]+:(?=.+)/gm
+};
+
+// Create a mapping of Content-Type headers to language definitions
+var httpLanguages = {
+    'application/json': Prism.languages.javascript,
+    'application/xml': Prism.languages.markup,
+    'text/xml': Prism.languages.markup,
+    'text/html': Prism.languages.markup
+};
+
+// Insert each content type parser that has its associated language
+// currently loaded.
+for (var contentType in httpLanguages) {
+    if (httpLanguages[contentType]) {
+        var options = {};
+        options[contentType] = {
+            pattern: new RegExp('(content-type:\\s*' + contentType + '[\\w\\W]*?)\\n\\n[\\w\\W]*', 'gi'),
+            lookbehind: true,
+            inside: {
+                rest: httpLanguages[contentType]
+            }
+        };
+        Prism.languages.insertBefore('http', 'keyword', options);
+    }
+}
diff --git a/components/prism-http.min.js b/components/prism-http.min.js
new file mode 100644
index 0000000..69824ae
--- /dev/null
+++ b/components/prism-http.min.js
@@ -0,0 +1 @@
+Prism.languages.http={"request-line":{pattern:/^(POST|GET|PUT|DELETE|OPTIONS)\b\shttps?:\/\/\S+\sHTTP\/[0-9.]+/g,inside:{property:/^\b(POST|GET|PUT|DELETE|OPTIONS)\b/g,"attr-name":/:\w+/g}},"response-status":{pattern:/^HTTP\/1.[01] [0-9]+.*/g,inside:{property:/[0-9]+[A-Z\s-]+$/g}},keyword:/^[\w-]+:(?=.+)/gm};var httpLanguages={"application/json":Prism.languages.javascript,"application/xml":Prism.languages.markup,"text/xml":Prism.languages.markup,"text/html":Prism.languages.markup};for(var contentType in httpLanguages){if(httpLanguages[contentType]){var options={};options[contentType]={pattern:new RegExp("(content-type:\\s*"+contentType+"[\\w\\W]*?)\\n\\n[\\w\\W]*","gi"),lookbehind:true,inside:{rest:httpLanguages[contentType]}};Prism.languages.insertBefore("http","keyword",options)}}
\ No newline at end of file
diff --git a/examples.html b/examples.html
index 0b1dd93..c13bb89 100644
--- a/examples.html
+++ b/examples.html
@@ -413,6 +413,54 @@ class CoffeeScript extends Prism.Javascript
 	</code></pre>
 </section>
 
+<section class="language-http">
+	<h1>HTTP</h1>
+
+	<h2>Request Examples</h2>
+	<p>A simple HTTP GET request:</p>
+	<pre><code>GET https://example.tld/v1/:serviceName/users.json?sort=:sortMethod HTTP/1.1</code></pre>
+	<p>Here is an HTTP POST with a body:</p>
+	<pre><code>POST https://example.tld/v1/:serviceName/users.json HTTP/1.1
+Content-Type: application/json
+
+{
+	"names": ["test1", "test2"],
+	"version": 1.0
+}
+</code></pre>
+
+	<h2>Response Examples</h2>
+	<p>JSON response body:</p>
+	<pre><code>HTTP/1.1 200 OK
+Content-Type: application/json
+X-Response-Time: 6ms
+
+{
+	"users": [
+		{
+			"name": "John",
+			"points": 24
+		},
+		{
+			"name": "Lea",
+			"points": 15
+		}
+	]
+}
+</code></pre>
+	<p>XML response body:</p>
+	<pre><code>HTTP/1.1 200 OK
+Content-Type: application/xml
+X-Response-Time: 10ms
+
+&lt;root&gt;
+	&lt;status code="0"&gt;
+		Successful
+	&lt;/status&gt;
+&lt;/root&gt;
+</code></pre>
+</section>
+
 <section id="failures" class="language-javascript">
 	<h1>Known failures (JavaScript)</h1>
 	<p>There are certain edge cases where Prism will fail.