vibepaper 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
vibepaper/__init__.py ADDED
@@ -0,0 +1 @@
1
+ version = "0.1.0"
vibepaper/build.py ADDED
@@ -0,0 +1,294 @@
1
+ """Orchestration: Jinja2 pass → table pass → pandoc."""
2
+
3
+ import json
4
+ import logging
5
+ import re
6
+ import shutil
7
+ import subprocess
8
+ import sys
9
+ import zipfile
10
+ from datetime import date
11
+ from pathlib import Path
12
+
13
+ from lxml import etree
14
+
15
+ log = logging.getLogger(__name__)
16
+
17
+ try:
18
+ import tomllib
19
+ except ImportError:
20
+ import tomli as tomllib # type: ignore
21
+
22
+ from .render import load_facts, make_jinja_env, render_file as render_jinja, sanity_check
23
+ from .tables import process_file as render_tables
24
+
25
+ W = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
26
+
27
+
28
+ def load_config(config_path: Path) -> dict:
29
+ """Read paper.toml and return config dict with defaults filled in.
30
+
31
+ Required: paper.sections (list of str).
32
+ Optional with defaults:
33
+ paper.supplementary = []
34
+ paper.name = parent directory name
35
+ paper.facts_dir = "output/facts"
36
+ paper.output_dir = "output"
37
+ paper.build_dir = "build"
38
+ paper.reference_doc = "paper/reference.docx" (only used if file exists)
39
+ """
40
+ with open(config_path, "rb") as fh:
41
+ raw = tomllib.load(fh)
42
+
43
+ paper = raw.get("paper", {})
44
+
45
+ if "sections" not in paper:
46
+ raise ValueError(f"{config_path}: [paper] must contain a 'sections' list")
47
+
48
+ return {
49
+ "sections": paper["sections"],
50
+ "supplementary": paper.get("supplementary", []),
51
+ "name": paper.get("name", config_path.parent.name),
52
+ "facts_dir": paper.get("facts_dir", "output/facts"),
53
+ "output_dir": paper.get("output_dir", "output"),
54
+ "build_dir": paper.get("build_dir", "build"),
55
+ "reference_doc": paper.get("reference_doc", "paper/reference.docx"),
56
+ "bibliography": paper.get("bibliography", None),
57
+ "csl": paper.get("csl", None),
58
+ }
59
+
60
+
61
+ def minimal_config(sections: list[str], name: str | None = None) -> dict:
62
+ """Return a default config for use without a paper.toml."""
63
+ return {
64
+ "sections": sections,
65
+ "supplementary": [],
66
+ "name": name or Path.cwd().name,
67
+ "facts_dir": "output/facts",
68
+ "output_dir": "output",
69
+ "build_dir": "build",
70
+ "reference_doc": "paper/reference.docx",
71
+ "bibliography": None,
72
+ "csl": None,
73
+ }
74
+
75
+
76
+ def load_sections_file(path: Path) -> list[str]:
77
+ """Read a plain text file and return a list of section paths.
78
+
79
+ Format: one file path per line. Blank lines and lines starting with
80
+ '#' are ignored. Paths are relative to the sections file's directory.
81
+
82
+ Example sections.txt:
83
+ # Main manuscript
84
+ paper/abstract.md
85
+ paper/introduction.md
86
+ paper/methods.md
87
+ paper/results.md
88
+ paper/discussion.md
89
+ """
90
+ base = path.parent
91
+ sections = []
92
+ for line in path.read_text().splitlines():
93
+ stripped = line.strip()
94
+ if stripped and not stripped.startswith("#"):
95
+ p = Path(stripped)
96
+ if not p.is_absolute():
97
+ p = base / p
98
+ sections.append(str(p))
99
+ if not sections:
100
+ raise ValueError(f"{path}: no sections found")
101
+ return sections
102
+
103
+
104
+ def load_json_data(source: str) -> dict:
105
+ """Load template context data from a JSON file path or inline JSON string.
106
+
107
+ Inline: --data '{"n_samples": 100, "stats": {"pvalue": 0.001}}'
108
+ From file: --data results.json
109
+
110
+ Top-level keys become template namespaces:
111
+ {"n_samples": 100} → {{ n_samples }}
112
+ {"stats": {"pvalue": 0.05}} → {{ stats.pvalue }}
113
+ """
114
+ source = source.strip()
115
+ if source.startswith("{"):
116
+ data = json.loads(source)
117
+ else:
118
+ data = json.loads(Path(source).read_text())
119
+ if not isinstance(data, dict):
120
+ raise ValueError("JSON data must be a top-level object (dict)")
121
+ return data
122
+
123
+
124
+ def strip_bookmarks(docx: Path):
125
+ """Remove Word bookmark elements that pandoc inserts for heading anchors.
126
+ These cause warnings when opening in Google Docs / older Word versions.
127
+ """
128
+ log.debug("Stripping bookmarks from %s", docx)
129
+ tmp = docx.with_suffix(".tmp.docx")
130
+ shutil.copy(docx, tmp)
131
+
132
+ with zipfile.ZipFile(tmp, "r") as zin, \
133
+ zipfile.ZipFile(docx, "w", zipfile.ZIP_DEFLATED) as zout:
134
+ for item in zin.infolist():
135
+ data = zin.read(item.filename)
136
+ if item.filename == "word/document.xml":
137
+ tree = etree.fromstring(data)
138
+ for tag in (f"{{{W}}}bookmarkStart", f"{{{W}}}bookmarkEnd"):
139
+ for el in tree.iter(tag):
140
+ el.getparent().remove(el)
141
+ data = etree.tostring(
142
+ tree, xml_declaration=True, encoding="UTF-8", standalone=True
143
+ )
144
+ zout.writestr(item, data)
145
+
146
+ tmp.unlink()
147
+
148
+
149
+ def pandoc_args(
150
+ sections: list[str],
151
+ output: Path,
152
+ reference_doc: Path | None,
153
+ bibliography: Path | None = None,
154
+ csl: Path | None = None,
155
+ ) -> list[str]:
156
+ """Build pandoc command."""
157
+ args = [
158
+ "pandoc",
159
+ *sections,
160
+ "--from", "markdown",
161
+ "--to", "docx",
162
+ "--output", str(output),
163
+ "--resource-path", ".",
164
+ ]
165
+ if reference_doc and reference_doc.exists():
166
+ args += ["--reference-doc", str(reference_doc)]
167
+ log.debug("Using reference doc: %s", reference_doc)
168
+ elif reference_doc:
169
+ log.warning("reference_doc %s not found; output will use default Word formatting.", reference_doc)
170
+ if bibliography and bibliography.exists():
171
+ args += ["--bibliography", str(bibliography), "--citeproc"]
172
+ log.debug("Using bibliography: %s", bibliography)
173
+ elif bibliography:
174
+ log.warning("bibliography %s not found; citations will not be resolved.", bibliography)
175
+ if csl and csl.exists():
176
+ args += ["--csl", str(csl)]
177
+ log.debug("Using CSL: %s", csl)
178
+ elif csl:
179
+ log.warning("CSL file %s not found; pandoc will use its default citation style.", csl)
180
+ return args
181
+
182
+
183
+ def build_docx(
184
+ sections: list[str],
185
+ output: Path,
186
+ reference_doc: Path | None,
187
+ bibliography: Path | None = None,
188
+ csl: Path | None = None,
189
+ ):
190
+ """Run pandoc to produce a .docx file."""
191
+ output.parent.mkdir(parents=True, exist_ok=True)
192
+ log.debug("Building %s", output)
193
+ subprocess.run(pandoc_args(sections, output, reference_doc, bibliography, csl), check=True)
194
+
195
+
196
+ def run_build(
197
+ config: dict,
198
+ project_root: Path,
199
+ output_dir: Path,
200
+ combined: bool = False,
201
+ extra_context: dict | None = None,
202
+ ):
203
+ """Full pipeline: Jinja2 pass → table pass → pandoc.
204
+
205
+ All paths are resolved relative to project_root; no os.chdir is used.
206
+
207
+ extra_context is merged into the Jinja2 context on top of any facts
208
+ CSVs. Nested dicts are deep-merged at the namespace level; scalar values
209
+ are set directly.
210
+ """
211
+ today = date.today()
212
+ paper_name = config["name"]
213
+
214
+ facts_dir = _resolve(config["facts_dir"], project_root)
215
+ build_dir = _resolve(config["build_dir"], project_root)
216
+ build_jinja = build_dir / "jinja" / "paper"
217
+ build_paper = build_dir / "paper"
218
+
219
+ reference_doc_path = _resolve(config["reference_doc"], project_root)
220
+ reference_doc = reference_doc_path if reference_doc_path.exists() else None
221
+
222
+ bibliography = _resolve(config["bibliography"], project_root) if config.get("bibliography") else None
223
+ csl = _resolve(config["csl"], project_root) if config.get("csl") else None
224
+
225
+ all_sections = config["sections"] + config["supplementary"]
226
+
227
+ _warn_stub_sections(all_sections, project_root)
228
+
229
+ # Build Jinja2 context: facts CSVs, then merge any extra JSON data
230
+ context: dict = {}
231
+ if facts_dir.exists():
232
+ context.update(load_facts(facts_dir))
233
+ else:
234
+ log.warning("%s not found — template context will be empty.", facts_dir)
235
+ if extra_context:
236
+ for key, value in extra_context.items():
237
+ if key in context and isinstance(context[key], dict) and isinstance(value, dict):
238
+ context[key].update(value)
239
+ else:
240
+ context[key] = value
241
+
242
+ jinja_env = make_jinja_env(project_root)
243
+
244
+ jinja_warnings = []
245
+ for section in all_sections:
246
+ section_path = _resolve(section, project_root)
247
+ out = render_jinja(section_path, build_jinja, context, jinja_env)
248
+ jinja_warnings.extend(sanity_check(out))
249
+ if jinja_warnings:
250
+ print("WARNING: suspicious content in rendered output:", file=sys.stderr)
251
+ for w in jinja_warnings:
252
+ print(w, file=sys.stderr)
253
+
254
+ for section in all_sections:
255
+ section_name = Path(section).name
256
+ render_tables(build_jinja / section_name, build_paper, project_root)
257
+
258
+ def build_paths(sections):
259
+ return [str(build_paper / Path(s).name) for s in sections]
260
+
261
+ # Main manuscript
262
+ main_sections = config["sections"] + (config["supplementary"] if combined else [])
263
+ main_docx = output_dir / f"{paper_name}_{today}.docx"
264
+ build_docx(build_paths(main_sections), main_docx, reference_doc, bibliography, csl)
265
+ strip_bookmarks(main_docx)
266
+ print(f"Done: {main_docx}")
267
+
268
+ # Supplementary (separate unless --combined)
269
+ if not combined and config["supplementary"]:
270
+ supp_docx = output_dir / f"{paper_name}_supplementary_{today}.docx"
271
+ build_docx(build_paths(config["supplementary"]), supp_docx, reference_doc, bibliography, csl)
272
+ strip_bookmarks(supp_docx)
273
+ print(f"Done: {supp_docx}")
274
+
275
+
276
+ def _resolve(path_str: str, project_root: Path) -> Path:
277
+ """Return absolute Path: if already absolute use as-is, else join with project_root."""
278
+ p = Path(path_str)
279
+ return p if p.is_absolute() else project_root / p
280
+
281
+
282
+ def _warn_stub_sections(sections: list[str], project_root: Path):
283
+ """Warn if any section files appear to be stubs (only comments/headings)."""
284
+ threshold = 100
285
+ for section in sections:
286
+ path = _resolve(section, project_root)
287
+ if not path.exists():
288
+ log.warning("section not found: %s", path)
289
+ continue
290
+ text = path.read_text()
291
+ stripped = re.sub(r"<!--.*?-->", "", text, flags=re.DOTALL)
292
+ stripped = re.sub(r"^#+.*$", "", stripped, flags=re.MULTILINE)
293
+ if len(stripped.strip()) < threshold:
294
+ log.warning("%s appears to be a stub", path)
vibepaper/cli.py ADDED
@@ -0,0 +1,116 @@
1
+ """Command-line entry point for vibepaper."""
2
+
3
+ import argparse
4
+ import logging
5
+ import sys
6
+ from pathlib import Path
7
+
8
+ from .build import load_config, load_json_data, load_sections_file, minimal_config, run_build
9
+
10
+
11
+ def main():
12
+ parser = argparse.ArgumentParser(
13
+ prog="vibepaper",
14
+ description=(
15
+ "Build a Word document from Markdown paper sections.\n\n"
16
+ "Three ways to specify which files to build:\n"
17
+ " 1. paper.toml config file (default, if present)\n"
18
+ " 2. --sections-file order.txt (plain text list of .md files)\n"
19
+ " 3. vibepaper intro.md methods.md results.md (positional args)"
20
+ ),
21
+ formatter_class=argparse.RawDescriptionHelpFormatter,
22
+ )
23
+
24
+ # --- Input ---
25
+ parser.add_argument(
26
+ "sections", nargs="*", metavar="FILE.md",
27
+ help="Markdown files to build in order. Skips paper.toml sections list.",
28
+ )
29
+ parser.add_argument(
30
+ "--sections-file", metavar="FILE",
31
+ help="Plain text file listing .md paths in order, one per line.",
32
+ )
33
+ parser.add_argument(
34
+ "--config", default="paper.toml", metavar="FILE",
35
+ help="paper.toml config file (default: paper.toml in cwd).",
36
+ )
37
+
38
+ # --- Data ---
39
+ parser.add_argument(
40
+ "--data", metavar="JSON",
41
+ help=(
42
+ "Extra template context as a JSON file path or inline JSON dict. "
43
+ "Merged on top of facts CSVs. "
44
+ "Example: --data '{\"n\": 100}' or --data stats.json"
45
+ ),
46
+ )
47
+ parser.add_argument(
48
+ "--facts-dir", metavar="DIR",
49
+ help="Directory of 1-row facts CSVs (overrides paper.toml / default).",
50
+ )
51
+
52
+ # --- Output ---
53
+ parser.add_argument(
54
+ "--output-dir", metavar="DIR",
55
+ help="Directory for output .docx files.",
56
+ )
57
+ parser.add_argument(
58
+ "--name", metavar="NAME",
59
+ help="Output filename stem, e.g. 'my_paper' produces my_paper_2025-01-01.docx.",
60
+ )
61
+ parser.add_argument(
62
+ "--combined", action="store_true",
63
+ help="Append supplementary into the main document instead of a separate file.",
64
+ )
65
+ parser.add_argument(
66
+ "--verbose", "-v", action="store_true",
67
+ help="Print detailed progress (section rendering, pandoc invocation, etc.).",
68
+ )
69
+
70
+ args = parser.parse_args()
71
+ level = logging.DEBUG if args.verbose else logging.WARNING
72
+ logging.basicConfig(level=level, format="%(message)s")
73
+
74
+ # --- Determine sections and project_root ---
75
+ if args.sections:
76
+ # Mode: positional file arguments
77
+ sections = [str(Path(s).resolve()) for s in args.sections]
78
+ project_root = Path.cwd()
79
+ config = minimal_config(sections, name=args.name)
80
+
81
+ elif args.sections_file:
82
+ # Mode: plain text sections file
83
+ sections_path = Path(args.sections_file).resolve()
84
+ sections = load_sections_file(sections_path)
85
+ project_root = Path.cwd()
86
+ config = minimal_config(sections, name=args.name)
87
+
88
+ else:
89
+ # Mode: paper.toml
90
+ config_path = Path(args.config).resolve()
91
+ if not config_path.exists():
92
+ print(
93
+ f"error: {config_path} not found.\n"
94
+ "Pass .md files directly, use --sections-file, or create a paper.toml.",
95
+ file=sys.stderr,
96
+ )
97
+ sys.exit(1)
98
+ config = load_config(config_path)
99
+ project_root = config_path.parent
100
+ if args.name:
101
+ config["name"] = args.name
102
+
103
+ # Apply CLI overrides that work across all modes
104
+ if args.facts_dir:
105
+ config["facts_dir"] = args.facts_dir
106
+
107
+ output_dir = (
108
+ Path(args.output_dir).resolve()
109
+ if args.output_dir
110
+ else project_root / config["output_dir"]
111
+ )
112
+
113
+ extra_context = load_json_data(args.data) if args.data else None
114
+
115
+ run_build(config, project_root, output_dir, combined=args.combined,
116
+ extra_context=extra_context)
vibepaper/render.py ADDED
@@ -0,0 +1,111 @@
1
+ """Jinja2 templating pass for paper markdown files.
2
+
3
+ Replaces {{ namespace.field | filter }} references with values loaded from
4
+ 1-row facts CSVs in output/facts/. Runs before tables.py so
5
+ that inline prose values are resolved before table directives are expanded.
6
+ """
7
+
8
+ import logging
9
+ import re
10
+ from pathlib import Path
11
+
12
+ import pandas as pd
13
+ from jinja2 import Environment, FileSystemLoader, StrictUndefined, UndefinedError
14
+
15
+ log = logging.getLogger(__name__)
16
+
17
+ # Regexes that indicate a render problem in the output file.
18
+ # Use word boundaries so "nan" doesn't match "annotated", "None" doesn't
19
+ # match prose like "None of the…" — only standalone tokens flag.
20
+ _SANITY_RE = re.compile(
21
+ r"\bnan\b" # pandas NaN rendered as "nan"
22
+ r"|\bundefined\b" # Jinja undefined leak
23
+ r"|\{\{" # unresolved template tag
24
+ )
25
+
26
+
27
+ def load_facts(facts_dir: Path) -> dict:
28
+ """Load all 1-row CSVs from facts_dir into a namespace dict.
29
+
30
+ Each file 'foo_bar.csv' becomes context['foo_bar'] = {col: value, ...}.
31
+ Raises ValueError if any CSV has more than one data row.
32
+ """
33
+ context = {}
34
+ for csv_path in sorted(facts_dir.glob("*.csv")):
35
+ df = pd.read_csv(csv_path)
36
+ if len(df) != 1:
37
+ raise ValueError(
38
+ f"{csv_path}: facts CSVs must have exactly 1 row, got {len(df)}"
39
+ )
40
+ namespace = csv_path.stem
41
+ context[namespace] = df.iloc[0].to_dict()
42
+ log.debug("Loaded facts: %s (%d fields)", namespace, len(context[namespace]))
43
+ return context
44
+
45
+
46
+ def make_jinja_env(project_root: Path) -> Environment:
47
+ """Create a Jinja2 environment with custom filters and strict undefined."""
48
+ env = Environment(
49
+ loader=FileSystemLoader(str(project_root)),
50
+ undefined=StrictUndefined,
51
+ keep_trailing_newline=True,
52
+ )
53
+
54
+ def filter_commas(value) -> str:
55
+ """Integer with thousands separator: 254129 → '254,129'"""
56
+ return f"{int(float(value)):,}"
57
+
58
+ def filter_pct(value, decimals=1) -> str:
59
+ """Format a pre-computed percentage: 52.2 → '52.2%'"""
60
+ return f"{float(value):.{decimals}f}%"
61
+
62
+ def filter_fold(value, decimals=1) -> str:
63
+ """Fold change: 2.003 → '2.0-fold'"""
64
+ return f"{float(value):.{decimals}f}-fold"
65
+
66
+ def filter_dp(value, decimals=1) -> str:
67
+ """Decimal places only, no suffix: 9.177 → '9.2'"""
68
+ return f"{float(value):.{decimals}f}"
69
+
70
+ def filter_fmt(value, spec) -> str:
71
+ """Escape hatch: raw Python format spec. {{ v | fmt('+,.0f') }}"""
72
+ return format(float(value), spec)
73
+
74
+ env.filters["commas"] = filter_commas
75
+ env.filters["pct"] = filter_pct
76
+ env.filters["fold"] = filter_fold
77
+ env.filters["dp"] = filter_dp
78
+ env.filters["fmt"] = filter_fmt
79
+
80
+ return env
81
+
82
+
83
+ def render_file(
84
+ input_path: Path,
85
+ build_dir: Path,
86
+ context: dict,
87
+ env: Environment,
88
+ ) -> Path:
89
+ """Render Jinja2 templates in a single markdown file and write output."""
90
+ content = input_path.read_text()
91
+ try:
92
+ rendered = env.from_string(content).render(**context)
93
+ except UndefinedError as exc:
94
+ raise RuntimeError(f"Template error in {input_path}: {exc}") from exc
95
+
96
+ output_path = build_dir / input_path.name
97
+ build_dir.mkdir(parents=True, exist_ok=True)
98
+ output_path.write_text(rendered)
99
+ log.debug("Rendered %s → %s", input_path, output_path)
100
+ return output_path
101
+
102
+
103
+ def sanity_check(path: Path) -> list:
104
+ """Return a list of warning strings for suspicious content in a rendered file."""
105
+ warnings = []
106
+ content = path.read_text()
107
+ for i, line in enumerate(content.splitlines(), start=1):
108
+ m = _SANITY_RE.search(line)
109
+ if m:
110
+ warnings.append(f" {path}:{i}: found '{m.group()}'")
111
+ return warnings
vibepaper/tables.py ADDED
@@ -0,0 +1,182 @@
1
+ """Preprocess markdown files: replace include-csv directives with rendered tables.
2
+
3
+ Scans markdown for <!-- include-csv: path/to/file.csv ... --> directives,
4
+ loads the referenced CSV/TSV, applies formatting options, and writes rendered
5
+ markdown to a build directory.
6
+
7
+ Directive syntax (all options are optional):
8
+
9
+ <!-- include-csv: output/exome_sizes.csv
10
+ columns: [source, feature, size_bp]
11
+ rename:
12
+ source: Source
13
+ size_bp: Size (bp)
14
+ format:
15
+ size_bp: ",d"
16
+ align: right
17
+ max_rows: 50
18
+ sort: [-size_bp]
19
+ filter: "feature == 'exon'"
20
+ na_rep: "—"
21
+ -->
22
+ """
23
+
24
+ import logging
25
+ import re
26
+ from pathlib import Path
27
+
28
+ import pandas as pd
29
+ import yaml
30
+
31
+ log = logging.getLogger(__name__)
32
+
33
+ DIRECTIVE_RE = re.compile(
34
+ r"<!-- include-csv:\s*(\S+)(.*?)-->",
35
+ re.DOTALL,
36
+ )
37
+
38
+
39
+ def parse_options(yaml_text: str) -> dict:
40
+ """Parse YAML options from the directive body."""
41
+ text = yaml_text.strip()
42
+ if not text:
43
+ return {}
44
+ try:
45
+ opts = yaml.safe_load(text)
46
+ except yaml.YAMLError as exc:
47
+ log.warning("Failed to parse YAML options: %s", exc)
48
+ return {}
49
+ return opts if isinstance(opts, dict) else {}
50
+
51
+
52
+ def load_table(path: Path, options: dict) -> pd.DataFrame:
53
+ """Load a CSV/TSV file and apply filter, sort, and row-limit options."""
54
+ sep = options.get("sep", "\t" if path.suffix in (".tsv",) else ",")
55
+ df = pd.read_csv(path, sep=sep)
56
+
57
+ if "filter" in options:
58
+ df = df.query(options["filter"])
59
+
60
+ if "sort" in options:
61
+ sort_cols = options["sort"]
62
+ if isinstance(sort_cols, str):
63
+ sort_cols = [sort_cols]
64
+ ascending = []
65
+ clean_cols = []
66
+ for col in sort_cols:
67
+ if col.startswith("-"):
68
+ ascending.append(False)
69
+ clean_cols.append(col[1:])
70
+ else:
71
+ ascending.append(True)
72
+ clean_cols.append(col)
73
+ df = df.sort_values(clean_cols, ascending=ascending)
74
+
75
+ if "max_rows" in options:
76
+ df = df.head(options["max_rows"])
77
+
78
+ return df
79
+
80
+
81
+ def format_cell(value, fmt: str, na_rep: str) -> str:
82
+ """Format a single cell value using a Python format spec."""
83
+ if pd.isna(value):
84
+ return na_rep
85
+ try:
86
+ return format(value, fmt)
87
+ except (ValueError, TypeError):
88
+ return str(value)
89
+
90
+
91
+ def render_markdown_table(df: pd.DataFrame, options: dict) -> str:
92
+ """Render a DataFrame as a markdown table string."""
93
+ na_rep = options.get("na_rep", "—")
94
+
95
+ # Select and order columns
96
+ if "columns" in options:
97
+ df = df[[c for c in options["columns"] if c in df.columns]]
98
+
99
+ # Rename columns for display
100
+ renames = options.get("rename", {})
101
+ display_cols = [renames.get(c, c) for c in df.columns]
102
+
103
+ # Determine alignment per column
104
+ align_opt = options.get("align", None)
105
+ alignments = []
106
+ for col in df.columns:
107
+ if isinstance(align_opt, dict):
108
+ a = align_opt.get(col, align_opt.get(renames.get(col, col), None))
109
+ elif isinstance(align_opt, str):
110
+ a = align_opt
111
+ else:
112
+ a = None
113
+ if a is None:
114
+ a = "right" if pd.api.types.is_numeric_dtype(df[col]) else "left"
115
+ alignments.append(a)
116
+
117
+ # Format cells
118
+ fmt_specs = options.get("format", {})
119
+ rows = []
120
+ for _, row in df.iterrows():
121
+ cells = []
122
+ for col in df.columns:
123
+ fmt = fmt_specs.get(col)
124
+ val = row[col]
125
+ if fmt:
126
+ cells.append(format_cell(val, fmt, na_rep))
127
+ elif pd.isna(val):
128
+ cells.append(na_rep)
129
+ else:
130
+ cells.append(str(val))
131
+ rows.append(cells)
132
+
133
+ # Build alignment markers
134
+ markers = []
135
+ for a in alignments:
136
+ if a == "right":
137
+ markers.append("---:")
138
+ elif a == "center":
139
+ markers.append(":---:")
140
+ else:
141
+ markers.append("---")
142
+
143
+ # Assemble table
144
+ lines = []
145
+ lines.append("| " + " | ".join(display_cols) + " |")
146
+ lines.append("| " + " | ".join(markers) + " |")
147
+ for cells in rows:
148
+ lines.append("| " + " | ".join(cells) + " |")
149
+
150
+ return "\n".join(lines)
151
+
152
+
153
+ def process_content(content: str, project_root: Path) -> str:
154
+ """Replace all include-csv directives in markdown content."""
155
+ def replace_directive(match):
156
+ rel_path = match.group(1)
157
+ yaml_text = match.group(2)
158
+ csv_path = project_root / rel_path
159
+
160
+ if not csv_path.exists():
161
+ log.warning("CSV file not found: %s", csv_path)
162
+ return f"*[Table data not found: {rel_path}]*"
163
+
164
+ options = parse_options(yaml_text)
165
+ df = load_table(csv_path, options)
166
+ table = render_markdown_table(df, options)
167
+ log.debug("Rendered %s (%d rows, %d cols)", rel_path, len(df), len(df.columns))
168
+ return table
169
+
170
+ return DIRECTIVE_RE.sub(replace_directive, content)
171
+
172
+
173
+ def process_file(input_path: Path, build_dir: Path, project_root: Path) -> Path:
174
+ """Process a single markdown file and write rendered output."""
175
+ content = input_path.read_text()
176
+ rendered = process_content(content, project_root)
177
+
178
+ output_path = build_dir / input_path.name
179
+ build_dir.mkdir(parents=True, exist_ok=True)
180
+ output_path.write_text(rendered)
181
+ log.debug("Wrote %s", output_path)
182
+ return output_path
@@ -0,0 +1,343 @@
1
+ Metadata-Version: 2.4
2
+ Name: vibepaper
3
+ Version: 0.1.0
4
+ Summary: Data-bound Markdown-to-Word builder for scientific papers
5
+ Project-URL: Repository, https://github.com/PLACEHOLDER/vibepaper
6
+ License: MIT
7
+ License-File: LICENSE
8
+ Keywords: jinja2,manuscript,markdown,pandoc,paper,reproducible-research,scientific-writing
9
+ Classifier: Development Status :: 3 - Alpha
10
+ Classifier: Intended Audience :: Science/Research
11
+ Classifier: License :: OSI Approved :: MIT License
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Programming Language :: Python :: 3.10
14
+ Classifier: Programming Language :: Python :: 3.11
15
+ Classifier: Programming Language :: Python :: 3.12
16
+ Classifier: Topic :: Scientific/Engineering
17
+ Classifier: Topic :: Text Processing :: General
18
+ Classifier: Topic :: Text Processing :: Markup :: Markdown
19
+ Requires-Python: >=3.10
20
+ Requires-Dist: jinja2>=3.0
21
+ Requires-Dist: lxml>=4.0
22
+ Requires-Dist: pandas>=1.3
23
+ Requires-Dist: pyyaml>=6.0
24
+ Requires-Dist: tomli>=2.0; python_version < '3.11'
25
+ Description-Content-Type: text/markdown
26
+
27
+ # vibepaper
28
+
29
+ Build scientific papers from Markdown where every number traces back to the analysis that produced it.
30
+
31
+ ## The problem
32
+
33
+ Numbers in scientific prose go stale. You finish the analysis, write up the results, and six months later a reviewer asks you to rerun with a corrected dataset. Now you have updated CSVs and a paper full of hardcoded figures: "the mean increased from 4.6 to 9.2", "of the 7,318 variants that lost HIGH impact". Finding every number, checking which is still current, updating without introducing new errors — this is tedious, error-prone, and nearly impossible to review.
34
+
35
+ ## The solution
36
+
37
+ vibepaper separates computation from communication. Analysis scripts write their key results to named CSV files. The paper references those values by name using Jinja2 template syntax. The build pipeline substitutes every reference before passing the document to pandoc for final Word output.
38
+
39
+ ```markdown
40
+ Mean transcripts per variant doubled from
41
+ {{ vep_impact.giab_mean_v112 | dp(1) }} to
42
+ {{ vep_impact.giab_mean_v115_full | dp(1) }} on upgrading to Ensembl v115.
43
+ ```
44
+
45
+ When you rerun the analysis, you rerun the build. The numbers update everywhere, simultaneously, with a loud error if any reference is missing.
46
+
47
+ **Three design principles:**
48
+
49
+ 1. **Templates express intent; scripts express computation.** No arithmetic in templates. If you need a percentage increase, the analysis script computes and writes it. The template formats it.
50
+ 2. **Loud failures over silent omissions.** A missing or renamed CSV column is a build error, not an empty string in the output.
51
+ 3. **Every number is traceable.** Any figure in the rendered paper can be grepped back to the template reference and the script that wrote the CSV.
52
+
53
+ ---
54
+
55
+ ## Installation
56
+
57
+ ```bash
58
+ pip install vibepaper
59
+ ```
60
+
61
+ Requires [pandoc](https://pandoc.org/installing.html) as a system dependency:
62
+
63
+ ```bash
64
+ # macOS
65
+ brew install pandoc
66
+
67
+ # Debian/Ubuntu
68
+ sudo apt-get install pandoc
69
+ ```
70
+
71
+ ---
72
+
73
+ ## Quick start
74
+
75
+ ### Option 1 — paper.toml (recommended for full papers)
76
+
77
+ Create `paper.toml` in your project root:
78
+
79
+ ```toml
80
+ [paper]
81
+ sections = [
82
+ "paper/abstract.md",
83
+ "paper/introduction.md",
84
+ "paper/methods.md",
85
+ "paper/results.md",
86
+ "paper/discussion.md",
87
+ "paper/references.md",
88
+ "paper/figures.md",
89
+ ]
90
+ supplementary = ["paper/supplementary.md"]
91
+ name = "my_paper"
92
+ ```
93
+
94
+ Then build:
95
+
96
+ ```bash
97
+ vibepaper
98
+ # outputs: output/my_paper_2025-06-01.docx
99
+ # output/my_paper_supplementary_2025-06-01.docx
100
+ ```
101
+
102
+ ### Option 2 — sections file
103
+
104
+ Create a plain text file listing your sections in order:
105
+
106
+ ```
107
+ # order.txt
108
+ paper/abstract.md
109
+ paper/methods.md
110
+ paper/results.md
111
+ paper/discussion.md
112
+ ```
113
+
114
+ Then:
115
+
116
+ ```bash
117
+ vibepaper --sections-file order.txt --name my_paper
118
+ ```
119
+
120
+ Lines starting with `#` and blank lines are ignored. Paths are relative to the sections file's location.
121
+
122
+ ### Option 3 — direct file list
123
+
124
+ ```bash
125
+ vibepaper paper/abstract.md paper/results.md paper/discussion.md --name my_paper
126
+ ```
127
+
128
+ ---
129
+
130
+ ## Template syntax
131
+
132
+ vibepaper uses [Jinja2](https://jinja.palletsprojects.com/) for template substitution. References follow the pattern `{{ namespace.field | filter }}`.
133
+
134
+ ### Number formatting filters
135
+
136
+ | Filter | Example | Output |
137
+ |---|---|---|
138
+ | `\| commas` | `{{ n \| commas }}` | `254,129` |
139
+ | `\| dp(n)` | `{{ mean \| dp(1) }}` | `9.2` |
140
+ | `\| pct(n)` | `{{ rate \| pct(1) }}` | `52.2%` |
141
+ | `\| fold(n)` | `{{ ratio \| fold(1) }}` | `2.0-fold` |
142
+ | `\| fmt(spec)` | `{{ v \| fmt('+.1f') }}` | `+3.7` |
143
+
144
+ `dp` (decimal places) is for numbers that will have surrounding text (e.g. "mean TPV was 9.2"). `pct` appends the `%` sign. Use `fmt` for any format string Python's `format()` accepts.
145
+
146
+ ### Examples
147
+
148
+ ```markdown
149
+ Of {{ clinvar.total_variants | commas }} ClinVar variants, {{ clinvar.gained_high_count | commas }}
150
+ ({{ clinvar.gained_high_pct | dp(2) }}%) gained HIGH impact on upgrading to v115.
151
+
152
+ Mean transcripts per variant increased {{ vep.mean_fold | fold }} from
153
+ {{ vep.mean_v112 | dp(1) }} to {{ vep.mean_v115 | dp(1) }}.
154
+ ```
155
+
156
+ ---
157
+
158
+ ## Data sources
159
+
160
+ ### Facts CSVs (primary)
161
+
162
+ The main data binding mechanism. Analysis scripts write **1-row CSVs** to `output/facts/`. The filename stem becomes the template namespace; column names become field names.
163
+
164
+ ```
165
+ output/facts/
166
+ transcript_growth.csv → {{ transcript_growth.v112_count | commas }}
167
+ vep_impact.csv → {{ vep_impact.giab_mean_v115_full | dp(1) }}
168
+ clinvar_reclassification.csv → {{ clinvar_reclassification.total_variants | commas }}
169
+ ```
170
+
171
+ A CSV named `transcript_growth.csv` with columns `v112_count, v115_count`:
172
+
173
+ ```csv
174
+ v112_count,v115_count
175
+ 254129,509650
176
+ ```
177
+
178
+ Is referenced as:
179
+
180
+ ```markdown
181
+ Transcripts grew from {{ transcript_growth.v112_count | commas }}
182
+ to {{ transcript_growth.v115_count | commas }}.
183
+ ```
184
+
185
+ vibepaper raises a hard error if a referenced column doesn't exist. It warns if the rendered output contains literal `nan`, `None`, or unresolved `{{`.
186
+
187
+ ### JSON data (supplemental)
188
+
189
+ Pass additional values directly without creating a CSV file:
190
+
191
+ ```bash
192
+ # Inline dict
193
+ vibepaper --data '{"cohort_size": 412, "stats": {"pvalue": 0.003}}'
194
+
195
+ # From file
196
+ vibepaper --data results.json
197
+ ```
198
+
199
+ Top-level keys become namespaces:
200
+
201
+ ```markdown
202
+ Cohort: {{ cohort_size }} participants (p = {{ stats.pvalue | dp(3) }}).
203
+ ```
204
+
205
+ JSON is merged on top of facts CSVs. Nested dicts are deep-merged at the namespace level; scalar values override directly.
206
+
207
+ ---
208
+
209
+ ## Table directives
210
+
211
+ For supplementary tables, embed CSVs directly into the Markdown with a directive comment:
212
+
213
+ ```markdown
214
+ <!-- include-csv: output/consequence_changes.csv
215
+ columns: [consequence, v112_count, v115_count, pct_change]
216
+ rename:
217
+ v112_count: v112
218
+ v115_count: v115
219
+ pct_change: Change (%)
220
+ format:
221
+ v112_count: ",d"
222
+ v115_count: ",d"
223
+ pct_change: ".1f"
224
+ sort: [-pct_change]
225
+ max_rows: 20
226
+ -->
227
+ ```
228
+
229
+ Directive options:
230
+
231
+ | Option | Description |
232
+ |---|---|
233
+ | `columns` | List of columns to include, in order |
234
+ | `rename` | Dict mapping column names to display names |
235
+ | `format` | Dict mapping column names to Python format specs |
236
+ | `align` | `left`, `right`, `center`, or per-column dict |
237
+ | `sort` | List of column names; prefix `-` for descending |
238
+ | `filter` | pandas `query()` expression |
239
+ | `max_rows` | Truncate to this many rows |
240
+ | `na_rep` | String to use for missing values (default: `—`) |
241
+
242
+ ---
243
+
244
+ ## paper.toml reference
245
+
246
+ ```toml
247
+ [paper]
248
+ # Manuscript sections in order (paths relative to paper.toml)
249
+ sections = [
250
+ "paper/title.md",
251
+ "paper/abstract.md",
252
+ "paper/introduction.md",
253
+ "paper/methods.md",
254
+ "paper/results.md",
255
+ "paper/discussion.md",
256
+ "paper/references.md",
257
+ "paper/figures.md",
258
+ ]
259
+
260
+ # Built as a separate .docx unless --combined is passed
261
+ supplementary = ["paper/supplementary.md"]
262
+
263
+ # Output filename stem: {name}_{date}.docx
264
+ # Default: parent directory name
265
+ name = "my_paper"
266
+
267
+ # Directory of 1-row facts CSVs
268
+ # Default: "output/facts"
269
+ facts_dir = "output/facts"
270
+
271
+ # Output directory for .docx files
272
+ # Default: "output"
273
+ output_dir = "output"
274
+
275
+ # Intermediate build directory
276
+ # Default: "build"
277
+ build_dir = "build"
278
+
279
+ # Word reference document for custom formatting (double spacing, line numbers, etc.)
280
+ # Only used if the file exists; silently skipped otherwise.
281
+ # Default: "paper/reference.docx"
282
+ reference_doc = "paper/reference.docx"
283
+ ```
284
+
285
+ ### Word reference document
286
+
287
+ To apply journal-specific formatting (e.g. double line spacing, continuous line numbering):
288
+
289
+ 1. Open a blank Word document
290
+ 2. Set paragraph spacing to Double and enable Layout → Line Numbers → Continuous
291
+ 3. Save as `paper/reference.docx`
292
+
293
+ vibepaper will use it automatically if it exists at the configured path.
294
+
295
+ ---
296
+
297
+ ## CLI reference
298
+
299
+ ```
300
+ vibepaper [FILE.md ...] [options]
301
+
302
+ Input (choose one):
303
+ FILE.md ... Markdown files in order (no paper.toml needed)
304
+ --sections-file FILE Plain text file with one .md path per line
305
+ --config FILE paper.toml config file (default: paper.toml)
306
+
307
+ Data:
308
+ --data JSON JSON file path or inline dict for template context
309
+ --facts-dir DIR Override facts CSV directory
310
+
311
+ Output:
312
+ --output-dir DIR Output directory for .docx files
313
+ --name NAME Output filename stem
314
+ --combined Merge supplementary into main document
315
+ ```
316
+
317
+ ---
318
+
319
+ ## Project layout convention
320
+
321
+ ```
322
+ my_paper/
323
+ ├── paper.toml
324
+ ├── paper/
325
+ │ ├── abstract.md
326
+ │ ├── introduction.md
327
+ │ ├── methods.md
328
+ │ ├── results.md
329
+ │ ├── discussion.md
330
+ │ ├── references.md
331
+ │ ├── figures.md
332
+ │ ├── supplementary.md
333
+ │ └── reference.docx ← optional Word formatting template
334
+ ├── output/
335
+ │ ├── facts/
336
+ │ │ ├── cohort.csv ← 1-row: n_patients, n_controls, ...
337
+ │ │ ├── model_results.csv ← 1-row: auc, pvalue, effect_size, ...
338
+ │ │ └── ...
339
+ │ └── tables/
340
+ │ └── full_results.csv ← multi-row: used in include-csv directives
341
+ └── scripts/
342
+ └── run_analysis.py ← writes to output/facts/
343
+ ```
@@ -0,0 +1,10 @@
1
+ vibepaper/__init__.py,sha256=aOHawL1zuHMfBWKXqwUkXcW96oXLNCY-CXdHDqkz4g4,18
2
+ vibepaper/build.py,sha256=aV5SsDJXfQkIFxe8f0C-ehxzBijbRix9a1ZRksJwXiM,10377
3
+ vibepaper/cli.py,sha256=fUpQ4JK9ntx5uLUOSrDC5XLQwZ4lQHwfrhgrbptK7ko,3959
4
+ vibepaper/render.py,sha256=CLL4s5PcoK0WTGtPiYBk-ZI4Cfav64aziWW2iNuPByc,3803
5
+ vibepaper/tables.py,sha256=NGY1u-YmQTxrEOL6S0O39sFYI5h0Jgyshu-3bnBFp5A,5402
6
+ vibepaper-0.1.0.dist-info/METADATA,sha256=uaxGCFAxjOcXuboKY8St9VH6W56LOX4taHWLD67XFbg,10157
7
+ vibepaper-0.1.0.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
8
+ vibepaper-0.1.0.dist-info/entry_points.txt,sha256=vxSzwDcsA3BNhSRxJyc6i15RULosdkYhcD5xSMJ9Ve8,49
9
+ vibepaper-0.1.0.dist-info/licenses/LICENSE,sha256=sC4Qc5X8JxIAWbDmnsQLQiiNS4iktXB2HzhvT02DV6A,1086
10
+ vibepaper-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.29.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ vibepaper = vibepaper.cli:main
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 ACRF Cancer Genomics Facility
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.