Skip to content

Commit

Permalink
onto working on the specific styles
Browse files Browse the repository at this point in the history
  • Loading branch information
Farhad Ghayour committed Jan 21, 2015
1 parent f9d7735 commit e0d6921
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.DS_Store
node_modules
codeMirror.css
codemirror
135 changes: 79 additions & 56 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,19 @@
/*******************************************************************************
* Dependencies
*/
var fs = require('fs');
var path = require('path');
var plist = require('plist');
var fs = require('fs');
var path = require('path');
var plist = require('plist');
var CSSJSON = require('CSSJSON');
var supportedScopes = require('./supportedScopes');

/*******************************************************************************
* TODO SCOPES
*/
var supportedScopes = {
activeGuide: '',
background: '',
bracketContentsForeground: '',
bracketContentsOptions: '',
bracketsForeground: '',
bracketsOptions: '',
caret: '',
findHighlight: '',
findHighlightForeground: '',
foreground: '',
inactiveSelection: '',
inactiveSelectionForeground: '',
invisibles: '',
lineHighlight: '',
selection: '',
selectionBorder: '',
selectionForeground: '',
tagsOptions: '',
};

/*******************************************************************************
* Parsying styles
* Parsying styles TODO????????
*/
function parseStyles(styles) {
var css = [];

console.log(styles);
var fontStyle = styles.fontStyle || '';

if (fontStyle.indexOf('underline') !== -1) {
Expand All @@ -56,34 +35,80 @@ function parseStyles(styles) {
return css.join('\n');
}

/*******************************************************************************
* Write to the file
*/
function writeFile(styles) {
var data = CSSJSON.toCSS(styles);
fs.writeFile(__dirname + '/codemirror/famous.css', data, function(err) {
if(err) console.log(err);
});
}

function convertClass(string) {
return '.' + string;
}

function buildClass(themeClass, selector) {
return [convertClass(themeClass), convertClass(selector)].join(' ');
}

function print(json) {
console.log(JSON.stringify(json, null, 4));
}

function generateThemeInfo(themeInfo, theme) {
var root = {};

for(var themeInfo in theme) {
if (themeInfo.toLowerCase() !== 'settings') {
var info = theme[themeInfo];
root[themeInfo] = info;
}
}

root.children = {};
root.unsupported = {};
return root;
}

function generateGlobalStyles(global, root, themeName, theme) {
var styles = root.children;

for(var scope in global) {
var codeMirrorScope = supportedScopes[scope];
if (codeMirrorScope) {
var selector = buildClass(themeName, codeMirrorScope.selector);
var property = codeMirrorScope.property;
var value = global[scope];
styles[selector] = {};
styles[selector].attributes = {};
styles[selector].attributes[property] = value;
} else {
root.unsupported[scope] = 'Global';
}
}
}

/*******************************************************************************
* Extarcting styles from a theme
*/
function extractStyles(theme) {
var styles = {};

// Global styling
var globalSettings = theme.settings[0];
console.log(globalSettings);

for(var i = 0; i < theme.settings.length; i++) {

var element = theme.settings[i];
// if (!element.scope || !element.settings) continue;

// console.log(element.settings);
// var scopes = element.scope.split(/\s*[|,]\s*/g);
// for(var j = 0; j < scopes.length; j++) {
// var scope = scopes[i];
// var style = parseStyles(element.settings);
// var codeMirrorScope = supportedScopes[scope];
// if (codeMirrorScope) {
// colors[codeMirrorScope] = style;
// }
// }
function extractStyles(themeName, theme) {
var root = generateThemeInfo(themeName, theme);
var settings = theme.settings;

for(var i = 0; i < settings.length; i++) {
if (!(settings[i].name || settings[i].scope)) {
// generateGlobalStyles(settings[i].settings, root, themeName, theme);
} else {
// now onto applying the correct styling to the rest of the elements
parseStyles(settings[i].settings);
}

}

return styles;
print(root)
return root;
}

/*******************************************************************************
Expand All @@ -98,13 +123,11 @@ function parseTheme(themeXml, callback) {
* Converting the theme
*/
function convertTheme(name, themePath, outputDirectory) {
console.log('Converting ' + name);
// console.log('Converting ' + name);
var srcTheme = fs.readFileSync(themePath, 'utf8');
parseTheme(srcTheme, function(theme) {
var styles = extractStyles(theme);
styles.cssClass = 'codeMirror-' + name.toLowerCase();
styles.uuid = theme.uuid;
// var css = fillTemplate(cssTemplate, styles);
styles = extractStyles(name, theme);
writeFile(styles);
});
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"author": "Farhad Ghayour",
"license": "ISC",
"dependencies": {
"cssjson": "^2.1.3",
"plist": "^1.1.0",
"xml2js": "^0.4.4"
}
Expand Down
21 changes: 21 additions & 0 deletions supportedScopes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*******************************************************************************
* Current supported scopes between Sublime + CodeMirror
*/
module.exports = {
background: {
selector: 'CodeMirror',
property: 'background'
},
foreground: {
selector: 'cm-variable',
property: 'color'
},
selection: {
selector: 'CodeMirror-selected',
property: 'background'
},
comment: {
selector: '...',
property: ''
}
};

0 comments on commit e0d6921

Please sign in to comment.