-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBBCExporter.cs
More file actions
150 lines (128 loc) · 6.33 KB
/
BBCExporter.cs
File metadata and controls
150 lines (128 loc) · 6.33 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using System;
using System.IO;
using System.Text;
using DevExpress.Utils;
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.Export;
using DevExpress.XtraRichEdit.Internal;
using DevExpress.XtraRichEdit.Model;
using System.Drawing;
using DevExpress.Office;
using DevExpress.Office.Export;
using DevExpress.Office.Internal;
using DevExpress.XtraRichEdit.API.Native.Implementation;
namespace RichEditBBCExporter {
public class BBCodeDocumentExporter : IExporter<DocumentFormat, bool> {
internal static readonly FileDialogFilter filter = new FileDialogFilter("Bulletin Board Code", "bbc");
public FileDialogFilter Filter { get { return filter; } }
public DocumentFormat Format { get { return BBCodeDocumentFormat.Id; } }
public IExporterOptions SetupSaving() {
return new BBCodeDocumentExporterOptions();
}
public bool SaveDocument(IDocumentModel documentModel, Stream stream, IExporterOptions options) {
BBCodeExporter exporter = new BBCodeExporter((DocumentModel)documentModel, (BBCodeDocumentExporterOptions)options);
exporter.Export(stream);
return true;
}
public static void Register(IServiceProvider provider) {
if(provider == null)
return;
IDocumentExportManagerService service = provider.GetService(typeof(IDocumentExportManagerService)) as IDocumentExportManagerService;
if(service != null)
service.RegisterExporter(new BBCodeDocumentExporter());
}
}
public class BBCodeDocumentExporterOptions : DocumentExporterOptions {
protected override DocumentFormat Format { get { return BBCodeDocumentFormat.Id; } }
}
public class BBCodeExporter : DocumentModelExporter {
readonly BBCodeDocumentExporterOptions options;
Stream outputStream;
StreamWriter documentContentWriter;
bool hyperlinkExporting;
public BBCodeExporter(DocumentModel documentModel, BBCodeDocumentExporterOptions options)
: base(documentModel) {
Guard.ArgumentNotNull(options, "options");
this.options = options;
}
protected BBCodeDocumentExporterOptions Options { get { return options; } }
protected internal StreamWriter DocumentContentWriter { get { return documentContentWriter; } set { documentContentWriter = value; } }
public virtual void Export(Stream outputStream) {
this.outputStream = outputStream;
using(StreamWriter streamWriter = new StreamWriter(outputStream, Encoding.UTF8)) {
DocumentContentWriter = streamWriter;
Export();
streamWriter.Flush();
}
}
protected override void ExportTextRun(TextRun run) {
string text = run.GetPlainText(PieceTable.TextBuffer);
if(!hyperlinkExporting) {
if(run.FontBold)
DocumentContentWriter.Write("[b]");
if(run.FontItalic)
DocumentContentWriter.Write("[i]");
if(run.FontUnderlineType != UnderlineType.None)
DocumentContentWriter.Write("[u]");
if(run.FontStrikeoutType != StrikeoutType.None)
DocumentContentWriter.Write("[s]");
if(run.ForeColorIndex != DevExpress.Office.Model.ColorModelInfoCache.EmptyColorIndex)
DocumentContentWriter.Write(string.Format("[color=#{0}]", ColorTranslator.ToHtml(DocumentModel.GetColor(run.ForeColorIndex))));
if(run.DoubleFontSize != DocumentModel.DefaultCharacterProperties.DoubleFontSize)
DocumentContentWriter.Write(string.Format("[size={0}]", Math.Min(run.DoubleFontSize, 39)));
}
DocumentContentWriter.Write(text);
if(!hyperlinkExporting) {
if(run.DoubleFontSize != DocumentModel.DefaultCharacterProperties.DoubleFontSize)
DocumentContentWriter.Write("[/size]");
if(run.ForeColorIndex != DevExpress.Office.Model.ColorModelInfoCache.EmptyColorIndex)
DocumentContentWriter.Write("[/color]");
if(run.FontStrikeoutType != StrikeoutType.None)
DocumentContentWriter.Write("[/s]");
if(run.FontUnderlineType != UnderlineType.None)
DocumentContentWriter.Write("[/u]");
if(run.FontBold)
DocumentContentWriter.Write("[/b]");
if(run.FontItalic)
DocumentContentWriter.Write("[/i]");
}
base.ExportTextRun(run);
}
protected override void ExportFieldCodeStartRun(FieldCodeStartRun run) {
base.ExportFieldCodeStartRun(run);
HyperlinkInfo hyperlinkInfo = GetHyperlinkInfo(run);
if(hyperlinkInfo == null)
return;
DocumentContentWriter.Write(string.Format("[url={0}]", hyperlinkInfo.NavigateUri));
hyperlinkExporting = true;
}
protected override void ExportFieldResultEndRun(FieldResultEndRun run) {
base.ExportFieldResultEndRun(run);
if(GetHyperlinkInfo(run) == null)
return;
DocumentContentWriter.Write("[/url]");
hyperlinkExporting = false;
}
protected override void ExportInlinePictureRun(InlinePictureRun run) {
DocumentContentWriter.Write(string.Format("[img]{0}[/img]", run.Image.Uri));
base.ExportInlinePictureRun(run);
}
protected override ParagraphIndex ExportParagraph(Paragraph paragraph) {
ParagraphIndex pi = base.ExportParagraph(paragraph);
DocumentContentWriter.Write(Environment.NewLine);
return pi;
}
HyperlinkInfo GetHyperlinkInfo(TextRun run) {
RunIndex runIndex = run.GetRunIndex();
Field field = PieceTable.FindFieldByRunIndex(runIndex);
System.Diagnostics.Debug.Assert(field != null);
HyperlinkInfo hyperlinkInfo = null;
if(PieceTable.HyperlinkInfos.TryGetHyperlinkInfo(field.Index, out hyperlinkInfo))
return hyperlinkInfo;
return null;
}
}
public static class BBCodeDocumentFormat {
public static readonly DocumentFormat Id = new DocumentFormat(431);
}
}