rank/getRank/HtmlOut.cs

168 строки
4.4 KiB
C#
Исходник Постоянная ссылка Обычный вид История

2010-09-01 03:24:03 +04:00
//
// HtmlOut.cs: Grabs data from the template and formats the user output.
//
// Author:
// David Mulder (dmulder@novell.com)
//
2011-02-19 01:30:36 +03:00
// Copyright (C) 2011 Novell, Inc (www.novell.com)
2010-09-01 03:24:03 +04:00
//
2010-09-01 02:51:13 +04:00
using System;
using System.IO;
using System.Reflection;
using Rankdblib;
using Gravatarlib;
2010-09-01 02:51:13 +04:00
namespace getRank
{
public class HtmlOut
{
private static string directory = "";
2010-09-01 02:51:13 +04:00
internal static string header = "";
private static string userData = "";
2010-09-01 02:51:13 +04:00
internal static string footer = "";
private static string userHeader = "";
private static string projData = "";
private static string userFooter = "";
internal static string TemplateDirectory
2010-09-01 02:51:13 +04:00
{
get
{
return directory;
}
set
{
if (value == "")
{
directory = Directory.GetCurrentDirectory() + "/htdocs/";
}
else
{
directory = value;
}
}
2010-09-01 02:51:13 +04:00
}
2010-09-01 03:24:03 +04:00
/// <summary>
/// Reads the template and sections of the template to header, footer, and user strings.
/// </summary>
2010-09-01 02:51:13 +04:00
internal static void Template()
{
StreamReader read = File.OpenText(directory + "/template.html");
2010-09-01 02:51:13 +04:00
string template = "";
while (!read.EndOfStream)
{
template += read.ReadLine() + Environment.NewLine;
}
int front_user = template.IndexOf("<!-- Begin User -->");
int aft_front_user = front_user + 19;
int end_user = template.IndexOf("<!-- End User -->");
int aft_end_user = end_user + 17;
header = template.Substring(0, front_user);
2010-10-28 01:05:56 +04:00
userData = template.Substring(front_user + 19, end_user - aft_front_user);
2010-09-01 02:51:13 +04:00
footer = template.Substring(aft_end_user, template.Length - aft_end_user);
2010-10-28 01:05:56 +04:00
UserTemplate(userData);
2010-09-01 02:51:13 +04:00
}
2010-09-01 03:24:03 +04:00
/// <summary>
/// Creates an html instance of a user.
/// </summary>
/// <param name="name">
/// User's name <see cref="System.String"/>
/// </param>
/// <param name="email">
/// User's e-mail address <see cref="System.String"/>
/// </param>
/// <param name="rank">
/// User's rank <see cref="System.Int32"/>
/// </param>
/// <param name="code">
/// User's contributed lines of code <see cref="System.Int32"/>
/// </param>
/// <returns>
/// The html string representing the user <see cref="System.String"/>
/// </returns>
internal static string UserRank(Users user, int rank)
2010-09-01 02:51:13 +04:00
{
string data = ReplaceKeywords(userHeader, user, rank);
foreach (Projects proj in user.projects)
2010-10-28 01:05:56 +04:00
{
data += ReplaceKeywords(projData, user, rank)
.Replace("<!-- Project -->", proj.name)
2010-12-02 01:54:44 +03:00
.Replace("<!-- projCode -->", "+" + proj.CodeAdded.ToString() + " -" + proj.CodeRemoved.ToString())
.Replace("<!-- BluePercent -->", proj.BluePercent().ToString());
2010-10-28 01:05:56 +04:00
}
for (int i = 0; i < (4 - user.projects.Count); i++)
{
data += "<td></td>";
}
2010-12-02 01:54:44 +03:00
data += ReplaceKeywords(userFooter, user, rank);
2010-10-28 01:05:56 +04:00
return data;
}
private static string ReplaceKeywords(string html, Users user, int rank)
{
Gravatar img = new Gravatar(user.email[0], Gravatar.IconSets.identicon, Gravatar.Ratings.g, 50);
2010-12-23 21:02:05 +03:00
string gravatar = img.GravatarURL();
return html.Replace("<!-- rank -->", rank.ToString())
2011-01-29 02:15:16 +03:00
.Replace("<!-- name -->", user.Name)
.Replace("<!-- code -->", user.CodeAdded().ToString())
.Replace("<!-- email -->", user.email[0])
2010-12-23 21:02:05 +03:00
.Replace("<!-- Gravatar -->", gravatar)
.Replace("<!-- Image -->", ReplaceImageName(user))
.Replace("<!-- score -->", user.Score().ToString())
.Replace("<!-- commits -->", user.CommitCount().ToString());
}
private static string ReplaceImageName(Users user)
{
2010-12-19 01:52:46 +03:00
int GROVE = 50000;
int TREE = 5000;
int CRATE = 500;
int BUNCHES = 50;
int BUNCH = 0;
int PEEL = 0;
2010-12-17 23:45:15 +03:00
if (user.Score() > GROVE)
{
2010-12-17 23:45:15 +03:00
return "grove.png";
}
else if (user.Score() > TREE)
{
return "banana_tree.png";
}
else if (user.Score() > CRATE)
{
2010-12-17 19:33:47 +03:00
return "crate.png";
}
else if (user.Score() > BUNCHES)
{
return "banana_bunches.png";
}
else if (user.Score() > BUNCH)
{
return "3_bananas.png";
}
else if (user.Score() == PEEL)
{
return "peel.png";
}
return "";
}
2010-10-28 01:05:56 +04:00
private static void UserTemplate(string template)
{
int front_user = template.IndexOf("<!-- Data -->");
int aft_front_user = front_user + 13;
int end_user = template.IndexOf("<!-- End Data -->");
int aft_end_user = end_user + 17;
userHeader = template.Substring(0, front_user);
projData = template.Substring(front_user + 13, end_user - aft_front_user);
userFooter = template.Substring(aft_end_user, template.Length - aft_end_user);
2010-09-01 02:51:13 +04:00
}
}
}