/// <summary> /// Generates 'count' paragraphs of lipsum text. /// </summary> /// <param name="count">The number of paragraphs desired.</param> /// <param name="options">Used to determine the minimum and maximum sentences per paragraphs, and format string if applicable.</param> /// <returns></returns> public string[] GenerateParagraphs(int count, Paragraph options) { /* * TODO: These generate methods could probably be * refactored into one method that takes a count * and a TextFeature. */ string[] paragraphs = new string[count]; string[] sentences = new string[] { }; for (int i = 0; i < count; i++) { /* Get a random amount of sentences based on the * min and max from the paragraph options */ sentences = GenerateSentences(LipsumUtilities. RandomInt((int)options.GetMinimum(), (int)options.GetMaximum())); // Shove them all together in sentence fashion. string joined = String.Join(options.Delimiter, sentences); // Format if allowed. paragraphs[i] = String.IsNullOrEmpty(options.FormatString) ? joined : options.Format(joined); } return(paragraphs); }
/// <summary> /// Instantiates a LipsumGenerator with the passed data. /// </summary> /// <param name="rawData">The data to be used as LipsumText.</param> /// <param name="isXml">Whether the data is in an Xml format and should be parsed for the LipsumText.</param> public LipsumGenerator(string rawData, bool isXml) { /* * If we're receiving an XML string we need to do some * parsing to retrieve the lipsum text. */ this.LipsumText = isXml ? LipsumUtilities.GetTextFromRawXml(rawData) : new StringBuilder(rawData); }
/// <summary> /// Generates the amount of lipsum words. /// </summary> /// <param name="count">The amount of words to generate.</param> /// <returns></returns> public string[] GenerateWords(int count) { string[] words = new string[count]; for (int i = 0; i < count; i++) { words[i] = LipsumUtilities.RandomElement(PreparedWords); } return(words); }
/// <summary> /// Reads raw Xml and grabs the <text> node's inner text. /// </summary> /// <param name="rawXml">The Xml to be parsed. /// It should follow the lipsum.dtd but really all it needs /// a text node as a child of the document element. /// (<root><text>your lipsum text</text></root> /// </param> /// <returns></returns> public static StringBuilder GetTextFromRawXml(string rawXml) { StringBuilder text = new StringBuilder(); XmlDocument data = LipsumUtilities.LoadXmlDocument(rawXml); XmlNode textNode = data.DocumentElement.SelectSingleNode("text"); if (textNode != null) { text.Append(textNode.InnerText); } return(text); }
/// <summary> /// Generates 'count' sentences of lipsum text. /// If options.FormatString is not null or empty that string used to format the sentences. /// </summary> /// <param name="count">The number of sentences desired.</param> /// <param name="options">Used to determine the minimum and maximum words per sentence, and format string if applicable.</param> /// <returns></returns> public string[] GenerateSentences(int count, Sentence options) { string[] sentences = new string[count]; string[] words = new string[] { }; for (int i = 0; i < count; i++) { /* Get a random amount of words based on the * min and max from the Sentence options */ words = GenerateWords(LipsumUtilities. RandomInt((int)options.MinimumWords, (int)options.MaximumWords)); // Shove them all together in sentence fashion. string joined = String.Join(options.Delimiter, words); // Format if allowed. sentences[i] = String.IsNullOrEmpty(options.FormatString) ? joined : options.Format(joined); } return(sentences); }
/// <summary> /// Reads raw Xml and grabs the <text> node's inner text. /// </summary> /// <param name="rawXml">The Xml to be parsed. /// It should follow the lipsum.dtd but really all it needs /// a text node as a child of the document element. /// (<root><text>your lipsum text</text></root> /// </param> /// <returns></returns> public static StringBuilder GetTextFromRawXml(string rawXml) { StringBuilder text = new StringBuilder(); #if PORTABLE XDocument data = LipsumUtilities.LoadXmlDocument(rawXml); XElement textNode = data.Root.Element("text"); if (textNode != null) { text.Append(textNode.Value); } #else XmlDocument data = LipsumUtilities.LoadXmlDocument(rawXml); XmlNode textNode = data.DocumentElement.SelectSingleNode("text"); if (textNode != null) { text.Append(textNode.InnerText); } #endif return(text); }
public string RandomWord() { return(LipsumUtilities.RandomElement(PreparedWords)); }
/// <summary> /// Retreives all of the words in the LipsumText as an array. /// </summary> /// <returns></returns> public string[] PrepareWords() { return(LipsumUtilities.RemoveEmptyElements( Regex.Split(this.LipsumText.ToString(), @"\s"))); }