-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathprprint
More file actions
executable file
·338 lines (293 loc) · 10.9 KB
/
prprint
File metadata and controls
executable file
·338 lines (293 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!/usr/bin/env python3
"""
prprint - Print an R/S source file with syntax highlighting and block annotations.
Usage:
prprint <file.[RrSs]> listing only
prprint <file.[RrSs]> st listing followed by a symbol table
The file is rendered as a color-highlighted PDF, opened in Preview.
Code blocks delimited by { } are annotated in the left margin with
circled symbols in this sequence:
Ⓐ-Ⓩ (blocks 1-26)
ⓐ-ⓩ (blocks 27-52)
①-⑳ (blocks 53-72)
㉑-㉟ (blocks 73-87)
㊱-㊿ (blocks 88-102)
(51),(52),... beyond that
Same-line { } pairs are not annotated. On lines that both close one
block and open another, both symbols appear in the margin, closing
symbol first.
The symbol table (option st) lists every variable and function name
found in the file in case-insensitive alphabetical order, with a
comma-separated list of line numbers (capped at 10, then ...) where
each name appears. Names that appear only as named arguments or list
element names (left of = inside parentheses) are omitted. The table
is printed in a two-column layout on a new page.
Requirements (install once):
brew install weasyprint # installs weasyprint + all native libraries
pip3 install pygments # pure Python, no native dependencies
If pip3 is not found:
brew install python3 # includes pip3
"""
import os
import sys
import shutil
import subprocess
from collections import defaultdict
# ---------------------------------------------------------------------------
# Dependency check
# ---------------------------------------------------------------------------
MISSING_PY = []
MISSING_BIN = []
try:
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
from pygments.token import Token
import pygments
except ImportError:
MISSING_PY.append("pygments")
if shutil.which("weasyprint") is None:
MISSING_BIN.append("weasyprint")
if MISSING_PY or MISSING_BIN:
if MISSING_PY:
pkgs = " ".join(MISSING_PY)
print(f"prprint: missing Python package(s): {pkgs}", file=sys.stderr)
print(f" Install with: pip3 install {pkgs}", file=sys.stderr)
print(f" (If pip3 is missing: brew install python3)", file=sys.stderr)
if MISSING_BIN:
print(f"prprint: 'weasyprint' command not found", file=sys.stderr)
print(f" Install with: brew install weasyprint", file=sys.stderr)
sys.exit(1)
# ---------------------------------------------------------------------------
# Argument handling
# ---------------------------------------------------------------------------
def usage():
print("Usage: prprint <file.[RrSs]> [st]", file=sys.stderr)
sys.exit(1)
if len(sys.argv) not in (2, 3):
usage()
filepath = sys.argv[1]
want_symtab = len(sys.argv) == 3 and sys.argv[2] == "st"
if len(sys.argv) == 3 and sys.argv[2] != "st":
print(f"prprint: unknown option '{sys.argv[2]}' (only 'st' is supported)",
file=sys.stderr)
usage()
if not os.path.isfile(filepath):
print(f"prprint: file not found: {filepath}", file=sys.stderr)
sys.exit(1)
ext = os.path.splitext(filepath)[1].lstrip(".")
if ext not in ("R", "r", "S", "s"):
print(f"prprint: file must have suffix .R, .r, .S, or .s (got: .{ext})",
file=sys.stderr)
sys.exit(1)
# ---------------------------------------------------------------------------
# Symbol sequence for block annotations
# ---------------------------------------------------------------------------
def index_to_symbol(i: int) -> str:
if i <= 26:
return chr(0x24B6 + i - 1)
elif i <= 52:
return chr(0x24D0 + i - 27)
else:
n = i - 52
if n <= 20: return chr(0x2460 + n - 1)
elif n <= 35: return chr(0x3251 + n - 21)
elif n <= 50: return chr(0x32B1 + n - 36)
else: return f"({n})"
# ---------------------------------------------------------------------------
# Single token walk: brace pairs + symbol table
# ---------------------------------------------------------------------------
def analyse_source(source: str, want_symtab: bool):
lexer = get_lexer_by_name("r")
tokens = list(pygments.lex(source, lexer))
brace_events = []
sym_lines = defaultdict(set) # name -> set of line numbers
arg_only = defaultdict(lambda: True) # name -> still arg-only?
lineno = 1
for i, (ttype, val) in enumerate(tokens):
# Brace detection
if ttype == Token.Punctuation:
for ch in val:
if ch == "{":
brace_events.append((lineno, "open"))
elif ch == "}":
brace_events.append((lineno, "close"))
# Symbol collection
if want_symtab and ttype in (Token.Name, Token.Name.Function):
if val.strip():
sym_lines[val].add(lineno)
# Look ahead past whitespace to check for '='
is_named_arg = False
for j in range(i + 1, min(i + 4, len(tokens))):
nt, nv = tokens[j]
if not nv.strip():
continue
if nt == Token.Operator and nv == "=":
is_named_arg = True
break
if not is_named_arg:
arg_only[val] = False
lineno += val.count("\n")
# Match brace pairs
stack = []
pairs = []
for idx, (ln, kind) in enumerate(brace_events):
if kind == "open":
stack.append((ln, idx))
elif kind == "close" and stack:
open_ln, _ = stack.pop()
if open_ln != ln:
pairs.append((open_ln, ln))
pairs.sort(key=lambda p: p[0])
margin_map: dict = {}
for block_idx, (open_ln, close_ln) in enumerate(pairs, start=1):
sym = index_to_symbol(block_idx)
margin_map.setdefault(close_ln, []).insert(0, sym)
margin_map.setdefault(open_ln, []).append(sym)
# Build filtered, sorted symbol table
sym_table = {}
if want_symtab:
for name, lines in sym_lines.items():
if not arg_only[name]: # exclude arg-only names
sym_table[name] = sorted(lines)
return margin_map, sym_table
# ---------------------------------------------------------------------------
# Build annotated highlighted HTML for the listing
# ---------------------------------------------------------------------------
with open(filepath, "r", encoding="utf-8") as fh:
source = fh.read()
source_lines = source.splitlines()
total = len(source_lines)
numwidth = len(str(total))
margin_map, sym_table = analyse_source(source, want_symtab)
max_syms = max((len(v) for v in margin_map.values()), default=1)
margwidth = max(numwidth, max_syms)
annotated_lines = []
for i, line in enumerate(source_lines, start=1):
if i in margin_map:
syms = "".join(margin_map[i])
margin = syms + " " * (margwidth - len(margin_map[i]))
else:
margin = str(i).rjust(margwidth)
annotated_lines.append(f"{margin} {line}")
annotated_source = "\n".join(annotated_lines) + "\n"
lexer = get_lexer_by_name("r")
formatter = HtmlFormatter(style="tango", nowrap=True)
css_defs = HtmlFormatter(style="tango").get_style_defs(".highlight")
highlighted = highlight(annotated_source, lexer, formatter)
# ---------------------------------------------------------------------------
# Build symbol table HTML
# ---------------------------------------------------------------------------
# Character budget for line-number column:
# 8.5in page, 0.5in margins -> 7.5in content
# Two CSS columns, 1.5em gap at 7pt (1em=7/72in): gap=0.146in
# Each column = (7.5-0.146)/2 = 3.677in; st-lines is 65% = 2.390in
# 7pt monospace char width ~0.6em = 0.058in -> ~41 chars/line -> 82 for two lines; 20% conservative = 66
_LINENO_BUDGET = 66
def truncate_linenos(line_list: list) -> str:
"""Format line numbers to fit within two lines of the st-lines column."""
result = []
used = 0
for n in line_list:
chunk = (", " if result else "") + str(n)
if used + len(chunk) <= _LINENO_BUDGET:
result.append(str(n))
used += len(chunk)
else:
return ", ".join(result) + ", \u2026"
return ", ".join(result)
def build_symtab_html(sym_table: dict) -> str:
if not sym_table:
return ""
sorted_names = sorted(sym_table.keys(), key=str.casefold)
rows = []
for name in sorted_names:
linenos = truncate_linenos(sym_table[name])
safe_name = name.replace("&", "&").replace("<", "<")
rows.append(
f'<tr>'
f'<td class="st-name">{safe_name}</td>'
f'<td class="st-lines">{linenos}</td>'
f'</tr>'
)
rows_html = "\n".join(rows)
return f"""
<div class="symtab">
<h2>Symbol Table</h2>
<div class="st-columns">
<table>
{rows_html}
</table>
</div>
</div>"""
symtab_html = build_symtab_html(sym_table) if want_symtab else ""
# ---------------------------------------------------------------------------
# Assemble full HTML document
# ---------------------------------------------------------------------------
title = os.path.basename(filepath)
html = f"""<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{title}</title>
<style>
@page {{ margin: 0.5in }}
body {{ font-family: monospace; font-size: 9pt }}
h1 {{ font-family: sans-serif; font-size: 11pt; margin-bottom: 0.2em }}
h2 {{ font-family: sans-serif; font-size: 10pt; margin-bottom: 0.3em }}
.highlight {{ white-space: pre }}
{css_defs}
/* Symbol table */
.symtab {{
page-break-before: always;
}}
.st-columns {{
column-count: 2;
column-gap: 1.5em;
column-rule: 1px solid #ccc;
}}
table {{
width: 100%;
border-collapse: collapse;
font-size: 7pt;
}}
td {{
padding: 1px 4px;
vertical-align: top;
}}
td.st-name {{
font-weight: bold;
white-space: nowrap;
width: 35%;
}}
td.st-lines {{
color: #444;
}}
tr:nth-child(even) {{
background-color: #f5f5f5;
}}
</style>
</head>
<body>
<h1>{title}</h1>
<div class="highlight">{highlighted}</div>
{symtab_html}
</body>
</html>"""
# ---------------------------------------------------------------------------
# Write HTML, render to PDF via weasyprint, open in Preview
# ---------------------------------------------------------------------------
pid = os.getpid()
htmlpath = f"/tmp/prprint_{pid}.html"
pdfpath = f"/tmp/prprint_{pid}.pdf"
with open(htmlpath, "w", encoding="utf-8") as fh:
fh.write(html)
try:
subprocess.run(
["weasyprint", htmlpath, pdfpath],
check=True,
stderr=subprocess.DEVNULL
)
finally:
os.unlink(htmlpath)
subprocess.run(["open", "-a", "Preview", pdfpath])