Created
December 28, 2013 23:16
-
-
Save superic/8165486 to your computer and use it in GitHub Desktop.
Fake Latin Generator in ASP.NET MVC. For more information: http://eric.tumblr.com/post/71437454508/fake-latin-generator-in-asp-net-mvc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Web.Mvc; | |
using System.Text; | |
namespace Web.Extensions | |
{ | |
public static class HtmlHelperExtensions | |
{ | |
#region Private Statics | |
private static int _seed; | |
#endregion | |
#region Extension Methods | |
public static string FakeLatinParagraph(this HtmlHelper helper, int wordCount) | |
{ | |
string[] dictionary = "lorem ipsum dolor sit amet consectetuer adipiscing elit sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat ut wisi enim ad minim veniam quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi Ut wisi enim ad minim veniam quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi".Split(' '); | |
StringBuilder words = new StringBuilder(); | |
Random rand = null; | |
string paragraph = string.Empty; | |
if (HtmlHelperExtensions._seed >= int.MaxValue - 1000) | |
HtmlHelperExtensions._seed = 0; | |
rand = new Random(HtmlHelperExtensions._seed += DateTime.Now.Millisecond); | |
for (int n = 0; n < wordCount; n++) | |
words.AppendFormat("{0} ", dictionary[rand.Next(0, dictionary.Length)]); | |
paragraph = words.ToString(); | |
paragraph = string.Format("{0}{1}.", char.ToUpperInvariant(paragraph[0]), paragraph.Substring(1).TrimEnd()); | |
return paragraph; | |
} | |
public static string FakeLatinParagraphs(this HtmlHelper helper, int paragraphCount, int wordsPerParagraph, string beforeParagraph, string afterParagraph) | |
{ | |
StringBuilder paragraphs = new StringBuilder(); | |
for (int n = 0; n < paragraphCount; n++) | |
{ | |
paragraphs.AppendFormat("\n{0}\n", beforeParagraph); | |
paragraphs.AppendFormat("\t{0}", HtmlHelperExtensions.FakeLatinParagraph(helper, wordsPerParagraph)); | |
paragraphs.AppendFormat("\n{0}\n", afterParagraph); | |
} | |
return paragraphs.ToString(); | |
} | |
public static string FakeLatinTitle(this HtmlHelper helper, int wordCount) | |
{ | |
string title = HtmlHelperExtensions.FakeLatinParagraph(helper, wordCount); | |
title = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(title); | |
title = title.Substring(0, title.Length - 1); // kill period from paragraph | |
return title; | |
} | |
#endregion | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Web; | |
using System.Web.Mvc; | |
// ... whatever other namespaces | |
using Web.Extensions; // YOU NEED ME! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1><%= Html.FakeLatinTitle(5) %></h1> | |
<p> | |
<%= Html.FakeLatinParagraph(500) %> | |
</p> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- SNIPPET OF WEB CONFIG --> | |
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"> | |
<namespaces> | |
<add namespace="System.Web.Mvc"/> | |
<add namespace="System.Web.Mvc.Ajax"/> | |
<add namespace="System.Web.Mvc.Html"/> | |
<add namespace="System.Web.Routing"/> | |
<add namespace="System.Linq"/> | |
<add namespace="System.Collections.Generic"/> | |
<add namespace="Web.Extensions"/> <!-- EXTENSION METHODS NAMESPACE --> | |
</namespaces> | |
</pages> | |
<!-- END SNIPPET OF WEB CONFIG (don't replace your entire web.config file with only this) --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment