Skip to content

Commit b435f14

Browse files
committed
Merge pull request #905 from Microsoft/langSpecStraightQuotes
Straight quotes in Language Specification
2 parents 49e606a + f505263 commit b435f14

6 files changed

+376
-316
lines changed

Jakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ compileFile(word2mdJs,
294294
file(specMd, [word2mdJs, specWord], function () {
295295
jake.cpR(headerMd, specMd, {silent: true});
296296
var specWordFullPath = path.resolve(specWord);
297-
var cmd = "cscript //nologo " + word2mdJs + ' "' + specWordFullPath + '" >>' + specMd;
297+
var cmd = "cscript //nologo " + word2mdJs + ' "' + specWordFullPath + '" ' + specMd;
298298
console.log(cmd);
299299
child_process.exec(cmd, function () {
300300
complete();
5.51 KB
Binary file not shown.
-3.12 KB
Binary file not shown.

doc/spec.md

Lines changed: 307 additions & 307 deletions
Large diffs are not rendered by default.

scripts/word2md.js

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,33 @@
11
var sys = (function () {
2+
var fileStream = new ActiveXObject("ADODB.Stream");
3+
fileStream.Type = 2;
4+
var binaryStream = new ActiveXObject("ADODB.Stream");
5+
binaryStream.Type = 1;
26
var args = [];
37
for (var i = 0; i < WScript.Arguments.length; i++) {
48
args[i] = WScript.Arguments.Item(i);
59
}
610
return {
711
args: args,
812
createObject: function (typeName) { return new ActiveXObject(typeName); },
9-
write: function (s) { return WScript.StdOut.Write(s); }
13+
write: function (s) {
14+
WScript.StdOut.Write(s);
15+
},
16+
writeFile: function (fileName, data) {
17+
fileStream.Open();
18+
binaryStream.Open();
19+
try {
20+
fileStream.Charset = "utf-8";
21+
fileStream.WriteText(data);
22+
fileStream.Position = 3;
23+
fileStream.CopyTo(binaryStream);
24+
binaryStream.SaveToFile(fileName, 2);
25+
}
26+
finally {
27+
binaryStream.Close();
28+
fileStream.Close();
29+
}
30+
}
1031
};
1132
})();
1233
function convertDocumentToMarkdown(doc) {
@@ -149,6 +170,10 @@ function convertDocumentToMarkdown(doc) {
149170
lastInTable = inTable;
150171
}
151172
function writeDocument() {
173+
var title = doc.builtInDocumentProperties.item(1) + "";
174+
if (title.length) {
175+
write("# " + title + "\n\n");
176+
}
152177
for (var p = doc.paragraphs.first; p; p = p.next()) {
153178
writeParagraph(p);
154179
}
@@ -168,16 +193,19 @@ function convertDocumentToMarkdown(doc) {
168193
findReplace("^19 REF", {}, "[^&](#^&)", {});
169194
doc.fields.toggleShowCodes();
170195
writeDocument();
196+
result = result.replace(/\x85/g, "\u2026");
197+
result = result.replace(/\x96/g, "\u2013");
198+
result = result.replace(/\x97/g, "\u2014");
171199
return result;
172200
}
173201
function main(args) {
174-
if (args.length !== 1) {
175-
sys.write("Syntax: word2md <filename>\n");
202+
if (args.length !== 2) {
203+
sys.write("Syntax: word2md <inputfile> <outputfile>\n");
176204
return;
177205
}
178206
var app = sys.createObject("Word.Application");
179207
var doc = app.documents.open(args[0]);
180-
sys.write(convertDocumentToMarkdown(doc));
208+
sys.writeFile(args[1], convertDocumentToMarkdown(doc));
181209
doc.close(false);
182210
app.quit();
183211
}

scripts/word2md.ts

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ module Word {
103103
export interface Document {
104104
fields: Fields;
105105
paragraphs: Paragraphs;
106+
builtInDocumentProperties: Collection<any>;
106107
close(saveChanges: boolean): void;
107108
range(): Range;
108109
}
@@ -118,14 +119,37 @@ module Word {
118119
}
119120

120121
var sys = (function () {
122+
var fileStream = new ActiveXObject("ADODB.Stream");
123+
fileStream.Type = 2 /*text*/;
124+
var binaryStream = new ActiveXObject("ADODB.Stream");
125+
binaryStream.Type = 1 /*binary*/;
121126
var args: string[] = [];
122127
for (var i = 0; i < WScript.Arguments.length; i++) {
123128
args[i] = WScript.Arguments.Item(i);
124129
}
125130
return {
126131
args: args,
127132
createObject: (typeName: string) => new ActiveXObject(typeName),
128-
write: (s: string) => WScript.StdOut.Write(s)
133+
write(s: string): void {
134+
WScript.StdOut.Write(s);
135+
},
136+
writeFile: (fileName: string, data: string): void => {
137+
fileStream.Open();
138+
binaryStream.Open();
139+
try {
140+
// Write characters in UTF-8 encoding
141+
fileStream.Charset = "utf-8";
142+
fileStream.WriteText(data);
143+
// We don't want the BOM, skip it by setting the starting location to 3 (size of BOM).
144+
fileStream.Position = 3;
145+
fileStream.CopyTo(binaryStream);
146+
binaryStream.SaveToFile(fileName, 2 /*overwrite*/);
147+
}
148+
finally {
149+
binaryStream.Close();
150+
fileStream.Close();
151+
}
152+
}
129153
};
130154
})();
131155

@@ -298,6 +322,10 @@ function convertDocumentToMarkdown(doc: Word.Document): string {
298322
}
299323

300324
function writeDocument() {
325+
var title = doc.builtInDocumentProperties.item(1) + "";
326+
if (title.length) {
327+
write("# " + title + "\n\n");
328+
}
301329
for (var p = doc.paragraphs.first; p; p = p.next()) {
302330
writeParagraph(p);
303331
}
@@ -321,17 +349,21 @@ function convertDocumentToMarkdown(doc: Word.Document): string {
321349

322350
writeDocument();
323351

352+
result = result.replace(/\x85/g, "\u2026");
353+
result = result.replace(/\x96/g, "\u2013");
354+
result = result.replace(/\x97/g, "\u2014");
355+
324356
return result;
325357
}
326358

327359
function main(args: string[]) {
328-
if (args.length !== 1) {
329-
sys.write("Syntax: word2md <filename>\n");
360+
if (args.length !== 2) {
361+
sys.write("Syntax: word2md <inputfile> <outputfile>\n");
330362
return;
331363
}
332364
var app: Word.Application = sys.createObject("Word.Application");
333365
var doc = app.documents.open(args[0]);
334-
sys.write(convertDocumentToMarkdown(doc));
366+
sys.writeFile(args[1], convertDocumentToMarkdown(doc));
335367
doc.close(false);
336368
app.quit();
337369
}

0 commit comments

Comments
 (0)