@@ -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+
43147function New-RunPropertiesXml {
44148 param (
45149 [string ]$FontName = " 宋体" ,
@@ -292,18 +396,29 @@ function Get-CourseDesignTableProfile {
292396}
293397
294398function 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