@@ -388,20 +388,76 @@ func (p *Parser) ParseDocxBlockTable(t *lark.DocxBlockTable) string {
388
388
// - First row as header
389
389
// - Ignore cell merging
390
390
var rows [][]string
391
+ mergeInfoMap := map [int64 ]map [int64 ]* lark.DocxBlockTablePropertyMergeInfo {}
392
+
393
+ // 构建单元格合并信息的映射
394
+ if t .Property .MergeInfo != nil {
395
+ for i , merge := range t .Property .MergeInfo {
396
+ rowIndex := int64 (i ) / t .Property .ColumnSize
397
+ colIndex := int64 (i ) % t .Property .ColumnSize
398
+ if _ , exists := mergeInfoMap [int64 (rowIndex )]; ! exists {
399
+ mergeInfoMap [int64 (rowIndex )] = map [int64 ]* lark.DocxBlockTablePropertyMergeInfo {}
400
+ }
401
+ mergeInfoMap [rowIndex ][colIndex ] = merge
402
+ }
403
+ }
391
404
for i , blockId := range t .Cells {
392
405
block := p .blockMap [blockId ]
393
406
cellContent := p .ParseDocxBlock (block , 0 )
394
407
cellContent = strings .ReplaceAll (cellContent , "\n " , "" )
395
408
rowIndex := int64 (i ) / t .Property .ColumnSize
396
- if len (rows ) < int (rowIndex )+ 1 {
409
+ colIndex := int64 (i ) % t .Property .ColumnSize
410
+ // 初始化行
411
+ for len (rows ) <= int (rowIndex ) {
397
412
rows = append (rows , []string {})
398
413
}
399
- rows [rowIndex ] = append (rows [rowIndex ], cellContent )
414
+ for len (rows [rowIndex ]) <= int (colIndex ) {
415
+ rows [rowIndex ] = append (rows [rowIndex ], "" )
416
+ }
417
+ // 设置单元格内容
418
+ rows [rowIndex ][colIndex ] = cellContent
400
419
}
401
420
421
+ // 渲染为 HTML 表格
402
422
buf := new (strings.Builder )
403
- buf .WriteString (renderMarkdownTable (rows ))
404
- buf .WriteString ("\n " )
423
+ buf .WriteString ("<table>\n " )
424
+
425
+ // 跟踪已经处理过的合并单元格
426
+ processedCells := map [string ]bool {}
427
+
428
+ // 构建 HTML 表格内容
429
+ for rowIndex , row := range rows {
430
+ buf .WriteString ("<tr>\n " )
431
+ for colIndex , cellContent := range row {
432
+ cellKey := fmt .Sprintf ("%d-%d" , rowIndex , colIndex )
433
+
434
+ // 跳过已处理的单元格
435
+ if processedCells [cellKey ] {
436
+ continue
437
+ }
438
+
439
+ mergeInfo := mergeInfoMap [int64 (rowIndex )][int64 (colIndex )]
440
+ if mergeInfo != nil {
441
+ // 合并单元格
442
+ buf .WriteString (fmt .Sprintf (
443
+ `<td rowspan="%d" colspan="%d">%s</td>` ,
444
+ mergeInfo .RowSpan , mergeInfo .ColSpan , cellContent ,
445
+ ))
446
+ // 标记合并范围内的所有单元格为已处理
447
+ for r := rowIndex ; r < rowIndex + int (mergeInfo .RowSpan ); r ++ {
448
+ for c := colIndex ; c < colIndex + int (mergeInfo .ColSpan ); c ++ {
449
+ processedCells [fmt .Sprintf ("%d-%d" , r , c )] = true
450
+ }
451
+ }
452
+ } else {
453
+ // 普通单元格
454
+ buf .WriteString (fmt .Sprintf ("<td>%s</td>" , cellContent ))
455
+ }
456
+ }
457
+ buf .WriteString ("</tr>\n " )
458
+ }
459
+ buf .WriteString ("</table>\n " )
460
+
405
461
return buf .String ()
406
462
}
407
463
0 commit comments