Commit 4a3ee71cdea94b81b6f96808df66ffd392948a8a

Lea Verou 2013-06-16T09:16:43

Merge pull request #112 from danielgtaylor/gh-pages Add highlighting support for raw HTTP

diff --git a/components.js b/components.js
index 5cb06e9..eb818b0 100644
--- a/components.js
+++ b/components.js
@@ -99,6 +99,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 bf9d468..cdb4f3a 100644
--- a/examples.html
+++ b/examples.html
@@ -419,6 +419,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.