Skip to content

Commit

Permalink
feat: add site statistics
Browse files Browse the repository at this point in the history
version: 0.2.5
  • Loading branch information
gvwilson committed Aug 30, 2024
1 parent 8aca1b9 commit 24c4ea8
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ After installation, the following commands will be available:

- `mccole lint`: internal project check
- `mccole render`: Markdown-to-HTML translator
- `mccole stats`: site statistics
6 changes: 6 additions & 0 deletions mccole/clui.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from .lint import lint, parse_args as lint_parser
from .render import render, parse_args as render_parser
from .stats import stats, parse_args as stats_parser


INSTALL_FILES = (
Expand All @@ -23,10 +24,13 @@ def main():
'''Main driver.'''
parser = argparse.ArgumentParser()
parser.add_argument('--version', action='store_true', help='show version')

subparsers = parser.add_subparsers(dest='cmd')
install_parser(subparsers.add_parser('install', help='install files'))
lint_parser(subparsers.add_parser('lint', help='check site'))
render_parser(subparsers.add_parser('render', help='build site'))
stats_parser(subparsers.add_parser('stats', help='show stats'))

opt = parser.parse_args()
if opt.version:
print(importlib.metadata.version('mccole'))
Expand All @@ -36,6 +40,8 @@ def main():
lint(opt)
elif opt.cmd == 'render':
render(opt)
elif opt.cmd == 'stats':
stats(opt)
else:
print(f'unknown command {opt.cmd}', file=sys.stderr)
sys.exit(1)
Expand Down
13 changes: 1 addition & 12 deletions mccole/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@
import re
import tomli

from .util import SUFFIXES, SUFFIXES_SRC, find_files
from .util import MD_LINK_DEF, SUFFIXES, SUFFIXES_SRC, find_files, find_key_defs


BIB_REF = re.compile(r"\[.+?\]\(b:(.+?)\)", re.MULTILINE)
GLOSS_REF = re.compile(r"\[.+?\]\(g:(.+?)\)", re.MULTILINE)
KEY_DEF = re.compile(r'^<span\s+id="(.+?)">.+?</span>\s*$', re.MULTILINE)
MD_CODEBLOCK_FILE = re.compile(r"^```\s*\{\s*\.(.+?)\s+\#(.+?)\s*\}\s*$(.+?)^```\s*$", re.DOTALL + re.MULTILINE)
MD_FILE_LINK = re.compile(r"\[(.+?)\]\((.+?)\)", re.MULTILINE)
MD_LINK_DEF = re.compile(r"^\[(.+?)\]:\s+(.+?)\s*$", re.MULTILINE)
MD_LINK_REF = re.compile(r"\[(.+?)\]\[(.+?)\]", re.MULTILINE)

DEFAULT_CONFIG = {
Expand Down Expand Up @@ -83,15 +81,6 @@ def check_references(files, term, regexp, available):
return ok


def find_key_defs(files, term):
"""Find key definitions in definition list file."""
candidates = [k for k in files if term in str(k).lower()]
if len(candidates) != 1:
return None
file_key = candidates[0]
return set(KEY_DEF.findall(files[file_key]))


def is_missing(actual, available):
"""Is a file missing?"""
return (not actual.exists()) or (
Expand Down
48 changes: 48 additions & 0 deletions mccole/stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""Report site stats."""

import argparse
from prettytable import PrettyTable

from .util import MD_LINK_DEF, SUFFIXES, SUFFIXES_SRC, find_files, find_key_defs


TABLE_FMT = {
"stat": "l",
"value": "r",
}


def stats(opt):
"""Main driver."""
table = PrettyTable(field_names=TABLE_FMT.keys())
for k, v in TABLE_FMT.items():
table.align[k] = v

files = find_files(opt, set(["bin", opt.out]))
table.add_row(("bibliography entries", len(find_key_defs(files, "bibliography"))))
table.add_row(("glossary entries", len(find_key_defs(files, "glossary"))))
table.add_row(("link definitions", len(find_markdown_link_defs(files))))

print(table)


def find_markdown_link_defs(files):
"""Collect Markdown link key definnitions."""
found = set()
for filepath, content in files.items():
if filepath.suffix == ".md":
for link in MD_LINK_DEF.finditer(content):
found.add(link[0])
return found


def parse_args(parser):
"""Parse command-line arguments."""
parser.add_argument("--out", type=str, default="docs", help="output directory")
parser.add_argument("--root", type=str, default=".", help="root directory")
return parser


if __name__ == "__main__":
opt = parse_args(argparse.ArgumentParser()).parse_args()
stats(opt)
12 changes: 12 additions & 0 deletions mccole/util.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
"""Utilities."""

from pathlib import Path
import re


KEY_DEF = re.compile(r'^<span\s+id="(.+?)">.+?</span>\s*$', re.MULTILINE)
MD_LINK_DEF = re.compile(r"^\[(.+?)\]:\s+(.+?)\s*$", re.MULTILINE)
SUFFIXES_BIN = {".ico", ".jpg", ".png"}
SUFFIXES_SRC = {".css", ".html", ".js", ".md", ".py", ".sh", ".txt"}
SUFFIXES_TXT = SUFFIXES_SRC | {".csv", ".json", ".svg"}
Expand All @@ -18,6 +21,15 @@ def find_files(opt, root_skips=[]):
}


def find_key_defs(files, term):
"""Find key definitions in definition list file."""
candidates = [k for k in files if term in str(k).lower()]
if len(candidates) != 1:
return None
file_key = candidates[0]
return set(KEY_DEF.findall(files[file_key]))


def find_symlinks(opt, root_skips=[]):
"""Collect all interesting files."""
return [
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mypkg = ["*.css", "*.js", "*.html"]

[project]
name = "mccole"
version = "0.2.4"
version = "0.2.5"
authors = [
{name = "Greg Wilson", email = "[email protected]"},
]
Expand All @@ -31,6 +31,7 @@ dependencies = [
"html5validator>=0.4.2",
"jinja2>=3.1.4",
"markdown>=3.6",
"prettytable>=3.11",
"ruff>=0.6.0",
"tomli>=2.0.1",
]
Expand Down

0 comments on commit 24c4ea8

Please sign in to comment.