[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
This commit is contained in:
Prateeksha Singh 2018-07-02 21:51:02 +05:30 committed by Rushabh Mehta
parent 6ff6a98f8c
commit cfec4c13df

View file

@ -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: