From cfec4c13df48ff07eaac09d7d26ae0aa3856ea8d Mon Sep 17 00:00:00 2001 From: Prateeksha Singh Date: Mon, 2 Jul 2018 21:51:02 +0530 Subject: [PATCH] [build-docs][context] look for _sidebar.json in parent directories (#5715) * [build-docs][context] look for _sidebar.json in parent directories * [build-docs] stop sidebar search at /www dir * [website-render] use look_for_sidebar_json hook * [website-sidebar] add hooks check --- frappe/website/context.py | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/frappe/website/context.py b/frappe/website/context.py index e948ad994f..f971fa0dcb 100644 --- a/frappe/website/context.py +++ b/frappe/website/context.py @@ -120,6 +120,32 @@ def build_context(context): return context +def load_sidebar(context, sidebar_json_path): + with open(sidebar_json_path, 'r') as sidebarfile: + context.sidebar_items = json.loads(sidebarfile.read()) + context.show_sidebar = 1 + +def get_sidebar_json_path(path, look_for=False): + ''' + Get _sidebar.json path from directory path + + :param path: path of the current diretory + :param look_for: if True, look for _sidebar.json going upwards from given path + + :return: _sidebar.json path + ''' + if os.path.split(path)[1] == 'www' or path == '/' or not path: + return '' + + sidebar_json_path = os.path.join(path, '_sidebar.json') + if os.path.exists(sidebar_json_path): + return sidebar_json_path + else: + if look_for: + return get_sidebar_json_path(os.path.split(path)[0], look_for) + else: + return '' + def add_sidebar_and_breadcrumbs(context): '''Add sidebar and breadcrumbs to context''' from frappe.website.router import get_page_info_from_template @@ -128,11 +154,14 @@ def add_sidebar_and_breadcrumbs(context): add_sidebar_data(context) else: if context.basepath: - sidebar_json_path = os.path.join(context.basepath, '_sidebar.json') - if os.path.exists(sidebar_json_path): - with open(sidebar_json_path, 'r') as sidebarfile: - context.sidebar_items = json.loads(sidebarfile.read()) - context.show_sidebar = 1 + hooks = frappe.get_hooks('look_for_sidebar_json') + look_for_sidebar_json = hooks[0] if hooks else 0 + sidebar_json_path = get_sidebar_json_path( + context.basepath, + look_for_sidebar_json + ) + if sidebar_json_path: + load_sidebar(context, sidebar_json_path) if context.add_breadcrumbs and not context.parents: if context.basepath: