Skip to content

Instantly share code, notes, and snippets.

View pauleveritt's full-sized avatar

Paul Everitt pauleveritt

View GitHub Profile
@pauleveritt
pauleveritt / scratch_2.py
Created September 8, 2024 17:13
Nested template
# We've generally thought of nesting being an interpolation
# that called the html "tag function" (now template function)
def Sidebar(heading: str, items: list[Any]) -> HTMLTemplate:
return t"""
<aside>
<h1>{heading}</h1>
<ul>
{html(t"<li>{item}</li>" for item in items}")}
</ul>
@pauleveritt
pauleveritt / test_svcs.py
Created August 31, 2024 13:28
Simulating Hopscotch in svcs
from dataclasses import dataclass
from pathlib import PurePath
from typing import Protocol, Callable, Any
import pytest
from svcs import Container, Registry
@dataclass
class Registration:
@pauleveritt
pauleveritt / template.tsx
Created June 19, 2024 20:29
11ty TSX subcomponents and shortcakes
/*
Summary: TSX subcomponents don't have access to shortcodes. The
this.context.shortcodes.css() function runs, but doesn't put
its content into the bundle.
*/
export type Context = {
// The "this" value has a "context" managed by Preact.
@pauleveritt
pauleveritt / site.test.js
Created April 17, 2024 17:40
11ty constructor, options, dirs
test("should load an Eleventy site", async () => {
const elev = new Eleventy("./", "./_site", {
dir: {
includes: "my_includes",
layouts: "my_layouts",
},
});
await elev.init();
const d = elev.config.dir;
expect(elev.config.dir.includes).toEqual("my_includes");
@pauleveritt
pauleveritt / memoize2.py
Created November 12, 2023 20:25
Series of examples gradually building up a memoize example.
from __future__ import annotations
from typing import Any
from tagstr import Thunk
from tagstr.memoize import TagStringArgs, TagStringCallable
def immutable_bits(*args: str | Thunk) -> tuple[str | tuple[Any], ...]:
bits = []
for arg in args:
@pauleveritt
pauleveritt / foo.test.tsx
Created May 5, 2023 17:31
Try to get `this` into a layout view.
import type { FunctionComponent, VNode } from "preact";
import { createContext, render as preactRender } from "preact";
import { expect, test } from "vitest";
export type EleventyContext = {
siteTitle: string;
};
const Context = createContext<EleventyContext | null>(null);
<h1>Hello Animals</h1>
<ng-container *ngIf="isFish(pet)">
<button (click)="pet.swim()">Swim!</button>
</ng-container>
<ng-container *ngIf="isFish(pet)">
<button (click)="pet.fly()">Fly!</button>
</ng-container>
@pauleveritt
pauleveritt / scratch.html
Created September 25, 2022 20:56
Idiomorph with a custom element.
<html>
<head>
<title>Custom Element Morph</title>
<script src="https://unpkg.com/idiomorph"></script>
</head>
<body>
<hello-world name="World"></hello-world>
<button id="update">Update</button>
<script defer>
class HelloWorld extends HTMLElement {
@pauleveritt
pauleveritt / custom_elements.html
Created September 25, 2022 20:56
Use of idiomorph with a custom element.
Idiomorph.morph(target, newHTML, {morphStyle: 'innerHTML'});
@pauleveritt
pauleveritt / scratchpad.py
Created August 4, 2021 08:21
Broken example of a protocol registry
"""A broken attempt at a Protocol-based registry.
I have a system with replaceable implementations. A ``Heading``
might be asked for on a page, but which one you get depends
on some criteria.
mypy doesn't seem to like using Protocols at runtime. This
small module has 7 errors.
"""
from dataclasses import dataclass