Skip to content

Commit

Permalink
Merge branch 'master' into GH-76_core_functionality_tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pydsigner committed Feb 3, 2024
2 parents 0f2b149 + 4726aa6 commit 66e90f4
Show file tree
Hide file tree
Showing 18 changed files with 248 additions and 250 deletions.
2 changes: 1 addition & 1 deletion examples/basic_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
Rule(
REMatcher(r'.*\.md'),
[OutputDirPathCalc('.html'), None],
JinjaMarkdownStep()
JinjaMarkdownStep(frontmatter_parser='simple')
),
# Copy everything else in static/ directories through.
Rule(
Expand Down
2 changes: 2 additions & 0 deletions examples/basic_site/index.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
---
template: base.jinja.html
footnote: Generated with Anchovy!
---

# Welcome to Example Site 1!

Expand Down
13 changes: 7 additions & 6 deletions examples/code_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
CustodyEntry,
DirectCopyStep,
InputBuildSettings,
JinjaExtendedMarkdownStep,
JinjaMarkdownStep,
JinjaRenderStep,
OutputDirPathCalc,
PathCalc,
REMatcher,
ResourcePackerStep,
Rule,
UnpackArchiveStep,
WebIndexPathCalc,
WorkingDirPathCalc,
)

Expand All @@ -28,8 +29,8 @@


MARKDOWN_TEMPLATE = """---
template = "base.jinja.html"
footnote = "Generated from {path} by Anchovy."
template: base.jinja.html
footnote: Generated from {path} by Anchovy.
---
# {path}
Expand Down Expand Up @@ -131,8 +132,8 @@ def __call__(self, path: Path, output_paths: list[Path]):
# Render markdown files and stop processing.
Rule(
REMatcher(r'.*\.md'),
[WorkingDirPathCalc('.html'), None],
JinjaExtendedMarkdownStep(
[WebIndexPathCalc('working_dir', '.html'), None],
JinjaMarkdownStep(
default_template='base.j.html',
pygments_params={'classprefix': 'pyg-'},
)
Expand All @@ -149,7 +150,7 @@ def __call__(self, path: Path, output_paths: list[Path]):
[WorkingDirPathCalc('.html'), None],
CodeIndexStep(
'*.py',
OutputDirPathCalc('.html')
WebIndexPathCalc('output_dir', '.html')
)
),
# Ignore all other Jinja templates.
Expand Down
2 changes: 1 addition & 1 deletion examples/code_index/index.jinja.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<main>
<h1>Examples</h1>
{% for source, target in leaves %}
<a href="{{target.as_posix()}}"><h2>{{ source.name }}</h2></a>
<a href="{{target.parent.as_posix()}}"><h2>{{ source.name }}</h2></a>
{% endfor %}
</main>
{% endblock %}
2 changes: 1 addition & 1 deletion examples/gallery.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
Rule(
REMatcher(r'.*\.md'),
[OutputDirPathCalc('.html'), None],
JinjaMarkdownStep()
JinjaMarkdownStep(frontmatter_parser='toml')
),
# Convert JPG files to WebP...
Rule(
Expand Down
14 changes: 8 additions & 6 deletions examples/gallery/index.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
template: base.jinja.html
footnote: Generated with Anchovy!
---
template = "base.jinja.html"
footnote = "Generated with Anchovy!"
---

# Photo Gallery

<div class="gallery">
<a href="static/images/home-sweet-home-cdk.webp"><img src="static/images/home-sweet-home-cdk.thumb.png" alt="A Vintage Automobile"></a>
<a href="static/images/nice-wheels-cdk.webp"><img src="static/images/nice-wheels-cdk.thumb.png" alt="A Old Wood-Sided House"></a>
<a href="static/images/stormy-cdk.webp"><img src="static/images/stormy-cdk.thumb.png" alt="A Boardwalk and Damaged Pines During a Storm"></a>
<a href="static/images/quayside-newcastle.webp"><img src="static/images/quayside-newcastle.thumb.png" alt="A Quay in Newcastle, England"></a>
<a href="static/images/home-sweet-home-cdk.webp"><img src="static/images/home-sweet-home-cdk.thumb.webp" alt="A Vintage Automobile"></a>
<a href="static/images/nice-wheels-cdk.webp"><img src="static/images/nice-wheels-cdk.thumb.webp" alt="A Old Wood-Sided House"></a>
<a href="static/images/stormy-cdk.webp"><img src="static/images/stormy-cdk.thumb.webp" alt="A Boardwalk and Damaged Pines During a Storm"></a>
<a href="static/images/quayside-newcastle.webp"><img src="static/images/quayside-newcastle.thumb.webp" alt="A Quay in Newcastle, England"></a>
</div>
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ Source = "https://github.com/pydsigner/anchovy"

[project.optional-dependencies]
toml = ['tomli >= 2.0.1; python_version < "3.11"']
yaml = ["ruamel.yaml>=0.18.5"]
jinja = ["Jinja2>=3.1.2"]
markdown = [
"anchovy[jinja]",
"anchovy[toml]",
"anchovy[yaml]",
"markdown_it_py>=3.0.0",
"mdit_py_plugins>=0.4.0",
# Pygments is a fairly large dependency and perhaps not critical; but we
Expand Down Expand Up @@ -71,7 +73,6 @@ all = ["anchovy[base]"]
# ignore this option.
cq = [
"anchovy[all]",
"commonmark>=0.9.1", "markdown>=3.4.3", "mistletoe>=1.1.0",
"tqdm>=4.65.0",
"minify-html>=0.11.1",
"pylint",
Expand Down
2 changes: 1 addition & 1 deletion src/anchovy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
from .include import RequestsFetchStep, UnpackArchiveStep, URLLibFetchStep
from .jinja import JinjaExtendedMarkdownStep, JinjaMarkdownStep, JinjaRenderStep
from .minify import AssetMinifierStep, CSSMinifierStep, HTMLMinifierStep, ResourcePackerStep
from .paths import DirPathCalc, OutputDirPathCalc, REMatcher, WorkingDirPathCalc
from .paths import DirPathCalc, OutputDirPathCalc, REMatcher, WebIndexPathCalc, WorkingDirPathCalc
from .simple import BaseCommandStep, BaseStandardStep, DirectCopyStep
51 changes: 51 additions & 0 deletions src/anchovy/components/md_frontmatter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import sys
import typing as t


def simple_frontmatter_parser(content: str) -> dict:
"""
Read metadata from the front of a markdown-formatted text in a very simple
YAML-like format, without value parsing.
"""
meta = {}
lines = content.splitlines()

for line in lines:
if ':' not in line:
break
key, value = line.split(':', 1)
if not key.isidentifier():
break
meta[key.strip()] = value.strip()

print(meta, '...')
return meta


def get_toml_frontmatter_parser():
if sys.version_info < (3, 11):
import tomli as tomllib
else:
import tomllib
return tomllib.loads


def get_yaml_frontmatter_parser():
from ruamel.yaml import YAML
return YAML(typ='safe').load


FrontMatterParser = t.Callable[[str], dict]
FrontMatterParserName = t.Literal['simple', 'toml', 'yaml']

FRONTMATTER_PARSER_FACTORIES: dict[FrontMatterParserName, t.Callable[[], FrontMatterParser]] = {
'simple': lambda: simple_frontmatter_parser,
'toml': get_toml_frontmatter_parser,
'yaml': get_yaml_frontmatter_parser,
}


def get_frontmatter_parser(parser) -> FrontMatterParser:
if callable(parser):
return parser
return FRONTMATTER_PARSER_FACTORIES[parser]()
15 changes: 9 additions & 6 deletions src/anchovy/components/md_rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@
"""
from __future__ import annotations

import sys
import typing as t

from markdown_it.common.utils import escapeHtml, unescapeAll
from markdown_it.renderer import RendererHTML

if sys.version_info < (3, 11):
import tomli as tomllib
else:
import tomllib
from .md_frontmatter import simple_frontmatter_parser, FrontMatterParser

if t.TYPE_CHECKING:
from collections.abc import Sequence
Expand Down Expand Up @@ -60,6 +56,10 @@ class AnchovyRendererHTML(RendererHTML):
A customized markdown-it-py HTML renderer, with hooks for better pygments
integration and toml frontmatter support.
"""
def __init__(self, parser: t.Any = None):
super().__init__(parser)
self.front_matter_parser: FrontMatterParser = simple_frontmatter_parser

# https://github.com/executablebooks/markdown-it-py/issues/256
def fence(self, tokens: Sequence[Token], idx: int, options: OptionsDict, env: EnvType):
"""
Expand All @@ -76,10 +76,13 @@ def fence(self, tokens: Sequence[Token], idx: int, options: OptionsDict, env: En
or escapeHtml(token.content)
)

def set_front_matter_parser(self, parser: FrontMatterParser):
self.front_matter_parser = parser

def front_matter(self, tokens: Sequence[Token], idx: int, _options: OptionsDict, env: EnvType):
"""
Handles parsing markdown frontmatter using TOML.
"""
parsed = tomllib.loads(tokens[idx].content)
parsed = self.front_matter_parser(tokens[idx].content)
env['anchovy_meta'].update(parsed)
return ''
3 changes: 2 additions & 1 deletion src/anchovy/custody.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class Custodian:
Class for managing custody info and intelligent rebuilds.
"""
encoding = 'utf-8'
newline = '\n'
context: 'Context'

def __init__(self,
Expand Down Expand Up @@ -193,7 +194,7 @@ def dump_file(self, path: Path):
'graph': self.graph,
'meta': self.meta,
}
with path.open('w', encoding=self.encoding) as file:
with path.open('w', encoding=self.encoding, newline=self.newline) as file:
json.dump(data, file, indent=2)

def update_meta(self, entry: CustodyEntry):
Expand Down
Loading

0 comments on commit 66e90f4

Please sign in to comment.