-
Notifications
You must be signed in to change notification settings - Fork 0
/
MarkdownVisitor.cs
95 lines (91 loc) · 3.11 KB
/
MarkdownVisitor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using DevExpress.XtraRichEdit.API.Native;
using System;
using System.Linq;
using System.Text;
namespace DocumentIteratorExample
{
public abstract class BufferedDocumentVisitor : DocumentVisitorBase
{
readonly StringBuilder buffer;
protected BufferedDocumentVisitor()
{
this.buffer = new StringBuilder();
}
protected StringBuilder Buffer { get { return buffer; } }
}
public class MarkdownVisitor : BufferedDocumentVisitor
{
const char lastLowSpecial = '\x1f';
const char firstHighSpecial = '\xffff';
public string Text { get { return Buffer.ToString(); } }
public override void Visit(DocumentText text)
{
string prefix = GetPrefix(text.TextProperties);
Buffer.Append(prefix);
int count = text.Length;
for (int i = 0; i < count; i++)
{
char ch = text.Text[i];
if (ch > lastLowSpecial && ch < firstHighSpecial)
Buffer.Append(ch);
else if (ch == '\x9' || ch == '\xA' || ch == '\xD')
Buffer.Append(ch);
}
Buffer.Append(prefix);
}
public override void Visit(DocumentParagraphStart paragraphStart)
{
if (paragraphStart.ParagraphProperties.ParagraphStyle.Name == "heading 1")
Buffer.Append("#");
}
public override void Visit(DocumentInlinePicture inlinePicture)
{
InsertImageUri(inlinePicture.Uri);
}
public override void Visit(DocumentPicture picture)
{
InsertImageUri(picture.Uri);
}
public override void Visit(DocumentParagraphEnd paragraphEnd)
{
AppendLineOnNonEmptyContent();
}
public override void Visit(DocumentSectionEnd sectionEnd)
{
AppendLineOnNonEmptyContent();
}
public override void Visit(DocumentHyperlinkStart hyperlinkStart)
{
if (!String.IsNullOrEmpty(hyperlinkStart.NavigateUri))
Buffer.Append("[");
}
public override void Visit(DocumentHyperlinkEnd hyperlinkEnd)
{
if (!String.IsNullOrEmpty(hyperlinkEnd.NavigateUri))
Buffer.Append(String.Format("]({0})", hyperlinkEnd.NavigateUri));
}
void InsertImageUri(string uri)
{
if (!string.IsNullOrEmpty(uri))
Buffer.Append(String.Format("![]({0})", uri));
else
Buffer.Append("[[img src=attached-image.jpg alt=foobar]]");
}
string GetPrefix(ReadOnlyTextProperties properties)
{
string prefix = string.Empty;
if (properties.FontBold)
prefix = "**";
if (properties.FontItalic)
prefix += "*";
if (properties.StrikeoutType == StrikeoutType.Single)
prefix += "~~";
return prefix;
}
void AppendLineOnNonEmptyContent()
{
if (Buffer.Length > 0)
Buffer.AppendLine();
}
}
}