Tag Archives: C#

Forum to Blog post converter

I have been trying to copy over my build thread posts from TBAJA over to my blog. This was not an easy task since forums use a special syntax for test formatting, while wordpress uses HTML. I started off doing this task by hand. but after a few posts, I was thinking there was an easier way.

I decided the write a simple program in C# that could read a file containing a forum post and convert it into HTML.

* converter.cs
* @date June 27, 2012
* @author rickbarrette@gmail.com
* Copyright 2012 Richard Barrette
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* See the License for the specific language governing permissions and
* limitations under the License

using System;
using System.IO;
using System.Text.RegularExpressions;

* This is a simple program that I will use to convert my build thread posts
* from an online forum to a format that is usable by wordpress
namespace Converter {
class Program{

* Main Method
static void Main(string[] args) {
try {
Console.WriteLine("Ricky's Build Thread Converter");

// Read the post file that is in the current working dir
                 string FILE = @"post";
string text = File.ReadAllText(FILE);

// IMG
Regex regex = new Regex(@"(?<A>[img width=d+ height=d+])(?<IMG>.*)(?<B>[/img])", RegexOptions.IgnoreCase);
text = regex.Replace(text, "<img class="alignnone" src="" + "${IMG}" + ""alt="" width="1010" height="758" />");

// IMG
regex = new Regex(@"(?<A>[img height=d+ width=d+])(?<IMG>.*)(?<B>[/img])", RegexOptions.IgnoreCase);
text = regex.Replace(text, "<img class="alignnone" src="" + "${IMG}" + ""alt="" width="1010" height="758" />");

// IMG
regex = new Regex(@"(?<A>[img])(?<IMG>.*)(?<B>[/img])", RegexOptions.IgnoreCase);
text = regex.Replace(text, "<img class="alignnone" src="" + "${IMG}" + ""alt="" width="1010" height="758" />");

// underline [u]
regex = new Regex(@"(?<A>[u])(?<B>.*)(?<C>[/u])", RegexOptions.IgnoreCase);
text = regex.Replace(text, "<u>" + "${B}" + "</u>");

// bold [b]
regex = new Regex(@"(?<A>[b])(?<B>.*)(?<C>[/b])", RegexOptions.IgnoreCase);
text = regex.Replace(text, "<strong>" + "${B}" + "</strong>");

// URL [url=http://]text[/url]
regex = new Regex(@"(?<A>[url=)(?<URL>.*)(?<B>])(?<TEXT>.*)(?<C>[/url])", RegexOptions.IgnoreCase);
text = regex.Replace(text, "<a href="" + "${URL}" +"">" + "${TEXT}" +"</a>");

// URL [url]http://[/url]
regex = new Regex(@"(?<A>[url])(?<URL>.*)(?<B>[/url])", RegexOptions.IgnoreCase);
text = regex.Replace(text, "<a href="" + "${URL}" +"">" + "${URL}" +"</a>");

//write the changes to the file
File.WriteAllText(FILE, text);
catch { }

you can download a compiled version of my converter here. To use simply create a file called post (with no file extension) and run the program. I run the program from the command prompt. Remember that the post file has to be in the same working directory as the program.