Skip to content

Commit

Permalink
Fix: navigation and exclusion
Browse files Browse the repository at this point in the history
  • Loading branch information
pylover committed Sep 1, 2024
1 parent 08dd195 commit add59b0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
1 change: 0 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/test_toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 == [
{
Expand Down
26 changes: 21 additions & 5 deletions yhttp/markdown/server.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import re
import functools

import sass as libsass
from mako.lookup import TemplateLookup
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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,
Expand Down
14 changes: 8 additions & 6 deletions yhttp/markdown/toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
)
Expand Down

0 comments on commit add59b0

Please sign in to comment.