def make_content_page_endpoint(item): if item.void: @app.before_first_request def register_node(): if not current_menu.submenu(item.menu_path, False): current_menu.submenu(item.menu_path).register(None, item.title, item.weight, item=item) else: @cached() def endpoint(): try: logger.info('Rendering %r, %r with relative URL %r', item.url_path, item.fs_path, item.parent_url) content = render_markdown_from_file(item.fs_path, item.parent_url) try: page_title = re.findall(r'<h(\d)>([^<]+)</h\1>', content)[0][1] except IndexError: page_title = item.title return render_template('content_page.html', content=content, title=page_title) except IsADirectoryError: if item.url_path.endswith('/tutorials') and item.category == 'Products': logger.info('Rendering tutorial listing at %r', item.url_path) docs = [] product = find_product(item) for tut in product.tutorial_items: docs.append({ 'title': tut.title, 'url': tut.url_path, 'excerpt': get_excerpt(tut.fs_path, tut.parent_url), 'image_url': get_image(tut.fs_path, tut.parent_url), # Not used now }) page_title = product.title + ' — Tutorials' return render_template('document_list.html', items=docs, title=page_title) else: logger.info('Rendering one level up from %r', item.url_path) return redirect(item.parent_url) endpoint.__name__ = 'content_page' + item.url_path.replace('/', '_') def active_when(): if item.main_page: return request.path == item.parent_url else: return request.path == item.url_path if item.main_page: app.add_url_rule(item.parent_url, view_func=endpoint) register_menu(app, item.menu_path, item.title, 0, active_when=active_when, item=item)(endpoint) else: app.add_url_rule(item.url_path, view_func=endpoint) register_menu(app, item.menu_path, item.title, item.weight, active_when=active_when, item=item)(endpoint)
def register_menu_ex(app, path, text, **kwargs): """ :param app: :param path: :param text: :param kwargs: """ new_visible_when = partial(visible_when, kwargs.get('roles')) kwargs['visible_when'] = new_visible_when return register_menu(app, path, text, **kwargs)
def decorator(f): """Menu decorator.""" for order, key in enumerate(args): def arguments(key): return lambda: {'page': key} f = register_menu(blueprint, 'main.about.{0}'.format(key), _('%(experiment)s Open Data', experiment=key), order=order + 1, endpoint_arguments_constructor=arguments(key))(f) return f
def register_breadcrumb( app, path, text, order=0, endpoint_arguments_constructor=None, dynamic_list_constructor=None, ): """Decorate endpoints that should be displayed as a breadcrumb. :param app: Application or Blueprint which owns the function. :param path: Path to this item in menu hierarchy ('breadcrumbs.' is automatically added). :param text: Text displayed as link. :param order: Index of item among other items in the same menu. :param endpoint_arguments_constructor: Function returning dict of arguments passed to url_for when creating the link. :param dynamic_list_constructor: Function returning a list of breadcrumbs to be displayed by this item. Every object should have 'text' and 'url' properties/dict elements. """ # Resolve blueprint-relative paths if path.startswith("."): def _evaluate_path(): """Lazy path evaluation.""" bl_path = Breadcrumbs.get_path(app) return (bl_path + path).strip(".") func_path = LocalProxy(_evaluate_path) else: func_path = path # Get standard menu decorator menu_decorator = register_menu( app, func_path, text, order, endpoint_arguments_constructor=endpoint_arguments_constructor, dynamic_list_constructor=dynamic_list_constructor, ) def breadcrumb_decorator(func): """Apply standard menu decorator and assign breadcrumb.""" func.__breadcrumb__ = func_path return menu_decorator(func) return breadcrumb_decorator
def register_breadcrumb(app, path, text, order=0, endpoint_arguments_constructor=None, dynamic_list_constructor=None): """Decorate endpoints that should be displayed as a breadcrumb. :param app: Application or Blueprint which owns the function. :param path: Path to this item in menu hierarchy ('breadcrumbs.' is automatically added). :param text: Text displayed as link. :param order: Index of item among other items in the same menu. :param endpoint_arguments_constructor: Function returning dict of arguments passed to url_for when creating the link. :param dynamic_list_constructor: Function returning a list of breadcrumbs to be displayed by this item. Every object should have 'text' and 'url' properties/dict elements. """ # Resolve blueprint-relative paths if path.startswith('.'): def _evaluate_path(): """Lazy path evaluation.""" bl_path = Breadcrumbs.get_path(app) return (bl_path + path).strip('.') func_path = LocalProxy(_evaluate_path) else: func_path = path # Get standard menu decorator menu_decorator = register_menu( app, func_path, text, order, endpoint_arguments_constructor=endpoint_arguments_constructor, dynamic_list_constructor=dynamic_list_constructor) def breadcrumb_decorator(func): """Applie standard menu decorator and assign breadcrumb.""" func.__breadcrumb__ = func_path return menu_decorator(func) return breadcrumb_decorator