Keep Blocks¶
Keep blocks are where the pattern-writing ends. The tail-capture regex kept reappearing everywhere — "everything past this point is mine" — and the realization followed: it would be better to just name the blocks that should not be touched. A keep block is exactly that: visible marker lines declaring a developer-owned region inside a provider-rendered file. The intention is self-documenting, and there is no regex to write.
The template ships a default between the markers for fresh projects. If your file already has the marker pair, what's between them survives every apply — the markers themselves stay in the output (visible contract), only the directive line is stripped.
Syntax¶
## repolish:keep-block[notes] start="<!-- notes-start -->" end="<!-- notes-end -->"
<!-- notes-start -->
Default content for new projects.
<!-- notes-end -->
start/endare literal marker strings — use whatever comment style fits the file (they are ordinary lines, and they remain in the output).- Either side can match by regular expression instead:
start-regex/end-regex(see start-regex and end-regex). - If your project file has no matching marker pair, the template default is kept.
- Supports the
|after-renderphase — required when the blocks are generated by Jinja (see end-regex).
!!! tip "Placement" A keep-block directive governs the region(s) matching its markers that follow it, up to the next keep directive line. Consecutive keep-block directive lines stack: the run shares one segment, and each directive's regions are found anywhere in it, in any order (see Stacked directives).
Worked example¶
repolish/README.md.jinja:
README.md — you already filled the block:
Save this as scratch.yaml:
template: |
# My Project
## repolish:keep-block[custom] start="<!-- custom-start -->" end="<!-- custom-end -->"
<!-- custom-start -->
Default content for new projects.
<!-- custom-end -->
target: |
# My Project
<!-- custom-start -->
Our setup notes: run `make bootstrap` first.
<!-- custom-end -->
and run:
Repeated regions¶
One directive covers many blocks that share the same markers — repolish matches them in encounter order, first block to first block, second to second:
## repolish:keep-block[notes] start="<!-- notes-start -->" end="<!-- notes-end -->"
## Installation
<!-- notes-start -->
_No notes yet._
<!-- notes-end -->
## Usage
<!-- notes-start -->
_No notes yet._
<!-- notes-end -->
If your README already has both marker pairs filled in (say install instructions
under Installation, an example under Usage), each block is preserved in its own
position — no notes-2 variant needed. Blocks without a matching pair in your
file keep the template default.
(To see the pairing, extend the Try-it scratch file above with a second marked section.)
Stacked directives¶
Keep-block directive lines can sit together instead of each one directly above its own region. Consecutive directive lines form a run that shares the segment between it and the next keep directive: each directive governs the region(s) matching its own markers anywhere in that segment, whatever the order.
## repolish:keep-block[plugins] start="# -- plugins-start" end="# -- plugins-end"
## repolish:keep-block[envs] start="# -- envs-start" end="# -- envs-end"
Intro default.
# -- plugins-start
PLUGINS = []
# -- plugins-end
# -- envs-start
ALLOWED_ENVIRONMENTS = ["development"]
# -- envs-end
Interleaving (each directive directly above the block(s) it manages) means the same thing as before; stacking is the compact form for files that manage several regions in one section. When several directives share the same markers, one directive already covers every repeated block — stacked duplicates are redundant, and only the first governs the regions.
Paste the example into a repolish preview scratch
file (template plus your filled target) to watch both regions reconcile.
In Python files¶
Comment style is free-form, so keep blocks work in code too — e.g. a provider-managed registry whose lists only developers can fill. The directives can be interleaved (each directly above its own region, as here) or stacked together:
"""Plugin registry — managed by repolish, lists owned by the project."""
# repolish:keep-block[plugins] start="# -- plugins-start" end="# -- plugins-end"
# -- plugins-start
PLUGINS = []
# -- plugins-end
# repolish:keep-block[envs] start="# -- envs-start" end="# -- envs-end"
# -- envs-start
ALLOWED_ENVIRONMENTS = ['development']
# -- envs-end
Once developers add entries, both lists survive every apply; the provider can still change imports, docstrings, and methods around them.
end-regex¶
end-regex closes each kept region dynamically, at the first following line
matching a pattern — the piece that made keep blocks usable inside Jinja loops,
where a literal end marker is awkward and every repetition is identical:
## repolish:keep-block[provider-additional|after-render] start="# additional-paths" end-regex="^provider[0-9]+:$"
{% for idx in providers %}
provider{{ idx }}:
- static{{ idx }}
# additional-paths
- default{{ idx }}
{% endfor %}
Two things do the work here:
|after-render— the blocks don't exist until Jinja renders the loop, so the directive waits for the second phase.end-regex="^provider[0-9]+:$"— each region closes at the next loop item (or at the end of the directive's section), so N rendered blocks pair first-to-first with N blocks in your file.
If no match is found before the section ends, the region closes at the section boundary.
Trying end-regex in preview
repolish preview runs the full pipeline — paste the
Jinja template as-is (loop and |after-render suffix included) plus the loop
variables under config.context, and the Result is the reconciled output.
start-regex¶
end-regex has a start-side sibling: start-regex opens each kept region at
the first line matching a pattern, instead of at one exact literal line. The
case that demands it is a template shipping a class that projects are meant to
make their own:
# repolish:keep-block[service] start-regex="^class \w+\((SomeBase|SomeOtherBase)\):$" end="# -- service-end"
class SomeService(SomeBase):
def handle(self, request):
raise NotImplementedError
# -- service-end
A literal start marker pins the region to one exact line. A project that
inherits differently (class CachedService(SomeOtherBase):) has no matching
marker, so its class could not live in the managed file — the only escape was
duplicating the class outside the provider's reach. With start-regex, the
region opens at the class line you wrote, whatever name or base it carries:
class CachedService(SomeOtherBase):
def handle(self, request):
return self.cache.get(request)
# -- service-end
The whole region — signature line included — survives every apply; the provider can still rewrite everything around it.
Matching follows the end-regex rules: the pattern is searched against each
line (stripped of surrounding whitespace), so anchor it with ^/$ when the
whole line should match. start-regex combines freely with end or
end-regex.
When to use¶
Keep blocks when a region is yours, not just a value. For single lines or structured key/value sections, regex and multiregex are enough; for the inverse — a provider filling blocks inside a file you own — see Insertions.