- 2.1.0 new opt-in `voice` module (D-0020): skill ckb-voice rewrites every prose file written under apply_to (outputs/answers, documents, reports, PROJECT-OVERVIEW) in a voice whose profile is a wiki page, wiki/voice/<name>.md (type voice-profile); settings in ckb.yaml (mode, profile, writer, apply_to, skip, chat); frozen spans never change, vague claims flagged not cut; `writer:` delegates to an external rewriting skill such as a personal ghost-writer. ckb-module writes a manifest's default_config into the entry's config:. Eval ckb-module/install-voice. - 2.2.0 new opt-in `convert` module (D-0021): skill ckb-convert and convert.py turn Markdown into Word/ODT/PDF/HTML through pandoc with a house style in templates/styles/; unit tests and eval ckb-module/install-convert. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
146 lines
6.2 KiB
Python
146 lines
6.2 KiB
Python
"""convert module: the Markdown clean-up is pure and always tested; the style
|
|
patching and real conversions run only where pandoc is installed."""
|
|
import contextlib
|
|
import importlib.util
|
|
import io
|
|
import re
|
|
import shutil
|
|
import tempfile
|
|
import unittest
|
|
import xml.dom.minidom
|
|
import zipfile
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
SPEC = importlib.util.spec_from_file_location(
|
|
"convert", str(ROOT / ".agents/modules/convert/skills/ckb-convert/scripts/convert.py"))
|
|
convert = importlib.util.module_from_spec(SPEC)
|
|
SPEC.loader.exec_module(convert)
|
|
|
|
DOC = """---
|
|
type: document
|
|
created: 2026-11-11
|
|
author: Ada Lovelace <ada@example.com>
|
|
---
|
|
|
|
# Status: Atlas
|
|
|
|
<!-- Ask the user. -->
|
|
See [[Atlas Retry Policy]] ([atlas](/wiki/entities/atlas.md)), [Maria](/wiki/entities/maria.md),
|
|
[[x/y/page|the page]], [web](https://example.com) and .
|
|
|
|
```md
|
|
[[keep]] <!-- keep --> [k](/wiki/k.md)
|
|
```
|
|
"""
|
|
|
|
|
|
class Prepare(unittest.TestCase):
|
|
def test_metadata_from_frontmatter_and_heading(self):
|
|
meta, body = convert.prepare(DOC, keep_links=False)
|
|
self.assertEqual(meta, {"title": "Status: Atlas", "author": "Ada Lovelace", "date": "2026-11-11"})
|
|
self.assertNotIn("# Status", body)
|
|
self.assertNotIn("type: document", body)
|
|
|
|
def test_kb_links_become_text_web_links_and_images_stay(self):
|
|
_, body = convert.prepare(DOC, keep_links=False)
|
|
self.assertIn("See Atlas Retry Policy, Maria,", body)
|
|
self.assertIn("the page,", body)
|
|
self.assertIn("[web](https://example.com)", body)
|
|
self.assertIn("", body)
|
|
self.assertNotIn("Ask the user", body)
|
|
|
|
def test_code_blocks_untouched(self):
|
|
_, body = convert.prepare(DOC, keep_links=False)
|
|
self.assertIn("[[keep]] <!-- keep --> [k](/wiki/k.md)", body)
|
|
|
|
def test_keep_links(self):
|
|
_, body = convert.prepare(DOC, keep_links=True)
|
|
self.assertIn("[Atlas Retry Policy](/wiki/entities/atlas.md)", body)
|
|
self.assertIn("[Maria](/wiki/entities/maria.md)", body)
|
|
|
|
def test_frontmatter_title_wins_and_heading_stays(self):
|
|
meta, body = convert.prepare("---\ntitle: Report\n---\n\n# Intro\n\nText\n", False)
|
|
self.assertEqual(meta["title"], "Report")
|
|
self.assertIn("# Intro", body)
|
|
|
|
def test_heading_after_text_is_not_the_title(self):
|
|
meta, body = convert.prepare("Some text.\n\n# Later\n", False)
|
|
self.assertNotIn("title", meta)
|
|
self.assertIn("# Later", body)
|
|
|
|
|
|
class SchemaOrder(unittest.TestCase):
|
|
def test_insert_respects_order(self):
|
|
inner = '<w:rFonts w:ascii="A"/><w:sz w:val="24"/><w:lang w:val="en"/>'
|
|
out = convert.set_child(inner, "color", '<w:color w:val="112233"/>', convert.RPR_ORDER)
|
|
self.assertLess(out.index("w:color"), out.index("w:sz"))
|
|
self.assertGreater(out.index("w:color"), out.index("w:rFonts"))
|
|
out = convert.set_child(out, "sz", '<w:sz w:val="22"/>', convert.RPR_ORDER)
|
|
self.assertEqual(out.count("<w:sz "), 1)
|
|
self.assertLess(out.index("w:sz"), out.index("w:lang"))
|
|
|
|
|
|
@unittest.skipUnless(shutil.which("pandoc"), "pandoc not installed")
|
|
class WithPandoc(unittest.TestCase):
|
|
def setUp(self):
|
|
self.tmp = tempfile.TemporaryDirectory()
|
|
self.root = Path(self.tmp.name)
|
|
(self.root / "outputs/documents").mkdir(parents=True)
|
|
(self.root / "outputs/documents/r.md").write_text(DOC, encoding="utf-8")
|
|
|
|
def tearDown(self):
|
|
self.tmp.cleanup()
|
|
|
|
def cli(self, *a):
|
|
with contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()):
|
|
return convert.main(["--root", str(self.root)] + list(a))
|
|
|
|
def test_style_settings_land_in_valid_xml(self):
|
|
self.assertEqual(self.cli("new-style", "house", "--body-font", "Georgia", "--size", "11",
|
|
"--heading-color", "#1f3864", "--paper", "a4", "--margin-cm", "2"), 0)
|
|
z = zipfile.ZipFile(self.root / "templates/styles/house.docx")
|
|
for n in z.namelist():
|
|
if n.endswith(".xml"):
|
|
xml.dom.minidom.parseString(z.read(n))
|
|
styles = z.read("word/styles.xml").decode()
|
|
doc = z.read("word/document.xml").decode()
|
|
default = re.search(r"<w:rPrDefault>.*?</w:rPrDefault>", styles, re.S).group(0)
|
|
self.assertIn('w:ascii="Georgia"', default)
|
|
self.assertIn('<w:sz w:val="22"/>', default)
|
|
h1 = re.search(r'<w:style [^>]*w:styleId="Heading1".*?</w:style>', styles, re.S).group(0)
|
|
self.assertIn('<w:color w:val="1F3864"/>', h1)
|
|
self.assertEqual(h1.count("<w:color "), 1)
|
|
self.assertIn('<w:pgSz w:w="11906" w:h="16838"/>', doc)
|
|
self.assertIn('w:top="1134"', doc)
|
|
|
|
def test_convert_docx_and_refuse_overwrite(self):
|
|
self.cli("new-style", "house")
|
|
self.assertEqual(self.cli("convert", str(self.root / "outputs/documents/r.md")), 0)
|
|
out = self.root / "outputs/documents/r.docx"
|
|
self.assertTrue(out.exists())
|
|
doc = zipfile.ZipFile(out).read("word/document.xml").decode()
|
|
self.assertIn("Status: Atlas", doc)
|
|
self.assertNotIn("Ask the user", doc)
|
|
self.assertEqual(self.cli("convert", str(self.root / "outputs/documents/r.md")), 1)
|
|
self.assertEqual(self.cli("convert", str(self.root / "outputs/documents/r.md"), "--force"), 0)
|
|
|
|
def test_dotx_becomes_docx(self):
|
|
self.cli("new-style", "house")
|
|
dotx = self.root / "Company.dotx"
|
|
shutil.copyfile(self.root / "templates/styles/house.docx", dotx)
|
|
convert.rewrite_zip(dotx, {"[Content_Types].xml": lambda x: x.replace(convert.DOCX_CT, convert.DOTX_CT)})
|
|
self.assertEqual(self.cli("new-style", "company", "--from", str(dotx)), 0)
|
|
ct = zipfile.ZipFile(self.root / "templates/styles/company.docx").read("[Content_Types].xml").decode()
|
|
self.assertIn(convert.DOCX_CT, ct)
|
|
self.assertNotIn(convert.DOTX_CT, ct)
|
|
|
|
def test_refusals(self):
|
|
self.assertEqual(self.cli("new-style", "Bad Name"), 1)
|
|
self.assertEqual(self.cli("edit-style", "nope", "--size", "10"), 1)
|
|
self.assertEqual(self.cli("convert", str(self.root / "outputs/documents/r.md"),
|
|
"--style", "nope", "--style-required"), 1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|