Skip to content

armgit / Filee

Public Imported from GitHub
0

Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.

Word to Markdown: plain tables without HTML, pictures linked relatively

KnifeLemonccf0f1877c83
diff --git a/docs/ENGINES.md b/docs/ENGINES.md
index 3ba038b0..c151a1a1 100644
--- a/docs/ENGINES.md
+++ b/docs/ENGINES.md
@@ -1195,6 +1195,14 @@ DocBook (`.dbk`, `.docbook`; written as DocBook 5), Jupyter notebooks (`.ipynb`)
- DocBook, man pages, OPML, Typst, HTML and LaTeX are written with `--standalone` (a root element, a `.TH` header,
a preamble), the text formats extract pictures into `<name>_files` next to the result (Markdown with *Leave out
pictures* gets none: a Lua filter drops them).
+- Pictures are linked relative to the result (`<name>_files/media/…`; Pandoc runs in the result's folder, with
+ `--resource-path` pointing at the source's for pictures the source links to), so both can be moved together.
+ EMF and WMF pictures, common in Word documents, become PNG (ImageMagick draws metafiles on Windows only; elsewhere
+ they stay).
+- Markdown is GitHub-flavoured and kept free of HTML, which many Markdown editors show as code (#391). A Lua filter
+ lays every table out as a pipe table: a merged cell keeps its text in its first position and leaves the cells it
+ covered empty, the paragraphs and list items of a cell are joined with `<br>`, and a table without a header row
+ gets its first row as the header. Pictures lose their Word size, which only an `<img>` tag could hold.
## Subtitles
diff --git a/src/Filee.Engines/Office/PandocConverter.cs b/src/Filee.Engines/Office/PandocConverter.cs
index fe7f2a83..91f67a83 100644
--- a/src/Filee.Engines/Office/PandocConverter.cs
+++ b/src/Filee.Engines/Office/PandocConverter.cs
@@ -3,11 +3,16 @@
// Typst, MediaWiki, DocBook, Jupyter notebooks, Textile, man pages, OPML) ↔ Markdown / HTML / DOCX / ODT and each
// other (and → RTF, EPUB, TXT), and Markdown and the lightweight markups → PPTX slides. Markdown → HTML / TXT / HWPX /
// DOCX is built in (MarkdownConverter, the HWPX and DOCX writers). Pictures of a text result are extracted into a
-// "<name>_files" folder next to it; Markdown with "Leave out pictures" gets none (a Lua filter drops them).
+// "<name>_files" folder next to it and linked relative to it (EMF/WMF become PNG); Markdown with "Leave out pictures"
+// gets none (a Lua filter drops them). Markdown stays free of HTML: tables come out as plain pipe tables (a Lua
+// filter splits merged cells) and pictures as ![](…).
using Filee.Core.Conversion;
using Filee.Core.Formats;
+using Filee.Core.Presets;
using Filee.Engines.Infrastructure;
+using Filee.Engines.Magick;
+using ImageMagick;
namespace Filee.Engines.Office;
@@ -119,39 +124,186 @@ public sealed class PandocConverter : IConverter
return [];
progress?.Report(0.1);
- List<string> args = [step.InputPath, "-f", Readers[step.From], "-t", Writers[step.To], "-o", output];
+ var input = Path.GetFullPath(step.InputPath);
+ var outputFolder = Path.GetDirectoryName(Path.GetFullPath(output))!;
+ List<string> args = [input, "-f", Readers[step.From], "-t", Writers[step.To], "-o", output];
if (Standalone.Contains(step.To))
args.Add("--standalone");
+ if (step.To == "md")
+ args.Add("--lua-filter=" + Filter(step.WorkDirectory, "markdown", PlainMarkdown));
+ string? media = null;
if (step.To == "md" && step.Preset.Document.LeaveOutPictures)
- args.Add("--lua-filter=" + WithoutPictures(step.WorkDirectory));
+ args.Add("--lua-filter=" + Filter(step.WorkDirectory, "no-pictures", WithoutPictures));
else if (ExtractsMedia.Contains(step.To))
- args.Add("--extract-media=" + Path.Combine(Path.GetDirectoryName(output)!, Path.GetFileNameWithoutExtension(output) + "_files"));
+ {
+ // Relative to the result (Pandoc runs in its folder), so the links keep working when both are moved.
+ // Pictures the source refers to by a relative path are still looked up next to the source.
+ media = Path.GetFileNameWithoutExtension(output) + "_files";
+ args.Add("--extract-media=" + media);
+ args.Add("--resource-path=" + Path.GetDirectoryName(input));
+ }
ProcessResult result;
using (ProgressEstimate.Start(progress, TimeSpan.FromSeconds(1.5)))
result = await ProcessRunner.RunAsync(pandoc, args, Timeout, cancellationToken,
- workingDirectory: Path.GetDirectoryName(Path.GetFullPath(step.InputPath)));
+ workingDirectory: media is null ? Path.GetDirectoryName(input) : outputFolder);
if (!File.Exists(output))
throw new InvalidOperationException($"Pandoc failed (exit {result.ExitCode}). {result.StandardError.Trim()}");
+ if (media is not null)
+ MetafilesToPng(output, Path.Combine(outputFolder, media), step.Preset);
progress?.Report(1);
return [output];
}
/// <summary>
- /// A Lua filter that drops every picture ("Leave out pictures", DocumentOptions.LeaveOutPictures); a figure keeps
- /// its caption as text.
+ /// Word documents often hold EMF/WMF pictures, which browsers and Markdown editors don't show: each extracted one
+ /// becomes a PNG and the result links to that. Where ImageMagick can't draw metafiles (outside Windows), they stay.
/// </summary>
- private static string WithoutPictures(string workDirectory)
+ private static void MetafilesToPng(string output, string mediaFolder, Preset preset)
+ {
+ if (!Directory.Exists(mediaFolder))
+ return;
+ var text = File.ReadAllText(output);
+ var changed = false;
+ foreach (var metafile in Directory.EnumerateFiles(mediaFolder, "*", SearchOption.AllDirectories)
+ .Where(f => Path.GetExtension(f).ToLowerInvariant() is ".emf" or ".wmf").ToList())
+ {
+ var png = Path.ChangeExtension(metafile, ".png");
+ if (File.Exists(png))
+ continue;
+ try
+ {
+ using var images = ImageReader.Read(metafile, Path.GetExtension(metafile)[1..].ToLowerInvariant(), preset);
+ images[0].Write(png, MagickFormat.Png);
+ }
+ catch (Exception ex) when (ex is MagickException or IOException)
+ {
+ continue;
+ }
+ var from = Path.GetRelativePath(Path.GetDirectoryName(output)!, metafile).Replace('\\', '/');
+ text = text.Replace(from, Path.ChangeExtension(from, ".png"), StringComparison.Ordinal);
+ File.Delete(metafile);
+ changed = true;
+ }
+ if (changed)
+ File.WriteAllText(output, text);
+ }
+
+ /// <summary>Writes a Lua filter into the job's work folder and returns its path.</summary>
+ private static string Filter(string workDirectory, string name, string code)
{
Directory.CreateDirectory(workDirectory);
- var path = Path.Combine(workDirectory, $"no-pictures-{Guid.NewGuid():N}.lua");
- File.WriteAllText(path, """
- function Image(image) return {} end
- function Figure(figure) return figure.caption.long end
- """);
+ var path = Path.Combine(workDirectory, $"{name}-{Guid.NewGuid():N}.lua");
+ File.WriteAllText(path, code);
return path;
}
+ /// <summary>
+ /// Drops every picture ("Leave out pictures", DocumentOptions.LeaveOutPictures); a figure keeps its caption as text.
+ /// </summary>
+ private const string WithoutPictures = """
+ function Image(image) return {} end
+ function Figure(figure) return figure.caption.long end
+ """;
+
+ /// <summary>
+ /// Keeps the Markdown free of HTML, which many Markdown editors show as code (#391). GitHub-flavoured Markdown
+ /// tables have one header row, one line per cell and no merged cells, and Pandoc writes any other table as HTML:
+ /// this lays every table out as a plain grid instead. A merged cell keeps its text in its first position and leaves
+ /// the others empty, the paragraphs and list items of a cell are joined with &lt;br&gt;, and a table without a
+ /// header row gets its first row as the header. Pictures lose their Word size, which only an &lt;img&gt; can hold.
+ /// </summary>
+ private const string PlainMarkdown = """
+ local br = pandoc.RawInline('html', '<br>')
+
+ local function inlines_of(blocks)
+ local out = pandoc.Inlines{}
+ local function add(inl)
+ if #inl == 0 then return end
+ if #out > 0 then out:insert(br) end
+ out:extend(inl)
+ end
+ for _, block in ipairs(blocks) do
+ if block.t == 'Para' or block.t == 'Plain' then
+ add(block.content)
+ elseif block.t == 'BulletList' or block.t == 'OrderedList' then
+ for i, item in ipairs(block.content) do
+ local mark = block.t == 'BulletList' and '•' or (tostring(i) .. '.')
+ local inl = pandoc.Inlines{pandoc.Str(mark), pandoc.Space()}
+ inl:extend(inlines_of(item))
+ add(inl)
+ end
+ elseif block.t == 'LineBlock' then
+ for _, line in ipairs(block.content) do add(line) end
+ elseif block.t == 'Div' or block.t == 'BlockQuote' then
+ add(inlines_of(block.content))
+ else
+ local text = pandoc.utils.stringify(block)
+ if text ~= '' then add(pandoc.Inlines{pandoc.Str(text)}) end
+ end
+ end
+ return out:walk{
+ LineBreak = function() return br end,
+ SoftBreak = function() return pandoc.Space() end,
+ }
+ end
+
+ local function empty() return pandoc.Cell({}) end
+
+ -- Rows of one section (head, an intermediate head, a body, the foot); row spans stay within it.
+ local function grid(rows, columns)
+ local below = {}
+ for _, row in ipairs(rows) do
+ local cells = pandoc.List()
+ local source = row.cells
+ local next = 1
+ local column = 1
+ while column <= columns do
+ if (below[column] or 0) > 0 then
+ below[column] = below[column] - 1
+ cells:insert(empty())
+ column = column + 1
+ elseif next <= #source then
+ local cell = source[next]
+ next = next + 1
+ local text = inlines_of(cell.contents)
+ cells:insert(pandoc.Cell(#text > 0 and {pandoc.Plain(text)} or {}, cell.alignment, 1, 1, cell.attr))
+ for c = column, column + cell.col_span - 1 do
+ if c > column then cells:insert(empty()) end
+ if cell.row_span > 1 then below[c] = cell.row_span - 1 end
+ end
+ column = column + cell.col_span
+ else
+ cells:insert(empty())
+ column = column + 1
+ end
+ end
+ while #cells > columns do cells:remove() end
+ row.cells = cells
+ end
+ return rows
+ end
+
+ function Image(image)
+ image.attributes = {}
+ return image
+ end
+
+ function Table(tbl)
+ local columns = #tbl.colspecs
+ if #tbl.head.rows == 0 and #tbl.bodies > 0 and #tbl.bodies[1].body > 0 then
+ tbl.head.rows:insert(tbl.bodies[1].body:remove(1))
+ end
+ tbl.head.rows = grid(tbl.head.rows, columns)
+ for _, body in ipairs(tbl.bodies) do
+ body.head = grid(body.head, columns)
+ body.body = grid(body.body, columns)
+ end
+ tbl.foot.rows = grid(tbl.foot.rows, columns)
+ return tbl
+ end
+ """;
+
/// <summary>Uses the selected copy, a bundled copy, then the system installation.</summary>
internal static string? Locate() =>
EngineEnvironment.OwnProgram("pandoc", "pandoc.exe") ?? LocateBundled() ?? EngineEnvironment.SystemProgram("pandoc", "pandoc.exe");
diff --git a/tests/Filee.Engines.Tests/PandocMarkupTests.cs b/tests/Filee.Engines.Tests/PandocMarkupTests.cs
index 482ae154..ac13c5a2 100644
--- a/tests/Filee.Engines.Tests/PandocMarkupTests.cs
+++ b/tests/Filee.Engines.Tests/PandocMarkupTests.cs
@@ -193,7 +193,9 @@ public class PandocMarkupTests(EngineFixture fx) : IClassFixture<EngineFixture>
Assert.Equal("pandoc", Assert.Single(fx.Catalog.CreatePlanner().Plan("docx", "md")!.Steps).Converter.Id);
var withPictures = await File.ReadAllTextAsync(await ConvertAsync(docx, "md"), TestContext.Current.CancellationToken);
- Assert.Contains("memo_files", withPictures);
+ // Linked relative to the Markdown file, so both can be moved together (#391).
+ Assert.Contains("](memo_files/media/", withPictures);
+ Assert.DoesNotContain(Path.GetDirectoryName(docx)!, withPictures);
var job = await fx.ConvertAsync([docx], new Preset { TargetFormat = "md", Document = { LeaveOutPictures = true } });
Assert.True(job.State == JobState.Completed, string.Join("; ", job.Files.Select(f => $"{f.ErrorKey} {f.ErrorDetail}")));
@@ -204,6 +206,33 @@ public class PandocMarkupTests(EngineFixture fx) : IClassFixture<EngineFixture>
Assert.Contains("After the chart.", textOnly);
}
+ [Fact]
+ public async Task Word_tables_with_merged_cells_become_markdown_tables()
+ {
+ // Merged cells, a cell of two paragraphs, a list in a cell and no header row: Pandoc alone writes this as an
+ // HTML <table>, which Markdown editors show as code (#391).
+ SkipUnlessInstalled();
+ var html = await WriteAsync("history.html", """
+ <table>
+ <tr><td>Version</td><td colspan="3">Date and author</td></tr>
+ <tr><td rowspan="2">1.0</td><td>2024</td><td>Kim</td><td><p>first part</p><p>second part</p></td></tr>
+ <tr><td>2025</td><td>Lee</td><td><ul><li>alpha</li><li>beta</li></ul></td></tr>
+ </table>
+ """);
+ var docx = await ConvertAsync(html, "docx");
+ File.Delete(html);
+
+ var markdown = await File.ReadAllTextAsync(await ConvertAsync(docx, "md"), TestContext.Current.CancellationToken);
+ Assert.DoesNotContain("<table", markdown);
+ var rows = markdown.Split('\n').Select(l => l.Trim()).Where(l => l.StartsWith('|')).ToList();
+ Assert.Equal(4, rows.Count); // header, separator and two rows
+ Assert.Matches(@"^\|\s*Version\s*\|\s*Date and author\s*\|", rows[0]);
+ Assert.Matches(@"^\|\s*\|\s*2025\s*\|\s*Lee\s*\|", rows[3]); // the row span leaves its column empty below
+ Assert.Contains("first part<br>second part", rows[2]);
+ Assert.Contains("• alpha<br>• beta", rows[3]);
+ Assert.All(rows, r => Assert.Equal(5, r.Count(c => c == '|')));
+ }
+
[Fact]
public async Task Latex_to_markdown_epub_and_back()
{