| 8 | | from pylons import config |
| 9 | | import os |
| 10 | | import logging |
| 11 | | from pylons.decorators.cache import beaker_cache |
| 12 | | import StringIO |
| 13 | | import re |
| 14 | | |
| 15 | | |
| 16 | | log = logging.getLogger(__name__) |
| 17 | | |
| 18 | | __javascript_include_tag = javascript_include_tag |
| 19 | | |
| 20 | | __stylesheet_link_tag = stylesheet_link_tag |
| 21 | | |
| 22 | | |
| 23 | | def javascript_include_tag(*sources, **options): |
| 24 | | |
| 25 | | @beaker_cache(key='sources', expire='never', type='dbm') |
| 26 | | def combine_sources(sources, fs_root): |
| 27 | | if len(sources) < 2: |
| 28 | | log.debug('No need to combine, only one source provided') |
| 29 | | return sources |
| 30 | | |
| 31 | | log.debug('combining javascripts: %r', sources) |
| 32 | | httpbase = os.path.commonprefix(['/'.join(s.split('/')[:-1])+'/' |
| 33 | | for s in sources]) |
| 34 | | jsbuffer = StringIO.StringIO() |
| 35 | | names = [] |
| 36 | | bases = os.path.commonprefix([b.split('/')[:-1] for b in sources]) |
| 37 | | log.debug('Base: %s', httpbase) |
| 38 | | for source in sources: |
| 39 | | log.debug('appending %s', source) |
| 40 | | _source = os.path.join(fs_root, *(source).split('/')) |
| 41 | | names.append(source.split('/')[-1:][0][:-3]) |
| 42 | | jsbuffer.write(open(_source, 'r').read()) |
| 43 | | jsbuffer.write('\n') |
| 44 | | fname = '.'.join(names+['COMBINED', 'js']) |
| 45 | | log.debug('Names: %r', names) |
| 46 | | log.debug('Combined Name: %s', fname) |
| 47 | | fpath = os.path.join(fs_root, *((httpbase+fname).split('/'))) |
| 48 | | log.debug('writing %s', fpath) |
| 49 | | open(fpath, 'w').write(jsbuffer.getvalue()) |
| 50 | | return [httpbase + fname] |
| 51 | | |
| 52 | | @beaker_cache(key='sources', expire='never', type='dbm') |
| 53 | | def get_sources(sources, fs_root=''): |
| 54 | | log.debug('Generating minified sources if needed') |
| 55 | | from pastie.lib.jsmin import JavascriptMinify |
| 56 | | jsm = JavascriptMinify() |
| 57 | | _sources = [] |
| 58 | | |
| 59 | | for source in sources: |
| 60 | | _source = os.path.join(fs_root, *(source[:-3]+'.min.js').split('/')) |
| 61 | | if os.path.exists(_source): |
| 62 | | _sources.append(source[:-3]+'.min.js') |
| 63 | | else: |
| 64 | | _source = os.path.join(fs_root, *source.split('/')) |
| 65 | | minified = _source[:-3]+'.min.js' |
| 66 | | log.debug('minifying %s -> %s', source, |
| 67 | | source[:-3]+'.min.js') |
| 68 | | jsm.minify(open(_source, 'r'), open(minified, 'w')) |
| 69 | | _sources.append(source[:-3]+'.min.js') |
| 70 | | return _sources |
| 71 | | |
| 72 | | if not config.get('debug', False): |
| 73 | | fs_root = root = config.get('pylons.paths').get('static_files') |
| 74 | | if options.pop('combined', False): |
| 75 | | sources = combine_sources([source for source in sources], fs_root) |
| 76 | | |
| 77 | | if options.pop('minified', False): |
| 78 | | sources = get_sources([source for source in sources], fs_root) |
| 79 | | return __javascript_include_tag(*sources, **options) |
| 80 | | |
| 81 | | def stylesheet_link_tag(*sources, **options): |
| 82 | | |
| 83 | | @beaker_cache(key='sources', expire='never', type='dbm') |
| 84 | | def combine_sources(sources, fs_root): |
| 85 | | if len(sources) < 2: |
| 86 | | log.debug('No need to combine, only one source provided') |
| 87 | | return sources |
| 88 | | |
| 89 | | log.debug('combining javascripts: %r', sources) |
| 90 | | httpbase = os.path.commonprefix(['/'.join(s.split('/')[:-1])+'/' |
| 91 | | for s in sources]) |
| 92 | | jsbuffer = StringIO.StringIO() |
| 93 | | names = [] |
| 94 | | log.debug('Base: %s', httpbase) |
| 95 | | for source in sources: |
| 96 | | log.debug('appending %s', source) |
| 97 | | _source = os.path.join(fs_root, *(source).split('/')) |
| 98 | | names.append(source.split('/')[-1:][0][:-4]) |
| 99 | | jsbuffer.write(open(_source, 'r').read()) |
| 100 | | jsbuffer.write('\n') |
| 101 | | fname = '.'.join(names+['COMBINED', 'css']) |
| 102 | | log.debug('Names: %r', names) |
| 103 | | log.debug('Combined Name: %s', fname) |
| 104 | | fpath = os.path.join(fs_root, *((httpbase+fname).split('/'))) |
| 105 | | log.debug('writing %s', fpath) |
| 106 | | open(fpath, 'w').write(jsbuffer.getvalue()) |
| 107 | | return [httpbase + fname] |
| 108 | | |
| 109 | | @beaker_cache(key='sources', expire='never', type='dbm') |
| 110 | | def get_sources(sources, fs_root): |
| 111 | | log.debug('Generating minified sources if needed') |
| 112 | | from pastie.lib.cssmin import CSSMinify |
| 113 | | cssm = CSSMinify() |
| 114 | | _sources = [] |
| 115 | | |
| 116 | | for source in sources: |
| 117 | | _source = os.path.join(fs_root, *(source[:-4]+'.min.css').split('/')) |
| 118 | | if os.path.exists(_source): |
| 119 | | _sources.append(source[:-4]+'.min.css') |
| 120 | | else: |
| 121 | | _source = os.path.join(fs_root, *source.split('/')) |
| 122 | | minified = _source[:-4]+'.min.css' |
| 123 | | log.debug('minifying %s -> %s', source, |
| 124 | | source[:-4]+'.min.css') |
| 125 | | cssm.minify(open(_source, 'r'), open(minified, 'w')) |
| 126 | | _sources.append(source[:-4]+'.min.css') |
| 127 | | return _sources |
| 128 | | |
| 129 | | if not config.get('debug', False): |
| 130 | | fs_root = root = config.get('pylons.paths').get('static_files') |
| 131 | | if options.pop('combined', False): |
| 132 | | sources = combine_sources([source for source in sources], fs_root) |
| 133 | | |
| 134 | | if options.pop('minified', False): |
| 135 | | sources = get_sources([source for source in sources], fs_root) |
| 136 | | return __stylesheet_link_tag(*sources, **options) |