redoc/lib/utils/pipes.js

132 lines
3.6 KiB
JavaScript
Raw Normal View History

2015-10-08 20:16:45 +03:00
'use strict';
2015-12-14 19:31:17 +03:00
import {Pipe} from 'angular2/core';
import {isString, stringify, isBlank} from 'angular2/src/facade/lang';
import {BaseException} from 'angular2/src/facade/exceptions';
2015-10-08 20:16:45 +03:00
import {JsonPointer} from './JsonPointer';
2016-01-31 20:37:35 +03:00
import Prism from 'prismjs';
import 'prismjs/components/prism-actionscript.js';
import 'prismjs/components/prism-c.js';
import 'prismjs/components/prism-cpp.js';
import 'prismjs/components/prism-csharp.js';
import 'prismjs/components/prism-php.js';
import 'prismjs/components/prism-coffeescript.js';
import 'prismjs/components/prism-go.js';
import 'prismjs/components/prism-haskell.js';
2016-02-01 15:47:21 +03:00
//import 'prismjs/components/prism-scala.js';
2016-01-31 20:37:35 +03:00
import 'prismjs/components/prism-java.js';
import 'prismjs/components/prism-lua.js';
import 'prismjs/components/prism-matlab.js';
import 'prismjs/components/prism-objectivec.js';
import 'prismjs/components/prism-perl.js';
import 'prismjs/components/prism-python.js';
import 'prismjs/components/prism-r.js';
import 'prismjs/components/prism-ruby.js';
import 'prismjs/components/prism-bash.js';
import 'prismjs/components/prism-swift.js';
import 'prismjs/components/prism-vim.js';
import 'prismjs/themes/prism-dark.css!css';
import marked from 'marked';
marked.setOptions({
renderer: new marked.Renderer(),
gfm: true,
tables: true,
breaks: false,
pedantic: false,
smartLists: true,
smartypants: false
});
2015-12-14 19:31:17 +03:00
class InvalidPipeArgumentException extends BaseException {
constructor(type, value) {
super(`Invalid argument '${value}' for pipe '${stringify(type)}'`);
}
}
2015-10-08 20:16:45 +03:00
2015-12-14 19:14:14 +03:00
@Pipe({ name: 'keys' })
2015-10-08 20:16:45 +03:00
export class KeysPipe {
2015-12-14 19:31:17 +03:00
transform(value) {
if (isBlank(value)) return value;
if (typeof value !== 'object') {
throw new InvalidPipeArgumentException(ValuesPipe, value);
2015-12-14 19:14:14 +03:00
}
2015-12-14 19:31:17 +03:00
return Object.keys(value);
2015-10-08 20:16:45 +03:00
}
}
2015-12-14 19:14:14 +03:00
@Pipe({ name: 'values' })
2015-10-08 20:16:45 +03:00
export class ValuesPipe {
2015-12-14 19:14:14 +03:00
transform(value) {
2015-12-14 19:31:17 +03:00
if (isBlank(value)) return value;
2015-12-14 19:14:14 +03:00
if (typeof value !== 'object') {
throw new InvalidPipeArgumentException(ValuesPipe, value);
}
return Object.keys(value).map(key => value[key]);
2015-10-08 20:16:45 +03:00
}
}
2015-12-14 19:14:14 +03:00
@Pipe({ name: 'jsonPointerEscape' })
2015-10-08 20:16:45 +03:00
export class JsonPointerEscapePipe {
2015-12-14 19:14:14 +03:00
transform(value) {
2015-12-14 19:31:17 +03:00
if (isBlank(value)) return value;
2015-12-14 19:14:14 +03:00
if (!isString(value)) {
throw new InvalidPipeArgumentException(JsonPointerEscapePipe, value);
}
return JsonPointer.escape(value);
2015-10-08 20:16:45 +03:00
}
}
2015-12-14 19:14:14 +03:00
@Pipe({ name: 'marked' })
export class MarkedPipe {
2015-12-14 19:14:14 +03:00
transform(value) {
2015-12-14 19:31:17 +03:00
if (isBlank(value)) return value;
2015-12-14 19:14:14 +03:00
if (!isString(value)) {
throw new InvalidPipeArgumentException(JsonPointerEscapePipe, value);
}
return `<span class="redoc-markdown-block">${marked(value)}</span>`;
}
}
2016-01-31 20:37:35 +03:00
const langMap = {
'c++': 'cpp',
'c#': 'csharp',
'objective-c': 'objectivec',
'shell': 'bash',
'viml': 'vim'
};
@Pipe({ name: 'prism' })
export class PrismPipe {
transform(value, args) {
if (isBlank(args) || args.length === 0) {
throw new BaseException('Prism pipe requires one argument');
}
if (isBlank(value)) return value;
if (!isString(value)) {
throw new InvalidPipeArgumentException(JsonPointerEscapePipe, value);
}
let lang = args[0].toString().trim().toLowerCase();
if (langMap[lang]) lang = langMap[lang];
let grammar = Prism.languages[lang];
//fallback to clike
if (!grammar) grammar = Prism.languages.clike;
return Prism.highlight(value, grammar);
}
}
2016-03-09 21:44:20 +03:00
@Pipe({ name: 'encodeURIComponent' })
export class EncodeURIComponentPipe {
transform(value) {
if (isBlank(value)) return value;
if (!isString(value)) {
throw new InvalidPipeArgumentException(EncodeURIComponentPipe, value);
}
return encodeURIComponent(value);
}
}