Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,9 @@ zh: 修复了 HTML 和 Word 转 PDF、PDF/A 表单值、PDF 页面编辑的书
- **Table captions kept in TXT and Markdown** (by @RionHwang0, #2105). HTML to TXT, and e-books to TXT or Markdown,
dropped the caption of a table. It is now written right before the table.

- **Tables inside table cells kept in TXT and Markdown** (by @RionHwang0, #2106). HTML to TXT, and e-books to TXT or
Markdown, dropped a table nested in a cell with all its text. Its text now stays in that cell, on one line.

## 2.5.0

<!-- update
Expand Down
23 changes: 17 additions & 6 deletions src/Filee.Engines/Hwp/Hwpx/MarkdownWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,7 @@ private void Table(HTable table, StringBuilder sb, string prefix)
column++;
if (column >= columns)
break;
// Header cells are bold by nature: no ** inside them.
var header = table.Rows[r].Header;
cells[column] = string.Join(" ", cell.Blocks.OfType<HParagraph>()
.Select(p => header ? p.Inlines.Select(i => i is HText t ? new HText(t.Text, t.Format with { Bold = null }) : i).ToList() : p.Inlines)
.Select(inlines => Inlines(inlines).Replace("\\\n", " ", StringComparison.Ordinal).Replace("\n", " ", StringComparison.Ordinal)))
.Replace("|", "\\|", StringComparison.Ordinal);
cells[column] = CellText(cell.Blocks, table.Rows[r].Header).Replace("|", "\\|", StringComparison.Ordinal);
for (var cc = column; cc < Math.Min(columns, column + cell.ColSpan); cc++)
freeFrom[cc] = Math.Max(freeFrom[cc], r + cell.RowSpan);
column += cell.ColSpan;
Expand All @@ -151,6 +146,22 @@ private void Table(HTable table, StringBuilder sb, string prefix)
sb.Append('\n');
}

/// <summary>
/// The Markdown of a cell on one line. A table nested in the cell keeps its text in reading order: its cells
/// separated by a space and its rows by " / ", since a pipe table cannot hold a table or a line break.
/// </summary>
private string CellText(List<HBlock> blocks, bool header) =>
string.Join(" ", blocks.Select(block => block switch
{
// Header cells are bold by nature: no ** inside them.
HParagraph p => Inlines(header ? p.Inlines.Select(i => i is HText t ? new HText(t.Text, t.Format with { Bold = null }) : i).ToList() : p.Inlines)
.Replace("\\\n", " ", StringComparison.Ordinal).Replace("\n", " ", StringComparison.Ordinal),
HTable table => string.Join(" / ", table.Rows
.Select(row => string.Join(" ", row.Cells.Select(c => CellText(c.Blocks, header || row.Header)).Where(t => t.Length > 0)))
.Where(t => t.Length > 0)),
_ => "",
}).Where(t => t.Length > 0));

private string Inlines(List<HInline> inlines)
{
var sb = new StringBuilder();
Expand Down
17 changes: 15 additions & 2 deletions src/Filee.Engines/Hwp/Hwpx/PlainTextWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,21 @@ private static void Blocks(List<HBlock> blocks, StringBuilder sb, List<string> n
private static bool IsCode(HParagraph paragraph) =>
paragraph.Inlines.Count > 0 && paragraph.Inlines.All(i => i is HText { Format.Shade: HtmlReader.CodeShade });

private static string Cell(HCell cell, List<string> notes) =>
string.Join(" ", cell.Blocks.OfType<HParagraph>().Select(p => Inlines(p.Inlines, notes).Replace('\n', ' ').Replace('\t', ' ')).Where(t => t.Length > 0));
private static string Cell(HCell cell, List<string> notes) => CellText(cell.Blocks, notes);

/// <summary>
/// The text of a cell on one line. A table nested in the cell keeps its text in reading order: its cells separated
/// by a space and its rows by " / ", since a tab or a line break would break the outer table's row.
/// </summary>
private static string CellText(List<HBlock> blocks, List<string> notes) =>
string.Join(" ", blocks.Select(block => block switch
{
HParagraph p => Inlines(p.Inlines, notes).Replace('\n', ' ').Replace('\t', ' '),
HTable table => string.Join(" / ", table.Rows
.Select(row => string.Join(" ", row.Cells.Select(c => CellText(c.Blocks, notes)).Where(t => t.Length > 0)))
.Where(t => t.Length > 0)),
_ => "",
}).Where(t => t.Length > 0));

private static string Inlines(List<HInline> inlines, List<string> notes)
{
Expand Down
33 changes: 33 additions & 0 deletions tests/Filee.Engines.Tests/HtmlReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -526,4 +526,37 @@ await File.WriteAllTextAsync(html, """
if (pages is not null)
Assert.Equal(2, pages);
}

[Fact]
public async Task Nested_table_text_reaches_text_and_markdown_from_html_and_epub()
{
var html = Path.Combine(fx.NewFolder(), "일정.html");
await File.WriteAllTextAsync(html, """
<!DOCTYPE html><html><head><meta charset="utf-8"><title>일정</title></head><body>
<table>
<tr><th>구분</th><th>내용</th></tr>
<tr><td>일정</td><td>개요
<table><tr><td>1일차</td><td>개막식</td></tr><tr><td>2일차</td><td>현장 견학</td></tr></table>
</td></tr>
</table>
</body></html>
""", TestContext.Current.CancellationToken);
async Task<string> Convert(string input, string target)
{
var job = await fx.ConvertAsync([input], new Preset { TargetFormat = target });
Assert.True(job.State == JobState.Completed, string.Join("; ", job.Files.Select(f => $"{f.ErrorKey} {f.ErrorDetail}")));
return job.Outputs.Single();
}
var epub = await Convert(html, "epub");

foreach (var (source, target) in new[] { (html, "txt"), (epub, "txt"), (epub, "md") })
{
var text = await File.ReadAllTextAsync(await Convert(source, target), TestContext.Current.CancellationToken);

// The nested table stays in its outer cell, on that cell's line: cells by a space, rows by " / ".
var row = text.Split('\n').Single(line => line.Contains("개요", StringComparison.Ordinal));
Assert.Contains("일정", row, StringComparison.Ordinal);
Assert.Contains("개요 1일차 개막식 / 2일차 현장 견학", row, StringComparison.Ordinal);
}
}
}
Loading