Skip to content

Commit 130b954

Browse files
author
lyfhf
committed
Tighten course-design fastline output
1 parent a81bb0a commit 130b954

5 files changed

Lines changed: 507 additions & 276 deletions

File tree

scripts/build-report.ps1

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,10 @@ function Get-CourseDesignFlowchartSteps {
419419
}
420420

421421
$steps.Add("功能联调与测试验证") | Out-Null
422-
$steps.Add("问题修正与优化完善") | Out-Null
422+
$steps.Add("是否达到设计要求?") | Out-Null
423+
$steps.Add("问题定位与方案调整") | Out-Null
424+
$steps.Add("实现优化与结果复测") | Out-Null
425+
$steps.Add("形成最终结果与文档输出") | Out-Null
423426

424427
return @($steps | Select-Object -Unique)
425428
}

scripts/format-docx-report-style.ps1

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,33 @@ function Get-ParagraphText {
334334
return Normalize-OpenXmlText -Text ($parts -join "")
335335
}
336336

337+
function Set-ParagraphText {
338+
param(
339+
[Parameter(Mandatory = $true)]
340+
[System.Xml.XmlNode]$Paragraph,
341+
342+
[AllowNull()]
343+
[string]$Text
344+
)
345+
346+
$targetText = if ($null -eq $Text) { "" } else { [string]$Text }
347+
$textNodes = @($Paragraph.SelectNodes(".//w:t | .//w:instrText", $script:namespaceManager))
348+
if ($textNodes.Count -eq 0) {
349+
$run = $Paragraph.OwnerDocument.CreateElement("w", "r", $wordNamespace)
350+
$textNode = $Paragraph.OwnerDocument.CreateElement("w", "t", $wordNamespace)
351+
$textNode.SetAttribute("space", "http://www.w3.org/XML/1998/namespace", "preserve")
352+
$textNode.InnerText = $targetText
353+
[void]$run.AppendChild($textNode)
354+
[void]$Paragraph.AppendChild($run)
355+
return
356+
}
357+
358+
$textNodes[0].InnerText = $targetText
359+
for ($i = $textNodes.Count - 1; $i -ge 1; $i--) {
360+
[void]$textNodes[$i].ParentNode.RemoveChild($textNodes[$i])
361+
}
362+
}
363+
337364
function Test-IsRemovableTrailingParagraph {
338365
param(
339366
[Parameter(Mandatory = $true)]
@@ -447,6 +474,14 @@ function Remove-CourseDesignDuplicatePlaceholderParagraphs {
447474
$removeParagraph = [string]::Equals([string]$nextInfo.Text, "摘要:", [System.StringComparison]::Ordinal)
448475
break
449476
}
477+
"系统设计" {
478+
$removeParagraph = (
479+
([string]$nextInfo.Text -match '^图\d+\s*系统实现流程图$') -or
480+
([string]$nextInfo.Text -match '^[四4][、\..]\s*(方案设计与实现|系统设计)$') -or
481+
([string]$nextInfo.Text -match '^\d+\.1\s*(系统总体设计|功能模块设计)$')
482+
)
483+
break
484+
}
450485
"实现结果" {
451486
$removeParagraph = ([string]$nextInfo.Text -match '^[五5][、\..]\s*实现结果$')
452487
break
@@ -466,6 +501,94 @@ function Remove-CourseDesignDuplicatePlaceholderParagraphs {
466501
return $removedCount
467502
}
468503

504+
function Convert-ChineseOrdinalToInt {
505+
param([AllowNull()][string]$Text)
506+
507+
$normalized = Normalize-OpenXmlText -Text $Text
508+
if ([string]::IsNullOrWhiteSpace($normalized)) {
509+
return $null
510+
}
511+
512+
$lookup = @{
513+
"" = 1
514+
"" = 2
515+
"" = 3
516+
"" = 4
517+
"" = 5
518+
"" = 6
519+
"" = 7
520+
"" = 8
521+
"" = 9
522+
"" = 10
523+
}
524+
525+
if ($lookup.ContainsKey($normalized)) {
526+
return [int]$lookup[$normalized]
527+
}
528+
529+
return $null
530+
}
531+
532+
function Get-SectionHeadingNumber {
533+
param([AllowNull()][string]$Text)
534+
535+
$normalized = Normalize-OpenXmlText -Text $Text
536+
if ([string]::IsNullOrWhiteSpace($normalized)) {
537+
return $null
538+
}
539+
540+
if ($normalized -match '^(?<num>\d+)\s*[\..、]') {
541+
return [int]$matches["num"]
542+
}
543+
544+
if ($normalized -match '^(?<cn>[一二三四五六七八九十]+)\s*[\..、]') {
545+
return Convert-ChineseOrdinalToInt -Text $matches["cn"]
546+
}
547+
548+
return $null
549+
}
550+
551+
function Renumber-CourseDesignDecimalSubheadings {
552+
param(
553+
[Parameter(Mandatory = $true)]
554+
[System.Xml.XmlNode]$Body,
555+
556+
[Parameter(Mandatory = $true)]
557+
[System.Xml.XmlNamespaceManager]$NamespaceManager
558+
)
559+
560+
$currentSectionNumber = $null
561+
$currentSubsectionNumber = 0
562+
$renumberedCount = 0
563+
foreach ($paragraph in @($Body.SelectNodes("./w:p", $NamespaceManager))) {
564+
$text = Get-ParagraphText -Paragraph $paragraph -NamespaceManager $NamespaceManager
565+
if ([string]::IsNullOrWhiteSpace($text)) {
566+
continue
567+
}
568+
569+
if (Test-IsSectionHeading -Text $text) {
570+
$currentSectionNumber = Get-SectionHeadingNumber -Text $text
571+
$currentSubsectionNumber = 0
572+
continue
573+
}
574+
575+
if ($null -eq $currentSectionNumber) {
576+
continue
577+
}
578+
579+
if ($text -match '^(?<chapter>\d+)\.(?<sub>\d+)\s+(?<title>\S.*)$') {
580+
$currentSubsectionNumber++
581+
$desiredText = "{0}.{1} {2}" -f $currentSectionNumber, $currentSubsectionNumber, [string]$matches["title"]
582+
if (-not [string]::Equals($text, $desiredText, [System.StringComparison]::Ordinal)) {
583+
Set-ParagraphText -Paragraph $paragraph -Text $desiredText
584+
$renumberedCount++
585+
}
586+
}
587+
}
588+
589+
return $renumberedCount
590+
}
591+
469592
function Get-HeadingPattern {
470593
param(
471594
[Parameter(Mandatory = $true)]
@@ -1740,9 +1863,11 @@ try {
17401863
}
17411864

17421865
$removedCourseDesignPlaceholderCount = 0
1866+
$renumberedCourseDesignSubheadingCount = 0
17431867
$enabledCourseDesignTitlePageCount = 0
17441868
if ($isCourseDesignReport) {
17451869
$removedCourseDesignPlaceholderCount = Remove-CourseDesignDuplicatePlaceholderParagraphs -Body $body -NamespaceManager $script:namespaceManager
1870+
$renumberedCourseDesignSubheadingCount = Renumber-CourseDesignDecimalSubheadings -Body $body -NamespaceManager $script:namespaceManager
17461871
if (Ensure-CourseDesignCoverUsesDifferentFirstPage -Body $body) {
17471872
$enabledCourseDesignTitlePageCount = 1
17481873
}
@@ -1783,6 +1908,7 @@ try {
17831908
styledTableParagraphCount = $styledTableParagraphCount
17841909
normalizedBodyRowCount = $normalizedBodyRowCount
17851910
removedCourseDesignPlaceholderCount = $removedCourseDesignPlaceholderCount
1911+
renumberedCourseDesignSubheadingCount = $renumberedCourseDesignSubheadingCount
17861912
enabledCourseDesignTitlePageCount = $enabledCourseDesignTitlePageCount
17871913
removedTrailingEmptyParagraphCount = $removedTrailingEmptyParagraphCount
17881914
}

scripts/insert-course-design-tables.ps1

Lines changed: 133 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,110 @@ function Get-NodeText {
4040
return (($parts -join "") -replace "\s+", " ").Trim()
4141
}
4242

43+
function Convert-ChineseOrdinalToInt {
44+
param([AllowNull()][string]$Text)
45+
46+
$normalized = if ($null -eq $Text) { "" } else { [string]$Text }
47+
$normalized = $normalized.Trim()
48+
if ([string]::IsNullOrWhiteSpace($normalized)) {
49+
return $null
50+
}
51+
52+
$lookup = @{
53+
"" = 1
54+
"" = 2
55+
"" = 3
56+
"" = 4
57+
"" = 5
58+
"" = 6
59+
"" = 7
60+
"" = 8
61+
"" = 9
62+
"" = 10
63+
}
64+
if ($lookup.ContainsKey($normalized)) {
65+
return [int]$lookup[$normalized]
66+
}
67+
68+
return $null
69+
}
70+
71+
function Get-HeadingChapterNumber {
72+
param([AllowNull()][string]$Text)
73+
74+
$normalized = if ($null -eq $Text) { "" } else { [string]$Text }
75+
$normalized = $normalized.Trim()
76+
if ([string]::IsNullOrWhiteSpace($normalized)) {
77+
return $null
78+
}
79+
80+
if ($normalized -match '^(?<num>\d+)\s*[\..、]') {
81+
return [int]$matches["num"]
82+
}
83+
84+
if ($normalized -match '^第\s*(?<num>\d+)\s*[章节]') {
85+
return [int]$matches["num"]
86+
}
87+
88+
if ($normalized -match '^(?<cn>[一二三四五六七八九十]+)\s*[\..、]') {
89+
return Convert-ChineseOrdinalToInt -Text $matches["cn"]
90+
}
91+
92+
if ($normalized -match '^第\s*(?<cn>[一二三四五六七八九十]+)\s*[章节]') {
93+
return Convert-ChineseOrdinalToInt -Text $matches["cn"]
94+
}
95+
96+
return $null
97+
}
98+
99+
function Get-NextSubsectionNumber {
100+
param(
101+
[Parameter(Mandatory = $true)]
102+
[System.Xml.XmlNode[]]$Children,
103+
104+
[int]$StartIndex = -1,
105+
106+
[AllowNull()]
107+
[System.Xml.XmlNode]$StopNode,
108+
109+
[Parameter(Mandatory = $true)]
110+
[System.Xml.XmlNamespaceManager]$NamespaceManager,
111+
112+
[int]$ChapterNumber = 4
113+
)
114+
115+
if ($StartIndex -lt 0) {
116+
return 1
117+
}
118+
119+
$maxSubsectionNumber = 0
120+
$pattern = '^{0}\.(?<sub>\d+)\s+' -f [regex]::Escape([string]$ChapterNumber)
121+
for ($i = $StartIndex + 1; $i -lt $Children.Count; $i++) {
122+
$child = $Children[$i]
123+
if ($null -ne $StopNode -and [object]::ReferenceEquals($child, $StopNode)) {
124+
break
125+
}
126+
127+
if ($child.LocalName -ne "p") {
128+
continue
129+
}
130+
131+
$text = Get-NodeText -Node $child -NamespaceManager $NamespaceManager
132+
if ([string]::IsNullOrWhiteSpace($text)) {
133+
continue
134+
}
135+
136+
if ($text -match $pattern) {
137+
$subsectionNumber = [int]$matches["sub"]
138+
if ($subsectionNumber -gt $maxSubsectionNumber) {
139+
$maxSubsectionNumber = $subsectionNumber
140+
}
141+
}
142+
}
143+
144+
return ($maxSubsectionNumber + 1)
145+
}
146+
43147
function New-RunPropertiesXml {
44148
param(
45149
[string]$FontName = "宋体",
@@ -292,18 +396,29 @@ function Get-CourseDesignTableProfile {
292396
}
293397

294398
function New-CourseDesignTablesXml {
295-
param([Parameter(Mandatory = $true)][object]$Profile)
399+
param(
400+
[Parameter(Mandatory = $true)]
401+
[object]$Profile,
402+
403+
[int]$ChapterNumber = 4,
404+
405+
[int]$StartingSubsectionNumber = 1
406+
)
407+
408+
$moduleSectionNumber = $StartingSubsectionNumber
409+
$databaseSectionNumber = $StartingSubsectionNumber + 1
410+
$fieldTableSectionNumber = $StartingSubsectionNumber + 2
296411

297412
$xml = New-Object System.Collections.Generic.List[string]
298-
[void]$xml.Add((New-ParagraphXml -Text "(1)功能模块设计" -FirstLineTwips 420 -BeforeTwips 80 -AfterTwips 0 -LineTwips 320 -FontName "宋体" -SizeHalfPoints 21))
299-
[void]$xml.Add((New-TableBlockXml -Caption "表4-1 功能模块表" -Headers @("功能模块", "包含子功能模块", "功能") -Rows @($Profile.modules) -Widths @(1800, 2200, 4600) -MergeFirstColumn))
300-
[void]$xml.Add((New-ParagraphXml -Text "(2)数据库设计" -FirstLineTwips 420 -BeforeTwips 120 -AfterTwips 0 -LineTwips 320 -FontName "宋体" -SizeHalfPoints 21))
301-
[void]$xml.Add((New-TableBlockXml -Caption "表4-2 数据库表" -Headers @("序号", "数据库表", "数据表存储的内容") -Rows @($Profile.database) -Widths @(900, 2500, 5200)))
302-
[void]$xml.Add((New-ParagraphXml -Text "(3)数据库表结构" -FirstLineTwips 420 -BeforeTwips 120 -AfterTwips 0 -LineTwips 320 -FontName "宋体" -SizeHalfPoints 21))
413+
[void]$xml.Add((New-ParagraphXml -Text ("{0}.{1} 功能模块设计" -f $ChapterNumber, $moduleSectionNumber) -FirstLineTwips 420 -BeforeTwips 80 -AfterTwips 0 -LineTwips 320 -FontName "宋体" -SizeHalfPoints 21))
414+
[void]$xml.Add((New-TableBlockXml -Caption ("表{0}-1 功能模块表" -f $ChapterNumber) -Headers @("功能模块", "包含子功能模块", "功能") -Rows @($Profile.modules) -Widths @(1800, 2200, 4600) -MergeFirstColumn))
415+
[void]$xml.Add((New-ParagraphXml -Text ("{0}.{1} 数据库设计" -f $ChapterNumber, $databaseSectionNumber) -FirstLineTwips 420 -BeforeTwips 120 -AfterTwips 0 -LineTwips 320 -FontName "宋体" -SizeHalfPoints 21))
416+
[void]$xml.Add((New-TableBlockXml -Caption ("表{0}-2 数据库表" -f $ChapterNumber) -Headers @("序号", "数据库表", "数据表存储的内容") -Rows @($Profile.database) -Widths @(900, 2500, 5200)))
417+
[void]$xml.Add((New-ParagraphXml -Text ("{0}.{1} 数据库表结构" -f $ChapterNumber, $fieldTableSectionNumber) -FirstLineTwips 420 -BeforeTwips 120 -AfterTwips 0 -LineTwips 320 -FontName "宋体" -SizeHalfPoints 21))
303418

304419
$tableNo = 3
305420
foreach ($fieldTable in @($Profile.fieldTables)) {
306-
$caption = "表4-{0} {1}" -f $tableNo, [string]$fieldTable.name
421+
$caption = "{0}-{1} {2}" -f $ChapterNumber, $tableNo, [string]$fieldTable.name
307422
[void]$xml.Add((New-TableBlockXml -Caption $caption -Headers @("序号", "字段名", "字段类型", "说明", "备注") -Rows @($fieldTable.rows) -Widths @(800, 1500, 2500, 2200, 1200)))
308423
$tableNo++
309424
}
@@ -382,7 +497,7 @@ try {
382497
}
383498

384499
$documentText = Get-NodeText -Node $body -NamespaceManager $namespaceManager
385-
if ($documentText -match "表4-1\s*功能模块表|功能模块表.*数据库表结构") {
500+
if ($documentText -match "表\d+-1\s*功能模块表|功能模块表.*数据库表结构") {
386501
Copy-Item -LiteralPath $resolvedDocxPath -Destination $resolvedOutPath -Force
387502
[pscustomobject]@{
388503
docxPath = $resolvedDocxPath
@@ -396,13 +511,15 @@ try {
396511

397512
$children = @($body.ChildNodes)
398513
$startIndex = -1
514+
$designHeadingText = $null
399515
for ($i = 0; $i -lt $children.Count; $i++) {
400516
if ($children[$i].LocalName -ne "p") {
401517
continue
402518
}
403519
$text = Get-NodeText -Node $children[$i] -NamespaceManager $namespaceManager
404520
if ($text -match "方案设计与实现|系统总体设计|系统设计") {
405521
$startIndex = $i
522+
$designHeadingText = $text
406523
break
407524
}
408525
}
@@ -435,8 +552,13 @@ try {
435552
}
436553

437554
$profile = Get-CourseDesignTableProfile -DocumentText $documentText
555+
$chapterNumber = Get-HeadingChapterNumber -Text $designHeadingText
556+
if ($null -eq $chapterNumber) {
557+
$chapterNumber = 4
558+
}
559+
$startingSubsectionNumber = Get-NextSubsectionNumber -Children $children -StartIndex $startIndex -StopNode $insertBefore -NamespaceManager $namespaceManager -ChapterNumber $chapterNumber
438560
$fragment = $documentXml.CreateDocumentFragment()
439-
$fragment.InnerXml = New-CourseDesignTablesXml -Profile $profile
561+
$fragment.InnerXml = New-CourseDesignTablesXml -Profile $profile -ChapterNumber $chapterNumber -StartingSubsectionNumber $startingSubsectionNumber
440562

441563
$insertedNodeCount = 0
442564
while ($fragment.HasChildNodes) {
@@ -452,6 +574,8 @@ try {
452574
outPath = $resolvedOutPath
453575
inserted = $true
454576
insertedNodeCount = $insertedNodeCount
577+
chapterNumber = $chapterNumber
578+
startingSubsectionNumber = $startingSubsectionNumber
455579
tableCount = 2 + @($profile.fieldTables).Count
456580
}
457581
} finally {

0 commit comments

Comments
 (0)