Skip to content

Commit 46f3597

Browse files
committed
Minor improvements to prepare for next release
1 parent dcb5a59 commit 46f3597

File tree

7 files changed

+96
-11
lines changed

7 files changed

+96
-11
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ Forked:
7171
- [b2xtranslator](https://github.com/EvolutionJobs/b2xtranslator)
7272
- [markdig.docx](https://github.com/morincer/markdig.docx)
7373

74-
Others (credits for parts of the logic):
74+
Others (credits for parts of the logic, not direct dependencies):
7575
- [Html2OpenXml](https://github.com/onizet/html2openxml) for images header decoding and unit conversions.
7676
- [dwml_cs](https://github.com/m-x-d/dwml_cs) for Office Math (OMML) to LaTex conversion
7777
- [addFormula2docx](https://github.com/Sun-ZhenXing/addFormula2docx) for Office Math (OMML) to MathML conversion
78-
- [RtfPipe](https://github.com/erdomke/RtfPipe), [FridaysForks.RtfPipe](https://github.com/cezarypiatek/FridaysForks.RtfPipe), [RtfConverter](https://github.com/jokecamp/RtfConverter) for part of the RTF parsing logic.
78+
- [RtfPipe](https://github.com/erdomke/RtfPipe) (and forks: [1](https://github.com/jasonwmeeks/RtfPipe), [2](https://github.com/cezarypiatek/FridaysForks.RtfPipe)), [RtfConverter](https://github.com/jokecamp/RtfConverter), [OpenRTFDoc](https://github.com/ChunbleBee/OpenRTFDoc), [ReasonableRTF.Standard](https://github.com/Flamifly/ReasonableRTF.Standard) for RTF parsing logic.
7979
- [ExcelNumberFormat](https://github.com/andersnm/ExcelNumberFormat) for Excel format strings parsing logic.
8080

8181
<a id="recommended_libraries"></a>

src/DocSharp.Docx/Formats/FileFormatHelpers.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@ public static ISaveOptions ToSaveOptions(this SaveFormat saveFormat)
3434
}
3535
}
3636

37+
public static LoadFormat ExtensionToLoadFormat(string ext)
38+
{
39+
switch (ext.ToUpperInvariant())
40+
{
41+
case ".DOCX":
42+
case ".DOTX":
43+
case ".DOCM":
44+
case ".DOTM":
45+
return LoadFormat.Docx;
46+
case ".RTF":
47+
return LoadFormat.Rtf;
48+
default:
49+
throw new NotImplementedException("Unrecognized load format. Please specify the LoadFormat explicitly.");
50+
}
51+
}
52+
3753
public static SaveFormat ExtensionToSaveFormat(string ext)
3854
{
3955
switch (ext.ToUpperInvariant())
@@ -66,7 +82,7 @@ public static SaveFormat ExtensionToSaveFormat(string ext)
6682
case ".TXT":
6783
return SaveFormat.Txt;
6884
default:
69-
throw new NotImplementedException("Unsupported save format.");
85+
throw new NotImplementedException("Unrecognized save format. Please specify the SaveFormat explicitly.");
7086
}
7187
}
7288

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace DocSharp.Docx;
8+
9+
public enum LoadFormat
10+
{
11+
Docx,
12+
Rtf
13+
}

src/DocSharp.Docx/Formats/SaveOptions.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,21 @@
1010

1111
namespace DocSharp.Docx;
1212

13-
public interface ISaveOptions { }
13+
public interface ISaveOptions
14+
{
15+
SaveFormat Format { get; }
16+
}
1417

1518
public class DocxSaveOptions : ISaveOptions
1619
{
20+
public SaveFormat Format => SaveFormat.Docx;
1721
public WordprocessingDocumentType DocumentType { get; set; } = WordprocessingDocumentType.Document;
1822
}
1923

2024
public class RtfSaveOptions : ISaveOptions
2125
{
26+
public SaveFormat Format => SaveFormat.Rtf;
27+
2228
/// <summary>
2329
/// Gets or set the default font and paragraph properties used in (rare) cases where
2430
/// they are not specified in in neither the document body, styles or default style.
@@ -49,6 +55,8 @@ public class RtfSaveOptions : ISaveOptions
4955

5056
public class HtmlSaveOptions : ISaveOptions
5157
{
58+
public SaveFormat Format => SaveFormat.Html;
59+
5260
/// <summary>
5361
/// Image converter to preserve TIFF, EMF and other image types when converting to HTML.
5462
/// If the DocSharp.ImageSharp or DocSharp.SystemDrawing package is installed,
@@ -107,6 +115,8 @@ public class HtmlSaveOptions : ISaveOptions
107115

108116
public class MarkdownSaveOptions : ISaveOptions
109117
{
118+
public SaveFormat Format => SaveFormat.Markdown;
119+
110120
/// <summary>
111121
/// If this property is set to a directory, images will be exported to that folder
112122
/// and a reference will be added in Markdown syntax,
@@ -165,6 +175,8 @@ public class MarkdownSaveOptions : ISaveOptions
165175

166176
public class TxtSaveOptions : ISaveOptions
167177
{
178+
public SaveFormat Format => SaveFormat.Txt;
179+
168180
/// <summary>
169181
/// Since plain text is not paginated, only the header of the first section and
170182
/// footer of the last section are exported.
@@ -183,4 +195,4 @@ public class TxtSaveOptions : ISaveOptions
183195
/// If null or empty, sub-documents will not be preserved.
184196
/// </summary>
185197
public string? OriginalFolderPath { get; set; }
186-
}
198+
}

src/DocSharp.Markdown/MarkdownConverter.cs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,49 @@ public string ToRtfString(MarkdownSource markdown, MarkdownToRtfSettings? settin
300300
RenderToRtf(markdown.Document, rtfBuilder, settings);
301301
return rtfBuilder.ToString();
302302
}
303-
303+
304+
/// <summary>
305+
/// Convert Markdown to HTML and save to file.
306+
/// </summary>
307+
/// <param name="markdown">The markdown source.</param>
308+
/// <param name="outputFilePath">The output HTML file path.</param>
309+
public void ToHtml(MarkdownSource markdown, string outputFilePath)
310+
{
311+
using (var sw = new StreamWriter(outputFilePath, append: false, encoding: Encodings.UTF8NoBOM))
312+
ToHtml(markdown, sw);
313+
}
314+
315+
/// <summary>
316+
/// Convert Markdown to HTML and save to a Stream.
317+
/// </summary>
318+
/// <param name="markdown">The markdown source.</param>
319+
/// <param name="outputStream">The output HTML stream.</param>
320+
public void ToHtml(MarkdownSource markdown, Stream outputStream)
321+
{
322+
using (var sw = new StreamWriter(outputStream, encoding: Encodings.UTF8NoBOM, bufferSize: 1024, leaveOpen: true))
323+
ToHtml(markdown, sw);
324+
}
325+
326+
/// <summary>
327+
/// Convert Markdown to HTML and write to a TextWriter.
328+
/// </summary>
329+
/// <param name="markdown">The markdown source.</param>
330+
/// <param name="output">The output writer.</param>
331+
public void ToHtml(MarkdownSource markdown, TextWriter output)
332+
{
333+
markdown.Document.ToHtml(output, MarkdownSource.DefaultPipeline);
334+
}
335+
336+
/// <summary>
337+
/// Convert Markdown to HTML and returns a string.
338+
/// </summary>
339+
/// <param name="markdown">The markdown source.</param>
340+
/// <returns>The HTML document as <see cref="string"/></returns>
341+
public string ToHtmlString(MarkdownSource markdown)
342+
{
343+
return markdown.Document.ToHtml(MarkdownSource.DefaultPipeline);
344+
}
345+
304346
private void RenderToRtf(MarkdownDocument document, RtfStringWriter rtfBuilder, MarkdownToRtfSettings settings)
305347
{
306348
var renderer = new RtfRenderer(rtfBuilder, settings)

src/DocSharp.Markdown/MarkdownSource.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,18 @@ public static MarkdownSource FromStream(Stream stream, Encoding? encoding = null
5858
}
5959
}
6060

61+
internal static readonly MarkdownPipeline DefaultPipeline = new MarkdownPipelineBuilder()
62+
.UseAdvancedExtensions()
63+
.UseEmojiAndSmiley()
64+
.Build();
65+
6166
/// <summary>
6267
/// Create a Markdown source from a Markdown string
6368
/// </summary>
6469
/// <param name="markdown">The Markdown content as string</param>
6570
public static MarkdownSource FromMarkdownString(string markdown)
6671
{
67-
var pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions()
68-
.UseEmojiAndSmiley()
69-
.Build();
70-
return Markdig.Markdown.Parse(markdown, pipeline);
72+
return Markdig.Markdown.Parse(markdown, DefaultPipeline);
7173
}
7274

7375
/// <summary>

src/WIP/DocSharp.Renderer/DocSharp.Renderer.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
</PropertyGroup>
1919

2020
<ItemGroup>
21-
<PackageReference Include="QuestPDF" Version="2025.12.2" />
21+
<PackageReference Include="QuestPDF" Version="2025.12.3" />
2222

2323
<PackageReference Include="CSharpMath.Rendering" Version="0.5.1" />
2424
<!-- <PackageReference Include="CSharpMath.SkiaSharp" Version="0.5.1" /> -->

0 commit comments

Comments
 (0)