32
32
import shutil
33
33
import time
34
34
import requests
35
+ import configparser
35
36
36
37
import pygments
37
38
from pygments .lexers import PythonLexer
@@ -131,6 +132,13 @@ class CommandTheme(Command):
131
132
'default' : 'base' ,
132
133
'help' : 'Parent to use for new theme (default: base)' ,
133
134
},
135
+ {
136
+ 'name' : 'new_legacy_meta' ,
137
+ 'long' : 'legacy-meta' ,
138
+ 'type' : bool ,
139
+ 'default' : False ,
140
+ 'help' : 'Create legacy meta files for new theme' ,
141
+ },
134
142
]
135
143
136
144
def _execute (self , options , args ):
@@ -147,6 +155,7 @@ def _execute(self, options, args):
147
155
new = options .get ('new' )
148
156
new_engine = options .get ('new_engine' )
149
157
new_parent = options .get ('new_parent' )
158
+ new_legacy_meta = options .get ('new_legacy_meta' )
150
159
command_count = [bool (x ) for x in (
151
160
install ,
152
161
uninstall ,
@@ -172,7 +181,7 @@ def _execute(self, options, args):
172
181
elif copy_template :
173
182
return self .copy_template (copy_template )
174
183
elif new :
175
- return self .new_theme (new , new_engine , new_parent )
184
+ return self .new_theme (new , new_engine , new_parent , new_legacy_meta )
176
185
177
186
def do_install_deps (self , url , name ):
178
187
"""Install themes and their dependencies."""
@@ -316,7 +325,7 @@ def copy_template(self, template):
316
325
LOGGER .error ("This file already exists in your templates directory ({0})." .format (base ))
317
326
return 3
318
327
319
- def new_theme (self , name , engine , parent ):
328
+ def new_theme (self , name , engine , parent , create_legacy_meta = False ):
320
329
"""Create a new theme."""
321
330
base = 'themes'
322
331
themedir = os .path .join (base , name )
@@ -326,9 +335,7 @@ def new_theme(self, name, engine, parent):
326
335
LOGGER .info ("Created directory {0}" .format (base ))
327
336
328
337
# Check if engine and parent match
329
- engine_file = utils .get_asset_path ('engine' , utils .get_theme_chain (parent , self .site .themes_dirs ))
330
- with io .open (engine_file , 'r' , encoding = 'utf-8' ) as fh :
331
- parent_engine = fh .read ().strip ()
338
+ parent_engine = utils .get_template_engine (utils .get_theme_chain (parent , self .site .themes_dirs ))
332
339
333
340
if parent_engine != engine :
334
341
LOGGER .error ("Cannot use engine {0} because parent theme '{1}' uses {2}" .format (engine , parent , parent_engine ))
@@ -342,12 +349,24 @@ def new_theme(self, name, engine, parent):
342
349
LOGGER .error ("Theme already exists" )
343
350
return 2
344
351
345
- with io .open (os .path .join (themedir , 'parent' ), 'w' , encoding = 'utf-8' ) as fh :
346
- fh .write (parent + '\n ' )
347
- LOGGER .info ("Created file {0}" .format (os .path .join (themedir , 'parent' )))
348
- with io .open (os .path .join (themedir , 'engine' ), 'w' , encoding = 'utf-8' ) as fh :
349
- fh .write (engine + '\n ' )
350
- LOGGER .info ("Created file {0}" .format (os .path .join (themedir , 'engine' )))
352
+ cp = configparser .ConfigParser ()
353
+ cp ['Theme' ] = {
354
+ 'engine' : engine ,
355
+ 'parent' : parent
356
+ }
357
+
358
+ theme_meta_path = os .path .join (themedir , name + '.theme' )
359
+ with io .open (theme_meta_path , 'w' , encoding = 'utf-8' ) as fh :
360
+ cp .write (fh )
361
+ LOGGER .info ("Created file {0}" .format (theme_meta_path ))
362
+
363
+ if create_legacy_meta :
364
+ with io .open (os .path .join (themedir , 'parent' ), 'w' , encoding = 'utf-8' ) as fh :
365
+ fh .write (parent + '\n ' )
366
+ LOGGER .info ("Created file {0}" .format (os .path .join (themedir , 'parent' )))
367
+ with io .open (os .path .join (themedir , 'engine' ), 'w' , encoding = 'utf-8' ) as fh :
368
+ fh .write (engine + '\n ' )
369
+ LOGGER .info ("Created file {0}" .format (os .path .join (themedir , 'engine' )))
351
370
352
371
LOGGER .info ("Theme {0} created successfully." .format (themedir ))
353
372
LOGGER .notice ('Remember to set THEME="{0}" in conf.py to use this theme.' .format (name ))
0 commit comments