แปล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**
Font.Bold = true
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// Use a document builder to add content to the document.
DocumentBuilder builder = new DocumentBuilder();
// Make the text Bold.
builder.getFont().setBold(true);
builder.writeln("This text will be Bold");
Italic
*italic text*
Font.Italic = true
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// Use a document builder to add content to the document.
DocumentBuilder builder = new DocumentBuilder();
// Make the text Italic.
builder.getFont().setItalic(true);
builder.writeln("This text will be Italic");
Strikethrough
~Strikethrough text~
Font.StrikeThrough = true
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// Use a document builder to add content to the document.
DocumentBuilder builder = new DocumentBuilder();
// Make the text Strikethrough.
builder.getFont().setStrikeThrough(true);
builder.writeln("This text will be Strikethrough");

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

Markdownคุณลักษณะ Aspose.Words
InlineCode
**inline code**
Font.StyleName = "InlineCode[.][N]"
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// Use a document builder to add content to the document.
DocumentBuilder builder = new DocumentBuilder();
// Number of backticks is missed, one backtick will be used by default.
Style inlineCode1BackTicks = builder.getDocument().getStyles().add(StyleType.CHARACTER, "InlineCode");
builder.getFont().setStyle(inlineCode1BackTicks);
builder.writeln("Text with InlineCode style with 1 backtick");
// There will be 3 backticks.
Style inlineCode3BackTicks = builder.getDocument().getStyles().add(StyleType.CHARACTER, "InlineCode.3");
builder.getFont().setStyle(inlineCode3BackTicks);
builder.writeln("Text with InlineCode style with 3 backtick");
Autolink
<scheme://domain.com>
<email@domain.com>
คลาสFieldHyperlink.
Link
[ลิงก์ข้อความ](url)
[ลิงก์ข้อความ](<url>"title")
[ลิงก์ข้อความ](url 'title')
[ลิงก์ข้อความ](url (title))
คลาสFieldHyperlink.
Image
![](/words/java/translate-markdown-to-document-object-model/url)
![ข้อความ](/words/java/translate-markdown-to-document-object-model/<url>"title")
![ข้อความ](/words/java/translate-markdown-to-document-object-model/url ‘title’)
![ข้อความ](/words/java/translate-markdown-to-document-object-model/url (title))
คลาสShape.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// Use a document builder to add content to the document.
DocumentBuilder builder = new DocumentBuilder();
// Insert image.
Shape shape = new Shape(builder.getDocument(), ShapeType.IMAGE);
shape.setWrapType(WrapType.INLINE);
shape.getImageData().setSourceFullName("/attachment/1456/pic001.png");
shape.getImageData().setTitle("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-Java
// Use a document builder to add content to the document.
DocumentBuilder builder = new DocumentBuilder();
// Insert horizontal rule.
builder.insertHorizontalRule();
ATX Heading
# H1, ## H2, ### H3…
ParagraphFormat.StyleName = "Heading N"ที่ไหน(1<=เอ็น <= 9).
นี้ถูกแปลเป็นสไตล์ในตัวและควรจะตรงกับรูปแบบที่ระบุ(ไม่อนุญาตให้ต่อท้ายหรือคำนำหน้า)
มิฉะนั้นมันจะเป็นเพียงย่อหน้าปกติที่มีสไตล์ที่สอดคล้องกัน.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// Use a document builder to add content to the document.
DocumentBuilder builder = new 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.getFont().setBold(false);
builder.getFont().setItalic(false);
builder.getParagraphFormat().setStyleName("Heading 1");
builder.writeln("This is an H1 tag");
Setext Heading
=== (if Heading level 1),
--- (if Heading level 2)
ParagraphFormat.StyleName = "SetextHeading[some suffix]"ขึ้นอยู่กับ’หัวข้อไม่มีสไตล์
ถ้า(ยังไม่มีข้อความ>=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-Java
// Use a document builder to add content to the document.
DocumentBuilder builder = new DocumentBuilder();
builder.getParagraphFormat().setStyleName("Heading 1");
builder.writeln("This is an H1 tag");
// Reset styles from the previous paragraph to not combine styles between paragraphs.
builder.getFont().setBold(false);
builder.getFont().setItalic(false);
Style setexHeading1 = builder.getDocument().getStyles().add(StyleType.PARAGRAPH, "SetexHeading1");
builder.getParagraphFormat().setStyle(setexHeading1);
builder.getDocument().getStyles().get("SetexHeading1").setBaseStyleName("Heading 1");
builder.writeln("Setex Heading level 1");
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get("Heading 3"));
builder.writeln("This is an H3 tag");
// Reset styles from the previous paragraph to not combine styles between paragraphs.
builder.getFont().setBold(false);
builder.getFont().setItalic(false);
Style setexHeading2 = builder.getDocument().getStyles().add(StyleType.PARAGRAPH, "SetexHeading2");
builder.getParagraphFormat().setStyle(setexHeading2);
builder.getDocument().getStyles().get("SetexHeading2").setBaseStyleName("Heading 3");
// Setex heading level will be reset to 2 if the base paragraph has a Heading level greater than 2.
builder.writeln("Setex Heading level 2");
Indented Code
<br/>if ()<br/>then<br/>else<br/>```
ParagraphFormat.StyleName = "IndentedCode[some suffix]"
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// Use a document builder to add content to the document.
DocumentBuilder builder = new DocumentBuilder();
Style fencedCode = builder.getDocument().getStyles().add(StyleType.PARAGRAPH, "FencedCode");
builder.getParagraphFormat().setStyle(fencedCode);
builder.writeln("This is an fenced code");
Style fencedCodeWithInfo = builder.getDocument().getStyles().add(StyleType.PARAGRAPH, "FencedCode.C#");
builder.getParagraphFormat().setStyle(fencedCodeWithInfo);
builder.writeln("This is a fenced code with info string");

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

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

Markdownคุณลักษณะ Aspose.Words
Quote
> quote,
>> nested quote
ParagraphFormat.StyleName = "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-Java
// 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.getParagraphFormat().setStyleName("Quote");
builder.writeln("Blockquote");
// Create styles for nested levels through style inheritance.
Style quoteLevel2 = builder.getDocument().getStyles().add(StyleType.PARAGRAPH, "Quote1");
builder.getParagraphFormat().setStyle(quoteLevel2);
builder.getDocument().getStyles().get("Quote1").setBaseStyleName("Quote");
builder.writeln("1. Nested blockquote");
BulletedList
- Item 1
- Item 2
- Item 2a
- Item 2b
รายการที่มีสัญลักษณ์แสดงโดยใช้หมายเลขย่อหน้า:
ListFormat.ApplyBulletDefault()
สามารถมี 3 ประเภทของรายการสัญลักษณ์ กต่างกันในรูปแบบตัวเลขของระดับแรกมาก เหล่านี้คือ:‘-’,‘+’หรือ‘*’ตามลำดับ.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// Use a document builder to add content to the document.
DocumentBuilder builder = new DocumentBuilder();
builder.getListFormat().applyBulletDefault();
builder.getListFormat().getList().getListLevels().get(0).setNumberFormat("-");
builder.writeln("Item 1");
builder.writeln("Item 2");
builder.getListFormat().listIndent();
builder.writeln("Item 2a");
builder.writeln("Item 2b");
OrderedList
1. Item 1
2. Item 2
1) Item 2a
2) Item 2b
รายการสั่งซื้อจะถูกแสดงโดยใช้หมายเลขย่อหน้า:
ListFormat.ApplyNumberDefault()
อาจมีเครื่องหมายรูปแบบหมายเลข 2 ตัว:‘.’และ‘)’ เครื่องหมายเริ่มต้นคือ‘.’.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getListFormat().applyBulletDefault();
builder.getListFormat().getList().getListLevels().get(0).setNumberFormat(MessageFormat.format("{0}.", (char)0));
builder.getListFormat().getList().getListLevels().get(1).setNumberFormat(MessageFormat.format("{0}.", (char)1));
builder.writeln("Item 1");
builder.writeln("Item 2");
builder.getListFormat().listIndent();
builder.writeln("Item 2a");
builder.writeln("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-Java
// 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");

ดูเพิ่มเติม