| 1 |
package website.controller; |
| 2 |
|
| 3 |
import haxelib.server.SiteDb; |
| 4 |
import ufront.web.Controller; |
| 5 |
import ufront.web.result.ContentResult; |
| 6 |
import website.api.ProjectListApi; |
| 7 |
using StringTools; |
| 8 |
using DateTools; |
| 9 |
using tink.CoreApi; |
| 10 |
|
| 11 |
class RSSController extends Controller { |
| 12 |
|
| 13 |
@inject public var projectListApi:ProjectListApi; |
| 14 |
|
| 15 |
@:route("/") |
| 16 |
public function rss( ?args:{ number:Int } ) { |
| 17 |
var number = (args.number!=null) ? args.number : 20; |
| 18 |
var releases = projectListApi.latest( number ).sure(); |
| 19 |
var rss = buildRss( releases ); |
| 20 |
var content = '<?xml version="1.0" encoding="UTF-8"?>'+rss.toString(); |
| 21 |
return new ContentResult( content, "text/xml" ); |
| 22 |
} |
| 23 |
|
| 24 |
function buildRss( releases:Array<{v:Version, p:Project}> ):Xml { |
| 25 |
// Helpers for building the XML |
| 26 |
var createChild = function(root:Xml, name:String){ |
| 27 |
var c = Xml.createElement( name ); |
| 28 |
root.addChild( c ); |
| 29 |
return c; |
| 30 |
} |
| 31 |
var createChildWithContent = function(root:Xml, name:String, content:String){ |
| 32 |
var e = Xml.createElement( name ); |
| 33 |
var c = Xml.createPCData( if (content != null) content else "" ); |
| 34 |
e.addChild( c ); |
| 35 |
root.addChild( e ); |
| 36 |
return e; |
| 37 |
} |
| 38 |
var createChildWithCdata = function(root:Xml, name:String, content:String){ |
| 39 |
var e = Xml.createElement( name ); |
| 40 |
var c = Xml.createCData( if (content != null) content else "" ); |
| 41 |
e.addChild( c ); |
| 42 |
root.addChild( e ); |
| 43 |
return e; |
| 44 |
} |
| 45 |
|
| 46 |
// Set some variables we'll use. |
| 47 |
Sys.setTimeLocale( "en_US.UTF8" ); |
| 48 |
var hostName = context.request.hostName; |
| 49 |
var url = "https://"+hostName; |
| 50 |
var num = releases.length; |
| 51 |
|
| 52 |
// Create the RSS document and headers. |
| 53 |
var rss = Xml.createElement( "rss" ); |
| 54 |
rss.set( "version", "2.0" ); |
| 55 |
rss.set( "xmlns:atom", "http://www.w3.org/2005/Atom" ); |
| 56 |
rss.set( "xmlns:dc", "http://purl.org/dc/elements/1.1/" ); |
| 57 |
var channel = createChild( rss, "channel" ); |
| 58 |
var link = createChild( channel, "atom:link" ); |
| 59 |
link.set( "href", 'https://$hostName/rss/' ); |
| 60 |
link.set( "rel", "self" ); |
| 61 |
link.set( "type", "application/rss+xml" ); |
| 62 |
createChildWithContent( channel, "title", 'Latest Haxelib Releases ($hostName)' ); |
| 63 |
createChildWithContent( channel, "link", url ); |
| 64 |
createChildWithContent( channel, "description", 'The latest $num haxelib releases on $hostName' ); |
| 65 |
createChildWithContent( channel, "generator", "haxe" ); |
| 66 |
createChildWithContent( channel, "language", "en" ); |
| 67 |
|
| 68 |
// Create the various RSS entries. |
| 69 |
for ( release in releases ) { |
| 70 |
var version = release.v; |
| 71 |
var project = release.p; |
| 72 |
var item = createChild(channel, "item"); |
| 73 |
var title = '${project.name} ${version.toSemver()}'; |
| 74 |
var description = '<p>${version.comments.htmlEscape()}</p><hr/><p>${project.description.htmlEscape()}</p>'; |
| 75 |
createChildWithContent( item, "title", title.htmlEscape() ); |
| 76 |
createChildWithContent( item, "link", url+"/p/"+project.name ); |
| 77 |
createChildWithContent( item, "guid", url+"/p/"+project.name+"?v="+version.id ); |
| 78 |
var date = Date.fromString( version.date ).format( "%a, %e %b %Y %H:%M:%S %z" ); |
| 79 |
createChildWithContent( item, "pubDate", date ); |
| 80 |
createChildWithContent( item, "dc:creator", project.ownerObj.name ); |
| 81 |
createChildWithContent( item, "description", description ); |
| 82 |
} |
| 83 |
|
| 84 |
return rss; |
| 85 |
} |
| 86 |
} |