diff --git a/scripts/word2md.js b/scripts/word2md.js index 0645acbb143..e80275d5b2d 100644 --- a/scripts/word2md.js +++ b/scripts/word2md.js @@ -1,8 +1,16 @@ +// word2md - Word to Markdown conversion tool +// +// word2md converts a Microsoft Word document to Markdown formatted text. The tool uses the +// Word Automation APIs to start an instance of Word and access the contents of the document +// being converted. The tool must be run using the cscript.exe script host and requires Word +// to be installed on the target machine. The name of the document to convert must be specified +// as a command line argument and the resulting Markdown is written to standard output. The +// tool recognizes the specific Word styles used in the TypeScript Language Specification. var sys = (function () { var fileStream = new ActiveXObject("ADODB.Stream"); - fileStream.Type = 2; + fileStream.Type = 2 /*text*/; var binaryStream = new ActiveXObject("ADODB.Stream"); - binaryStream.Type = 1; + binaryStream.Type = 1 /*binary*/; var args = []; for (var i = 0; i < WScript.Arguments.length; i++) { args[i] = WScript.Arguments.Item(i); @@ -17,11 +25,13 @@ var sys = (function () { fileStream.Open(); binaryStream.Open(); try { + // Write characters in UTF-8 encoding fileStream.Charset = "utf-8"; fileStream.WriteText(data); + // We don't want the BOM, skip it by setting the starting location to 3 (size of BOM). fileStream.Position = 3; fileStream.CopyTo(binaryStream); - binaryStream.SaveToFile(fileName, 2); + binaryStream.SaveToFile(fileName, 2 /*overwrite*/); } finally { binaryStream.Close(); @@ -59,6 +69,17 @@ function convertDocumentToMarkdown(doc) { setProperties(replace, replaceOptions); find.execute(findText, false, false, false, false, false, true, 0, true, replaceText, 2); } + function fixHyperlinks() { + var count = doc.hyperlinks.count; + for (var i = 0; i < count; i++) { + var hyperlink = doc.hyperlinks.item(i + 1); + var address = hyperlink.address; + if (address && address.length > 0) { + var textToDisplay = hyperlink.textToDisplay; + hyperlink.textToDisplay = "[" + textToDisplay + "](" + address + ")"; + } + } + } function write(s) { result += s; } @@ -184,14 +205,15 @@ function convertDocumentToMarkdown(doc) { findReplace("<", { style: "Code Fragment" }, "<", {}); findReplace("<", { style: "Terminal" }, "<", {}); findReplace("", { font: { subscript: true } }, "^&", { font: { subscript: false } }); - findReplace("", { style: "Code Fragment" }, "`^&`", { style: -66 }); - findReplace("", { style: "Production" }, "*^&*", { style: -66 }); - findReplace("", { style: "Terminal" }, "`^&`", { style: -66 }); + findReplace("", { style: "Code Fragment" }, "`^&`", { style: -66 /* default font */ }); + findReplace("", { style: "Production" }, "*^&*", { style: -66 /* default font */ }); + findReplace("", { style: "Terminal" }, "`^&`", { style: -66 /* default font */ }); findReplace("", { font: { bold: true, italic: true } }, "***^&***", { font: { bold: false, italic: false } }); findReplace("", { font: { italic: true } }, "*^&*", { font: { italic: false } }); doc.fields.toggleShowCodes(); findReplace("^19 REF", {}, "[^&](#^&)", {}); doc.fields.toggleShowCodes(); + fixHyperlinks(); writeDocument(); result = result.replace(/\x85/g, "\u2026"); result = result.replace(/\x96/g, "\u2013"); @@ -210,3 +232,4 @@ function main(args) { app.quit(); } main(sys.args); +//# sourceMappingURL=file:///c:/ts/scripts/word2md.js.map \ No newline at end of file diff --git a/scripts/word2md.ts b/scripts/word2md.ts index 65b39d6e4f6..ec9ed634b3c 100644 --- a/scripts/word2md.ts +++ b/scripts/word2md.ts @@ -100,9 +100,19 @@ module Word { toggleShowCodes(): void; } + export interface Hyperlink { + address: string; + textToDisplay: string; + range: Range; + } + + export interface Hyperlinks extends Collection { + } + export interface Document { fields: Fields; paragraphs: Paragraphs; + hyperlinks: Hyperlinks; builtInDocumentProperties: Collection; close(saveChanges: boolean): void; range(): Range; @@ -195,6 +205,18 @@ function convertDocumentToMarkdown(doc: Word.Document): string { find.execute(findText, false, false, false, false, false, true, 0, true, replaceText, 2); } + function fixHyperlinks() { + var count = doc.hyperlinks.count; + for (var i = 0; i < count; i++) { + var hyperlink = doc.hyperlinks.item(i + 1); + var address = hyperlink.address; + if (address && address.length > 0) { + var textToDisplay = hyperlink.textToDisplay; + hyperlink.textToDisplay = "[" + textToDisplay + "](" + address + ")"; + } + } + } + function write(s: string) { result += s; } @@ -347,6 +369,8 @@ function convertDocumentToMarkdown(doc: Word.Document): string { findReplace("^19 REF", {}, "[^&](#^&)", {}); doc.fields.toggleShowCodes(); + fixHyperlinks(); + writeDocument(); result = result.replace(/\x85/g, "\u2026");