diff --git a/run.n b/run.n index f1fa7906e..3e3ed35b7 100644 Binary files a/run.n and b/run.n differ diff --git a/script/src/RunScript.hx b/script/src/RunScript.hx index d1f950d7f..95a23e0c1 100644 --- a/script/src/RunScript.hx +++ b/script/src/RunScript.hx @@ -115,6 +115,31 @@ class RunScript { } + } else if (target == "documentation") { + + var wiki = null; + var output = null; + + for (define in defines) { + + if (StringTools.startsWith (define, "-Dwiki=")) { + + wiki = define.substr (define.indexOf ("=") + 1); + + } else if (StringTools.startsWith (define, "-Doutput=")) { + + output = define.substr (define.indexOf ("=") + 1); + + } + + } + + if (wiki != null && output != null) { + + copyDocumentation (wiki, output); + + } + } else if (target == "clean") { var directories = [ PathHelper.combine (path, "obj") ]; @@ -546,6 +571,135 @@ class RunScript { } + private static function copyDocumentation (source:String, output:String):Void { + + var sourcePaths = PathHelper.readDirectory (source, [ ".git" ]); + var targetPaths = []; + var names = []; + var titles = []; + + for (sourcePath in sourcePaths) { + + var path = sourcePath.substr (source.length); + var components = path.split ("/"); + var targetPath = ""; + var name = ""; + var title = ""; + + for (i in 0...components.length - 1) { + + var component = components[i]; + var index = component.lastIndexOf (".-"); + + if (index > -1) { + + targetPath += component.substr (index + 2).toLowerCase () + "/"; + + } else if (component != "") { + + targetPath += component + "/"; + + } + + } + + var lastComponent = components[components.length - 1]; + var index = lastComponent.lastIndexOf (".-"); + name = lastComponent.substr (0, lastComponent.length - 3); + + if (index > -1) { + + var trim = name.substr (index + 2); + title = StringTools.replace (trim, "-", " "); + targetPath += trim.toLowerCase () + "/index.md"; + + } else { + + title = StringTools.replace (lastComponent, "-", " "); + targetPath += lastComponent; + + } + + targetPaths.push (targetPath); + names.push (name); + titles.push (title); + + } + + var links = ~/\[\[(.+)\]\]/g; + + for (i in 0...sourcePaths.length) { + + var sourcePath = sourcePaths[i]; + var targetPath = targetPaths[i]; + var outputPath = PathHelper.combine (output, targetPath); + var title = titles[i]; + + Sys.println (outputPath); + PathHelper.mkdir (Path.directory (outputPath)); + + var content = File.getContent (sourcePath); + + while (links.match (content)) { + + var link = links.matched (0); + link = link.substr (2, link.length - 4); + var components = link.split ("|"); + + var title = ""; + var target = ""; + + if (components.length > 1) { + + title = components[0]; + target = components[1]; + + } else { + + title = target = components[0]; + + } + + if (StringTools.endsWith (target, ".md")) { + + target = target.substr (0, target.length - 3); + + } + + var replacement = "[" + title + "](/documentation/)"; + + for (i in 0...names.length) { + + if (target == names[i]) { + + var path = targetPaths[i]; + path = path.substr (0, path.lastIndexOf ("/")); + + replacement = "[" + title + "](/documentation/" + path + ")"; + break; + + } + + } + + content = links.matchedLeft () + replacement + links.matchedRight (); + + } + + var output = File.write (outputPath, false); + output.writeString ("---\n"); + output.writeString ("layout: default\n"); + output.writeString ("title: " + title + "\n"); + output.writeString ("---\n\n"); + output.writeString ("# " + title + "\n\n"); + output.writeString (content); + output.close (); + + } + + } + + private static function downloadFile (remotePath:String, localPath:String) { var out = File.write (localPath, true);