agentleFS
jyjeannejyjeanne/dita-skills/context-setup/SKILL.md

context-setup

Skill2 starsChanged 6 months agoMITView on GitHub

Guides correct setup of pydita's DitaContext for a DITA processing pipeline. Use this skill when you need to wire together resolveMap(), KeyspaceManager, DitavalFilter, and DitaContext, or when a pipeline is failing because of incorrect initialisation order, missing DITA-OT configuration, or a mismatched errors dictionary.

1 other project uses a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

1 rule. About 0 are common, 0 are distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Use one errors = {} dict and pass it to every pydita call.

    line 84

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Overview

DitaContext is pydita's central processing object. It bundles:

  • a KeyspaceManager (key spaces built from the resolved map)
  • a DitavalFilter (conditional filtering)
  • a shared errors dict (error accumulation across all pydita calls)
  • an optional mapContext element (current position in the map tree)

Initialisation must follow a strict order — every step depends on the output of the previous one.


Canonical setup pattern

from pydita import resolvemap, xmlutils
from pydita.ditaval import DitavalFilter
from pydita.keyspacemgr import KeyspaceManager
from pydita.ditacontext import DitaContext

# 1. Shared error collector (pass the same dict everywhere)
errors: dict = {}

# 2. Optionally load a DITAVAL filter
ditaval_filter = DitavalFilter(errors=errors)
# — or load from a file (pass a list of paths):
# ditaval_filter = DitavalFilter(ditavalFiles=["path/to/filter.ditaval"], errors=errors)

# 3. Resolve the root map (flattens the map tree into a single document)
resolved_map = resolvemap.resolveMap(
    "path/to/root.ditamap",
    ditavalFilter=ditaval_filter,
    errors=errors,
    debug=False,
)

# 4. Build the key space manager from the resolved map
ksm = KeyspaceManager(resolvedMap=resolved_map, errors=errors)

# 5. Assemble DitaContext
ctx = DitaContext(
    keySpaceManager=ksm,
    ditavalFilter=ditaval_filter,
    errors=errors,
)

DitaContext constructor parameters

ParameterTypeDefaultNotes
keySpaceManagerKeyspaceManagerrequiredMust be pre-populated
keySpaceKeySpaceNoneDefaults to ksm.getRootKeyspace()
mapContextElementNoneCurrent <topicref> or map element
ditavalFilterDitavalFilterNoneDefaults to a pass-through filter
errorsdict{}Always pass the same dict you used in steps 2–4
debugboolFalseEnables verbose print output

Common mistakes

Wrong order: KeyspaceManager built before resolveMap

# WRONG — KeyspaceManager receives an unresolved ElementTree
ksm = KeyspaceManager(resolvedMap=etree.parse("root.ditamap"))

KeyspaceManager expects a map that has already had submaps inlined by resolvemap.resolveMap(). Building it from a raw parse result silently produces an incomplete key space.

Separate errors dicts

# WRONG — errors reported during resolveMap() are not visible in DitaContext
resolved_map = resolvemap.resolveMap("root.ditamap", errors={})
ctx = DitaContext(ksm, errors={})

Use one errors = {} dict and pass it to every pydita call.

Missing DITA-OT

resolveMap() calls xmlutils.getDTDAwareParser() which requires DITA-OT. If DITA_OT_DIR is not set:

KeyError: 'DITA_OT_DIR'

Set the environment variable or add dita.ot.dir=/path/to/dita-ot to ~/.build.properties.


Updating the context during processing

# Switch the active key space (e.g., when entering a keyscoped submap)
ctx._keySpace = ctx.getKeyspaceManager().getKeyspaceByMapUri(submap_uri)

# Replace the DITAVAL filter
ctx.setDitavalFilter(new_filter)

# Record an error from within processing code
ctx.recordError("my-key", exception, operation="myFunction()")

# Enable debug logging
ctx.setDebug(True)

Checking for errors after processing

from pydita import loggingutils

if ctx.getErrors():
    print(loggingutils.reportErrors(ctx.getErrors(), showTraceBack=True))

See also

  • map-resolve — detailed resolveMap() API reference
  • keyspace-debug — how to inspect and debug key spaces
  • error-handling — full error model reference

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.