Skip to content

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 5 commits into from
Sep 25, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified doc/TypeScript Language Specification (Change Markup).docx
Binary file not shown.
Binary file modified doc/TypeScript Language Specification (Change Markup).pdf
Binary file not shown.
Binary file modified doc/TypeScript Language Specification.docx
Binary file not shown.
Binary file modified doc/TypeScript Language Specification.pdf
Binary file not shown.
2 changes: 2 additions & 0 deletions doc/header.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# TypeScript Language Specification

5,274 changes: 5,274 additions & 0 deletions doc/spec.md

Large diffs are not rendered by default.

188 changes: 188 additions & 0 deletions scripts/word2md.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
// word2md - Word to Markdown conversion tool
Copy link
Contributor

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.

Copy link
Member Author

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.

Copy link
Contributor

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 :)

var docDirectory = "doc/";

var word2MDJS = path.join(scriptsDirectory, "word2md.js");
var word2MDTS = path.join(scriptsDirectory, "word2md.ts");
var specWord = path.join(docDirectory, "TypeScript Language Specification.docx");
var specMD = path.join(docDirectory, "spec.ms");

file(processDiagnosticMessagesTs)

// processDiagnosticMessages script
compileFile(word2MDJS,
            [word2MDTS],
            [word2MDJS],
            [],
            false);

// The generated diagnostics map; built for the compiler and for the 'generate-diagnostics' task
file(specMD, [word2MDJS, specWord], function () {
    var cmd = "node " + word2MDJS + " "  + specWord;
    console.log(cmd);
    var ex = jake.createExec([cmd]);
    // Add listeners for output and error
    ex.addListener("stdout", function(output) {
        process.stdout.write(output);
    });
    ex.addListener("stderr", function(error) {
        process.stderr.write(error);
    });
    ex.addListener("cmdEnd", function() {
        complete();
    });
    ex.run();    
}, {async: true})


desc("Generates a markdown version of the language specificiation");
task("generate-spec", [specMD])

Copy link
Member Author

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?

Copy link
Contributor

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).

//
// 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("&emsp;&emsp;" + text.replace(/\s\s\s/g, "&emsp;").replace(/\x0B/g, " \n&emsp;&emsp;&emsp;") + "\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);
Loading