Merge pull request #112 from danielgtaylor/gh-pages Add highlighting support for raw HTTP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
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
+
+<root>
+ <status code="0">
+ Successful
+ </status>
+</root>
+</code></pre>
+</section>
+
<section id="failures" class="language-javascript">
<h1>Known failures (JavaScript)</h1>
<p>There are certain edge cases where Prism will fail.