Przetłumacz Markdown do Document Object Model (DOM)

Aby programowo czytać, manipulować i modyfikować zawartość i formatowanie dokumentu, należy przetłumaczyć go na Aspose.Words Document Object Model (DOM).

W przeciwieństwie do dokumentów Worda, Markdown nie jest zgodny z DOM opisane w Aspose.Words Document Object Model (DOM) Artykuł. Jednakże, Aspose.Words zapewnia własny mechanizm tłumaczenia Markdown dokumenty DOM i z powrotem, abyśmy mogli z powodzeniem pracować z ich elementami, takimi jak formatowanie tekstu, tabele, nagłówki i inne.

Ten artykuł wyjaśnia, jak różne markdown funkcje można przetłumaczyć na Aspose.Words DOM i powrót do Markdown format.

Kompleksowość tłumaczenia Markdown - DOM - Markdown

Główną trudnością tego mechanizmu jest nie tylko tłumaczenie Markdown do DOM, ale także zrobić odwrotną transformację - zapisać dokument z powrotem do Markdown format z minimalną stratą. Istnieją elementy, takie jak cytaty wielopoziomowe, dla których transformacja odwrotna nie jest trywialna.

Nasz silnik tłumaczeniowy pozwala użytkownikom nie tylko pracować z skomplikowanymi elementami w istniejących Markdown dokument, ale także do tworzenia własnego dokumentu w Markdown format z oryginalną strukturą od podstaw. Aby stworzyć różne elementy, musisz używać stylów z konkretnymi nazwami zgodnie z niektórymi zasadami opisanymi później w tym artykule. Style takie mogą być tworzone programowo.

Wspólne tłumaczenie Zasady

Używamy Font formatowanie bloków wewnętrznych. W przypadku braku bezpośredniej korespondencji Markdown funkcja w Aspose.Words DOM, Używamy stylu postaci o nazwie, która zaczyna się od specjalnych słów.

Dla bloków kontenerowych, używamy stylu dziedziczenia do określenia gniazda Markdown funkcje. W tym przypadku, nawet jeśli nie ma zagnieżdżonych funkcji, używamy również stylów paragrafów z nazwą, która zaczyna się od niektórych słów specjalnych.

Wystawione i zamówione listy są bloki kontenerowe w Markdown Jak również. Ich gniazda są reprezentowane w DOM w ten sam sposób jak dla wszystkich innych bloków kontenerowych przy użyciu stylu dziedziczenia. Jednakże, dodatkowo, wykazy w DOM mają odpowiadający numer formatowania w stylu listy lub w formacie paragrafu.

Bloki wewnętrzne

Używamy Font formatowanie podczas tłumaczenia Bold, Italic lub ~ ~ Strikethrough ~ ~ inline markdown funkcje.

Markdown funkcja Aspose.Words
Bold
{1}
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");

Używamy stylu znaków z nazwą, która zaczyna się od słowa InlineCode, > po której następuje opcjonalna kropka (.) i kilka tyłków (`) dla InlineCode funkcja. W przypadku pominięcia pewnej liczby typków, domyślnie zostanie użyty jeden.

Markdown funkcja Aspose.Words
InlineCode
{1}
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>
W FieldHyperlink Klasa
Link
{1}
{2}
{3}
{4})
W FieldHyperlink Klasa
Image
{1}
{2}
{3}
{4})
W Shape Klasa
// 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);

Bloki kontenerów

Dokument jest sekwencją bloków kontenerowych, takich jak nagłówki, paragrafy, listy, cytaty i inne. Bloki kontenerów można podzielić na 2 klasy: Bloki liści i złożone kontenery. Bloki liści mogą zawierać tylko zawartość inline. Z kolei złożone pojemniki mogą zawierać inne bloki kontenerowe, w tym bloki Leaf.

Bloki liści

Poniższa tabela przedstawia przykłady stosowania Markdown Bloki liści w Aspose.Words:

Markdown funkcja Aspose.Words
HorizontalRule
-----
Jest to prosty akapit o odpowiednim kształcie horyzontalnej zasady:
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", gdzie (1 < = N < = 9).
To jest przetłumaczone na built- w stylu i powinno być dokładnie określonego wzoru (nie są dozwolone przyrostki lub przedrostki).
W przeciwnym razie będzie to zwykły akapit o odpowiednim stylu
// 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
=== (jeżeli zakres 1),
--- (jeżeli zakres 2)
ParagraphFormat.StyleName = “SetextHeading[some suffix]”, w oparciu o ‘Heading N’ Styl.
Jeśli (N > = 2), to ‘Heading 2’ zostaną wykorzystane, w przeciwnym razie ‘Heading 1’.
Każdy przyrostek jest dozwolony, ale Aspose.Words Importer stosuje odpowiednio numery “1” i “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 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 indentedCode = builder.getDocument().getStyles().add(StyleType.PARAGRAPH, "IndentedCode");
builder.getParagraphFormat().setStyle(indentedCode);
builder.writeln("This is an indented code");
Fenced Code
``` java
if ()
then
else
```
ParagraphFormat.StyleName = “FencedCode[.][info string]”
W [.] oraz [info string] są opcjonalne
// 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");

Kontenery złożone

Poniższa tabela przedstawia przykłady stosowania Markdown Kompleksowe pojemniki w Aspose.Words:

Markdown funkcja Aspose.Words
Quote
> quote,
>> nested quote
ParagraphFormat.StyleName = “Quote[some suffix]”
Przyrostek w nazwie stylu jest opcjonalny, ale Aspose.Words Importer stosuje zamówione numery 1, 2, 3,…. w przypadku zagnieżdżonych cytatów.
Zagnieżdżenie jest zdefiniowane poprzez odziedziczone style
// 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
Listy zbiorcze są reprezentowane przy użyciu numeracji paragrafów:
ListFormat.ApplyBulletDefault()
Mogą być 3 rodzaje list bulletowanych. Są one tylko diff w formacie numeracji pierwszego poziomu. Są to: ‘-’, ‘+’ lub ‘*’ odpowiednio
// 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
Zamówione listy są reprezentowane przy użyciu numeracji paragrafów:
ListFormat.ApplyNumberDefault()
Mogą być 2 znaczniki formatu liczb: ‘.’ i') ‘. Domyślny znacznik to’ .'
// 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");

Tabele

Aspose.Words pozwala również na tłumaczenie tabel na DOM, jak przedstawiono poniżej:

Markdown funkcja Aspose.Words
Table
“a
b<br />-
// 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");

Zobacz również