Oben Sonne  committed b0126e7

Support custom converters in the macros module

It is now possible to define custom converters in the macros module
(e.g. for SCSS or LESS files). Converters must be defined using a
dictionary named 'converter' with file name patterns as keys and a
converter funcion as well as a target file name extension as values.

converter = {
r'\.less': (less_to_css, 'css'),

The converter function `less_to_css` must accept the source file name
and the desitnation file name as arguments. The destination file name is
a suggestion, it is the source filename mapped to the project's output
directory with the extension given in the converter dictionary. Example:

def less_to_css(src, dst):
# when `src` is '/path/to/project/input/foo.less' then `dst` is
# '/path/to/project/output/foo.css'

  • Participants
  • Parent commits 49ec4a5
  • Branches default

Comments (0)

Files changed (1)

     pages = []
     page_global = macros.get("page", {})
+    custom_converter = macros.get('converter', {})
     for cwd, dirs, files in os.walk(dir_in.decode(opts.filename_enc)):
         cwd_site = cwd[len(dir_in):].lstrip(os.path.sep)
         for sdir in dirs[:]:
                 page = Page(page_global, opj(cwd, f), dir_in, opts)
-                shutil.copy(opj(cwd, f), opj(dir_out, cwd_site))
+                # either us a custom converter or do a plain copy
+                for patt, (func, ext) in custom_converter.items():
+                    if, f):
+                        f_src = opj(cwd, f)
+                        f_dst = opj(dir_out, cwd_site, f)
+                        f_dst = '%s.%s' % (os.path.splitext(f_dst)[0], ext)
+                        print('info   : convert %s (%s)' % (f_src, func.__name__))
+                        func(f_src, f_dst)
+                        break
+                else:
+                    shutil.copy(opj(cwd, f), opj(dir_out, cwd_site))
     pages.sort(key=lambda p: int(p.get("sval", "0")))