Přeložit Markdown až Document Object Model (DOM)

Chcete-li programově číst, manipulovat a upravovat obsah a formátování dokumentu, musíte jej přeložit do Aspose.Words Document Object Model (DOM).

Na rozdíl od dokumentů Word, Markdown neodpovídá DOM popsané v Aspose.Words Document Object Model (DOM) článek. Nicméně, Aspose.Words poskytuje svůj vlastní mechanismus pro překlad Markdown doklady k DOM a zpět, abychom mohli úspěšně pracovat s jejich prvky, jako je formátování textu, tabulky, hlavičky, a další.

Tento článek vysvětluje, jak různé markdown funkce lze přeložit do Aspose.Words DOM a zpět k Markdown formát.

Složitost překladu Markdown? DOM? Markdown

Hlavní obtížností tohoto mechanismu je nejen překládat Markdown až DOM, ale také k provedení reverzní transformace, aby byl dokument uložen zpět Markdown formát s minimální ztrátou. Existují prvky, jako jsou víceúrovňové citace, pro které reverzní transformace není triviální.

Náš překladatelský motor umožňuje uživatelům nejen pracovat se složitými prvky v existující Markdown dokument, ale také vytvořit svůj vlastní dokument v Markdown formát s původní strukturou od nuly. Chcete-li vytvořit různé prvky, musíte použít styly s konkrétními názvy podle některých pravidel popsaných později v tomto článku. Takové styly lze vytvořit programově.

Společný překlad Zásady

Používáme Font formátování pro inline bloky. Pokud neexistuje přímá korespondence pro Markdown funkce v Aspose.Words DOM, používáme charakterový styl se jménem, který začíná z některých speciálních slov.

Pro kontejnerové bloky používáme styl dědictví k označení hnízda Markdown funkce. V tomto případě, i když tam nejsou žádné vnořené rysy, používáme také odstavce styly s názvem, který začíná z některých zvláštních slov.

Kulky a objednané seznamy jsou kontejnerové bloky v Markdown Taky. Jejich hnízdění je zastoupeno v DOM stejně jako u všech ostatních kontejnerových bloků za použití stylu dědictví. Avšak kromě toho seznam DOM odpovídají formátování čísel buď ve stylu seznamu, nebo ve formátu odstavce.

Inline bloky

Používáme Font formátování při překladu Bold, Italic nebo Strikethrough inline markdown funkce.

Markdown funkce 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");

Používáme styl postavy se jménem, který začíná od slova InlineCode, následuje volitelná tečka (.) a řada odplat (`) pro InlineCode bonus. Pokud se vynechá řada backticks, pak jeden backtick bude použit ve výchozím nastavení.

Markdown funkce 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>
The FieldHyperlink třída
Link
{1}
{2}
{3}
{4})
The FieldHyperlink třída
Image
{1}
{2}
{3}
{4})
The Shape třída
// 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);

Bloky kontejnerů

Dokument je posloupnost kontejnerových bloků, jako jsou nadpisy, odstavce, seznamy, uvozovky a další. Kontejnerové bloky lze rozdělit do dvou tříd: Listové bloky a komplexní kontejnery. Listové bloky mohou obsahovat pouze inline obsah. Komplexní kontejnery zase mohou obsahovat další kontejnerové bloky, včetně Leaf bloků.

Listové bloky

Níže uvedená tabulka uvádí příklady použití Markdown Listové bloky v Aspose.Words:

Markdown funkce Aspose.Words
HorizontalRule
-----
Jedná se o jednoduchý odstavec s odpovídajícím tvarem horizontálního pravidla:
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 písm. c) nařízení o kapitálových požadavcích.
To je přeloženo do vestavěného stylu a mělo by být přesně zadaného vzoru (nejsou povoleny žádné přípony nebo předpony).
Jinak to bude jen pravidelný odstavec s odpovídajícím stylem
// 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
=== (pokud úroveň názvu 1),
--- (je-li položka úrovně 2)
ParagraphFormat.StyleName = “SetextHeading[some suffix]”, na základě ‘Heading N’ Styl.
Pokud (N < 2), pak ‘Heading 2’ bude použito jinak ‘Heading 1’.
Každá přípona je povolena, ale Aspose.Words dovozce používá čísla 1,0% a 2,0%
// 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]”
The [.] a [info string] jsou nepovinné
// 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");

Komplexní kontejnery

Níže uvedená tabulka uvádí příklady použití Markdown Komplexní kontejnery v Aspose.Words:

Markdown funkce Aspose.Words
Quote
> quote,
>> nested quote
ParagraphFormat.StyleName = “Quote[some suffix]”
Přípona ve stylu je volitelná, ale Aspose.Words dovozce používá objednaná čísla 1, 2, 3, …. v případě hnízdě kotací.
Hnízdo je definováno prostřednictvím dědičných stylů
// 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
Seznamy s kulkou jsou zastoupeny číslováním odstavce:
ListFormat.ApplyBulletDefault()
Můžou tu být tři druhy zastřelených seznamů. Jsou pouze rozdíl v číslování formátu úplně první úrovně. Jedná se o: ‘-’, ‘+’ nebo ‘*’ resp
// 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
Objednané seznamy jsou zastoupeny podle číslování odstavce:
ListFormat.ApplyNumberDefault()
Mohou existovat 2 znaky formátů čísel: Výchozím ukazatelem je:
// 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");

Tabulky

Aspose.Words také umožňuje překládat tabulky do DOM, jak je uvedeno níže:

Markdown funkce 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");

Viz také