Add themes as a submodule and handle both builtin themes and local themes.

This commit is contained in:
Florian Mounier
2015-10-14 13:19:52 +02:00
parent 2d554483e1
commit 7b37716177
7 changed files with 87 additions and 37 deletions

3
.gitmodules vendored
View File

@@ -0,0 +1,3 @@
[submodule "butterfly/themes"]
path = butterfly/themes
url = https://github.com/paradoxxxzero/butterfly-themes

View File

@@ -14,7 +14,7 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
__version__ = '2.0.8'
__version__ = '2.0.10'
import os
@@ -43,6 +43,24 @@ class Route(tornado.web.RequestHandler):
def log(self):
return log
@property
def builtin_themes_dir(self):
return os.path.join(
os.path.dirname(__file__), 'themes')
@property
def themes_dir(self):
return os.path.join(
self.application.butterfly_dir, 'themes')
def get_theme_dir(self, theme):
if theme.startswith('built-in-'):
return os.path.join(
self.builtin_themes_dir, theme[len('built-in-'):])
return os.path.join(
self.themes_dir, theme)
# Imported from executable
if hasattr(tornado.options.options, 'debug'):
application = tornado.web.Application(

View File

@@ -56,10 +56,8 @@ class Theme(Route):
'You must install libsass to use sass '
'(pip install libsass)')
return
base_dir = self.get_theme_dir(theme)
themes_dir = os.path.join(
self.application.butterfly_dir, 'themes')
base_dir = os.path.join(themes_dir, theme)
style = None
for ext in ['css', 'scss', 'sass']:
probable_style = os.path.join(base_dir, 'style.%s' % ext)
@@ -96,9 +94,7 @@ class ThemeStatic(Route):
if '..' in name:
raise tornado.web.HTTPError(403)
themes_dir = os.path.join(
self.application.butterfly_dir, 'themes')
base_dir = os.path.join(themes_dir, theme)
base_dir = self.get_theme_dir(theme)
fn = os.path.normpath(os.path.join(base_dir, name))
if not fn.startswith(base_dir):
@@ -326,19 +322,29 @@ class ThemesList(Route):
"""Get the theme list"""
def get(self):
themes_dir = os.path.join(
self.application.butterfly_dir, 'themes')
self.set_header('Content-Type', 'application/json')
if os.path.exists(themes_dir):
if os.path.exists(self.themes_dir):
themes = [
theme
for theme in os.listdir(themes_dir)
if os.path.isdir(os.path.join(themes_dir, theme)) and
for theme in os.listdir(self.themes_dir)
if os.path.isdir(os.path.join(self.themes_dir, theme)) and
not theme.startswith('.')]
else:
themes = []
if os.path.exists(self.builtin_themes_dir):
builtin_themes = [
'built-in-%s' % theme
for theme in os.listdir(self.builtin_themes_dir)
if os.path.isdir(os.path.join(
self.builtin_themes_dir, theme)) and
not theme.startswith('.')]
else:
builtin_themes = []
self.set_header('Content-Type', 'application/json')
self.write(tornado.escape.json_encode({
'themes': sorted(themes),
'dir': themes_dir
'builtin_themes': sorted(builtin_themes),
'dir': self.themes_dir
}))

View File

@@ -614,30 +614,37 @@
}
oReq = new XMLHttpRequest();
oReq.addEventListener('load', function() {
var inner, j, len1, ref, response, theme, theme_list, themes, url;
var builtin_themes, inner, j, k, len1, len2, option, response, theme, theme_list, themes, url;
response = JSON.parse(this.responseText);
builtin_themes = response.builtin_themes;
themes = response.themes;
if (themes.length === 0) {
alert("No themes found in " + response.dir + ".\n Please install themes with butterfly.server.py --install-themes");
return;
}
inner = "<form>\n <h2>Pick a theme:</h2>\n <select id=\"theme_list\">";
ref = ['default'].concat(themes);
for (j = 0, len1 = ref.length; j < len1; j++) {
theme = ref[j];
if (theme === 'default') {
url = "/static/main.css";
} else {
url = "/theme/" + theme + "/style.css";
}
option = function(url, theme) {
inner += '<option ';
if (_theme === url) {
inner += 'selected ';
}
inner += "value=\"" + url + "\">";
inner += theme;
inner += '</option>';
return inner += '</option>';
};
option("/static/main.css", 'default');
if (themes.length) {
inner += '<optgroup label="Local themes">';
for (j = 0, len1 = themes.length; j < len1; j++) {
theme = themes[j];
url = "/theme/" + theme + "/style.css";
option(url, theme);
}
inner += '</optgroup>';
}
inner += '<optgroup label="Built-in themes">';
for (k = 0, len2 = builtin_themes.length; k < len2; k++) {
theme = builtin_themes[k];
url = "/theme/" + theme + "/style.css";
option(url, theme.slice('built-in-'.length));
}
inner += '</optgroup>';
inner += " </select>\n <label>You can create yours in " + response.dir + ".</label>\n</form>";
popup.open(inner);
theme_list = document.getElementById('theme_list');

1
butterfly/themes Submodule

Submodule butterfly/themes added at c9dd55f4e6

View File

@@ -25,23 +25,20 @@ document.addEventListener 'keydown', (e) ->
oReq = new XMLHttpRequest()
oReq.addEventListener 'load', ->
response = JSON.parse(@responseText)
builtin_themes = response.builtin_themes
themes = response.themes
if themes.length is 0
alert("No themes found in #{response.dir}.\n
Please install themes with butterfly.server.py --install-themes")
return
# if themes.length is 0
# alert("No themes found in #{response.dir}.\n
# Please install themes with butterfly.server.py --install-themes")
# return
inner = """
<form>
<h2>Pick a theme:</h2>
<select id="theme_list">
"""
for theme in ['default'].concat(themes)
if theme is 'default'
url = "/static/main.css"
else
url = "/theme/#{theme}/style.css"
option = (url, theme) ->
inner += '<option '
if _theme is url
@@ -50,6 +47,22 @@ document.addEventListener 'keydown', (e) ->
inner += "value=\"#{url}\">"
inner += theme
inner += '</option>'
option "/static/main.css", 'default'
if themes.length
inner += '<optgroup label="Local themes">'
for theme in themes
url = "/theme/#{theme}/style.css"
option url, theme
inner += '</optgroup>'
inner += '<optgroup label="Built-in themes">'
for theme in builtin_themes
url = "/theme/#{theme}/style.css"
option url, theme.slice('built-in-'.length)
inner += '</optgroup>'
inner += """
</select>
<label>You can create yours in #{response.dir}.</label>

View File

@@ -29,6 +29,8 @@ options = dict(
package_data={
'butterfly': [
'sass/*.sass',
'themes/*',
'themes/**/*',
'static/fonts/*',
'static/images/favicon.png',
'static/main.css',