dsh-work-summary 0.0.0-stage → 0.3.0

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.
@@ -0,0 +1,403 @@
1
+ # push-to-board.ps1 —— 把日报的「明日开场清单」追加到工作看板(看板.md)。
2
+ # 纯 Markdown + 复选框,不依赖任何 DSH 插件,可 git 追踪、可手工改。
3
+ # 本脚本属于 dsh-work-summary 插件仓库,而看板/日报在数据目录(默认 ~/.dsh/dsh-work-summary),
4
+ # 所以脚本不再猜看板位置:-BoardPath 必须显式给出(插件会传)。
5
+ #
6
+ # 用法:
7
+ # pwsh -NoProfile -File push-to-board.ps1 -BoardPath ~\.dsh\dsh-work-summary\看板.md -Show
8
+ # pwsh -NoProfile -File push-to-board.ps1 -BoardPath ~\.dsh\dsh-work-summary\看板.md -Report ~\.dsh\dsh-work-summary\日报-2026-10-29.md
9
+ # pwsh -NoProfile -File push-to-board.ps1 -BoardPath ... -Report ...\周报-2026-11-18_2026-11-24.md -Section 下周开场
10
+ # pwsh -NoProfile -File push-to-board.ps1 -BoardPath ... -DryRun # 只解析不写入
11
+ # pwsh -NoProfile -File push-to-board.ps1 -BoardPath ... -Done "git init" # 移到「已完成」
12
+ # pwsh -NoProfile -File push-to-board.ps1 -BoardPath ... -Drop "git init" # 移到「已放弃」
13
+ # pwsh -NoProfile -File push-to-board.ps1 -BoardPath ... -Update "git init" -Priority P1 -Due 2026-11-30 -Status doing
14
+ # pwsh -NoProfile -File push-to-board.ps1 -BoardPath ... -Update "#h1234abcd" -Priority none -Due none
15
+ #
16
+ # 条目格式:- [ ] 日期 | 标题 | 来源:… | #h指纹 | @P1 | !2026-11-30
17
+ # 后两段可省略,老看板(无优先级/截止)照常读写。日报条目末尾写 @P1 / !2026-11-30 会被带进看板。
18
+ # 去重:同一标题(条目取前 36 字)已在看板任意一栏出现时跳过。
19
+ # 小节名由 -Section 指定:日报默认「明日开场清单」,周报/月报传「下周开场」「下月开场」。
20
+
21
+ param(
22
+ [string]$Report,
23
+ [string]$BoardPath,
24
+ [string]$Section = '明日开场清单',
25
+ [string]$Done,
26
+ [string]$Drop,
27
+ [string]$Update,
28
+ [string]$Priority,
29
+ [string]$Due,
30
+ [string]$Status,
31
+ [switch]$DryRun,
32
+ [switch]$Show
33
+ )
34
+
35
+ $ErrorActionPreference = 'Stop'
36
+ $Today = (Get-Date -Format 'yyyy-MM-dd')
37
+ if (-not $BoardPath) {
38
+ throw '缺少 -BoardPath:看板在数据目录(默认 ~\.dsh\dsh-work-summary\看板.md),不在脚本旁边。'
39
+ }
40
+
41
+ function Read-Board {
42
+ if (-not (Test-Path $BoardPath)) {
43
+ return @(
44
+ '# 工作看板', '',
45
+ '> 由 `dsh-work-summary\push-to-board.ps1` 自动追加;手工维护也完全可以。', '',
46
+ '## 待办', '', '## 进行中', '', '## 已完成', ''
47
+ )
48
+ }
49
+ return @(Get-Content $BoardPath -Encoding UTF8)
50
+ }
51
+
52
+ function Get-Hash([string]$s) {
53
+ $norm = ($s -replace '\s', '' -replace '`', '' -replace '\*\*', '')
54
+ $bytes = [System.Text.Encoding]::UTF8.GetBytes($norm)
55
+ $sha = [System.Security.Cryptography.SHA256]::Create()
56
+ $hex = ([System.BitConverter]::ToString($sha.ComputeHash($bytes)) -replace '-', '').ToLower()
57
+ $sha.Dispose()
58
+ return $hex.Substring(0, 8)
59
+ }
60
+
61
+ function Get-Title([string]$s) {
62
+ $t = ($s -replace '\*\*', '' -replace '`', '').Trim()
63
+ foreach ($sep in ':', ';', '。', ',') {
64
+ $i = $t.IndexOf($sep)
65
+ if ($i -ge 8 -and $i -le 36) { return $t.Substring(0, $i) }
66
+ }
67
+ if ($t.Length -gt 36) { return $t.Substring(0, 36) + '…' }
68
+ return $t
69
+ }
70
+
71
+ function Find-Section([string[]]$lines, [string]$name) {
72
+ for ($i = 0; $i -lt $lines.Count; $i++) {
73
+ if ($lines[$i] -match ('^##\s*' + [regex]::Escape($name))) { return $i }
74
+ }
75
+ return -1
76
+ }
77
+
78
+ function Section-End([string[]]$lines, [int]$start) {
79
+ for ($i = $start + 1; $i -lt $lines.Count; $i++) {
80
+ if ($lines[$i] -match '^##\s') { return $i }
81
+ }
82
+ return $lines.Count
83
+ }
84
+
85
+ function Get-EntryMeta([string]$text) {
86
+ $priority = ''
87
+ $due = ''
88
+ $p = [regex]::Match($text, '(?i)(?:^|\s)@(p[1-3])(?=\s|$)')
89
+ if ($p.Success) { $priority = $p.Groups[1].Value.ToUpper() }
90
+ $d = [regex]::Match($text, '(?:^|\s)!(\d{4}-\d{2}-\d{2})(?=\s|$)')
91
+ if ($d.Success) { $due = $d.Groups[1].Value }
92
+ return @{ Priority = $priority; Due = $due }
93
+ }
94
+
95
+ function Remove-EntryMeta([string]$text) {
96
+ $clean = $text -replace '(?i)(?:^|\s)@p[1-3](?=\s|$)', ' '
97
+ $clean = $clean -replace '(?:^|\s)!\d{4}-\d{2}-\d{2}(?=\s|$)', ' '
98
+ return ($clean -replace '\s+', ' ').Trim()
99
+ }
100
+
101
+ # 重写条目的元数据段,保留原有顺序与详情行;没有优先级/截止时就是老格式。
102
+ function Set-EntryMeta([string]$line, [string]$priority, [string]$due) {
103
+ $body = ($line -replace '^\s*-\s*(?:\[[ xX]\]|⛔)\s*', '').Trim()
104
+ $body = $body -replace '((?:完成|放弃) \d{4}-\d{2}-\d{2})(?=\s*(?:||$))', ''
105
+ $parts = @()
106
+ foreach ($part in ($body -split '|')) {
107
+ $value = $part.Trim()
108
+ if ($value -eq '') { continue }
109
+ if ($value -match '^@[Pp][1-3]$' -or $value -match '^!\d{4}-\d{2}-\d{2}$') { continue }
110
+ $parts += (Remove-EntryMeta $value)
111
+ }
112
+ if ($priority) { $parts += ('@' + $priority.ToUpper()) }
113
+ if ($due) { $parts += ('!' + $due) }
114
+ return ('- [ ] ' + ($parts -join ' | '))
115
+ }
116
+
117
+ function Get-EntryEnd([string[]]$lines, [int]$start) {
118
+ $end = $start
119
+ while ($end + 1 -lt $lines.Count -and $lines[$end + 1] -match '^\s{2,}\S') { $end++ }
120
+ return $end
121
+ }
122
+
123
+ function Save-Board([string[]]$lines) {
124
+ $outList = New-Object System.Collections.Generic.List[string]
125
+ foreach ($l in $lines) {
126
+ if ($l.Trim() -eq '' -and $outList.Count -gt 0 -and $outList[$outList.Count - 1].Trim() -eq '') { continue }
127
+ $outList.Add($l)
128
+ }
129
+ while ($outList.Count -gt 0 -and $outList[$outList.Count - 1].Trim() -eq '') { $outList.RemoveAt($outList.Count - 1) }
130
+ $outList.Add('')
131
+ Set-Content -Path $BoardPath -Value $outList -Encoding UTF8
132
+ }
133
+
134
+ $board = Read-Board
135
+
136
+ if ($Show) { $board | ForEach-Object { Write-Output $_ }; return }
137
+
138
+ # ---------- -Update:改优先级 / 截止日 / 状态,一条一条来 ----------
139
+ if ($Update) {
140
+ if ($Done -or $Drop) { throw '-Update 不能和 -Done / -Drop 同用。' }
141
+ if (-not $Priority -and -not $Due -and -not $Status) {
142
+ throw '-Update 至少要给一个 -Priority / -Due / -Status。'
143
+ }
144
+ if ($Priority -and $Priority -notmatch '^(P[1-3]|none)$') { throw "-Priority 只能是 P1/P2/P3/none,收到:$Priority" }
145
+ if ($Due -and $Due -ne 'none') {
146
+ if ($Due -notmatch '^\d{4}-\d{2}-\d{2}$') { throw "-Due 要写 YYYY-MM-DD 或 none,收到:$Due" }
147
+ try { [void][datetime]::ParseExact($Due, 'yyyy-MM-dd', $null) }
148
+ catch { throw "-Due 不是有效日期:$Due" }
149
+ }
150
+ $statusValue = if ($Status) { $Status.ToLower() } else { '' }
151
+ if ($statusValue -and $statusValue -notin @('todo', 'doing', 'done', 'drop')) {
152
+ throw "-Status 只能是 todo/doing/done/drop,收到:$Status"
153
+ }
154
+
155
+ $hits = @()
156
+ $needle = $Update.Trim()
157
+ for ($i = 0; $i -lt $board.Count; $i++) {
158
+ if ($board[$i] -notmatch '^\s*-\s*(?:\[[ xX]\]|⛔)') { continue }
159
+ if ($board[$i].IndexOf($needle, [StringComparison]::OrdinalIgnoreCase) -ge 0) { $hits += $i }
160
+ }
161
+ if ($hits.Count -eq 0) { throw "看板里没有匹配「$needle」的条目。" }
162
+ if ($hits.Count -gt 1) {
163
+ throw "「$needle」匹配到 $($hits.Count) 条,请写得更具体(也可以直接用条目里的 #h 指纹)。"
164
+ }
165
+
166
+ $start = $hits[0]
167
+ $end = Get-EntryEnd $board $start
168
+ $detail = @()
169
+ if ($end -gt $start) { $detail = @($board[($start + 1)..$end]) }
170
+
171
+ $sectionName = ''
172
+ for ($i = $start; $i -ge 0; $i--) {
173
+ if ($board[$i] -match '^##\s+(.+?)\s*$') { $sectionName = $Matches[1]; break }
174
+ }
175
+ $currentStatus = switch ($sectionName) {
176
+ '待办' { 'todo' }
177
+ '进行中' { 'doing' }
178
+ '已完成' { 'done' }
179
+ '已放弃' { 'drop' }
180
+ default { 'todo' }
181
+ }
182
+ $targetStatus = if ($statusValue) { $statusValue } else { $currentStatus }
183
+
184
+ $old = Get-EntryMeta $board[$start]
185
+ $newPriority = if ($Priority) { if ($Priority -eq 'none') { '' } else { $Priority.ToUpper() } } else { $old.Priority }
186
+ $newDue = if ($Due) { if ($Due -eq 'none') { '' } else { $Due } } else { $old.Due }
187
+ $body = (Set-EntryMeta $board[$start] $newPriority $newDue) -replace '^- \[ \]\s*', ''
188
+ $existingStamp = ''
189
+ if ($board[$start] -match '((?:完成|放弃) \d{4}-\d{2}-\d{2})') { $existingStamp = $Matches[0] }
190
+ $body = $body -replace '((?:完成|放弃) \d{4}-\d{2}-\d{2})(?=\s*(?:||$))', ''
191
+ # 只改优先级/截止日时保留原来的完成/放弃日期,别把历史改写成今天。
192
+ $stamp = if ($targetStatus -eq $currentStatus -and $existingStamp) { $existingStamp } else { "(完成 $Today)" }
193
+ $newLine = switch ($targetStatus) {
194
+ 'done' { '- [x] ' + $body + $stamp }
195
+ 'drop' { '- ⛔ ' + $body + $(if ($existingStamp -and $targetStatus -eq $currentStatus) { $existingStamp } else { "(放弃 $Today)" }) }
196
+ default { '- [ ] ' + $body }
197
+ }
198
+
199
+ if ($targetStatus -eq $currentStatus) {
200
+ if ($DryRun) {
201
+ Write-Output 'DryRun:将改为:'
202
+ Write-Output $newLine
203
+ return
204
+ }
205
+ $board[$start] = $newLine
206
+ Save-Board $board
207
+ Write-Output ("已更新 1 条 → {0}" -f $BoardPath)
208
+ Write-Output $newLine
209
+ return
210
+ }
211
+
212
+ $targetSection = switch ($targetStatus) {
213
+ 'doing' { '进行中' }
214
+ 'done' { '已完成' }
215
+ 'drop' { '已放弃' }
216
+ default { '待办' }
217
+ }
218
+ if ($DryRun) {
219
+ Write-Output ("DryRun:将移到「{0}」:" -f $targetSection)
220
+ Write-Output $newLine
221
+ return
222
+ }
223
+
224
+ $kept = New-Object System.Collections.Generic.List[string]
225
+ for ($i = 0; $i -lt $board.Count; $i++) {
226
+ if ($i -ge $start -and $i -le $end) { continue }
227
+ $kept.Add($board[$i])
228
+ }
229
+ $targetIdx = Find-Section $kept.ToArray() $targetSection
230
+ if ($targetIdx -lt 0) {
231
+ if ($kept.Count -gt 0 -and $kept[$kept.Count - 1].Trim() -ne '') { $kept.Add('') }
232
+ $kept.Add('## ' + $targetSection)
233
+ $targetIdx = $kept.Count - 1
234
+ }
235
+ $targetEnd = Section-End $kept.ToArray() $targetIdx
236
+ $insertAt = $targetEnd
237
+ while ($insertAt -gt $targetIdx + 1 -and $kept[$insertAt - 1].Trim() -eq '') { $insertAt-- }
238
+
239
+ $out = New-Object System.Collections.Generic.List[string]
240
+ for ($i = 0; $i -lt $insertAt; $i++) { $out.Add($kept[$i]) }
241
+ if ($out.Count -gt 0 -and $out[$out.Count - 1] -match '^##\s') { $out.Add('') }
242
+ $out.Add($newLine)
243
+ foreach ($line in $detail) { $out.Add($line) }
244
+ for ($i = $insertAt; $i -lt $kept.Count; $i++) { $out.Add($kept[$i]) }
245
+
246
+ Save-Board $out.ToArray()
247
+ Write-Output ("已移到「{0}」→ {1}" -f $targetSection, $BoardPath)
248
+ Write-Output $newLine
249
+ return
250
+ }
251
+
252
+ # ---------- -Done / -Drop:把匹配的待办移到「已完成」/「已放弃」 ----------
253
+ $mode = if ($Drop) { 'drop' } elseif ($Done) { 'done' } else { $null }
254
+ if ($mode) {
255
+ $keyword = if ($mode -eq 'drop') { $Drop } else { $Done }
256
+ $targetSection = if ($mode -eq 'drop') { '已放弃' } else { '已完成' }
257
+ $todoIdx = Find-Section $board '待办'
258
+ if ($todoIdx -lt 0) { throw "看板缺少「## 待办」小节:$BoardPath" }
259
+ $todoEnd = Section-End $board $todoIdx
260
+
261
+ $kept = @(); $moved = @()
262
+ for ($i = 0; $i -lt $board.Count; $i++) {
263
+ $inTodo = ($i -gt $todoIdx -and $i -lt $todoEnd)
264
+ if ($inTodo -and $board[$i] -match '^\s*-\s*\[ \]' -and $board[$i].Contains($keyword)) {
265
+ $body = ($board[$i] -replace '^\s*-\s*\[ \]\s*', '').Trim()
266
+ if ($mode -eq 'drop') { $moved += ('- ⛔ {0}(放弃 {1})' -f $body, $Today) }
267
+ else { $moved += ('- [x] {0}(完成 {1})' -f $body, $Today) }
268
+ while ($i + 1 -lt $todoEnd -and $board[$i + 1] -match '^\s{2,}\S') { $i++ }
269
+ continue
270
+ }
271
+ $kept += $board[$i]
272
+ }
273
+
274
+ if ($moved.Count -eq 0) { Write-Output ("「待办」里没有包含「{0}」的条目。" -f $keyword); return }
275
+ if ($DryRun) {
276
+ Write-Output ("DryRun:将处理 {0} 条:" -f $moved.Count)
277
+ $moved | ForEach-Object { Write-Output $_ }
278
+ return
279
+ }
280
+
281
+ $doneIdx = Find-Section $kept $targetSection
282
+ if ($doneIdx -lt 0) {
283
+ if ($kept.Count -gt 0 -and $kept[-1].Trim() -ne '') { $kept += '' }
284
+ $kept += ('## ' + $targetSection)
285
+ $doneIdx = $kept.Count - 1
286
+ }
287
+
288
+ $out = @()
289
+ $out += $kept[0..$doneIdx]
290
+ $tail = @()
291
+ if ($doneIdx + 1 -lt $kept.Count) { $tail = @($kept[($doneIdx + 1)..($kept.Count - 1)]) }
292
+ # 去掉紧跟标题的空行,再统一补一个空行,避免出现连续空行
293
+ while ($tail.Count -gt 0 -and $tail[0].Trim() -eq '') { $tail = @($tail[1..($tail.Count - 1)]) }
294
+ $out += ''
295
+ $out += $moved
296
+ if ($tail.Count -gt 0) { $out += $tail }
297
+
298
+ Save-Board $out
299
+ $verb = if ($mode -eq 'drop') { '已放弃' } else { '已标记完成' }
300
+ Write-Output ("{0} {1} 条:" -f $verb, $moved.Count)
301
+ $moved | ForEach-Object { Write-Output $_ }
302
+ return
303
+ }
304
+
305
+ # ---------- 从日报/周报解析指定小节(默认「明日开场清单」) ----------
306
+ if (-not $Report) {
307
+ $Report = (Get-ChildItem (Split-Path -Parent $BoardPath) -Filter '日报-*.md' -File |
308
+ Sort-Object LastWriteTime -Descending | Select-Object -First 1).FullName
309
+ }
310
+ if (-not $Report -or -not (Test-Path $Report)) { throw "找不到日报文件:$Report" }
311
+
312
+ $lines = Get-Content $Report -Encoding UTF8
313
+ $start = -1
314
+ $sectionPattern = '^##\s.*' + [regex]::Escape($Section)
315
+ for ($i = 0; $i -lt $lines.Count; $i++) {
316
+ if ($lines[$i] -match $sectionPattern) { $start = $i + 1; break }
317
+ }
318
+ if ($start -lt 0) { throw "日报里没有「$Section」小节:$Report" }
319
+
320
+ $items = @()
321
+ for ($i = $start; $i -lt $lines.Count; $i++) {
322
+ if ($lines[$i] -match '^##\s') { break }
323
+ if ($lines[$i] -match '^\s*(?:\d+[\.、]|[-*])\s+(.+)$') {
324
+ $t = $Matches[1].Trim()
325
+ # 以 ✅ 开头或标注「已完成」的条目视为已办,不进看板
326
+ if ($t -notmatch '^(✅|\[x\])' -and $t -notmatch '已完成') {
327
+ $meta = Get-EntryMeta $t
328
+ $clean = Remove-EntryMeta $t
329
+ if ($clean -ne '') { $items += [pscustomobject]@{ Text = $clean; Priority = $meta.Priority; Due = $meta.Due } }
330
+ }
331
+ }
332
+ }
333
+ if ($items.Count -eq 0) { throw "「$Section」里没有解析到条目:$Report" }
334
+
335
+ Write-Output ("解析到 {0} 条待办:来自 {1}" -f $items.Count, $Report)
336
+ foreach ($it in $items) { Write-Output (' - ' + $it.Text) }
337
+
338
+ # ---------- 去重:同时比对「详情指纹」与「标题指纹」 ----------
339
+ function Get-TitleOfLine([string]$line) {
340
+ $parts = $line -split '|'
341
+ if ($parts.Count -ge 2) { return $parts[1].Trim() }
342
+ return ''
343
+ }
344
+
345
+ # 只在「## 待办」里查重:已完成/已放弃的条目允许再次进入待办(是新的待办,不是重复)
346
+ $existing = @{}
347
+ $todoStart = Find-Section $board '待办'
348
+ $todoEndIdx = if ($todoStart -ge 0) { Section-End $board $todoStart } else { 0 }
349
+ for ($i = 0; $i -lt $board.Count; $i++) {
350
+ if ($todoStart -lt 0 -or $i -le $todoStart -or $i -ge $todoEndIdx) { continue }
351
+ $l = $board[$i]
352
+ if ($l -notmatch '^\s*-\s*\[ \]') { continue }
353
+ $tk = Get-TitleOfLine $l
354
+ if ($tk) { $existing[(Get-Hash $tk)] = $true }
355
+ if ($l -match '#h([0-9a-f]{8})') { $existing[$Matches[1]] = $true; continue }
356
+ # 兼容手写/旧格式条目:用紧随其后的「详情:」行(或条目正文)算指纹
357
+ $src2 = $l
358
+ if ($i + 1 -lt $board.Count -and $board[$i + 1] -match '^\s{2,}-\s*详情:(.+)$') { $src2 = $Matches[1] }
359
+ $existing[(Get-Hash $src2)] = $true
360
+ }
361
+
362
+ $newLines = @(); $added = 0; $skipped = @()
363
+ $src = Split-Path $Report -Leaf
364
+ foreach ($it in $items) {
365
+ $title = Get-Title $it.Text
366
+ $hash = Get-Hash $it.Text
367
+ $tkey = Get-Hash $title
368
+ if ($existing.ContainsKey($hash) -or $existing.ContainsKey($tkey)) { $skipped += $title; continue }
369
+ $metaText = ''
370
+ if ($it.Priority) { $metaText += ' | @' + $it.Priority }
371
+ if ($it.Due) { $metaText += ' | !' + $it.Due }
372
+ $newLines += ('- [ ] {0} | {1} | 来源:{2} | #h{3}{4}' -f $Today, $title, $src, $hash, $metaText)
373
+ $newLines += (' - 详情:{0}' -f $it.Text)
374
+ $existing[$hash] = $true
375
+ $existing[$tkey] = $true
376
+ $added++
377
+ }
378
+
379
+ if ($added -eq 0) { Write-Output ("已写入 0 条,跳过重复 {0} 条 → {1}" -f $skipped.Count, $BoardPath); return }
380
+ if ($DryRun) {
381
+ Write-Output ("DryRun:将写入 {0} 条:" -f $added)
382
+ $newLines | ForEach-Object { Write-Output $_ }
383
+ return
384
+ }
385
+
386
+ # ---------- 插入到「## 待办」末尾 ----------
387
+ $todoIdx = Find-Section $board '待办'
388
+ if ($todoIdx -lt 0) { throw "看板缺少「## 待办」小节:$BoardPath" }
389
+ $todoEnd = Section-End $board $todoIdx
390
+
391
+ $insertAt = $todoEnd
392
+ while ($insertAt -gt $todoIdx + 1 -and $board[$insertAt - 1].Trim() -eq '') { $insertAt-- }
393
+
394
+ $out = @()
395
+ if ($insertAt -gt 0) { $out += $board[0..($insertAt - 1)] }
396
+ # 待办为空时插在标题正下方,补一个空行保持 Markdown 结构
397
+ if ($out.Count -gt 0 -and $out[-1] -match '^##\s') { $out += '' }
398
+ $out += $newLines
399
+ if ($insertAt -lt $board.Count) { $out += $board[$insertAt..($board.Count - 1)] }
400
+
401
+ Save-Board $out
402
+ Write-Output ("已写入 {0} 条,跳过重复 {1} 条 → {2}" -f $added, $skipped.Count, $BoardPath)
403
+ if ($skipped.Count -gt 0) { foreach ($s in $skipped) { Write-Output (' 跳过:' + $s) } }