"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/lib/text-mate-theme.js" (7 Feb 2017, 9382 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 (function() {
    2   var ScopeSelector, SyntaxVariablesTemplate, TextMateTheme, _, plist;
    3 
    4   _ = require('underscore-plus');
    5 
    6   plist = require('plist');
    7 
    8   ScopeSelector = require('first-mate').ScopeSelector;
    9 
   10   module.exports = TextMateTheme = (function() {
   11     function TextMateTheme(contents) {
   12       this.contents = contents;
   13       this.rulesets = [];
   14       this.buildRulesets();
   15     }
   16 
   17     TextMateTheme.prototype.buildRulesets = function() {
   18       var background, caret, foreground, i, invisibles, len, lineHighlight, name, ref, ref1, ref2, scope, selection, setting, settings, variableSettings;
   19       settings = ((ref = plist.parseStringSync(this.contents)) != null ? ref : {}).settings;
   20       if (settings == null) {
   21         settings = [];
   22       }
   23       for (i = 0, len = settings.length; i < len; i++) {
   24         setting = settings[i];
   25         ref1 = setting.settings, scope = ref1.scope, name = ref1.name;
   26         if (scope || name) {
   27           continue;
   28         }
   29         ref2 = setting.settings, background = ref2.background, foreground = ref2.foreground, caret = ref2.caret, selection = ref2.selection, invisibles = ref2.invisibles, lineHighlight = ref2.lineHighlight;
   30         if (background && foreground && caret && selection && lineHighlight && invisibles) {
   31           variableSettings = setting.settings;
   32           break;
   33         }
   34       }
   35       if (variableSettings == null) {
   36         throw new Error("Could not find the required color settings in the theme.\n\nThe theme being converted must contain a settings array with all of the following keys:\n  * background\n  * caret\n  * foreground\n  * invisibles\n  * lineHighlight\n  * selection");
   37       }
   38       this.buildSyntaxVariables(variableSettings);
   39       this.buildGlobalSettingsRulesets(variableSettings);
   40       return this.buildScopeSelectorRulesets(settings);
   41     };
   42 
   43     TextMateTheme.prototype.getStylesheet = function() {
   44       var i, len, lines, name, properties, ref, ref1, selector, value;
   45       lines = ['@import "syntax-variables";', ''];
   46       ref = this.getRulesets();
   47       for (i = 0, len = ref.length; i < len; i++) {
   48         ref1 = ref[i], selector = ref1.selector, properties = ref1.properties;
   49         lines.push(selector + " {");
   50         for (name in properties) {
   51           value = properties[name];
   52           lines.push("  " + name + ": " + value + ";");
   53         }
   54         lines.push("}\n");
   55       }
   56       return lines.join('\n');
   57     };
   58 
   59     TextMateTheme.prototype.getRulesets = function() {
   60       return this.rulesets;
   61     };
   62 
   63     TextMateTheme.prototype.getSyntaxVariables = function() {
   64       return this.syntaxVariables;
   65     };
   66 
   67     TextMateTheme.prototype.buildSyntaxVariables = function(settings) {
   68       var key, replaceRegex, value;
   69       this.syntaxVariables = SyntaxVariablesTemplate;
   70       for (key in settings) {
   71         value = settings[key];
   72         replaceRegex = new RegExp("\\{\\{" + key + "\\}\\}", 'g');
   73         this.syntaxVariables = this.syntaxVariables.replace(replaceRegex, this.translateColor(value));
   74       }
   75       return this.syntaxVariables;
   76     };
   77 
   78     TextMateTheme.prototype.buildGlobalSettingsRulesets = function(settings) {
   79       this.rulesets.push({
   80         selector: 'atom-text-editor',
   81         properties: {
   82           'background-color': '@syntax-background-color',
   83           'color': '@syntax-text-color'
   84         }
   85       });
   86       this.rulesets.push({
   87         selector: 'atom-text-editor .gutter',
   88         properties: {
   89           'background-color': '@syntax-gutter-background-color',
   90           'color': '@syntax-gutter-text-color'
   91         }
   92       });
   93       this.rulesets.push({
   94         selector: 'atom-text-editor .gutter .line-number.cursor-line',
   95         properties: {
   96           'background-color': '@syntax-gutter-background-color-selected',
   97           'color': '@syntax-gutter-text-color-selected'
   98         }
   99       });
  100       this.rulesets.push({
  101         selector: 'atom-text-editor .gutter .line-number.cursor-line-no-selection',
  102         properties: {
  103           'color': '@syntax-gutter-text-color-selected'
  104         }
  105       });
  106       this.rulesets.push({
  107         selector: 'atom-text-editor .wrap-guide',
  108         properties: {
  109           'color': '@syntax-wrap-guide-color'
  110         }
  111       });
  112       this.rulesets.push({
  113         selector: 'atom-text-editor .indent-guide',
  114         properties: {
  115           'color': '@syntax-indent-guide-color'
  116         }
  117       });
  118       this.rulesets.push({
  119         selector: 'atom-text-editor .invisible-character',
  120         properties: {
  121           'color': '@syntax-invisible-character-color'
  122         }
  123       });
  124       this.rulesets.push({
  125         selector: 'atom-text-editor .search-results .marker .region',
  126         properties: {
  127           'background-color': 'transparent',
  128           'border': '@syntax-result-marker-color'
  129         }
  130       });
  131       this.rulesets.push({
  132         selector: 'atom-text-editor .search-results .marker.current-result .region',
  133         properties: {
  134           'border': '@syntax-result-marker-color-selected'
  135         }
  136       });
  137       this.rulesets.push({
  138         selector: 'atom-text-editor.is-focused .cursor',
  139         properties: {
  140           'border-color': '@syntax-cursor-color'
  141         }
  142       });
  143       this.rulesets.push({
  144         selector: 'atom-text-editor.is-focused .selection .region',
  145         properties: {
  146           'background-color': '@syntax-selection-color'
  147         }
  148       });
  149       return this.rulesets.push({
  150         selector: 'atom-text-editor.is-focused .line-number.cursor-line-no-selection, atom-text-editor.is-focused .line.cursor-line',
  151         properties: {
  152           'background-color': this.translateColor(settings.lineHighlight)
  153         }
  154       });
  155     };
  156 
  157     TextMateTheme.prototype.buildScopeSelectorRulesets = function(scopeSelectorSettings) {
  158       var i, len, name, ref, results, scope, settings;
  159       results = [];
  160       for (i = 0, len = scopeSelectorSettings.length; i < len; i++) {
  161         ref = scopeSelectorSettings[i], name = ref.name, scope = ref.scope, settings = ref.settings;
  162         if (!scope) {
  163           continue;
  164         }
  165         results.push(this.rulesets.push({
  166           comment: name,
  167           selector: this.translateScopeSelector(scope),
  168           properties: this.translateScopeSelectorSettings(settings)
  169         }));
  170       }
  171       return results;
  172     };
  173 
  174     TextMateTheme.prototype.translateScopeSelector = function(textmateScopeSelector) {
  175       return new ScopeSelector(textmateScopeSelector).toCssSyntaxSelector();
  176     };
  177 
  178     TextMateTheme.prototype.translateScopeSelectorSettings = function(arg) {
  179       var background, fontStyle, fontStyles, foreground, properties;
  180       foreground = arg.foreground, background = arg.background, fontStyle = arg.fontStyle;
  181       properties = {};
  182       if (fontStyle) {
  183         fontStyles = fontStyle.split(/\s+/);
  184         if (_.contains(fontStyles, 'bold')) {
  185           properties['font-weight'] = 'bold';
  186         }
  187         if (_.contains(fontStyles, 'italic')) {
  188           properties['font-style'] = 'italic';
  189         }
  190         if (_.contains(fontStyles, 'underline')) {
  191           properties['text-decoration'] = 'underline';
  192         }
  193       }
  194       if (foreground) {
  195         properties['color'] = this.translateColor(foreground);
  196       }
  197       if (background) {
  198         properties['background-color'] = this.translateColor(background);
  199       }
  200       return properties;
  201     };
  202 
  203     TextMateTheme.prototype.translateColor = function(textmateColor) {
  204       var a, b, g, r;
  205       textmateColor = "#" + (textmateColor.replace(/^#+/, ''));
  206       if (textmateColor.length <= 7) {
  207         return textmateColor;
  208       } else {
  209         r = this.parseHexColor(textmateColor.slice(1, 3));
  210         g = this.parseHexColor(textmateColor.slice(3, 5));
  211         b = this.parseHexColor(textmateColor.slice(5, 7));
  212         a = this.parseHexColor(textmateColor.slice(7, 9));
  213         a = Math.round((a / 255.0) * 100) / 100;
  214         return "rgba(" + r + ", " + g + ", " + b + ", " + a + ")";
  215       }
  216     };
  217 
  218     TextMateTheme.prototype.parseHexColor = function(color) {
  219       var parsed;
  220       parsed = Math.min(255, Math.max(0, parseInt(color, 16)));
  221       if (isNaN(parsed)) {
  222         return 0;
  223       } else {
  224         return parsed;
  225       }
  226     };
  227 
  228     return TextMateTheme;
  229 
  230   })();
  231 
  232   SyntaxVariablesTemplate = "// This defines all syntax variables that syntax themes must implement when they\n// include a syntax-variables.less file.\n\n// General colors\n@syntax-text-color: {{foreground}};\n@syntax-cursor-color: {{caret}};\n@syntax-selection-color: {{selection}};\n@syntax-background-color: {{background}};\n\n// Guide colors\n@syntax-wrap-guide-color: {{invisibles}};\n@syntax-indent-guide-color: {{invisibles}};\n@syntax-invisible-character-color: {{invisibles}};\n\n// For find and replace markers\n@syntax-result-marker-color: {{invisibles}};\n@syntax-result-marker-color-selected: {{foreground}};\n\n// Gutter colors\n@syntax-gutter-text-color: {{foreground}};\n@syntax-gutter-text-color-selected: {{foreground}};\n@syntax-gutter-background-color: {{background}};\n@syntax-gutter-background-color-selected: {{lineHighlight}};\n\n// For git diff info. i.e. in the gutter\n// These are static and were not extracted from your textmate theme\n@syntax-color-renamed: #96CBFE;\n@syntax-color-added: #A8FF60;\n@syntax-color-modified: #E9C062;\n@syntax-color-removed: #CC6666;";
  233 
  234 }).call(this);