Site Refactor — named refactoring patterns
You are running the refactor skill. Your job: apply a specific, named refactoring pattern to a website codebase. Unlike site-audit which diagnoses, refactor acts.
Available patterns
1. extract-template
Generate a Jinja2 base template + per-page content blocks + build script. The output is still plain .html files — Jinja2 is build-time only, no framework migration.
Steps:
- Read all HTML files, identify the shared skeleton (head, nav, analytics, footer)
- Extract shared parts into
templates/base.htmlwith{% block content %}{% endblock %} - Extract analytics into
templates/partials/analytics.html - Extract nav/footer into
templates/partials/nav.htmlandfooter.html - Create per-page templates in
templates/pages/that extendbase.html - Create
site.jsonwith per-page data (title, description, OG tags, course-specific data) - Test that
python3 tools/html_template_engine.py --templates templates/ --out . --data site.jsonrebuilds identical HTML
2. centralize-analytics
Extract all analytics/tracking code (GA4, Meta Pixel, Clarity, CAPI) from individual pages into shared files.
Steps:
- Identify all tracking snippets across pages (use
site-auditfindings) - Create
analytics-head.html(gtag, fbq, clarity — goes in<head>) - Create
analytics.js(event handlers, form tracking, CAPI calls) - Update all pages to include the shared files
- Verify tracking IDs are consistent
3. centralize-forms
Extract form handling (form webhook, server-side event submission) into shared form-handler.js.
Steps:
- Identify the form submission pattern duplicated across pages
- Create
form-handler.jswith configurable data attributes (data-course,data-source) - Update all pages to use the shared script
- Test form submission still works
4. add-seo-basics
Generate missing SEO files and tags.
Steps:
- Generate
sitemap.xmllisting all HTML pages with their URLs - Generate
robots.txtwithSitemap:directive - Add
<link rel="canonical">to all pages - Add JSON-LD
Courseschema to course pages - Add JSON-LD
Organizationschema to index page
5. add-cache-busting
Replace manual version query strings with content-hash based cache busting.
Steps:
- Compute MD5 hash of each CSS/JS file (first 8 chars)
- Update all
<link>and<script>references to use?h=<hash>instead of?v=N - Document the process so future changes auto-update
6. optimize-images
Add missing image attributes and flag oversized images.
Steps:
- Scan all
<img>tags for missingwidth/heightattributes - Add
loading="lazy"to below-the-fold images - Flag images over 200KB for manual WebP conversion
- Report total image weight savings potential
7. accessibility-fixes
Fix mechanical accessibility issues.
Steps:
- Add placeholder
alt=""to decorative images,alt="[REVIEW]"to content images - Add
<label>elements for all form inputs - Fix heading hierarchy gaps
- Add
<main>landmark if missing
How to use
Step 0 — Parse arguments
First word of $ARGUMENTS is the pattern name. Rest is the site root path.
Default path: current working directory (else ask for the site root).
If the pattern name is not recognized, list available patterns and STOP.
Step 1 — Pre-flight analysis
Before any changes:
- Read the files that will be affected (Glob + Read)
- Count: how many files, estimated lines changed
- APPROVAL GATE: Present the user with:
- Pattern name and what it does
- Files that will be modified
- Preview of the first file's changes (first 20 lines of diff)
- Estimated scope
- Wait for explicit "go ahead" before proceeding
Step 2 — Apply the pattern
Follow the steps listed for the chosen pattern. Make changes with Edit/Write.
Step 3 — Verify
Run python3 tools/site_audit.py --root "<site_root>" --mechanical-only --checks <relevant> to confirm the finding is resolved.
Report: files modified, lines changed, findings before/after.
Important constraints
- Approval gate is mandatory. Never modify files without showing the plan first.
- Preserve existing functionality. Every refactoring must produce functionally identical output.
- One pattern per invocation. User can chain by re-invoking the skill.
- Don't force a framework.
extract-templateuses Jinja2 as a build tool — output is plain HTML.
See also
- tools/site_audit.py — diagnostic tool
- tools/html_template_engine.py — Jinja2 build tool
- workflows/site_refactor.md — the SOP