Skip to content
Mostey edited this page Apr 5, 2015 · 5 revisions

Introduction

Every Message may contain content elements (implemented as Content.Element) that can be used for formatting plain text. For example, the spoiler BB code tag [spoiler] is used for embedding text into a expander object to hide it from a page refresh.

A Content object's formatting functionality can be used anywhere in elitepvpers where submitted text is interpreted using BB code tags. If it's not, you are able to use the class anyway by passing in a string into the Content constructor or adding Content.Element.PlainText objects manually though.

If you want to get the plain representation of all elements, call Content.ToString() or loop through all elements (Content.Elements) and call the Element.Plain property.

In case you need the source representation (i.e. [URL="http://www.google.de"]Google Link[/URL]), loop through all elements and call the Element.Source property.

Available element types

Base class

Every content element is derived from type Content.Element and therefore inherits 3 properties:

  • Tag as string - The tag that is used for triggering interpretation. B for bold text, for example.
  • Childs as List<Content.Element> - Elements wrapped by this element
  • Plain as string - Plain string representation containing the opening and ending tag, the elements value and the child elements plain representation that is used in requests to transmit the content with the specified formatting

Derived classes

  • Content.Element.PlainText - Default plain text element
  • Content.Element.BoldText
  • Content.Element.ItalicText
  • Content.Element.StruckThrough
  • Content.Element.UnderlinedText
  • Content.Element.CenteredText
  • Content.Element.LeftAlignedText
  • Content.Element.RightAlignedText
  • Content.Element.JustifiedText
  • Content.Element.IndentedText
  • Content.Element.Spoiler - Expander that hides text on page refresh. May have an optional title set (Title property)
  • Content.Element.Image - Embedded image, Url contains the url to the image.
  • Content.Element.Link - Reference to another url, Url contains the other url.
  • Content.Element.Code - Formatted container for code hightling (use the Content property to access the actual code)
  • Content.Element.Quote - The quote author can be set and retrieved using the Author property of type User
  • Content.Element.FontSize - The container for formatting content to be displayed in another font size

Element wrapping

A content element can have multiple content elements as child elements. Multiple elements may be wrapped. For example, if I want to have bold text embedded in a spoiler, I'd use the [B] BB code tag within the [spoiler] tag as follows:

`[spoiler] Plain[B]Bold[/B] Text [/spoiler]`

whereas the phrase Plain is printed without any formatting and the Bold phrase is printed as bold text. The Content.Element.Spoiler variable would now have exactly 3 child elements:

  • Content.Element.PlainText matching Plain
  • Content.Element.BoldText matching Bold
  • Content.Element.PlainText matching Text

that can be accessed using the Content.Element.Spoiler.Childs property.

Using the Content class

A Content object contains a list of elements Content.Elements that can be filled with your custom elements. For example, I have a spoiler and want to place bold text into it. How'd I do that?

new PrivateMessage(session.User, new Content(
                    new List<Content.Element>
                    {
                        new Content.Element.Spoiler()
                        {
                            Childs = new List<Content.Element>() { new Content.Element.BoldText("Bold") }
                        }
                    }), "Hey").Send(session);

Well, it may seem complicated at the first look but trust me, it isn't. This sample code will send a private message to yourself with the content described above. First, we initialize the content element list containing all the specified formattings that will be send. Then, we create a spoiler element since that is what we wanted to have as wrapper. Since the bold text (that is placed straight into the spoiler) will need to be wrapped by the spoiler ([spoiler][B]Bold[/B][/spoiler]), the element is required to be a child element of the Content.Element.Spoiler object. Since this also is a list of content elements, we need to initialize the list in the same as we did earlier.

Adding elements to a Content object

Adding elements to Content objects is by far more readable than creating new Content objects with list initializers:

var content = new Content();
var spoiler = new Content.Element.Spoiler();
spoiler.Childs.Add(new Content.Element.BoldText("Bold"));
content.Elements.Add(spoiler);

The code does nearly the same as the sample in the previous paragraph. This time we haven't used the list initializers. Instead, we created the objects manually and added them to the Content.Elements list.

Filtering

You can of course also filter the Content.Elements list for different content element types. We already implemented some pre-defined properties that filter the list for their types:

  • Content.PlainTexts
  • Content.Spoilers
  • Content.Quotes
  • Content.Images
  • Content.Links
  • Content.BoldTexts
  • Content.ItalicTexts
  • Content.UnderlinedTexts
  • Content.StruckThroughTexts
  • Content.IndentedTexts
  • Content.CenteredTexts
  • Content.LeftAlignedTexts
  • Content.RightAlignedTexts
  • Content.JustifiedTexts
  • Content.FontSizes
  • Content.Codes

These properties are returning all corresponding elements (including child elements) that have been found.

Implementening own elements

As mentioned before, all elements derive from the Content.Element base class. Say, elitepvpers got a super new formatting element that is triggered as follows:

[foo]super cool[/foo]

To be able to use this in the library, we would implement this as follows:

public class Foo : Element
{
    public Foo() :
        base("foo") // foo being the tag name
    { }
}

Note that the default constructor needs to be provided in order to Content.Filter<T>() the Content.Elements list by your own element.

I don't need all this formatting stuff, how can I write messages?

If you don't wish to use the formatting features at all, you can create a Content object by passing in plain text content into the constructor:

var plainContent = new Content("Blahblah");