Markdownをドキュメントオブジェクトモデル(DOM)に変換する

文書の内容と書式をプログラムで読み取り、操作し、変更するには、それをAspose.Wordsドキュメントオブジェクトモデル(DOM)に変換する必要があります。

Word文書とは対照的に、Markdownは、Word文書で説明されているDOMに準拠していません。 Aspose.Wordsドキュメントオブジェクトモデル(DOM) 記事。 しかし、Aspose.Wordsは、MarkdownドキュメントをDOMに変換して戻すための独自のメカニズムを提供しているため、テキストの書式設定、テーブル、ヘッダーなどの要素を正常に処理できます。

この記事では、さまざまなmarkdown機能をAspose.WordsDOMに変換してMarkdown形式に戻す方法について説明します。

翻訳の複雑さMarkdown-DOM-Markdown

このメカニズムの主な難しさは、MarkdownをDOMに変換するだけでなく、逆変換を行うことです–文書を最小限の損失でMarkdown形式に保存し直すことです。 マルチレベル引用符などの要素がありますが、逆変換は簡単ではありません。

私たちの翻訳エンジンは、ユーザーが既存のMarkdown文書内の複雑な要素を操作するだけでなく、元の構造を持つMarkdown形式で独自の文書を最初から作成すること さまざまな要素を作成するには、この記事の後半で説明する特定の規則に従って、特定の名前のスタイルを使用する必要があります。 このようなスタイルはプログラムで作成できます。

一般的な翻訳の原則

インラインブロックにはFont書式を使用します。 Aspose.WordsDOMにMarkdown機能に対する直接の対応がない場合、いくつかの特別な単語から始まる名前を持つ文字スタイルを使用します。

コンテナブロックでは、ネストされたMarkdownフィーチャを示すためにスタイル継承を使用します。 この場合、ネストされたフィーチャがない場合でも、いくつかの特別な単語から始まる名前を持つ段落スタイルも使用します。

箇条書きリストと順序付けられたリストはMarkdownのコンテナブロックでもあります。 それらのネストは、スタイル継承を使用する他のすべてのコンテナブロックと同じ方法でDOMで表されます。 ただし、さらに、DOM内のリストは、リストスタイルまたは段落書式のいずれかで数値書式設定に対応しています。

インラインブロック

BoldItalic、またはStrikethroughインラインmarkdown機能を翻訳するときは、Font書式を使用します。

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機能のバッククォート(`)の数が続きます。 いくつかのバックティックが欠落している場合、デフォルトで1つのバックティックが使用されます。

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クラスに分けることができます:リーフブロックと複雑なコンテナです。 リーフブロックにはインラインコンテンツのみを含めることができます。 複雑なコンテナには、リーフブロックを含む他のコンテナブロックを含めることができます。

葉のブロック

次の表は、Aspose.WordsでMarkdownリーフブロックを使用した例を示しています:

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<=N <= 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"スタイルに基づいています。
(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");

複雑な容器

次の表は、Aspose.WordsでMarkdown複合コンテナを使用した例を示しています:

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種類があります。 それらは、最初のレベルの番号付け形式でのみdiffです。 これらは、それぞれ‘-’‘+’、または‘*’です。
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
TableRowCellクラス。
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");

また見て下さい