From 80a1134f207b8817c29e9f9d17ffa35542ca49ca Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Mon, 13 Jan 2014 13:10:34 +0000 Subject: [PATCH] Highlighting active nav links. Closes #3. Closes #4. --- mkdocs/mkdocs.py | 43 +++++++++++++++++++++++++++++++++++-------- theme/base.html | 28 +++++++++++++++++----------- 2 files changed, 52 insertions(+), 19 deletions(-) diff --git a/mkdocs/mkdocs.py b/mkdocs/mkdocs.py index 5d20520e..94a33f9a 100755 --- a/mkdocs/mkdocs.py +++ b/mkdocs/mkdocs.py @@ -18,6 +18,12 @@ suffix = '.html' index = 'index.html' +class NavItem(object): + def __init__(self, title, url, children): + self.title, self.url, self.children = title, url, children + self.active = False + + def build_theme(config): for (source_dir, dirnames, filenames) in os.walk(config['theme_dir']): relative_path = os.path.relpath(source_dir, config['theme_dir']) @@ -55,6 +61,7 @@ def build_html(config): template = jinja2.Template(open(template_path, 'r').read()) for path, title in config['index']: + set_nav_active(path, config, nav) homepage_url = path_to_url('index.md', config) url = path_to_url(path, config) previous_url, next_url = path_to_previous_and_next_urls(path, config) @@ -99,21 +106,41 @@ def build_html(config): def build_nav(config): - ret = collections.OrderedDict() + # TODO: Allow more than two levels of nav. + ret = [] for path, title in config['index']: url = path_to_url(path, config) - title, sep, child = title.partition('/') + title, sep, child_title = title.partition('/') title = title.strip() - child = child.strip() - if not child: - ret[title] = url - elif title not in ret: - ret[title] = collections.OrderedDict({child: url}) + child_title = child_title.strip() + if not child_title: + # New top level nav item + nav = NavItem(title=title, url=url, children=[]) + ret.append(nav) + elif not ret or (ret[-1].title != title): + # New second level nav item + child = NavItem(title=child_title, url=url, children=[]) + nav = NavItem(title=title, url=None, children=[child]) + ret.append(nav) else: - ret[title][child] = url + # Additional second level nav item + child = NavItem(title=child_title, url=url, children=[]) + ret[-1].children.append(child) return ret +def set_nav_active(path, config, nav): + url = path_to_url(path, config) + for nav_item in nav: + nav_item.active = (nav_item.url == url) + for child in nav_item.children: + if child.url == url: + child.active = True + nav_item.active = True + else: + child.active = False + + def path_to_url(path, config): path = os.path.splitext(path)[0] url = path.replace(os.path.pathsep, '/') diff --git a/theme/base.html b/theme/base.html index 695cc39a..491014ad 100644 --- a/theme/base.html +++ b/theme/base.html @@ -43,18 +43,18 @@