This repository has been archived by the owner on Nov 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlgen.ts
156 lines (153 loc) · 3.8 KB
/
xmlgen.ts
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
151
152
153
154
155
156
/*
* Created by aimozg on 01.11.2017.
* Confidential until published on GitHub
*/
interface TExportResult {
xml:string
}
function exportSection(header:Paragraph,options:LWAPreferences):string {
type RunStyle = ''|'b'|'i'|'bi'|'ib';
const ItalicOn = {
'':'i','b':'bi','i':'i','bi':'bi','ib':'ib'
} as {[index:string]:RunStyle};
const BoldOn = {
'':'b','b':'b','i':'ib','bi':'bi','ib':'ib'
} as {[index:string]:RunStyle};
const ItalicOff = {
'':'','b':'b','i':'','bi':'b','ib':'b'
} as {[index:string]:RunStyle};
const BoldOff = {
'':'','b':'','i':'i','bi':'i','ib':'i'
} as {[index:string]:RunStyle};
const IsItalic = {
'':false,'b':false,'i':true,'bi':true,'ib':true
} as {[index:string]:boolean};
const IsBold = {
'':false,'b':true,'i':false,'bi':true,'ib':true
} as {[index:string]:boolean};
function applyStyle(prev:RunStyle, next:RunStyle):string {
return {
'':{
'':'',
'b':'<b>',
'i':'<i>',
'bi':'<b><i>',
'ib':'<i><b>'
},
'b':{
'':'</b>',
'b':'',
'i':'</b><i>',
'bi':'<i>',
'ib':'</b><i><b>'
},
'i':{
'':'</i>',
'b':'</i><b>',
'i':'',
'bi':'</i><b><i>',
'ib':'<b>'
},
'bi':{
'':'</i></b>',
'b':'</i>',
'i':'</i></b><i>',
'bi':'',
'ib':'</i></b><i><b>'
},
'ib':{
'':'</b></i>',
'b':'</b></i><b>',
'i':'</b>',
'bi':'</b></i><b><i>',
'ib':''
},
}[prev][next]
}
interface Story {
heading:number,
name:string,
content:(Story|string)[],
lastStyle:RunStyle;
}
function myName(par:Paragraph):string {
return par.getText().split('/').reverse()[0];
}
function exportStory(s:Story):string {
let tag:string = 'text';
let body:string = s.content.map(obj=>{
if (typeof obj === 'string') {
return obj;
} else {
tag = 'lib';
return exportStory(obj);
}
}).join('\n') + applyStyle(s.lastStyle,'');
return `<${tag} name="${s.name}">`+'\n'+body+'\n'+`</${tag}>`;
}
let root:Story = {
heading:headingValue(header.getHeading()),
name:myName(header),
content:[],
lastStyle:''
};
let stack:Story[] = [root];
iterateFromHeader(header,par=>{
let top = stack[0];
if (par.getHeading() === DocumentApp.ParagraphHeading.NORMAL) {
let runs = splitParagraph(par);
let s = '';
for (let run of runs) {
let newStyle = top.lastStyle;
if (run.italic && !IsItalic[top.lastStyle]) {
newStyle = ItalicOn[newStyle];
} else if (!run.italic && IsItalic[top.lastStyle]) {
newStyle = ItalicOff[newStyle];
}
if (run.bold && !IsBold[top.lastStyle]) {
newStyle = BoldOn[newStyle];
} else if (!run.bold && IsBold[top.lastStyle]) {
newStyle = BoldOff[newStyle];
}
s += applyStyle(top.lastStyle,newStyle)+run.text;
if (top.lastStyle != newStyle) Logger.log("'"+top.lastStyle+"' -> '"+newStyle+"' by "+applyStyle(top.lastStyle,newStyle));
top.lastStyle = newStyle;
}
s += applyStyle(top.lastStyle,'');
top.lastStyle = '';
top.content.push(s);
} else {
let h = headingValue(par.getHeading());
while (h <= stack[0].heading) {
Logger.log("pop("+stack[0].name+")");
stack.shift();
}
// h > stack.heading
let next:Story = {
heading:h,
name:myName(par),
content:[],
lastStyle:''
};
Logger.log("push("+next.name+")");
stack[0].content.push(next);
stack.unshift(next);
}
});
return exportStory(root);
}
function exportDocument(options:LWAPreferences={}):TExportResult {
Logger.clear();
Logger.log("exportDocument("+JSON.stringify(options)+")");
let doc = DocumentApp.getActiveDocument();
let pos = doc.getCursor();
let par = paragraphOf(pos.getElement());
while (par && par.getHeading() === DocumentApp.ParagraphHeading.NORMAL) {
par = paragraphOf(par.getPreviousSibling());
}
if (!par) return;
let xml = exportSection(par,options);
return {
xml:xml
};
}