There are a couple of posts I have to write. But before doing that I need to reinvestigate the way to format source code. The Codebetter site supports the Google syntax highlighter. Which works great for C# code but up till now I havn’t had any luck with XML of HTML. This post was started as a public experiment to get that working. In the process I have posted it again and again to see what it looks like. Now things are straightened out and the cheat-sheet remains.
To use Syntax highlighter you needs a blogging tool which supports direct editing of the underlying HTML. As a non native speaker I also need a good spelling checker. Both are combined in LiveWriter,
Formatting some C#
- Select the source in Visual studio and copy it to the clipboard
- Paste the code into notepad. This will paste the raw text only and remove any formatting
- Copy the code from notepad to the clipboard
- Go to HTML view
- Paste the code
- Enclose it in a pair of pre tags
<pre class="c-sharp" name="code">
public Term GetOrAddTerm(string word, Language language)
{
Term result = GetTerm(word, language);
if (result == null)
{
result = new Term();
result.Word = word;
result.Language = language;
Save(result);
}
return result;
}
</pre>
And this will be the result.
public Term GetOrAddTerm(string word, Language language)
{
Term result = GetTerm(word, language);
if (result == null)
{
result = new Term();
result.Word = word;
result.Language = language;
Save(result);
}
return result;
}
That’ no big deal. But for HTML or XML it is harder. When pasting the code in HTML view the code itself will turn into markup of the page. Not the intention. To solve this use the textarea tag instead of the pre tag.
The html markup of the post will look like this.
<textarea class=”html” name=”code”>
<table>
<%
foreach (var crumb in Model.BreadCrumbs)
{%>
<tr>
<td>
<%= Html.ActionLink(crumb.Naam, crumb.ID.ToString())%>
</td>
</tr>
<%
} %>
<tr>
<td>
<%= Model.ArtikelGroep.Naam %>
</td>
</tr>
<tr>
<td>
</td>
</tr>
</table>
</textarea>
This is pretty weird. Looks like markup, but it is no markup as it is enclosed between the <textarea> tags. Some pieces of software will trip over this. Like the post editor, which might change the html into escaped html (<). But on the CB site the result looks just great.
In the end it’s quite simple. Having this settled I can start working on the real posts. Stay tuned.
Posted
Tue, Feb 24 2009 9:16 AM
by
pvanooijen