翻譯 Markdown 到 Document Object Model (DOM)

「要以程式方式讀取、處理和修改文件的內容和格式,您需要將其翻譯成 Aspose.Words Document Object Model (DOM)。」

與 Word 文檔不同,Markdown 不符合DOM於該Aspose.Words Document Object Model (DOM)篇文章所述的描述。 「不過,Aspose.Words 提供自己的機制來翻譯Markdown 文檔到DOM並回來,所以我們可以成功地與他們的元素如文字格式化、表格、標題和其他的元素合作。」

這篇文章說明了各種 markdown 功能如何翻譯成 Aspose.Words DOM 格式,以及反向轉為 Markdown 格式。

翻譯的複雜度 Markdown – DOM – Markdown

這機制的主要困難,不僅是將 Markdown 翻譯成 DOM ,而且也要進行反向轉換 – 以最少的損失將文件儲存回 Markdown 格式。 有些元素,例如多層引號,其逆轉換並不簡單。

「我們的翻譯引擎除了允許使用者可以在一個現有的 Markdown 文檔中操作複雜元素外,也讓使用者可以從原始結構開始,以 Markdown 格式創建自己的文檔。」 「要創作各種元素,你必須根據本篇文章稍後描述的特定規則,使用具特定名稱的樣式。」 這樣的風格可以程式方式來創造。

常見的翻譯原則

「我們使用 Font 格式化內聯方塊。」 「當 Markdown 功能沒有直接對應於 Aspose.Words 的 DOM 時,我們使用以一些特殊詞語開始的字符格式。」

「對容器塊,我們使用風格繼承來表示嵌套的 Markdown 特徵。」 「在這種情況下,即使沒有嵌套特徵,我們也會使用以特定字詞開頭的段落樣式。」

「項目標記和有序列表是 Markdown 中的容器塊。」 「他們的嵌套方式,以 DOM 的方式來表示,與所有其他容器塊使用風格傳承的方式相同。」 「然而,此外,在 DOM 中的清單有對應的數字格式化,無論是清單格式或段落格式。」

內聯塊

我們在翻譯「Bold」、「Italic」或「Strikethrough內嵌 Font」功能時使用「markdown」格式。

「Markdown功能」 Aspose.Words
Bold
{1}
Font.Bold = true
Italic
*italic text*
Font.Italic = true
Strikethrough
~Strikethrough text~
Font.StrikeThrough = true
// Use a document builder to add content to the document.
DocumentBuilder builder = new DocumentBuilder();

// Make the text Strikethrough.
builder.Font.Strikethrough = true;
builder.Writeln("This text will be Strikethrough");

「我們使用一個以字元 InlineCode 開始,並接著可選點 (.) 和任意數量的反引號 (`) 來命名之字元風格,用於 InlineCode 特徵。」 「如果遺漏了幾個反引號,則一個反引號將默認使用。」

「Markdown 功能」 Aspose.Words
InlineCode
{1}
Font.StyleName = “InlineCode[.][N]”
Autolink
<scheme://domain.com>
<email@domain.com>
FieldHyperlink 類別。」
Link
{1}
{2}
{3}
{4})
FieldHyperlink 類別。」
Image
{1}
{2}
{3}
{4})
Shape 類別。」

容器塊

文件是由標頭、段落、清單、引文等容器塊組成的序列。 容器塊可以分為 2 類:葉片塊與複雜容器。 葉塊只能包含內嵌內容。 「複雜容器可以包含其他容器的塊,包括葉塊。」

葉塊

下表列出了在 Aspose.Words 中使用 Markdown Leaf 塊的範例:

「Markdown功能」 Aspose.Words
HorizontalRule
-----
「這是個簡單的段落,其對應的HorizontalRule形狀:
DocumentBuilder.InsertHorizontalRule()
ATX Heading
# H1, ## H2, ### H3…
ParagraphFormat.StyleName = “Heading N”,其中 1 <= N <= 9」。
這被翻譯成內建样式,且應完全符合指定模式(不允許有後綴或前綴)。
否則,它只是一個普通段落,具有相應的样式。
Setext Heading
===(若為第1級標題),
---(若為第2級標題)」
ParagraphFormat.StyleName = “SetextHeading[some suffix]”,以 “Heading N” 樣式為基礎。
若 (N >= 2),則使用 “Heading 2”,否則使用 “Heading 1”
允許任何尾數,但 Aspose.Words 進口器分別使用數字"1"和"2”。」
// Use a document builder to add content to the document.
DocumentBuilder builder = new DocumentBuilder();

builder.ParagraphFormat.StyleName = "Heading 1";
builder.Writeln("This is an H1 tag");

// Reset styles from the previous paragraph to not combine styles between paragraphs.
builder.Font.Bold = false;
builder.Font.Italic = false;

Style setexHeading1 = builder.Document.Styles.Add(StyleType.Paragraph, "SetextHeading1");
builder.ParagraphFormat.Style = setexHeading1;
builder.Document.Styles["SetextHeading1"].BaseStyleName = "Heading 1";
builder.Writeln("Setext Heading level 1");

builder.ParagraphFormat.Style = builder.Document.Styles["Heading 3"];
builder.Writeln("This is an H3 tag");

// Reset styles from the previous paragraph to not combine styles between paragraphs.
builder.Font.Bold = false;
builder.Font.Italic = false;

Style setexHeading2 = builder.Document.Styles.Add(StyleType.Paragraph, "SetextHeading2");
builder.ParagraphFormat.Style = setexHeading2;
builder.Document.Styles["SetextHeading2"].BaseStyleName = "Heading 3";

// Setex heading level will be reset to 2 if the base paragraph has a Heading level greater than 2.
builder.Writeln("Setext Heading level 2");
Indented Code ParagraphFormat.StyleName = “IndentedCode[some suffix]”
Fenced Code
``` c#
if ()
then
else
```
ParagraphFormat.StyleName = “FencedCode[.][info string]”
[.][info string] 是可選的。」

「複雜容器」

下面的表格顯示了 Markdown 複雜容器在 Aspose.Words 中的使用範例:

「Markdown功能」 Aspose.Words
Quote
> quote,
>> nested quote
ParagraphFormat.StyleName = “Quote[some suffix]”
」在風格名稱中使用的後綴是可選的,「Aspose.Words」匯入器使用嵌套引號中的有序數字(1、2、3…)。
」嵌套定義透過遺傳風格來定義。
// Use a document builder to add content to the document.
DocumentBuilder builder = new DocumentBuilder();

// By default a document stores blockquote style for the first level.
builder.ParagraphFormat.StyleName = "Quote";
builder.Writeln("Blockquote");

// Create styles for nested levels through style inheritance.
Style quoteLevel2 = builder.Document.Styles.Add(StyleType.Paragraph, "Quote1");
builder.ParagraphFormat.Style = quoteLevel2;
builder.Document.Styles["Quote1"].BaseStyleName = "Quote";
builder.Writeln("1. Nested blockquote");
BulletedList
- Item 1
- Item 2
- Item 2a
- Item 2b
「無序清單是以段落計數的方式來表示:
ListFormat.ApplyBulletDefault()
無序清單有 3 種类型。」 「他們只是在第一層的數字格式上有所不同。」 這些是:‘-’‘+’‘*’
OrderedList
1. Item 1
2. Item 2
1) Item 2a
2) Item 2b
「有序清單是使用段落計數來表示:
ListFormat.ApplyNumberDefault()
可有 2 個數字格式記號:. 和 `)'。」 預設的標記是「.」 |
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.ListFormat.ApplyBulletDefault();
builder.ListFormat.List.ListLevels[0].NumberFormat = $"{(char) 0}.";
builder.ListFormat.List.ListLevels[1].NumberFormat = $"{(char) 1}.";

builder.Writeln("Item 1");
builder.Writeln("Item 2");

builder.ListFormat.ListIndent();

builder.Writeln("Item 2a");
builder.Writeln("Item 2b");

「表」

「Aspose.Words 也讓您將表格翻譯成 DOM,如下所示:」

Markdown 功能 Aspose.Words
Table
a | b
- | -
`c | d’ | 「TableRowCell 課程。」 |
// Use a document builder to add content to the document.
DocumentBuilder builder = new DocumentBuilder();

// Add the first row.
builder.InsertCell();
builder.Writeln("a");
builder.InsertCell();
builder.Writeln("b");

// Add the second row.
builder.InsertCell();
builder.Writeln("c");
builder.InsertCell();
builder.Writeln("d");

另見: