แปลMarkdownไปยังรูปแบบวัตถุเอกสาร(DOM)

ในการอ่านจัดการและปรับเปลี่ยนเนื้อหาและการฟอร์แมตของเอกสารคุณต้องแปลไปยังโมเดลออบเจกต์ของเอกสารAspose.Words(DOM).

ในทางตรงกันข้ามกับเอกสารคำMarkdownไม่สอดคล้องกับDOMอธิบายไว้ใน Aspose.Wordsรูปแบบวัตถุเอกสาร(DOM) บทความ. อย่างไรก็ตามAspose.WordsมีกลไกของตัวเองสำหรับการแปลเอกสารMarkdownไปยังDOMและกลับ,เพื่อให้เราสามาร.

บทความนี้อธิบายวิธีการแปลคุณลักษณะต่างๆmarkdownเป็นAspose.WordsDOMและกลับไปยังรูปแบบMarkdown.

ความซับซ้อนของการแปลMarkdown – DOM – Markdown

ปัญหาหลักของกลไกนี้ไม่ได้เป็นเพียงการแปลMarkdownถึงDOMแต่ยังจะทำการเปลี่ยนแปลงย้อนกลับ-เพื่อบันทึกเอกสารกลับไปยังรูปแบบMarkdownที่มีการสูญเสียน้อยที่สุด มีองค์ประกอบเช่นคำพูดหลายระดับซึ่งการเปลี่ยนแปลงย้อนกลับไม่น่ารำคาญ.

เครื่องมือแปลของเราช่วยให้ผู้ใช้ไม่เพียงแต่ทำงานกับองค์ประกอบที่ซับซ้อนในเอกสารที่มีอยู่Markdownแต่ยังสร้างเอกสารของตนเองในรูปแบบของMarkdownด้วยโครงสร้างเดิมตั้งแต่เริ่มต้น เพื่อสร้างองค์ประกอบต่างๆคุณจำเป็นต้องใช้รูปแบบที่มีชื่อเฉพาะตามกฎระเบียบบางอย่างที่อธิบายไว้ในบทความนี้ รูปแบบดังกล่าวสามารถสร้างโปรแกรม.

หลักการแปลทั่วไป

เราใช้Fontการจัดรูปแบบสำหรับบล็อกแบบอินไลน์ เมื่อไม่มีการติดต่อโดยตรงสำหรับคุณลักษณะMarkdownในAspose.WordsDOMเราใช้รูปแบบตัวอักษรที่มีชื่อที่เริ่มต้นจาก.

สำหรับบล็อกคอนเทนเนอร์เราใช้มรดกสไตล์เพื่อแสดงคุณลักษณะMarkdownที่ซ้อนกัน ในกรณีนี้แม้ในขณะที่ไม่มีคุณลักษณะที่ซ้อนกัน,เรายังใช้รูปแบบย่อหน้าที่มีชื่อที่เริ่มต้นจากคำพิเศ.

รายการที่มีสัญลักษณ์แสดงหัวข้อย่อยและสั่งเป็นบล็อกคอนเทนเนอร์ในMarkdownเช่นกัน รทำรังของพวกเขาจะแสดงในDOMเช่นเดียวกับบล็อกคอนเทนเนอร์อื่นๆทั้งหมดโดยใช้การสืบทอ อย่างไรก็ตาม,นอกจากนี้,รายการในDOMมีการจัดรูปแบบตัวเลขที่สอดคล้องกันทั้งในรูปแบบราย.

บล็อกอินไลน์

เราใช้Fontการจัดรูปแบบเมื่อแปลBold,ItalicหรือStrikethroughอินไลน์markdownคุณสมบัติ.

Markdownคุณลักษณะ Aspose.Words
Bold
**bold text**
get_Font()->set_Bold(true)
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// Use a document builder to add content to the document.
auto builder = System::MakeObject<DocumentBuilder>();
// Make the text Bold.
builder->get_Font()->set_Bold(true);
builder->Writeln(u"This text will be Bold");
Italic
*italic text*
get_Font()->set_Italic(true)
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// Use a document builder to add content to the document.
auto builder = System::MakeObject<DocumentBuilder>();
// Make the text Italic.
builder->get_Font()->set_Italic(true);
builder->Writeln(u"This text will be Italic");
Strikethrough
~Strikethrough text~
get_Font()->set_StrikeThrough(true)
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// Use a document builder to add content to the document.
auto builder = System::MakeObject<DocumentBuilder>();
// Make the text Strikethrough.
builder->get_Font()->set_StrikeThrough(true);
builder->Writeln(u"This text will be Strikethrough");

เราใช้รูปแบบอักขระที่มีชื่อที่เริ่มต้นจากคำว่าInlineCodeตามด้วยจุดเสริม(.)และจำนวนของติ๊กหลัง(`)สำหรับคุณลักษณะInlineCode หากมีข้อผิดพลาดหลายข้อผิดพลาดจะมีการใช้เครื่องหมายย้อนกลับหนึ่งตัวโดยค่าเริ่มต้น.

Markdownคุณลักษณะ Aspose.Words
InlineCode
**inline code**
get_Font()->set_StyleName(u"InlineCode[.][N]")
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// Use a document builder to add content to the document.
auto builder = System::MakeObject<DocumentBuilder>();
// Number of backticks is missed, one backtick will be used by default.
auto inlineCode1BackTicks = builder->get_Document()->get_Styles->Add(StyleType::Character, u"InlineCode");
builder->get_Font()->set_Style(inlineCode1BackTicks);
builder->Writeln(u"Text with InlineCode style with 1 backtick");
// There will be 3 backticks.
auto inlineCode3BackTicks = builder->get_Document()->get_Styles->Add(StyleType::Character, u"InlineCode.3");
builder->get_Font()->set_Style(inlineCode3BackTicks);
builder->Writeln(u"Text with InlineCode style with 3 backtick");
Autolink
<scheme://domain.com>
<email@domain.com>
คลาสFieldHyperlink.
Link
[link text](url)
[link text](<url>"title")
[link text](url 'title')
[link text](url (title))
คลาสFieldHyperlink.
Image
![](url)
![alt text](<url>"title")
![alt text](url ‘title’)
![alt text](url (title))
คลาสShape.
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// Use a document builder to add content to the document.
auto doc = System::MakeObject<Document>();
auto builder = System::MakeObject<DocumentBuilder>(doc);
// Insert image.
auto shape = System::MakeObject<Shape>(doc, ShapeType::Image);
shape->set_WrapType(WrapType::Inline);
shape->get_ImageData()->set_SourceFullName(u"/attachment/1456/pic001.png");
shape->get_ImageData()->set_Title(u"title");
builder->InsertNode(shape);

บล็อกคอนเทนเนอร์

เอกสารเป็นลำดับของคอนเทนเนอร์บล็อกเช่นส่วนหัวย่อหน้ารายการใบเสนอราคาและอื่ บล็อกคอนเทนเนอร์สามารถแบ่งออกเป็น2ชั้น:บล็อกใบและภาชนะบรรจุที่ซับซ้อน บล็อกใบสามารถมีเนื้อหาแบบอินไลน์เท่านั้น Contชนะบรรจุที่ซับซ้อนในที่สุดก็สามารถมีบล็อกภาชนะอื่นๆรวมทั้งบล็อกใบ.

บล็อกใบ

ตารางด้านล่างแสดงตัวอย่างการใช้บล็อกใบMarkdownในAspose.Words:

Markdownคุณลักษณะ Aspose.Words
HorizontalRule
-----
นี่เป็นย่อหน้าง่ายๆที่มีรูปร่างHorizontalRuleที่สอดคล้องกัน:
DocumentBuilder::InsertHorizontalRule()
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// Use a document builder to add content to the document.
auto builder = System::MakeObject<DocumentBuilder>();
// Insert horizontal rule.
builder->InsertHorizontalRule();
ATX Heading
# H1, ## H2, ### H3…
get_ParagraphFormat()->set_StyleName(u"Heading N")ที่ไหน(1<=เอ็น <= 9).
นี้ถูกแปลเป็นสไตล์ในตัวและควรจะตรงกับรูปแบบที่ระบุ(ไม่อนุญาตให้ต่อท้ายหรือคำนำหน้า)
มิฉะนั้นมันจะเป็นเพียงย่อหน้าปกติที่มีสไตล์ที่สอดคล้องกัน.
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// Use a document builder to add content to the document.
auto builder = System::MakeObject<DocumentBuilder>();
// By default Heading styles in Word may have Bold and Italic formatting.
// If we do not want to be emphasized, set these properties explicitly to false.
builder->get_Font()->set_Bold(false);
builder->get_Font()->set_Italic(false);
builder->get_ParagraphFormat()->set_StyleName(u"Heading 1");
builder->Writeln(u"This is an H1 tag");
Setext Heading
=== (if Heading level 1),
--- (if Heading level 2)
get_ParagraphFormat->set_StyleName(u"SetextHeading[some suffix]")ขึ้นอยู่กับ"Heading N"สไตล์
ถ้า(ยังไม่มีข้อความ>=2),แล้ว"Heading 2"จะถูกใช้,มิฉะนั้น"Heading 1"
คำต่อท้ายใดๆที่ได้รับอนุญาตแต่ผู้นำเข้าAspose.Wordsใช้หมายเลข"1"และ"2"ตามลำดับ.
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// Use a document builder to add content to the document.
auto doc = System::MakeObject<Document>();
auto builder = System::MakeObject<DocumentBuilder>(doc);
builder->get_ParagraphFormat()->set_StyleName(u"Heading 1");
builder->Writeln(u"This is an H1 tag");
// Reset styles from the previous paragraph to not combine styles between paragraphs.
builder->get_Font()->set_Bold(false);
builder->get_Font()->set_Italic(false);
auto setexHeading1 = builder->get_Document()->get_Styles->Add(StyleType::Paragraph, u"SetextHeading1");
builder->get_ParagraphFormat()->set_Style(setexHeading1);
doc->get_Styles()->idx_get(u"SetextHeading1")->set_BaseStyleName(u"Heading 1");
builder->Writeln(u"Setext Heading level 1");
builder->get_ParagraphFormat()->set_Style(doc->get_Styles()->idx_get(u"Heading 3"));
builder->Writeln(u"This is an H3 tag");
// Reset styles from the previous paragraph to not combine styles between paragraphs.
builder->get_Font()->set_Bold(false);
builder->get_Font()->set_Italic(false);
auto setexHeading2 = builder->get_Document()->get_Styles->Add(StyleType::Paragraph, u"SetextHeading2");
builder->get_ParagraphFormat()->set_Style(setexHeading2);
doc->get_Styles()->idx_get(u"SetextHeading2")->set_BaseStyleName(u"Heading 3");
// Setex heading level will be reset to 2 if the base paragraph has a Heading level greater than 2.
builder->Writeln(u"Setext Heading level 2");
Indented Code get_ParagraphFormat->set_StyleName(u"IndentedCode[some suffix]")
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// Use a document builder to add content to the document.
auto doc = System::MakeObject<Document>();
auto builder = System::MakeObject<DocumentBuilder>(doc);
auto indentedCode = builder->get_Document()->get_Styles->Add(StyleType::Paragraph, u"IndentedCode");
builder->get_ParagraphFormat()->set_StyleName(indentedCode);
builder->Writeln(u"This is an indented code");
Fenced Code
``` c#
if ()
then
else
```
get_ParagraphFormat()->set_StyleName(u"FencedCode[.][info string]")
[.]และ[info string]เป็นตัวเลือก.
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// Use a document builder to add content to the document.
auto doc = System::MakeObject<Document>();
auto builder = System::MakeObject<DocumentBuilder>(doc);
auto fencedCode = builder->get_Document()->get_Styles->Add(StyleType::Paragraph, u"FencedCode");
builder->get_ParagraphFormat()->set_StyleName(fencedCode);
builder->Writeln(u"This is an fenced code");
auto fencedCodeWithInfo = builder->get_Document()->get_Styles->Add(StyleType::Paragraph, u"FencedCode.C#");
builder->get_ParagraphFormat()->set_StyleName(fencedCodeWithInfo);
builder->Writeln(u"This is a fenced code with info string");

Ersชนะบรรจุที่ซับซ้อน

ตารางด้านล่างแสดงตัวอย่างของการใช้Markdownคอนเทนเนอร์ที่ซับซ้อนในAspose.Words:

Markdownคุณลักษณะ Aspose.Words
Quote
> quote,
>> nested quote
get_ParagraphFormat()->set_StyleName(u"Quote[some suffix]")
ต่อท้ายในชื่อสไตล์เป็นตัวเลือกแต่ผู้นำเข้าAspose.Wordsใช้หมายเลขเรียงลำดับ1, 2, 3, …. ในกรณีของคำพูดที่ซ้อนกัน
รังจะถูกกำหนดผ่านทางรูปแบบที่สืบทอด.
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// Use a document builder to add content to the document.
auto doc = System::MakeObject<Document>();
auto builder = System::MakeObject<DocumentBuilder>(doc);
// By default a document stores blockquote style for the first level.
builder->get_ParagraphFormat()->set_StyleName(u"Quote");
builder->Writeln(u"Blockquote");
// Create styles for nested levels through style inheritance.
auto quoteLevel2 = builder->get_Document()->get_Styles->Add(StyleType::Paragraph, u"Quote1");
builder->get_ParagraphFormat()->set_StyleName(quoteLevel2);
builder->get_Document()->get_Styles->idx_get(u"Quote1")->set_BaseStyleName(u"Quote");
builder->Writeln(u"1. Nested blockquote");
BulletedList
- Item 1
- Item 2
- Item 2a
- Item 2b
รายการที่มีสัญลักษณ์แสดงโดยใช้หมายเลขย่อหน้า:
get_ListFormat()->ApplyBulletDefault()
สามารถมี3ประเภทของรายการที่มีสัญลักษณ์ กต่างกันในรูปแบบตัวเลขของระดับแรกมาก เหล่านี้คือ:‘-’,‘+’หรือ‘*’ตามลำดับ.
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// Use a document builder to add content to the document.
auto builder = System::MakeObject<DocumentBuilder>();
builder->get_ListFormat()->ApplyBulletDefault();
builder->get_ListFormat()->get_List()->get_ListLevels()->idx_get(0)->set_NumberFormat(u"-");
builder->Writeln(u"Item 1");
builder->Writeln(u"Item 2");
builder->get_ListFormat()->ListIndent();
builder->Writeln(u"Item 2a");
builder->Writeln(u"Item 2b");
OrderedList
1. Item 1
2. Item 2
1) Item 2a
2) Item 2b
รายการสั่งซื้อจะถูกแสดงโดยใช้หมายเลขย่อหน้า:
get_ListFormat()->ApplyNumberDefault()
สามารถมีเครื่องหมายรูปแบบหมายเลข2:‘.’และ‘)’ เครื่องหมายเริ่มต้นคือ‘.’.
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// Use a document builder to add content to the document.
auto builder = System::MakeObject<DocumentBuilder>();
builder->get_ListFormat()->ApplyBulletDefault();
builder->get_ListFormat()->get_List()->get_ListLevels()->idx_get(0)->set_NumberFormat(System::String::Format(u"{0}.", (char16_t)0));
builder->get_ListFormat()->get_List()->get_ListLevels()->idx_get(1)->set_NumberFormat(System::String::Format(u"{0}.", (char16_t)1));
builder->Writeln(u"Item 1");
builder->Writeln(u"Item 2");
builder->get_ListFormat()->ListIndent();
builder->Writeln(u"Item 2a");
builder->Writeln(u"Item 2b");

โต๊ะ

Aspose.Wordsยังช่วยให้การแปลตารางเป็นDOMดังแสดงด้านล่าง:

Markdownคุณลักษณะ Aspose.Words
Table
a|b
-|-
c|d
Table,RowและCellชั้นเรียน.
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// Use a document builder to add content to the document.
auto builder = System::MakeObject<DocumentBuilder>();
// Add the first row.
builder->InsertCell();
builder->Writeln(u"a");
builder->InsertCell();
builder->Writeln(u"b");
// Add the second row.
builder->InsertCell();
builder->Writeln(u"c");
builder->InsertCell();
builder->Writeln(u"d");

ดูเพิ่มเติม