翻譯 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 |
||||||||||||||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
|||||||||||||||
Italic*italic text* |
Font.Italic = true |
||||||||||||||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
|||||||||||||||
Strikethrough~Strikethrough text~ |
Font.StrikeThrough = true |
||||||||||||||
|
我們使用一個以字元 InlineCode
開始,並接著可選點 (.)
和任意數量的反引號 (`)
來命名之字元風格,用於 InlineCode
特徵。 如果遺漏了幾個反引號,則一個反引號將默認使用。
Markdown 功能 | Aspose.Words | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
InlineCode {1} |
Font.StyleName = “InlineCode[.][N]” |
||||||||||||||||||||||||||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
|||||||||||||||||||||||||||
Autolink<scheme://domain.com> <email@domain.com> |
FieldHyperlink 類別。 | ||||||||||||||||||||||||||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
|||||||||||||||||||||||||||
Link{1} {2} {3} {4}) |
FieldHyperlink 類別。 | ||||||||||||||||||||||||||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
|||||||||||||||||||||||||||
Image{1} {2} {3} {4}) |
Shape 類別。 | ||||||||||||||||||||||||||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
容器塊
文件是由標頭、段落、清單、引文等容器塊組成的序列。 容器塊可以分為 2 類:葉片塊與複雜容器。 葉塊只能包含內嵌內容。 複雜容器可以包含其他容器的塊,包括葉塊。
葉塊
下表列出了在 Aspose.Words 中使用 Markdown Leaf 塊的範例:
Markdown功能 | Aspose.Words | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
HorizontalRule----- |
這是個簡單的段落,其對應的HorizontalRule形狀:DocumentBuilder.InsertHorizontalRule() |
||||||||||||||||||||||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
|||||||||||||||||||||||
ATX Heading# H1, ## H2, ### H3… |
ParagraphFormat.StyleName = "Heading N" ,其中 1 <= N <= 9。這被翻譯成內建样式,且應完全符合指定模式(不允許有後綴或前綴)。 否則,它只是一個普通段落,具有相應的样式。 |
||||||||||||||||||||||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
|||||||||||||||||||||||
Setext Heading=== (若為第1級標題),--- (若為第2級標題) |
ParagraphFormat.StyleName = “SetextHeading[some suffix]” ,以 ‘Heading N’ 樣式為基礎。若 (N >= 2),則使用 ‘Heading 2’,否則使用 ‘Heading 1’。 允許任何尾數,但 Aspose.Words 進口器分別使用數字"1"和"2"。 |
||||||||||||||||||||||
|
|||||||||||||||||||||||
Indented Code | ParagraphFormat.StyleName = “IndentedCode[some suffix]” |
||||||||||||||||||||||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
|||||||||||||||||||||||
Fenced Code
|
ParagraphFormat.StyleName = “FencedCode[.][info string]” [.] 和 [info string] 是可選的。 |
||||||||||||||||||||||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
複雜容器
下面的表格顯示了 Markdown 複雜容器在 Aspose.Words 中的使用範例:
Markdown功能 | Aspose.Words | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Quote> quote, >> nested quote |
ParagraphFormat.StyleName = “Quote[some suffix]” 在風格名稱中使用的後綴是可選的,Aspose.Words匯入器使用嵌套引號中的有序數字(1、2、3…)。 嵌套定義透過遺傳風格來定義。 |
||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||
BulletedList - Item 1 - Item 2 - Item 2a - Item 2b |
無序清單是以段落計數的方式來表示:ListFormat.ApplyBulletDefault() 無序清單有 3 種类型。 他們只是在第一層的數字格式上有所不同。 這些是: ‘-’ 、‘+’ 或 ‘*’ 。 |
||||||||||||||||||||||||||||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
|||||||||||||||||||||||||||||
OrderedList 1. Item 1 2. Item 2 1) Item 2a 2) Item 2b |
有序清單是使用段落計數來表示:ListFormat.ApplyNumberDefault() 可有 2 個數字格式記號: . 和 `)'。 預設的標記是. | |
||||||||||||||||||||||||||||
|
表
Aspose.Words 也讓您將表格翻譯成 DOM,如下所示:
Markdown 功能 | Aspose.Words |
---|---|
Table a|b -|- `c|d' | Table、Row 和 Cell 課程。 | |
|
|