views:

94

answers:

1

What I got:

I got a textual representation which my program converts to a much more readable format, especcially for forums, websites and so on.

Why do I need such templage engine

As there are many different forums and blogs, the syntax of each one might be different. Instead of hard-coding the different syntax I would like to generate one class for each of those syntax (preferable extandable with easy modified xml-files) to format my output with the desired syntax.

What I did imagine

For example I need something like

class xyz {
   private string start_bold = "[B]";
   private string end_bold = "[/B]";

   public string bold(string s) {
       return start_bold + s + end_bold;
   }
}

How can I do that the most elegant way? Feel free to edit this question as I'm not entirely sure it's a template engine I need. Just don't got a better word for it now.

Thanks for any help.

Some additional information: Andrew's answer was a great hint, but I don't understand how I could several different styles with this method. Currently I do it the hard way:

string s = String.Format("Output of [B]{0}[b] with number [i]{1}[/i]", 
                          Data.Type,
                          Data.Number);

For this example, I want the output to be designed for a forum. In future I would like to do it like this:

Layout l = new Layout("html");
string s = String.Format("Output of {0} with number {1}, 
                          l.bold(Data.Type),
                          l.italic(Data.Number);
//desired output if style "html" is chosen:
"Output of <b>Name</b> with number <i>5</i>"

//desired output if style "phpbb" is chosen:
"Output of [b]Name[/b] with number [i]5[/i]"

I just don't know how this can be done in the most elegant way.

About the XML: Only the styling conventions should be derived by a xml-document, i.e. adding custom styles without using code.

+1  A: 

I would use exension metods. Then you could call string.bold().

I think this would be the syntax:

class xyz {
   private string start_bold = "[B]";
   private string end_bold = "[/B]";
   public static string bold(this string x) {
       return start_bold + x + end_bold;
   }
}

See: http://msdn.microsoft.com/en-us/library/bb383977.aspx I'm leaving the code below as an example, but I think what you really need is something along the lines of a "token system"

Say you have a string as such:

string s = "I want {~b}this text to be bold{~~b} and {~i}this text to be italics{~~i}"

You XML document should contain these nodes (i think, my xml is kinda rusty)

<site>
  <html>
     <style value="{~b}">[b]</style>
     <style value="{~~b}">[/b]</style> 
     <style value="{~i}">[i]</style>
     <style value="{~~i}">[/i]</style> 
  </html>
  <phpBBCode>

      ......


public class Layout {
               //private string start_bold = "[B]";
               //private string end_bold = "[/B]";
               //private string start_italics = "[I]";
               //private string end_italics = "[/I]";

               private string _stringtoformat;
               public string StringToFormat {set{ _stringtoformat = value;}};//syntax is wrong

               private string _formattedString;
               public string FormattedString {get return _formattedString;}

               public Layout(string formattype, int siteid)
               {
                    //get format type logic here
                    //if(formattype.ToLower() =="html")
                    //{ . . . do something . . . }

                    //call XML Doc for specific site, based upon formattype


                  if(!String.IsNullorEmpty(_stringtoformat))                   
                      {
                      //you will want to put another loop here to loop over all of the custom styles
                         foreach(node n in siteNode)
                          {
                           _stringtoformat.Replace(n.value, n.text); 
                          }
                      }
                      //Sorry, can't write XML document parsing code off the top of my head

                     _formattedString = _stringtoformat;
                }          
               public string bold(this string x) {
                   return start_bold + x + end_bold;
               }
               public string italics(this string x) {
                   return start_italics + x+ end_italics;
               }

            }

IMPLEMENTATION

   Layout l = new Layout("html", siteidorsomeuniqeidentifier);
   l.html = stringtoformat;
   output = l.formattedstring;

The code can be better, but it should give you a kick in the right direction :)

EDIT 2: based upon further info.....

If you want to do this:

Layout l = new Layout("html");
string s = String.Format("Output of {0} with number {1}, 
                          l.bold(Data.Type),
                          l.italic(Data.Number);

and you are looking to change l.bold() and l.italic() based upon the blog engines specific mark up . . .

public class Layout {
           private string start_bold = "[B]";
           private string end_bold = "[/B]";
           private string start_italics = "[I]";
           private string end_italics = "[/I]";

           public Layout(string formattype, int siteid)
           {
           //get format type logic here
           //if(formattype.ToLower() =="html")
           //{ . . . do something . . . }

           //call XML Doc for specific site, based upon formattype
            start_bold = Value.From.XML["bold_start"];
            end_bold = Value.From.XML["bold_end"];
           //Sorry, can't write XML document parsing code off the top of my head
           }          
           public string bold(this string x) {
               return start_bold + x + end_bold;
           }
           public string italics(this string x) {
               return start_italics + x+ end_italics;
           }


        }

Layout l = new Layout("html", siteid);
string s = String.Format("Output of {0} with number {1}, 
                          ValueToBeBoldAsAstring.bold(),
                          ValueToBeItalicAsAstring.italic());
andrewWinn
how can I define which style to use?
BeatMe
you would have serveral public strings, you would concatinate the string fragments after calling .bold() or .italics() or .hyperlink() etc. http://stackoverflow.com/questions/487904/what-advantages-of-extension-methods-have-you-found is a good pointer in the right direction. This assumes you are on c# 3.0 or is it 3.5? I don't remember which.
andrewWinn
@AndrewWinn- it's C# 3.0. The .NET framework version is however 3.5.
RichardOD
@RichardOD - Thanks, I couldn't remember :). Like I said, just getting into extension methods.
andrewWinn
just added some more information in my question.
BeatMe
thanks for spending your time to help me with that :D I think your lates edit fits it perfectly.
BeatMe
I was looking @ the code . . it is wrong and won't run, but its some pretty good looking pseudo code in my book!!If this is the answer please select it. :D Thanks.
andrewWinn