Skip to content

Commit

Permalink
feat: better warning messages
Browse files Browse the repository at this point in the history
  • Loading branch information
gvwilson committed Apr 4, 2024
1 parent e462450 commit 31fa53e
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
6 changes: 6 additions & 0 deletions lib/mccole/extensions/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

import ark

EXCLUSIONS = {
"__pycache__",
}


@ark.filters.register(ark.filters.Filter.LOAD_NODE_DIR)
def keep_dir(value, path):
"""Do not process directories excluded by parent."""
if path.name in EXCLUSIONS:
return False
path = str(path).replace(ark.site.src(), "").lstrip("/")
return not any(path.startswith(x) for x in ark.site.config["exclude"])

Expand Down
13 changes: 8 additions & 5 deletions lib/mccole/extensions/inclusions.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ def inclusion(pargs, kwargs, node):
if not kwargs:
body = _whole(path)
elif "pattern" in kwargs:
body = _match(path, kwargs["pattern"], indent)
body = _match(node, path, kwargs["pattern"], indent)
elif "mark" in kwargs:
body = _extract(path, kwargs["mark"], indent)
body = _extract(node, path, kwargs["mark"], indent)
else:
util.fail(f"Badly-formed inclusion for '{path}' in {node} with '{kwargs}'")
body = f"```{kind}\n{body}\n```\n"
Expand All @@ -58,7 +58,7 @@ def inclusion(pargs, kwargs, node):
util.fail(f"Unable to read inclusion '{path}' in {node}.")


def _extract(filepath, mark, indent):
def _extract(node, filepath, mark, indent):
"""Extract portion of file in comment markers."""
if isinstance(filepath, str):
filepath = Path(filepath)
Expand All @@ -74,19 +74,22 @@ def _extract(filepath, mark, indent):
text = text.split(before)[1].split(after)[0]
elif before_in or after_in:
util.fail(
f"Mis-matched mark with '{mark}' in {filename} in {node.path}"
f"Mis-matched mark in {node.path}: '{mark}' in '{filepath}'"
)
else:
util.warn(f"No mark in {node.slug}: '{mark}' in '{filepath}'")
return text


def _match(filepath, pattern, indent):
def _match(node, filepath, pattern, indent):
"""Match a pattern against the contents of a file."""
if isinstance(filepath, str):
filepath = Path(filepath)
doc = parse(filepath.read_text())
matchers = _translate_pattern(pattern)
node = _match_rec(doc, matchers)
if not node:
util.warn(f"Failed to match inclusion pattern in {node.path}: '{filepath}' and '{pattern}'")
return None
result = unparse(node)
if indent:
Expand Down
3 changes: 1 addition & 2 deletions lib/mccole/extensions/termdefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import ark
import ibis
import sys

from glossary import glossary_ref
import util
Expand All @@ -28,5 +27,5 @@ def _make_ref(node, glossary, lang, key):
try:
return glossary_ref([key, glossary[key][lang]["term"]], {}, node)
except KeyError:
print(f"Unknown glossary key {key} in {node.slug}", file=sys.stderr)
util.warn(f"Unknown glossary key {key} in {node.slug}")
return None
7 changes: 6 additions & 1 deletion lib/mccole/extensions/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def ensure_links():

def fail(msg):
"""Fail unilaterally."""
print(msg, file=sys.stderr)
warn(msg)
raise AssertionError(msg)


Expand Down Expand Up @@ -96,3 +96,8 @@ def require_file(node, filename, kind):
"""Require that a file exists."""
filepath = Path(Path(node.filepath).parent, filename)
require(filepath.exists(), f"Missing {kind} file {filename} from {node.path}")


def warn(msg):
"""Print warning message."""
print(msg, file=sys.stderr)
2 changes: 1 addition & 1 deletion lib/mccole/resources/mccole.css
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

/* Font sizings */
--size-body: large;
--size-code: 90%;
--size-code: 80%;
--size-subtitle: x-large;

/* Spacings */
Expand Down

0 comments on commit 31fa53e

Please sign in to comment.