diff --git a/TODO.md b/TODO.md index b79d48f..2189a84 100644 --- a/TODO.md +++ b/TODO.md @@ -1,5 +1,4 @@ # Now -- FIXME: excluded fiels and directories are visible in navigation - dont's show oh, sorry! for directories without index.md - current dir - readme diff --git a/tests/test_toc.py b/tests/test_toc.py index 3e50be6..e44c9a5 100644 --- a/tests/test_toc.py +++ b/tests/test_toc.py @@ -12,7 +12,7 @@ def test_toc_extractdir(mockupfs): '.qux.md': '## qux', }) - headings, subdirs = toc.extractdir(root, '.') + headings, subdirs = toc.extractdir(root) assert subdirs == ['bar'] assert headings == [ { diff --git a/yhttp/markdown/server.py b/yhttp/markdown/server.py index 82cc1d2..41d9d73 100644 --- a/yhttp/markdown/server.py +++ b/yhttp/markdown/server.py @@ -1,5 +1,6 @@ import os import re +import functools import sass as libsass from mako.lookup import TemplateLookup @@ -124,9 +125,8 @@ def get(req, path=None): ) # Check exclusiono - for pat in app.excludes: - if pat.match(path): - return notfound(req, path, **renderargs) + if app.excluded(path): + return notfound(req, path, **renderargs) # Default document default = cfg.default @@ -142,7 +142,11 @@ def get(req, path=None): if os.path.exists(targetpath): # Generate TOC - headings, subdirs = toc.extractdir(targetpath, '', cfg.toc.depth) + headings, subdirs = toc.extractdir( + targetpath, + app.excluded, + depth=cfg.toc.depth, + ) renderargs['toc'] = headings renderargs['subdirs'] = subdirs @@ -158,13 +162,25 @@ def get(req, path=None): ) +def _excluded(patterns, path): + for pat in patterns: + if pat.match(path): + return True + + return False + + @app.when def ready(app): # metadata (favicon, logo and etc) if not os.path.isdir(cfg.metadata.physical): cfg.metadata.physical = os.path.join(here, 'defaultmetadata') - app.excludes = [re.compile(p) for p in cfg.exclude or []] + app.excluded = functools.partial( + _excluded, + [re.compile(p) for p in cfg.exclude or []] + ) + app.loopkup = TemplateLookup( directories=[os.path.join(here, 'templates')], cache_enabled=not cfg.debug, diff --git a/yhttp/markdown/toc.py b/yhttp/markdown/toc.py index d154ee2..82638db 100644 --- a/yhttp/markdown/toc.py +++ b/yhttp/markdown/toc.py @@ -68,18 +68,20 @@ def extract(filename, lines, depth=6): return headings -def extractdir(root, directory, depth=6): +def extractdir(directory, excluded=None, depth=6): headings = [] stack = [headings] subdirs = [] - root = os.path.abspath(root) + directory = os.path.abspath(directory) - dirpath = os.path.join(root, directory) - for item in sorted(os.listdir(dirpath)): + for item in sorted(os.listdir(directory)): if SYSFILES.match(item): continue - filepath = os.path.join(dirpath, item) + if excluded and excluded(item): + continue + + filepath = os.path.join(directory, item) if os.path.isdir(filepath): subdirs.append(item) continue @@ -90,7 +92,7 @@ def extractdir(root, directory, depth=6): with open(filepath) as file: _extract( stack, - os.path.relpath(filepath, root), + os.path.relpath(filepath, directory), file, depth )