def __gen_html_album(opts, album_name, output_dir='.', page=1): global __generated, __items_no entry_id = '%s:%s' % (album_name, page) if entry_id in __generated: return __generated.add(entry_id) if page == 1: print(album_name, end=' ') data = webxiang.get_data(album=album_name, page=page) if not data: return tpl = data['meta'].get('template') or 'default.html' if not tpl.endswith('.html'): tpl += '.html' data['STATIC_URL'] = opts['assets_url'] try: html = render_to_string(tpl, data) except TemplateDoesNotExist: html = render_to_string('default.html', data) if page > 1: output_file = os.path.join( output_dir, album_name, _('page-%(number)s.html') % {'number': page}) else: output_file = os.path.join(output_dir, album_name, 'index.html') if opts['verbose'] > 1: print('writing %s' % output_file) elif opts['verbose'] == 1: sys.stdout.write('.') sys.stdout.flush() if not os.path.exists(os.path.dirname(output_file)): os.makedirs(os.path.dirname(output_file)) f = codecs.open(output_file, 'w', 'utf-8') f.write(six.text_type(html)) f.close() __items_no += 1 # symlink '/index.html' to '/index/index.html' if album_name == 'index': os.symlink('index/index.html', os.path.join(output_dir, 'index.html')) for i in data['entries'].paginator.page_range_limited: __gen_html_album(opts, album_name, output_dir=output_dir, page=i) for entry in data['entries']: if 'album' in entry: __gen_html_album(opts, entry['album'], output_dir) else: __gen_html_photo(opts, album_name, '%s/' % entry['index'], output_dir)
def __gen_html_album(opts, album_name, output_dir='.', page=1): global __generated, __items_no entry_id = '%s:%s' % (album_name, page) if entry_id in __generated: return __generated.add(entry_id) if page == 1: print(album_name, end=' ') data = webxiang.get_data(album=album_name, page=page) if not data: return tpl = data['meta'].get('template') or 'default.html' if not tpl.endswith('.html'): tpl += '.html' data['STATIC_URL'] = opts['assets_url'] try: html = render_to_string(tpl, data) except TemplateDoesNotExist: html = render_to_string('default.html', data) if page > 1: output_file = os.path.join(output_dir, album_name, _('page-%(number)s.html') % {'number': page}) else: output_file = os.path.join(output_dir, album_name, 'index.html') if opts['verbose'] > 1: print('writing %s' % output_file) elif opts['verbose'] == 1: sys.stdout.write('.') sys.stdout.flush() if not os.path.exists(os.path.dirname(output_file)): os.makedirs(os.path.dirname(output_file)) f = codecs.open(output_file, 'w', 'utf-8') f.write(six.text_type(html)) f.close() __items_no += 1 # symlink '/index.html' to '/index/index.html' if album_name == 'index': os.symlink('index/index.html', os.path.join(output_dir, 'index.html')) for i in data['entries'].paginator.page_range_limited: __gen_html_album(opts, album_name, output_dir=output_dir, page=i) for entry in data['entries']: if 'album' in entry: __gen_html_album(opts, entry['album'], output_dir) else: __gen_html_photo(opts, album_name, '%s/' % entry['index'], output_dir)
def __gen_html_photo(opts, album_name, entry_idx, output_dir='.'): global __generated, __items_no entry_id = '%s/%s' % (album_name, entry_idx) if entry_id in __generated: return __generated.add(entry_id) photo_idx = entry_idx.split('/')[0] data = webxiang.get_data(album=album_name, photo=entry_idx) if not data: return tpl = data['meta'].get('template') or 'default.html' if not tpl.endswith('.html'): tpl += '.html' data['STATIC_URL'] = opts['assets_url'] try: html = render_to_string(tpl, data) except TemplateDoesNotExist: html = render_to_string('default.html', data) try: os.makedirs(os.path.join(output_dir, album_name)) except: pass entry = data['entries'][int(photo_idx) - 1] if 'slug' in entry: photo_name = '%s/%s.html' % (photo_idx, entry['slug']) else: photo_name = '%s.html' % photo_idx output_file = os.path.join(output_dir, album_name, photo_name) if not os.path.exists(os.path.dirname(output_file)): os.makedirs(os.path.dirname(output_file)) if opts['verbose'] > 1: print('writing %s' % output_file) elif opts['verbose'] == 1: sys.stdout.write('.') sys.stdout.flush() f = codecs.open(output_file, 'w', 'utf-8') f.write(six.text_type(html)) f.close() __items_no += 1