|
| 1 | +// word2md - Word to Markdown conversion tool |
| 2 | +// |
| 3 | +// word2md converts a Microsoft Word document to Markdown formatted text. The tool uses the |
| 4 | +// Word Automation APIs to start an instance of Word and access the contents of the document |
| 5 | +// being converted. The tool must be run using the cscript.exe script host and requires Word |
| 6 | +// to be installed on the target machine. The name of the document to convert must be specified |
| 7 | +// as a command line argument and the resulting Markdown is written to standard output. The |
| 8 | +// tool recognizes the specific Word styles used in the TypeScript Language Specification. |
| 9 | +var sys = (function () { |
| 10 | + var args = []; |
| 11 | + for (var i = 0; i < WScript.Arguments.length; i++) { |
| 12 | + args[i] = WScript.Arguments.Item(i); |
| 13 | + } |
| 14 | + return { |
| 15 | + args: args, |
| 16 | + createObject: function (typeName) { return new ActiveXObject(typeName); }, |
| 17 | + write: function (s) { return WScript.StdOut.Write(s); } |
| 18 | + }; |
| 19 | +})(); |
| 20 | +function convertDocumentToMarkdown(doc) { |
| 21 | + var result = ""; |
| 22 | + var lastStyle; |
| 23 | + var lastInTable; |
| 24 | + var tableColumnCount; |
| 25 | + var tableCellIndex; |
| 26 | + var columnAlignment = []; |
| 27 | + function reformatSubscripts() { |
| 28 | + var find = doc.range().find; |
| 29 | + find.clearFormatting(); |
| 30 | + find.font.subscript = true; |
| 31 | + var replace = find.replacement; |
| 32 | + replace.clearFormatting(); |
| 33 | + replace.font.subscript = false; |
| 34 | + find.execute("", false, false, false, false, false, true, 0, true, "<sub>^&</sub>", 2); |
| 35 | + } |
| 36 | + function reformatCodeFragments() { |
| 37 | + var find = doc.range().find; |
| 38 | + find.clearFormatting(); |
| 39 | + find.style = "Code Fragment"; |
| 40 | + var replace = find.replacement; |
| 41 | + replace.clearFormatting(); |
| 42 | + replace.style = -66; // Default Paragraph Font |
| 43 | + find.execute("", false, false, false, false, false, true, 0, true, "`^&`", 2); |
| 44 | + } |
| 45 | + function reformatProductions() { |
| 46 | + var find = doc.range().find; |
| 47 | + find.clearFormatting(); |
| 48 | + find.style = "Production"; |
| 49 | + var replace = find.replacement; |
| 50 | + replace.clearFormatting(); |
| 51 | + replace.style = -66; // Default Paragraph Font |
| 52 | + find.execute("", false, false, false, false, false, true, 0, true, "*^&*", 2); |
| 53 | + } |
| 54 | + function reformatTerminals() { |
| 55 | + var find = doc.range().find; |
| 56 | + find.clearFormatting(); |
| 57 | + find.style = "Terminal"; |
| 58 | + var replace = find.replacement; |
| 59 | + replace.clearFormatting(); |
| 60 | + replace.style = -66; // Default Paragraph Font |
| 61 | + find.execute("", false, false, false, false, false, true, 0, true, "`^&`", 2); |
| 62 | + } |
| 63 | + function reformatBoldItalic() { |
| 64 | + var find = doc.range().find; |
| 65 | + find.clearFormatting(); |
| 66 | + find.font.bold = true; |
| 67 | + find.font.italic = true; |
| 68 | + var replace = find.replacement; |
| 69 | + replace.clearFormatting(); |
| 70 | + replace.font.bold = false; |
| 71 | + replace.font.italic = false; |
| 72 | + find.execute("", false, false, false, false, false, true, 0, true, "***^&***", 2); |
| 73 | + } |
| 74 | + function reformatItalic() { |
| 75 | + var find = doc.range().find; |
| 76 | + find.clearFormatting(); |
| 77 | + find.font.italic = true; |
| 78 | + var replace = find.replacement; |
| 79 | + replace.clearFormatting(); |
| 80 | + replace.font.italic = false; |
| 81 | + find.execute("", false, false, false, false, false, true, 0, true, "*^&*", 2); |
| 82 | + } |
| 83 | + function reformatReferences() { |
| 84 | + doc.fields.toggleShowCodes(); |
| 85 | + var find = doc.range().find; |
| 86 | + find.clearFormatting(); |
| 87 | + var replace = find.replacement; |
| 88 | + replace.clearFormatting(); |
| 89 | + find.execute("^19 REF", false, false, false, false, false, true, 0, true, "[^&](#^&)", 2); |
| 90 | + doc.fields.toggleShowCodes(); |
| 91 | + } |
| 92 | + function write(s) { |
| 93 | + result += s; |
| 94 | + } |
| 95 | + function writeTableHeader() { |
| 96 | + for (var i = 0; i < tableColumnCount - 1; i++) { |
| 97 | + switch (columnAlignment[i]) { |
| 98 | + case 1: |
| 99 | + write("|:---:"); |
| 100 | + break; |
| 101 | + case 2: |
| 102 | + write("|---:"); |
| 103 | + break; |
| 104 | + default: |
| 105 | + write("|---"); |
| 106 | + } |
| 107 | + } |
| 108 | + write("|\n"); |
| 109 | + } |
| 110 | + function stripFormattingMarks(text) { |
| 111 | + var i = text.length; |
| 112 | + while (i > 0 && text.charCodeAt(i - 1) < 0x20) |
| 113 | + i--; |
| 114 | + return text.substr(0, i); |
| 115 | + } |
| 116 | + function writeBlockEnd() { |
| 117 | + switch (lastStyle) { |
| 118 | + case "Code": |
| 119 | + write("```\n\n"); |
| 120 | + break; |
| 121 | + case "List Paragraph": |
| 122 | + case "Table": |
| 123 | + case "TOC": |
| 124 | + write("\n"); |
| 125 | + break; |
| 126 | + } |
| 127 | + } |
| 128 | + function writeParagraph(p) { |
| 129 | + var text = p.range.text; |
| 130 | + var style = p.style.nameLocal; |
| 131 | + var inTable = p.range.tables.count > 0; |
| 132 | + var level = 1; |
| 133 | + var sectionBreak = text.indexOf("\x0C") >= 0; |
| 134 | + text = stripFormattingMarks(text); |
| 135 | + if (inTable) { |
| 136 | + style = "Table"; |
| 137 | + } |
| 138 | + else if (style.match(/\s\d$/)) { |
| 139 | + level = +style.substr(style.length - 1); |
| 140 | + style = style.substr(0, style.length - 2); |
| 141 | + } |
| 142 | + if (lastStyle && style !== lastStyle) { |
| 143 | + writeBlockEnd(); |
| 144 | + } |
| 145 | + switch (style) { |
| 146 | + case "Heading": |
| 147 | + case "Appendix": |
| 148 | + var section = p.range.listFormat.listString; |
| 149 | + write("####".substr(0, level) + " <a name=\"" + section + "\"/>" + section + " " + text + "\n\n"); |
| 150 | + break; |
| 151 | + case "Normal": |
| 152 | + if (text.length) { |
| 153 | + write(text + "\n\n"); |
| 154 | + } |
| 155 | + break; |
| 156 | + case "List Paragraph": |
| 157 | + write(" ".substr(0, p.range.listFormat.listLevelNumber * 2 - 2) + "* " + text + "\n"); |
| 158 | + break; |
| 159 | + case "Grammar": |
| 160 | + write("  " + text.replace(/\s\s\s/g, " ").replace(/\x0B/g, " \n   ") + "\n\n"); |
| 161 | + break; |
| 162 | + case "Code": |
| 163 | + if (lastStyle !== "Code") { |
| 164 | + write("```TypeScript\n"); |
| 165 | + } |
| 166 | + else { |
| 167 | + write("\n"); |
| 168 | + } |
| 169 | + write(text.replace(/\x0B/g, " \n") + "\n"); |
| 170 | + break; |
| 171 | + case "Table": |
| 172 | + if (!lastInTable) { |
| 173 | + tableColumnCount = p.range.tables.item(1).columns.count + 1; |
| 174 | + tableCellIndex = 0; |
| 175 | + } |
| 176 | + if (tableCellIndex < tableColumnCount) { |
| 177 | + columnAlignment[tableCellIndex] = p.alignment; |
| 178 | + } |
| 179 | + write("|" + text); |
| 180 | + tableCellIndex++; |
| 181 | + if (tableCellIndex % tableColumnCount === 0) { |
| 182 | + write("\n"); |
| 183 | + if (tableCellIndex === tableColumnCount) { |
| 184 | + writeTableHeader(); |
| 185 | + } |
| 186 | + } |
| 187 | + break; |
| 188 | + case "TOC Heading": |
| 189 | + write("## " + text + "\n\n"); |
| 190 | + break; |
| 191 | + case "TOC": |
| 192 | + var strings = text.split("\t"); |
| 193 | + write(" ".substr(0, level * 2 - 2) + "* [" + strings[0] + " " + strings[1] + "](#" + strings[0] + ")\n"); |
| 194 | + break; |
| 195 | + } |
| 196 | + if (sectionBreak) { |
| 197 | + write("<br/>\n\n"); |
| 198 | + } |
| 199 | + lastStyle = style; |
| 200 | + lastInTable = inTable; |
| 201 | + } |
| 202 | + function writeDocument() { |
| 203 | + var p = doc.paragraphs.first; |
| 204 | + while (p) { |
| 205 | + writeParagraph(p); |
| 206 | + p = p.next(); |
| 207 | + } |
| 208 | + writeBlockEnd(); |
| 209 | + } |
| 210 | + reformatSubscripts(); |
| 211 | + reformatCodeFragments(); |
| 212 | + reformatProductions(); |
| 213 | + reformatTerminals(); |
| 214 | + reformatBoldItalic(); |
| 215 | + reformatItalic(); |
| 216 | + reformatReferences(); |
| 217 | + writeDocument(); |
| 218 | + return result; |
| 219 | +} |
| 220 | +function main(args) { |
| 221 | + if (args.length !== 1) { |
| 222 | + sys.write("Syntax: word2md <filename>\n"); |
| 223 | + return; |
| 224 | + } |
| 225 | + var app = sys.createObject("Word.Application"); |
| 226 | + var doc = app.documents.open(args[0]); |
| 227 | + sys.write(convertDocumentToMarkdown(doc)); |
| 228 | + doc.close(false); |
| 229 | + app.quit(); |
| 230 | +} |
| 231 | +main(sys.args); |
0 commit comments