package website.controller;
import haxe.DynamicAccess;
import haxe.Json;
import haxelib.SemVer;
import ufront.MVC;
import website.api.ProjectApi;
import highlighter.Highlighter;
import haxe.ds.Option;
import markdown.AST;
import Markdown;
using tink.CoreApi;
using haxe.io.Path;
using CleverSort;
using Lambda;
using DateTools;
using StringTools;
class ProjectController extends Controller {
@inject public var projectApi:ProjectApi;
@:route("/$projectName")
public function project( projectName:String ) {
var info = projectApi.projectInfo( projectName ).sure();
return version( projectName );
}
@:route("/$projectName/versions/")
public function versionList( projectName:String ) {
var info = projectApi.projectInfo( projectName ).sure();
info.versions.sort(function(v1, v2) return SemVer.compare(v2.name, v1.name));
return new ViewResult({
title: 'All versions of $projectName',
description: info.desc,
project: projectName,
escape: function(str:String) return StringTools.htmlEscape(str, true),
allVersions: info.versions,
info: info,
});
}
function markdownToHtml(markdown:String, prefix:String) {
// this function is basically a copy of Markdown.markdownToHtml
// default md->html rendering function, but it adds a filter that
// fixes relative URLs in IMG tags
try {
var imgSrcfixer = new MarkdownImgRelativeSrcFixer(prefix);
var document = new Document();
var lines = ~/(\r\n|\r)/g.replace(markdown, '\n').split("\n");
document.parseRefLinks(lines);
var blocks = document.parseLines(lines);
for (block in blocks) block.accept(imgSrcfixer); // fix relative image links
var html = Markdown.renderHtml(blocks);
// prefix relative image urls in generated html (for plain
-tags)
var regexp = ~/
$e';
}
}
@:route("/$projectName/$semver/download/")
public function download( projectName:String, semver:String ) {
var zipFile = projectApi.getZipFilePath( projectName, semver );
return new RedirectResult( "/" + zipFile, true );
}
@cacheRequest
@:route("/$projectName/$semver/doc/$typePath")
public function docs( projectName:String, semver:String, ?typePath:String ) {
return new ViewResult({
title: 'View project $projectName docs for $typePath',
});
}
@cacheRequest
@:route("/$projectName/$semver/files/*")
public function file( projectName:String, semver:String, rest:Array ) {
var filePath = rest.join("/");
var downloadLink = baseUri+'$projectName/$semver/raw-files/$filePath';
var info = projectApi.projectInfo( projectName ).sure();
if ( semver==null )
semver = info.curversion;
var currentVersion = info.versions.find( function(v) return v.name==semver );
if ( currentVersion==null )
throw HttpError.pageNotFound();
var data:TemplateData = {
title: 'Viewing $filePath on $projectName:$semver',
project: projectName,
description: info.desc,
info: info,
allVersions: info.versions,
versionDate: Date.fromString(currentVersion.date).format('%F'),
version: semver,
size: null,
fileParts: rest,
filePath: filePath,
fileExt: Path.extension(filePath),
downloadLink: downloadLink,
icon: function(file:String) {
return switch Path.extension(file) {
case "zip"|"rar"|"tar.gx": "file-archive-o";
case "pdf": "file-pdf-o";
case "hx"|"hxml": "file-code-o";
case "jpg"|"jpeg"|"gif"|"png"|"svg"|"ico": "file-image-o";
case "txt"|"md"|"mdown"|"markdown": "file-text-o";
case other: "file-o";
}
},
type: "download",
};
switch projectApi.getInfoForPath( projectName, semver, filePath ).sure() {
case Directory(dirs,files):
data["type"] = "directory";
data["dirListing"] = dirs;
data["fileListing"] = files;
data["currentDir"] = baseUri+'$projectName/$semver/files/$filePath'.removeTrailingSlashes();
case Text(str,ext,size):
if ( ["md","mdown","markdown"].indexOf(ext)>-1 ) {
str = markdownToHtml( str, '/p/$projectName/$semver/raw-files/' );
str = str.replace("