Skip to content

Instantly share code, notes, and snippets.

@andy-uq
Last active December 28, 2015 07:19
Show Gist options
  • Save andy-uq/7463387 to your computer and use it in GitHub Desktop.
Save andy-uq/7463387 to your computer and use it in GitHub Desktop.
A simple placeholder content generator that can be switched between alternate providers. http://nightmage-blog.azurewebsites.net/simple-content-generator/
public static class Placeholder
{
private static readonly ILoremContent Lorem = new ZombieLorem();
private static readonly ILoremContent AlternateLorem = new KittenLorem();
private static readonly Random _random = new Random();
public static string ImageUrl(int width, int height = 0)
{
if ( height == 0 )
{
height = width;
}
return Lorem.ImageUrl(width, height);
}
public static string Words(int count, bool capitaliseFirstWord = true)
{
return Lorem.Words(count, capitaliseFirstWord);
}
public static string Text(int paragraphCount = 1)
{
if ( paragraphCount < 1 )
paragraphCount = 1;
if ( paragraphCount > 5 )
paragraphCount = 5;
return Lorem.Text(paragraphCount);
}
public static string Title()
{
return Words(_random.Next(2, 5), capitaliseFirstWord: true);
}
}
public abstract class LoremText
{
private class CircularStringArray
{
private readonly string[] _array;
private int _offset;
public CircularStringArray(IEnumerable<string> array)
{
_array = array.ToArray();
}
public IEnumerable<string> Next(int count)
{
while (count > 0)
{
count--;
yield return Next();
}
}
public string Next()
{
string value = _array[_offset];
_offset = (_offset + 1)%_array.Length;
return value;
}
}
private readonly Lazy<CircularStringArray> _paragraphs;
private readonly Lazy<CircularStringArray> _words;
protected LoremText()
{
_paragraphs = new Lazy<CircularStringArray>(() => new CircularStringArray(GetRawLoremText()));
_words = new Lazy<CircularStringArray>(() => new CircularStringArray(GetWordsFromParagraphs(GetRawLoremText())));
}
protected abstract IEnumerable<string> GetRawLoremText();
private IEnumerable<string> GetWordsFromParagraphs(IEnumerable<string> rawLoremText)
{
return rawLoremText
.SelectMany(p =>
Regex.Matches(p, "[a-z]+", RegexOptions.IgnoreCase)
.Cast<Match>()
.Select(m => m.Value.ToLowerInvariant()));
}
public string Text(int paragraphCount)
{
return string.Join(Environment.NewLine, _paragraphs.Value.Next(paragraphCount));
}
public string Words(int count, bool capitaliseFirstWord)
{
return string.Join(" ", NextWords(count, capitaliseFirstWord));
}
private IEnumerable<string> NextWords(int count, bool capitaliseFirstWord)
{
if ( capitaliseFirstWord )
{
var firstWord = _words.Value.Next();
yield return char.ToUpperInvariant(firstWord[0]) + firstWord.Substring(1);
count--;
}
while ( count > 0 )
{
yield return _words.Value.Next();
count--;
}
}
}
public class KittenLorem : LoremText, ILoremContent
{
private static readonly string[] _text = new[]
{
@"Kitten) Ipsum rescue, sleep on your face size meet cat cat. Mix curls age pillow little, watched bed pumpkin hiding rescuing sleep in the sink fight cats nap shed everywhere sleeping muffin nina siamesecalico browsing eat. Found, peaceful psycho short hair attack lived buddy. Judging you, pillow nina love cat found together hot happy cat medium hair siamese cat's standing. Sneak he fire friend cat, muffin lounging pillow love furball nina heart smokey caturday attack meow cat year girlfriend hiss persian happy!",
@"Cat, age kat girlfriend skeptical roll kitten cat! Around house birthday, cat home breeds sunbathe cat sniff friend boy fluff. Psycho chase the red dot this zzz, mom siamese lounging passive best tongue lounging hot aggressive persian dogs. Adoption, friend nap run chuf kitty bites neighbors. Lick, friendly box cat year? Kitten fight brought stowaway, beautiful lady she pillow grazing cramped. Catnip home disapproval success sniff girlfriend classy, purses sleeping kitties year cats petting kitten?",
@"Nina my here, siamesecalico lounging claw classy year siamese. Name nap petting vet around puss and boots cat royal special hairball size success cat, napping pillow kitty cats kitten feel likes dang cats peaceful 9th! Crosseyed cat home meow kitten hendrix pillow keyboard, furball buddy finally roll baby attack kitty reddit best grown. Medium hair, if I fits I sits old judging you stowaway nina cute his sister's.",
@"Hairball kills snuggliest family 9 lives cat size, feline feline run purses kitties boy brought cat. Buddy love, waffles smile buddy sleeping in the sink siamesecalico judging you buddy friends cats. Kitten, here cold eat the grass crosseyed. Sleep on your keyboard other cakeday grazing photobomb redditor my run buddy cat local hiding siamese reddit eyes tux favorite Baxter rip the couch rescue awesomeness sitting socks eyes cat.",
@"Headbutt, room cat keyboard run picture cat hot feed me fun. Best, rescue tubes attack your ankles kitten cat medium hair outside cat happy. Comfortable, amused aww sunbathe cute best kitten judging you. Kitten, cat photobomb mcfuzzy chuf. Sneak buddy home kitty cats rescue cramped sleep on your keyboard basket healing discovered cute, his baby age hiss kitten kitty cat drinks if I fits I sits. Awesomeness her loved little sleep on your keyboard cats lived steal loved redditor happy highness royal cute sleeping fostering muffin flying lady her she's me friend.",
};
public string ImageUrl(int width, int height)
{
return string.Format("http://placekitten.com/{0}/{1}", width, height);
}
protected override IEnumerable<string> GetRawLoremText()
{
return _text;
}
}
public class ZombieLorem : LoremText, ILoremContent
{
private readonly string[] _text = new[]
{
"Zombie ipsum reversus ab viral inferno, nam rick grimes malum cerebro. De carne lumbering animata corpora quaeritis. Summus brains sit​​, morbo vel maleficia? De apocalypsi gorger omero undead survivor dictum mauris. Hi mindless mortuis soulless creaturas, imo evil stalking monstra adventus resi dentevil vultus comedat cerebella viventium. Qui animated corpse, cricket bat max brucks terribilem incessu zomby. The voodoo sacerdos flesh eater, suscitat mortuos comedere carnem virus. Zonbi tattered for solum oculi eorum defunctis go lum cerebro. Nescio brains an Undead zombies. Sicut malus putrid voodoo horror. Nigh tofth eliv ingdead.",
"Cum horribilem walking dead resurgere de crazed sepulcris creaturis, zombie sicut de grave feeding iride et serpens. Pestilentia, shaun ofthe dead scythe animated corpses ipsa screams. Pestilentia est plague haec decaying ambulabat mortuos. Sicut zeder apathetic malus voodoo. Aenean a dolor plan et terror soulless vulnerum contagium accedunt, mortui iam vivam unlife. Qui tardius moveri, brid eof reanimator sed in magna copia sint terribiles undeath legionis. Alii missing oculis aliorum sicut serpere crabs nostram. Putridi braindead odores kill and infect, aere implent left four dead.",
"Lucio fulci tremor est dark vivos magna. Expansis creepy arm yof darkness ulnis witchcraft missing carnem armis Kirkman Moore and Adlard caeruleum in locis. Romero morbo Congress amarus in auras. Nihil horum sagittis tincidunt, zombie slack-jawed gelida survival portenta. The unleashed virus est, et iam zombie mortui ambulabunt super terram. Souless mortuum glassy-eyed oculos attonitos indifferent back zom bieapoc alypse. An hoc dead snow braaaiiiins sociopathic incipere Clairvius Narcisse, an ante? Is bello mundi z?</p>",
"In Craven omni memoria patriae zombieland clairvius narcisse religionis sunt diri undead historiarum. Golums, zombies unrelenting et Raimi fascinati beheading. Maleficia! Vel cemetery man a modern bursting eyeballs perhsaps morbi. A terrenti flesh contagium. Forsitan deadgurl illud corpse Apocalypsi, vel staggering malum zomby poenae chainsaw zombi horrifying fecimus burial ground. Indeflexus shotgun coup de poudre monstra per plateas currere. Fit de decay nostra carne undead. Poenitentiam violent zom biehig hway agite RE:dead pœnitentiam! Vivens mortua sunt apud nos night of the living dead.",
"Whyt zomby Ut fames after death cerebro virus enim carnis grusome, viscera et organa viventium. Sicut spargit virus ad impetum, qui supersumus flesh eating. Avium, brains guts, ghouls, unholy canum, fugere ferae et infecti horrenda monstra. Videmus twenty-eight deformis pale, horrenda daemonum. Panduntur brains portae rotting inferi. Finis accedens walking deadsentio terrore perterritus et twen tee ate daze leighter taedium wal kingdead. The horror, monstra epidemic significant finem. Terror brains sit unum viral superesse undead sentit, ut caro eaters maggots, caule nobis."
};
public string ImageUrl(int width, int height)
{
return string.Format("http://placezombies.com/{0}x{1}", width, height);
}
protected override IEnumerable<string> GetRawLoremText()
{
return _text;
}
}
public interface ILoremContent
{
string ImageUrl(int width, int height);
string Text(int paragraphCount);
string Words(int count, bool capitaliseFirstWord);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment