예제 #1
0
 def test_stamp(self):
     assert stamp('/javascript/foo.js') == '/javascript/foo.js?XYZ'
예제 #2
0
    def base_link(cls, ext, *sources, **options):
        from pecan import conf, request
        from draughtcraft.templates.helpers import stamp

        combined = options.pop('combined', False)
        minified = options.pop('minified', False)

        fs_root = conf.app.static_root

        cache = request.environ['beaker.cache']

        @cache.cache(conf.cache['key'])
        def combine_sources(sources, ext, fs_root):
            if len(sources) < 2:
                return sources

            names = list()
            js_buffer = StringIO.StringIO()
            base = os.path.commonprefix([os.path.dirname(s) for s in sources])

            for source in sources:
                # get a list of all filenames without extensions
                js_file = os.path.basename(source)
                js_file_name = os.path.splitext(js_file)[0]
                names.append(js_file_name)

                # build a master file with all contents
                full_source = os.path.join(fs_root, source.lstrip('/'))
                f = cls.retrieve_readable(full_source)
                js_buffer.write(f.read())
                js_buffer.write('\n')
                f.close()

            # glue a new name and generate path to it
            fname = '.'.join(names + ['COMBINED', ext])
            fpath = os.path.join(fs_root, base.strip('/'), fname)

            # write the combined file
            cls.write_combine(js_buffer, fpath)

            return [os.path.join(base, fname)]

        @cache.cache(conf.cache['key'])
        def minify_sources(sources, ext, fs_root=''):

            minified_sources = []

            for source in sources:
                # generate full path to source
                no_ext_source = os.path.splitext(source)[0]
                full_source = os.path.join(
                    fs_root, (no_ext_source + ext).lstrip('/'))

                # generate minified source path
                full_source = os.path.join(fs_root, (source).lstrip('/'))
                no_ext_full_source = os.path.splitext(full_source)[0]
                minified = no_ext_full_source + ext

                if 'js' in ext:
                    # minify js source (read stream is auto-closed inside)
                    cls.write_minify(
                        cls.retrieve_readable(full_source),
                        minified
                    )

                minified_sources.append(no_ext_source + ext)

            return minified_sources

        if combined:
            sources = combine_sources(list(sources), ext, fs_root)

        if minified:
            sources = minify_sources(list(sources), '.min.' + ext, fs_root)

        # append a version stamp
        sources = [stamp(s) for s in sources]

        if 'js' in ext:
            return native_javascript_link(*sources, **options)
        if 'css' in ext:
            return native_stylesheet_link(*sources, **options)
예제 #3
0
    def base_link(cls, ext, *sources, **options):
        from pecan import conf, request
        from draughtcraft.templates.helpers import stamp

        combined = options.pop('combined', False)
        minified = options.pop('minified', False)

        fs_root = conf.app.static_root

        cache = request.environ['beaker.cache']

        @cache.cache(conf.cache['key'])
        def combine_sources(sources, ext, fs_root):
            if len(sources) < 2:
                return sources

            names = list()
            js_buffer = StringIO.StringIO()
            base = os.path.commonprefix([os.path.dirname(s) for s in sources])

            for source in sources:
                # get a list of all filenames without extensions
                js_file = os.path.basename(source)
                js_file_name = os.path.splitext(js_file)[0]
                names.append(js_file_name)

                # build a master file with all contents
                full_source = os.path.join(fs_root, source.lstrip('/'))
                f = cls.retrieve_readable(full_source)
                js_buffer.write(f.read())
                js_buffer.write('\n')
                f.close()

            # glue a new name and generate path to it
            fname = '.'.join(names + ['COMBINED', ext])
            fpath = os.path.join(fs_root, base.strip('/'), fname)

            # write the combined file
            cls.write_combine(js_buffer, fpath)

            return [os.path.join(base, fname)]

        @cache.cache(conf.cache['key'])
        def minify_sources(sources, ext, fs_root=''):

            minified_sources = []

            for source in sources:
                # generate full path to source
                no_ext_source = os.path.splitext(source)[0]
                full_source = os.path.join(fs_root,
                                           (no_ext_source + ext).lstrip('/'))

                # generate minified source path
                full_source = os.path.join(fs_root, (source).lstrip('/'))
                no_ext_full_source = os.path.splitext(full_source)[0]
                minified = no_ext_full_source + ext

                if 'js' in ext:
                    # minify js source (read stream is auto-closed inside)
                    cls.write_minify(cls.retrieve_readable(full_source),
                                     minified)

                minified_sources.append(no_ext_source + ext)

            return minified_sources

        if combined:
            sources = combine_sources(list(sources), ext, fs_root)

        if minified:
            sources = minify_sources(list(sources), '.min.' + ext, fs_root)

        # append a version stamp
        sources = [stamp(s) for s in sources]

        if 'js' in ext:
            return native_javascript_link(*sources, **options)
        if 'css' in ext:
            return native_stylesheet_link(*sources, **options)