def _generate_site_navigation(pages_config, url_context, use_directory_urls=True): """ Returns a list of Page and Header instances that represent the top level site navigation. """ nav_items = [] pages = [] previous = None for config_line in pages_config: if isinstance(config_line, str): path = config_line title, child_title = None, None elif len(config_line) in (1, 2, 3): # Pad any items that don't exist with 'None' padded_config = (list(config_line) + [None, None])[:3] path, title, child_title = padded_config else: msg = ( "Line in 'page' config contained %d items. " "Expected 1, 2 or 3 strings." % len(config_line) ) assert False, msg if title is None: filename = path.split('/')[0] title = filename_to_title(filename) if child_title is None and '/' in path: filename = path.split('/')[1] child_title = filename_to_title(filename) url = utils.get_url_path(path, use_directory_urls) if not child_title: # New top level page. page = Page(title=title, url=url, path=path, url_context=url_context) if not utils.is_homepage(path): nav_items.append(page) elif not nav_items or (nav_items[-1].title != title): # New second level page. page = Page(title=child_title, url=url, path=path, url_context=url_context) header = Header(title=title, children=[page]) nav_items.append(header) page.ancestors = [header] else: # Additional second level page. page = Page(title=child_title, url=url, path=path, url_context=url_context) header = nav_items[-1] header.children.append(page) page.ancestors = [header] # Add in previous and next information. if previous: page.previous_page = previous previous.next_page = page previous = page pages.append(page) return (nav_items, pages)
def file_to_title(filename): """ Automatically generate a default title, given a filename. The method parses the file to check for a title, uses the filename as a title otherwise. """ if utils.is_homepage(filename): return 'Home' try: with open(filename, 'r') as f: lines = f.read() _, table_of_contents, meta = utils.convert_markdown(lines, ['meta', 'toc']) if "title" in meta: return meta["title"][0] if len(table_of_contents.items) > 0: return table_of_contents.items[0].title except IOError: # File couldn't be found - use filename as the title # this is used in tests. We use the filename as the title # in that case pass # No title found in the document, using the filename title = os.path.splitext(filename)[0] title = title.replace('-', ' ').replace('_', ' ') # Capitalize if the filename was all lowercase, otherwise leave it as-is. if title.lower() == title: title = title.capitalize() return title
def filename_to_title(filename): """ Automatically generate a default title, given a filename. """ if utils.is_homepage(filename): return 'Home' return utils.filename_to_title(filename)
def filename_to_title(filename): """ Automatically generate a default title, given a filename. """ if utils.is_homepage(filename): return 'Home' title = os.path.splitext(filename)[0] title = title.replace('-', ' ').replace('_', ' ') # Captialize if the filename was all lowercase, otherwise leave it as-is. if title.lower() == title: title = title.capitalize() return title
def is_homepage(self): return utils.is_homepage(self.input_path)