/*!-----------------------------------------------------------
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Version: 0.2.3(84e204b238a1eff25d4c6f611fdcd76a494005a7)
 * Released under the MIT license
 * https://github.com/Microsoft/vscode/blob/master/LICENSE.txt
 *-----------------------------------------------------------*/

define("vs/base/common/marked/marked", ["require", "exports"], function (require, exports) {
    "use strict";
    /*---------------------------------------------------------------------------------------------
     *  Copyright (c) Microsoft Corporation. All rights reserved.
     *  Licensed under the MIT License. See License.txt in the project root for license information.
     *--------------------------------------------------------------------------------------------*/
    function noop(value) {
        return value;
    }
    noop.Renderer = function () {
        // No-op
    };
    var mock = {
        marked: noop
    };
    return mock;
});

define("vs/editor/common/modes/textToHtmlTokenizer", ["require", "exports", 'vs/base/common/strings', 'vs/editor/common/modes/nullMode'], function (require, exports, strings, nullMode_1) {
    /*---------------------------------------------------------------------------------------------
     *  Copyright (c) Microsoft Corporation. All rights reserved.
     *  Licensed under the MIT License. See License.txt in the project root for license information.
     *--------------------------------------------------------------------------------------------*/
    'use strict';
    function tokenizeToHtmlContent(text, mode) {
        return _tokenizeToHtmlContent(text, _getSafeTokenizationSupport(mode));
    }
    exports.tokenizeToHtmlContent = tokenizeToHtmlContent;
    function tokenizeToString(text, mode, extraTokenClass) {
        return _tokenizeToString(text, _getSafeTokenizationSupport(mode), extraTokenClass);
    }
    exports.tokenizeToString = tokenizeToString;
    function _getSafeTokenizationSupport(mode) {
        if (mode && mode.tokenizationSupport) {
            return mode.tokenizationSupport;
        }
        return {
            shouldGenerateEmbeddedModels: false,
            getInitialState: function () { return new nullMode_1.NullState(null, null); },
            tokenize: function (buffer, state, deltaOffset, stopAtOffset) {
                if (deltaOffset === void 0) { deltaOffset = 0; }
                return nullMode_1.nullTokenize(null, buffer, state, deltaOffset, stopAtOffset);
            }
        };
    }
    function _tokenizeToHtmlContent(text, tokenizationSupport) {
        var result = {
            tagName: 'div',
            style: 'white-space: pre-wrap',
            children: []
        };
        var emitToken = function (className, tokenText) {
            result.children.push({
                tagName: 'span',
                className: className,
                text: tokenText
            });
        };
        var emitNewLine = function () {
            result.children.push({
                tagName: 'br'
            });
        };
        _tokenizeLines(text, tokenizationSupport, emitToken, emitNewLine);
        return result;
    }
    function _tokenizeToString(text, tokenizationSupport, extraTokenClass) {
        if (extraTokenClass === void 0) { extraTokenClass = ''; }
        if (extraTokenClass && extraTokenClass.length > 0) {
            extraTokenClass = ' ' + extraTokenClass;
        }
        var result = '';
        var emitToken = function (className, tokenText) {
            result += '<span class="' + className + extraTokenClass + '">' + strings.escape(tokenText) + '</span>';
        };
        var emitNewLine = function () {
            result += '<br/>';
        };
        result = '<div style="white-space: pre-wrap;">';
        _tokenizeLines(text, tokenizationSupport, emitToken, emitNewLine);
        result += '</div>';
        return result;
    }
    function _tokenizeLines(text, tokenizationSupport, emitToken, emitNewLine) {
        var lines = text.split(/\r\n|\r|\n/);
        var currentState = tokenizationSupport.getInitialState();
        for (var i = 0; i < lines.length; i++) {
            currentState = _tokenizeLine(lines[i], tokenizationSupport, emitToken, currentState);
            // Keep new lines
            if (i < lines.length - 1) {
                emitNewLine();
            }
        }
    }
    function _tokenizeLine(line, tokenizationSupport, emitToken, startState) {
        var tokenized = tokenizationSupport.tokenize(line, startState), endState = tokenized.endState, tokens = tokenized.tokens, offset = 0, tokenText;
        // For each token inject spans with proper class names based on token type
        for (var j = 0; j < tokens.length; j++) {
            var token = tokens[j];
            // Tokens only provide a startIndex from where they are valid from. As such, we need to
            // look ahead the value of the token by advancing until the next tokens start inex or the
            // end of the line.
            if (j < tokens.length - 1) {
                tokenText = line.substring(offset, tokens[j + 1].startIndex);
                offset = tokens[j + 1].startIndex;
            }
            else {
                tokenText = line.substr(offset);
            }
            var className = 'token';
            var safeType = token.type.replace(/[^a-z0-9\-]/gi, ' ');
            if (safeType.length > 0) {
                className += ' ' + safeType;
            }
            emitToken(className, tokenText);
        }
        return endState;
    }
});

var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
define("vs/languages/markdown/common/markdownWorker", ["require", "exports", 'vs/base/common/winjs.base', 'vs/base/common/uri', 'vs/base/common/types', 'vs/base/common/paths', 'vs/base/common/marked/marked', 'vs/editor/common/modes/textToHtmlTokenizer', 'vs/base/common/platform', 'vs/editor/common/services/modeService', 'vs/editor/common/services/resourceService', 'vs/platform/markers/common/markers'], function (require, exports, WinJS, uri_1, Types, Paths, Marked, textToHtmlTokenizer_1, platform_1, modeService_1, resourceService_1, markers_1) {
    /*---------------------------------------------------------------------------------------------
     *  Copyright (c) Microsoft Corporation. All rights reserved.
     *  Licensed under the MIT License. See License.txt in the project root for license information.
     *--------------------------------------------------------------------------------------------*/
    'use strict';
    var Theme;
    (function (Theme) {
        Theme[Theme["LIGHT"] = 0] = "LIGHT";
        Theme[Theme["DARK"] = 1] = "DARK";
        Theme[Theme["HC_BLACK"] = 2] = "HC_BLACK";
    })(Theme || (Theme = {}));
    var MarkdownWorker = (function () {
        function MarkdownWorker(modeId, resourceService, markerService, modeService) {
            this.theme = Theme.DARK;
            this._modeId = modeId;
            this.resourceService = resourceService;
            this.markerService = markerService;
            this.modeService = modeService;
        }
        MarkdownWorker.prototype._doConfigure = function (options) {
            if (options && options.theme) {
                this.theme = (options.theme === 'vs-dark') ? Theme.DARK : (options.theme === 'vs') ? Theme.LIGHT : Theme.HC_BLACK;
            }
            if (options && Types.isArray(options.styles)) {
                this.cssLinks = options.styles;
            }
            return WinJS.TPromise.as(void 0);
        };
        MarkdownWorker.prototype.getEmitOutput = function (resource, absoluteWorkersResourcePath) {
            var _this = this;
            var model = this.resourceService.get(resource);
            var cssLinks = this.cssLinks || [];
            // Custom Renderer to fix href in images
            var renderer = new Marked.marked.Renderer();
            var $this = this;
            renderer.image = function (href, title, text) {
                var out = '<img src="' + $this.fixHref(resource, href) + '" alt="' + text + '"';
                if (title) {
                    out += ' title="' + title + '"';
                }
                out += (this.options && this.options.xhtml) ? '/>' : '>';
                return out;
            };
            // Custom Renderer to open links always in a new tab
            var superRenderLink = renderer.link;
            renderer.link = function (href, title, text) {
                var link = superRenderLink.call(this, href, title, text);
                // We cannot support local anchor tags because the iframe editor does not have a src set
                if (href && href[0] === '#') {
                    link = link.replace('href=', 'localhref=');
                }
                else {
                    link = link.replace('<a', '<a target="_blank"');
                }
                return link;
            };
            var modeService = this.modeService;
            // Custom highlighter to use our modes to render code
            var highlighter = function (code, lang, callback) {
                // Lookup the mode and use the tokenizer to get the HTML
                var mimeForLang = modeService.getModeIdForLanguageName(lang) || lang || MarkdownWorker.DEFAULT_MODE;
                modeService.getOrCreateMode(mimeForLang).then(function (mode) {
                    callback(null, textToHtmlTokenizer_1.tokenizeToString(code, mode));
                });
            };
            return new WinJS.Promise(function (c, e) {
                // Render markdown file contents to HTML
                Marked.marked(model.getValue(), {
                    gfm: true,
                    renderer: renderer,
                    highlight: highlighter
                }, function (error, htmlResult) {
                    // Compute head
                    var head = [
                        '<!DOCTYPE html>',
                        '<html>',
                        '<head>',
                        '<meta http-equiv="Content-type" content="text/html;charset=UTF-8">',
                        (cssLinks.length === 0) ? '<link rel="stylesheet" href="' + absoluteWorkersResourcePath + '/markdown.css" type="text/css" media="screen">' : '',
                        (cssLinks.length === 0) ? '<link rel="stylesheet" href="' + absoluteWorkersResourcePath + '/tokens.css" type="text/css" media="screen">' : '',
                        (_this.theme === Theme.LIGHT) ? MarkdownWorker.LIGHT_SCROLLBAR_CSS : (_this.theme === Theme.DARK) ? MarkdownWorker.DARK_SCROLLBAR_CSS : MarkdownWorker.HC_BLACK_SCROLLBAR_CSS,
                        cssLinks.map(function (style) {
                            return '<link rel="stylesheet" href="' + _this.fixHref(resource, style) + '" type="text/css" media="screen">';
                        }).join('\n'),
                        '</head>',
                        platform_1.isMacintosh ? '<body class="mac">' : '<body>'
                    ].join('\n');
                    // Compute body
                    var body = [
                        (_this.theme === Theme.LIGHT) ? '<div class="monaco-editor vs">' : (_this.theme === Theme.DARK) ? '<div class="monaco-editor vs-dark">' : '<div class="monaco-editor hc-black">',
                        htmlResult,
                        '</div>',
                    ].join('\n');
                    // Tail
                    var tail = [
                        '</body>',
                        '</html>'
                    ].join('\n');
                    c({
                        head: head,
                        body: body,
                        tail: tail
                    });
                });
            });
        };
        MarkdownWorker.prototype.fixHref = function (resource, href) {
            if (href) {
                // Return early if href is already a URL
                if (uri_1.default.parse(href).scheme) {
                    return href;
                }
                // Otherwise convert to a file URI by joining the href with the resource location
                return uri_1.default.file(Paths.join(Paths.dirname(resource.fsPath), href)).toString();
            }
            return href;
        };
        MarkdownWorker.DEFAULT_MODE = 'text/plain';
        // Custom Scrollbar CSS (inlined because of pseudo elements that cannot be made theme aware)
        MarkdownWorker.LIGHT_SCROLLBAR_CSS = [
            '<style type="text/css">',
            '	::-webkit-scrollbar {',
            '		width: 14px;',
            '		height: 14px;',
            '	}',
            '',
            '	::-webkit-scrollbar-thumb {',
            '		background-color: rgba(100, 100, 100, 0.4);',
            '	}',
            '',
            '	::-webkit-scrollbar-thumb:hover {',
            '		background-color: rgba(100, 100, 100, 0.7);',
            '	}',
            '',
            '	::-webkit-scrollbar-thumb:active {',
            '		background-color: rgba(0, 0, 0, 0.6);',
            '	}',
            '</style>'
        ].join('\n');
        MarkdownWorker.DARK_SCROLLBAR_CSS = [
            '<style type="text/css">',
            '	::-webkit-scrollbar {',
            '		width: 14px;',
            '		height: 14px;',
            '	}',
            '',
            '	::-webkit-scrollbar-thumb {',
            '		background-color: rgba(121, 121, 121, 0.4);',
            '	}',
            '',
            '	::-webkit-scrollbar-thumb:hover {',
            '		background-color: rgba(100, 100, 100, 0.7);',
            '	}',
            '',
            '	::-webkit-scrollbar-thumb:active {',
            '		background-color: rgba(85, 85, 85, 0.8);',
            '	}',
            '</style>'
        ].join('\n');
        MarkdownWorker.HC_BLACK_SCROLLBAR_CSS = [
            '<style type="text/css">',
            '	::-webkit-scrollbar {',
            '		width: 14px;',
            '		height: 14px;',
            '	}',
            '',
            '	::-webkit-scrollbar-thumb {',
            '		background-color: rgba(111, 195, 223, 0.3);',
            '	}',
            '',
            '	::-webkit-scrollbar-thumb:hover {',
            '		background-color: rgba(111, 195, 223, 0.4);',
            '	}',
            '',
            '	::-webkit-scrollbar-thumb:active {',
            '		background-color: rgba(111, 195, 223, 0.4);',
            '	}',
            '</style>'
        ].join('\n');
        MarkdownWorker = __decorate([
            __param(1, resourceService_1.IResourceService),
            __param(2, markers_1.IMarkerService),
            __param(3, modeService_1.IModeService)
        ], MarkdownWorker);
        return MarkdownWorker;
    }());
    exports.MarkdownWorker = MarkdownWorker;
});

//# sourceMappingURL=markdownWorker.js.map
