CSharpFeeds - All your C# feeds in one place.

Sponsors

Friday, December 08, 2006

CS Dev Guide: TextTokens

by Keyvan Nayyeri via Keyvan Nayyeri on 12/8/2006 6:10:07 PM

TextToken is another feature in Community Server and has special implementations in some applications.  For example TextParts (which are now renamed to Snippets in Community Server 2.1) are their implementation in weblog application.  I can remember this feature in .Text, too.

If you want to know what they do, they let you to use some text keys and corresponding values in your application or specific sections.  When you put one of these key texts in your post bodies, they will be replaced with their corresponding value.  This saves you from typing a long texts and helps you to automatically put a link for a text without typing it for each insertion.

In this CS Dev Guide post I want to talk about TextTokens in Community Server APIs.

CommunityServer.Components.TextToken class and static methods in CommunityServer.Components.TextTokens are main APIs to work with TextTokens.

TextToken class provides six properties:

  • Link: An optional Url for the case when you want to put a link when text is being replaced.
  • Pattern: Returns a Regular Expression based on your TextToken values.  You can use this Regular Expression to find any matches in post bodies.
  • SectionID: Identifier of section where TextToken is defined in.
  • Text: String value of text that should be inserted instead of token values.
  • Token: String value of text that should be replaced with a new value.
  • TokenID: Identifier of TextToken.

This class also provides a FormatText() function which lets you to format any string value (body of your posts) based on TextToken values.  It returns a formatted text.

Static methods in CommunityServer.Components.TextTokens namespace let you to get TextTokens for a section as well as update, add or delete a TextToken object.  There is also a Format() method which gets an ITextToken and a string value and formats this text based on TextToken then returns the formatted text.

Let's discover some examples.

GetFormattedBody() function in below code returns the formatted body of current post.  It gets all TextTokens for current section and formats post body using these TextTokens.

String GetFormattedBody()

{

    CSContext context = CSContext.Current;

 

    String formattedBody = context.Post.Body;

 

    ArrayList tokens = TextTokens.GetTokens(context.SectionID);

    foreach (TextToken token in tokens)

    {

        formattedBody = token.FormatText(formattedBody);

    }

 

    return formattedBody;

}

In second example UpdateAllTokens() method updates all tokens for current section and adds a constant link to all tokens.

void UpdateAllTokens()

{

    CSContext context = CSContext.Current;

 

    ArrayList tokens = TextTokens.GetTokens(context.SectionID);

    foreach (TextToken token in tokens)

    {

        token.Link = "http://nayyeri.net";

        TextTokens.Update(token);

    }

}

And finally in the last example AddNewToken() method adds a new token to current section.

void AddNewToken()

{

    CSContext context = CSContext.Current;

 

    TextToken textToken = new TextToken();

    textToken.Token = "CS";

    textToken.Text = "Community Server";

    textToken.SectionID = context.SectionID;

    textToken.Link = "http://communityserver.org";

 

    TextTokens.Add(textToken);

}

In near future I'll write about one of common implementations of TextTokens in weblog application, named TextPart (Snippet).

email it!bookmark it!digg it!

Original Post: CS Dev Guide: TextTokens

Subscribe

New Feed

Product Spotlight

Recently Updated Sources

Legal Note

The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.

Advertise with us