-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Language Specification in Markdown format #749
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7d18236
Language Specification in Markdown format
ahejlsberg d6618e1
Removing script tool copyright message
ahejlsberg d2717eb
Fixing formatting glitch
ahejlsberg 061e92b
Formatting fix
ahejlsberg a8514da
Addressing CR feedback
ahejlsberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# TypeScript Language Specification | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
// 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 args = []; | ||
for (var i = 0; i < WScript.Arguments.length; i++) { | ||
args[i] = WScript.Arguments.Item(i); | ||
} | ||
return { | ||
args: args, | ||
createObject: function (typeName) { return new ActiveXObject(typeName); }, | ||
write: function (s) { return WScript.StdOut.Write(s); } | ||
}; | ||
})(); | ||
function convertDocumentToMarkdown(doc) { | ||
var result = ""; | ||
var lastStyle; | ||
var lastInTable; | ||
var tableColumnCount; | ||
var tableCellIndex; | ||
var columnAlignment = []; | ||
function setProperties(target, properties) { | ||
for (var name in properties) { | ||
if (properties.hasOwnProperty(name)) { | ||
var value = properties[name]; | ||
if (typeof value === "object") { | ||
setProperties(target[name], value); | ||
} | ||
else { | ||
target[name] = value; | ||
} | ||
} | ||
} | ||
} | ||
function findReplace(findText, findProps, replaceText, replaceProps) { | ||
var find = doc.range().find; | ||
find.clearFormatting(); | ||
setProperties(find, findProps); | ||
var replace = find.replacement; | ||
replace.clearFormatting(); | ||
setProperties(replace, replaceProps); | ||
find.execute(findText, false, false, false, false, false, true, 0, true, replaceText, 2); | ||
} | ||
function write(s) { | ||
result += s; | ||
} | ||
function writeTableHeader() { | ||
for (var i = 0; i < tableColumnCount - 1; i++) { | ||
switch (columnAlignment[i]) { | ||
case 1: | ||
write("|:---:"); | ||
break; | ||
case 2: | ||
write("|---:"); | ||
break; | ||
default: | ||
write("|---"); | ||
} | ||
} | ||
write("|\n"); | ||
} | ||
function trimEndFormattingMarks(text) { | ||
var i = text.length; | ||
while (i > 0 && text.charCodeAt(i - 1) < 0x20) | ||
i--; | ||
return text.substr(0, i); | ||
} | ||
function writeBlockEnd() { | ||
switch (lastStyle) { | ||
case "Code": | ||
write("```\n\n"); | ||
break; | ||
case "List Paragraph": | ||
case "Table": | ||
case "TOC": | ||
write("\n"); | ||
break; | ||
} | ||
} | ||
function writeParagraph(p) { | ||
var text = p.range.text; | ||
var style = p.style.nameLocal; | ||
var inTable = p.range.tables.count > 0; | ||
var level = 1; | ||
var sectionBreak = text.indexOf("\x0C") >= 0; | ||
text = trimEndFormattingMarks(text); | ||
if (inTable) { | ||
style = "Table"; | ||
} | ||
else if (style.match(/\s\d$/)) { | ||
level = +style.substr(style.length - 1); | ||
style = style.substr(0, style.length - 2); | ||
} | ||
if (lastStyle && style !== lastStyle) { | ||
writeBlockEnd(); | ||
} | ||
switch (style) { | ||
case "Heading": | ||
case "Appendix": | ||
var section = p.range.listFormat.listString; | ||
write("####".substr(0, level) + ' <a name="' + section + '"/>' + section + " " + text + "\n\n"); | ||
break; | ||
case "Normal": | ||
if (text.length) { | ||
write(text + "\n\n"); | ||
} | ||
break; | ||
case "List Paragraph": | ||
write(" ".substr(0, p.range.listFormat.listLevelNumber * 2 - 2) + "* " + text + "\n"); | ||
break; | ||
case "Grammar": | ||
write("  " + text.replace(/\s\s\s/g, " ").replace(/\x0B/g, " \n   ") + "\n\n"); | ||
break; | ||
case "Code": | ||
if (lastStyle !== "Code") { | ||
write("```TypeScript\n"); | ||
} | ||
else { | ||
write("\n"); | ||
} | ||
write(text.replace(/\x0B/g, " \n") + "\n"); | ||
break; | ||
case "Table": | ||
if (!lastInTable) { | ||
tableColumnCount = p.range.tables.item(1).columns.count + 1; | ||
tableCellIndex = 0; | ||
} | ||
if (tableCellIndex < tableColumnCount) { | ||
columnAlignment[tableCellIndex] = p.alignment; | ||
} | ||
write("|" + text); | ||
tableCellIndex++; | ||
if (tableCellIndex % tableColumnCount === 0) { | ||
write("\n"); | ||
if (tableCellIndex === tableColumnCount) { | ||
writeTableHeader(); | ||
} | ||
} | ||
break; | ||
case "TOC Heading": | ||
write("## " + text + "\n\n"); | ||
break; | ||
case "TOC": | ||
var strings = text.split("\t"); | ||
write(" ".substr(0, level * 2 - 2) + "* [" + strings[0] + " " + strings[1] + "](#" + strings[0] + ")\n"); | ||
break; | ||
} | ||
if (sectionBreak) { | ||
write("<br/>\n\n"); | ||
} | ||
lastStyle = style; | ||
lastInTable = inTable; | ||
} | ||
function writeDocument() { | ||
for (var p = doc.paragraphs.first; p; p = p.next()) { | ||
writeParagraph(p); | ||
} | ||
writeBlockEnd(); | ||
} | ||
findReplace("", { font: { subscript: true } }, "<sub>^&</sub>", { font: { subscript: false } }); | ||
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(); | ||
writeDocument(); | ||
return result; | ||
} | ||
function main(args) { | ||
if (args.length !== 1) { | ||
sys.write("Syntax: word2md <filename>\n"); | ||
return; | ||
} | ||
var app = sys.createObject("Word.Application"); | ||
var doc = app.documents.open(args[0]); | ||
sys.write(convertDocumentToMarkdown(doc)); | ||
doc.close(false); | ||
app.quit(); | ||
} | ||
main(sys.args); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do not think we need this file checked in. we just need a Jake target, say "Jake spec" that would build this script and run it on the latest word doc to generate the .md file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, we probably don't need the .js file. I wasn't sure how to modify the jakefile, but once I figure it out I can make it an automated step.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
task generate-diagnostics should be a lot like this work. you will need to first compile scripts\word2md.ts, then run.
here is a sample.. I did not try to run it though :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The word2md tool has to run with cscript.exe and requires Word to be installed on the target machine (it uses the Word Automation APIs). Are we ok with those requirements in the jakefile?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think so. it is not needed as part of the normal build targets (jake local, jake runtests).