api_doc_viewer 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 25c2ca3661218734a8b9cbf73ea26f37439670eb
4
- data.tar.gz: c74bb775676baaa917b630144748afd5bd971bc6
3
+ metadata.gz: bd19e929a151817bf18a84741bb67b7ecf35ca59
4
+ data.tar.gz: 6ecf6fa47cec026d70c86b70882209a9d7e86a80
5
5
  SHA512:
6
- metadata.gz: 0b4521dc23aa928c4dc37f2c3a33f5b1c7783484203fa309cd960ae8ceb91de17dee528f07403b21260f7f2d84aaf4db9e8e6d05032ee44491e3cb33f9f209ea
7
- data.tar.gz: 3b6888920a487570fee74909ec648360f8633996723a906e91d5ada8fca31c78ad1e2acf90b1edd97b0b350ea834302863f57d7141680e86ddd46093d58daeb1
6
+ metadata.gz: 89b93415c7d71a8361602a31aae026025b4843c7996b96338fd22ea3dbf70987730a71db05c568191bf604277581bd96ee32f093d18881a825167d6b35bd53c4
7
+ data.tar.gz: cea5cfe932b8d158b319b1fd8d4e08dd4e55253314f43e222ef33cf6974bdfa49d8ca7b9e440ad1be20f1b124c7af1e3ad353dc24c3a689d4666c320e6325a47
@@ -8,25 +8,17 @@ angular.module('controllers').controller 'mainController', ['$scope', 'mainServi
8
8
  $scope.activeLink = link
9
9
 
10
10
  mainService.resourceLink(link).get (data) ->
11
+ mainService.formatData(data)
12
+
11
13
  $scope.parameters = data.parameters
12
14
  $scope.request =
13
15
  httpMethod: data.http_method
14
16
  route: data.route
15
- headers: filterHeaders data.requests[0].request_headers
16
- body: formatJSONWithSpaces data.requests[0].request_body
17
+ headers: data.request_headers
18
+ body: data.request_body
17
19
  $scope.response =
18
20
  status: data.requests[0].response_status
19
21
  status_text: data.requests[0].response_status_text
20
- headers: filterHeaders data.requests[0].response_headers
21
- body: formatJSONWithSpaces data.requests[0].response_body
22
+ headers: data.response_headers
23
+ body: data.response_body
22
24
  ]
23
-
24
- formatJSONWithSpaces = (data) ->
25
- if data && data.trim().match(/^[\[\{].*[\}\]]$/)
26
- JSON.stringify(JSON.parse(data), undefined, 2);
27
-
28
- filterHeaders = (headers) ->
29
- delete headers['Origin']
30
- if Object.keys(headers).length > 0
31
- headers
32
-
@@ -1,10 +1,28 @@
1
1
  angular.module('services').factory 'mainService', ['$resource', ($resource) ->
2
- return {
3
- resourceLink: (link = null) ->
4
- urlSuffix = if link? then "?link=#{link}" else ''
5
- $resource "#{location.pathname}/documentation#{urlSuffix}", {},
6
- get:
7
- method: 'GET'
8
- cache: true
9
- }
2
+ return {
3
+ hiddenHeaders: ['Origin', 'Host', 'Cookie', 'X-Frame-Options', 'X-XSS-Protection', 'X-Content-Type-Options', 'X-UA-Compatible', 'ETag', 'Cache-Control', 'X-Request-Id', 'X-Runtime', 'Content-Length']
4
+ resourceLink: (link = null) ->
5
+ urlSuffix = if link? then "?link=#{link}" else ''
6
+ $resource "#{location.pathname}/documentation#{urlSuffix}", {},
7
+ get:
8
+ method: 'GET'
9
+ cache: true
10
+ formatData: (data) ->
11
+ data.request_headers = filterHeaders(data.requests[0].request_headers, @hiddenHeaders)
12
+ data.response_headers = filterHeaders(data.requests[0].response_headers, @hiddenHeaders)
13
+ data.request_body = formatBody(data.requests[0].request_body, true)
14
+ data.response_body = formatBody(data.requests[0].response_body)
15
+ }
10
16
  ]
17
+
18
+ filterHeaders = (headers, hiddenHeaders) ->
19
+ for headerKey in hiddenHeaders
20
+ delete headers[headerKey]
21
+ if Object.keys(headers).length > 0 then headers
22
+
23
+ formatBody = (body, deparam = false) ->
24
+ return unless body
25
+ if deparam
26
+ body = JSON.stringify($.deparam(body))
27
+ if body.match(/^[\[\{].*[\}\]]$/)
28
+ JSON.stringify(JSON.parse(body), undefined, 2)
@@ -1,7 +1,4 @@
1
- app = angular.module('asdf', [
2
- 'controllers'
3
- 'services'
4
- ])
1
+ app = angular.module('apiDocViewer', ['controllers', 'services', 'hljs'])
5
2
 
6
3
  angular.module('controllers', [])
7
4
  angular.module('services', ['ngResource'])
@@ -0,0 +1,287 @@
1
+ // AngularJS directive for syntax highlighting with highlight.js
2
+ // Source: https://github.com/pc035860/angular-highlightjs
3
+
4
+ /*global angular*/
5
+ angular.module('hljs', [])
6
+
7
+ .provider('hljsService', function () {
8
+ var _hljsOptions = {};
9
+
10
+ return {
11
+ setOptions: function (options) {
12
+ angular.extend(_hljsOptions, options);
13
+ },
14
+ getOptions: function () {
15
+ return angular.copy(_hljsOptions);
16
+ },
17
+ $get: ['$window', function ($window) {
18
+ ($window.hljs.configure || angular.noop)(_hljsOptions);
19
+ return $window.hljs;
20
+ }]
21
+ };
22
+ })
23
+
24
+ .factory('hljsCache', [
25
+ '$cacheFactory',
26
+ function ($cacheFactory) {
27
+ return $cacheFactory('hljsCache');
28
+ }])
29
+
30
+ .controller('HljsCtrl', [
31
+ 'hljsCache', 'hljsService',
32
+ function HljsCtrl (hljsCache, hljsService) {
33
+ var ctrl = this;
34
+
35
+ var _elm = null,
36
+ _lang = null,
37
+ _code = null,
38
+ _hlCb = null;
39
+
40
+ ctrl.init = function (codeElm) {
41
+ _elm = codeElm;
42
+ };
43
+
44
+ ctrl.setLanguage = function (lang) {
45
+ _lang = lang;
46
+
47
+ if (_code) {
48
+ ctrl.highlight(_code);
49
+ }
50
+ };
51
+
52
+ ctrl.highlightCallback = function (cb) {
53
+ _hlCb = cb;
54
+ };
55
+
56
+ ctrl.highlight = function (code) {
57
+ if (!_elm) {
58
+ return;
59
+ }
60
+
61
+ var res, cacheKey;
62
+
63
+ _code = code;
64
+
65
+ if (_lang) {
66
+ // language specified
67
+ cacheKey = ctrl._cacheKey(_lang, _code);
68
+ res = hljsCache.get(cacheKey);
69
+
70
+ if (!res) {
71
+ res = hljsService.highlight(_lang, hljsService.fixMarkup(_code), true);
72
+ hljsCache.put(cacheKey, res);
73
+ }
74
+ }
75
+ else {
76
+ // language auto-detect
77
+ cacheKey = ctrl._cacheKey(_code);
78
+ res = hljsCache.get(cacheKey);
79
+
80
+ if (!res) {
81
+ res = hljsService.highlightAuto(hljsService.fixMarkup(_code));
82
+ hljsCache.put(cacheKey, res);
83
+ }
84
+ }
85
+
86
+ _elm.html(res.value);
87
+ // language as class on the <code> tag
88
+ _elm.addClass(res.language);
89
+
90
+ if (_hlCb !== null && angular.isFunction(_hlCb)) {
91
+ _hlCb();
92
+ }
93
+ };
94
+
95
+ ctrl.clear = function () {
96
+ if (!_elm) {
97
+ return;
98
+ }
99
+ _code = null;
100
+ _elm.text('');
101
+ };
102
+
103
+ ctrl.release = function () {
104
+ _elm = null;
105
+ };
106
+
107
+ ctrl._cacheKey = function () {
108
+ var args = Array.prototype.slice.call(arguments),
109
+ glue = "!angular-highlightjs!";
110
+ return args.join(glue);
111
+ };
112
+ }])
113
+
114
+ .directive('hljs', ['$compile', '$parse', function ($compile, $parse) {
115
+ return {
116
+ restrict: 'EA',
117
+ controller: 'HljsCtrl',
118
+ compile: function(tElm, tAttrs, transclude) {
119
+ // get static code
120
+ // strip the starting "new line" character
121
+ var staticCode = tElm[0].innerHTML.replace(/^(\r\n|\r|\n)/m, '');
122
+
123
+ // put template
124
+ tElm.html('<pre><code class="hljs"></code></pre>');
125
+
126
+ return function postLink(scope, iElm, iAttrs, ctrl) {
127
+ var compileCheck;
128
+
129
+ if (angular.isDefined(iAttrs.compile)) {
130
+ compileCheck = $parse(iAttrs.compile);
131
+ }
132
+
133
+ ctrl.init(iElm.find('code'));
134
+
135
+ if (iAttrs.onhighlight) {
136
+ ctrl.highlightCallback(function () {
137
+ scope.$eval(iAttrs.onhighlight);
138
+ });
139
+ }
140
+
141
+ if (staticCode) {
142
+ ctrl.highlight(staticCode);
143
+
144
+ // Check if the highlight result needs to be compiled
145
+ if (compileCheck && compileCheck(scope)) {
146
+ // compile the new DOM and link it to the current scope.
147
+ // NOTE: we only compile .childNodes so that
148
+ // we don't get into infinite loop compiling ourselves
149
+ $compile(iElm.find('code').contents())(scope);
150
+ }
151
+ }
152
+
153
+ scope.$on('$destroy', function () {
154
+ ctrl.release();
155
+ });
156
+ };
157
+ }
158
+ };
159
+ }])
160
+
161
+ .directive('language', [function () {
162
+ return {
163
+ require: 'hljs',
164
+ restrict: 'A',
165
+ link: function (scope, iElm, iAttrs, ctrl) {
166
+ iAttrs.$observe('language', function (lang) {
167
+ if (angular.isDefined(lang)) {
168
+ ctrl.setLanguage(lang);
169
+ }
170
+ });
171
+ }
172
+ };
173
+ }])
174
+
175
+ .directive('source', ['$compile', '$parse', function ($compile, $parse) {
176
+ return {
177
+ require: 'hljs',
178
+ restrict: 'A',
179
+ link: function(scope, iElm, iAttrs, ctrl) {
180
+ var compileCheck;
181
+
182
+ if (angular.isDefined(iAttrs.compile)) {
183
+ compileCheck = $parse(iAttrs.compile);
184
+ }
185
+
186
+ scope.$watch(iAttrs.source, function (newCode, oldCode) {
187
+ if (newCode) {
188
+ ctrl.highlight(newCode);
189
+
190
+ // Check if the highlight result needs to be compiled
191
+ if (compileCheck && compileCheck(scope)) {
192
+ // compile the new DOM and link it to the current scope.
193
+ // NOTE: we only compile .childNodes so that
194
+ // we don't get into infinite loop compiling ourselves
195
+ $compile(iElm.find('code').contents())(scope);
196
+ }
197
+ }
198
+ else {
199
+ ctrl.clear();
200
+ }
201
+ });
202
+ }
203
+ };
204
+ }])
205
+
206
+ .directive('include', [
207
+ '$http', '$templateCache', '$q', '$compile', '$parse',
208
+ function ($http, $templateCache, $q, $compile, $parse) {
209
+ return {
210
+ require: 'hljs',
211
+ restrict: 'A',
212
+ compile: function(tElm, tAttrs, transclude) {
213
+ var srcExpr = tAttrs.include;
214
+
215
+ return function postLink(scope, iElm, iAttrs, ctrl) {
216
+ var changeCounter = 0, compileCheck;
217
+
218
+ if (angular.isDefined(iAttrs.compile)) {
219
+ compileCheck = $parse(iAttrs.compile);
220
+ }
221
+
222
+ scope.$watch(srcExpr, function (src) {
223
+ var thisChangeId = ++changeCounter;
224
+
225
+ if (src && angular.isString(src)) {
226
+ var templateCachePromise, dfd;
227
+
228
+ templateCachePromise = $templateCache.get(src);
229
+ if (!templateCachePromise) {
230
+ dfd = $q.defer();
231
+ $http.get(src, {
232
+ cache: $templateCache,
233
+ transformResponse: function(data, headersGetter) {
234
+ // Return the raw string, so $http doesn't parse it
235
+ // if it's json.
236
+ return data;
237
+ }
238
+ }).success(function (code) {
239
+ if (thisChangeId !== changeCounter) {
240
+ return;
241
+ }
242
+ dfd.resolve(code);
243
+ }).error(function() {
244
+ if (thisChangeId === changeCounter) {
245
+ ctrl.clear();
246
+ }
247
+ dfd.resolve();
248
+ });
249
+ templateCachePromise = dfd.promise;
250
+ }
251
+
252
+ $q.when(templateCachePromise)
253
+ .then(function (code) {
254
+ if (!code) {
255
+ return;
256
+ }
257
+
258
+ // $templateCache from $http
259
+ if (angular.isArray(code)) {
260
+ // 1.1.5
261
+ code = code[1];
262
+ }
263
+ else if (angular.isObject(code)) {
264
+ // 1.0.7
265
+ code = code.data;
266
+ }
267
+
268
+ code = code.replace(/^(\r\n|\r|\n)/m, '');
269
+ ctrl.highlight(code);
270
+
271
+ // Check if the highlight result needs to be compiled
272
+ if (compileCheck && compileCheck(scope)) {
273
+ // compile the new DOM and link it to the current scope.
274
+ // NOTE: we only compile .childNodes so that
275
+ // we don't get into infinite loop compiling ourselves
276
+ $compile(iElm.find('code').contents())(scope);
277
+ }
278
+ });
279
+ }
280
+ else {
281
+ ctrl.clear();
282
+ }
283
+ });
284
+ };
285
+ }
286
+ };
287
+ }]);