Code Snippets in Windows Live Writer

I was asked how I insert code snippets with Windows Live Writer. I actually did it the hard way. I wrote a Windows Live Writer plug-in, and then I found out someone had already done the same thing.

I originally got scared away from it because when I switch to the HTML view in Live Writer, it shows up like this:

<div class="wlWriterEditableSmartContent" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:19d17d14-ac74-45e7-9276-5c6a3031b09d" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"><DIV class=dp-highlighter>
<DIV class=bar>
<DIV class=tools><A onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;" href="about:blank#">view plain</A><A onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;" href="about:blank#">copy to clipboard</A><A onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;" href="about:blank#">print</A><A onclick="dp.sh.Toolbar.Command('About',this);return false;" href="about:blank#">?</A></DIV></DIV>
<OL class=dp-xml>
<LI class=alt><SPAN><SPAN>test&nbsp;&nbsp;</SPAN></SPAN></LI></OL></DIV><PRE class=xml style="DISPLAY: none" name="code">test</PRE>

That is just unacceptable. Fortunately, I realized that it just renders that temporarily, so that you can see a better preview of what it will actually look like on your site.

When you post it, it still has the annoying div above the pre, but I can live with that. All of the other added tags are not there, and you just get your code wrapped in a nice pre. I’m assuming it’s there so that it recognizes that as a code block if you want to edit it later.

I like the SyntaxHighlighter code because it uses JavaScript to parse apart the code and add in the appropriate styles. It’s a nice separation between content and presentation.

I do recommend that if you use the SyntaxHighlighter, that you combine the JavaScript files into one, and then run it through a JavaScript compressor. That will minimize file sizes and page requests.

For example, in this blog, I simply include this in each page:

<link href='http://www.young-technologies.com/sh/SyntaxHighlighter.css' rel='stylesheet' type='text/css'/>
<script src='http://www.young-technologies.com/sh/sh.js' type='text/javascript'></script>

Before the body tag, I then use this code:

<script type='text/javascript'>
	dp.SyntaxHighlighter.ClipboardSwf = 'http://www.young-technologies.com/sh/clipboard.swf';
	dp.SyntaxHighlighter.HighlightAll('code');
</script>

This has been working great on my site. Hope that helps you out!

Kick It!

1 Comment so far »

  1. shailesh said,

    Wrote on January 2, 2010 @ 7:54 am

    Hi,

    Actually I am developing one plug for windows live writer. for that I made this class
    using System;
    using System.Windows.Forms;
    using WindowsLive.Writer.Api;
    using System.Net;
    using System.Reflection;
    using System.IO;
    using System.Collections.Generic;
    using System.Text;
    using System.Collections;
    using WindowsLive.Writer.BlogClient;
    using WindowsLive.Writer.CoreServices.Settings;
    using Microsoft.Win32;
    using WindowsLive.Writer.BlogClient.Clients;
    using WindowsLive.Writer.CoreServices;
    using WindowsLive.Writer.Mshtml;
    using System.Configuration;
    using System.Text.RegularExpressions;
    using System.Drawing;
    using System.Drawing.Imaging;
    namespace SamplePlugIn
    {
    [WriterPlugin("a285f0bc-81ba-44ef-b1e0-70700cf62ef0", "Gallery Plugin",
    Description = "Server Gallery pictures.",
    - Show quoted text -

    HasEditableOptions = false)]
    //ImagePath = “Images.writer_thumb.png”,

    [InsertableContentSource("Picture Gallery", SidebarText = "Picture Gallery")]

    public class Plugin : SmartContentSource
    {
    public override DialogResult CreateContent(IWin32Window dialogOwner, ISmartContent content)
    {
    DialogResult result = DialogResult.Cancel;
    try
    {
    RegistrySettingsPersister persister = new RegistrySettingsPersister(Registry.CurrentUser, “Software\\Microsoft\\Windows Live\\Writer\\Weblogs”);

    string[] names = persister.GetSubSettings();
    for (int i = 0; i < names.Length; i++)
    {
    ISettingsPersister blogPersister = persister.GetSubSettings(names[i]);

    SettingsPersisterHelper settings = new SettingsPersisterHelper(blogPersister);

    CredentialsDomain domain = new CredentialsDomain("name", "description", null, null);

    BlogCredentials creds = new BlogCredentials(settings, domain);

    string blogName = blogPersister.Get(" BlogName ") as string;
    string apiurl = blogPersister.Get("PostApiUrl").ToString();
    if (apiurl == Global.MetaUrl)
    {
    Global.userid = creds.Username;
    break;
    }

    }

    if (Global.userid != "")
    {
    PluginSettings settings = new PluginSettings(content.Properties);
    using (frmMain form = new frmMain(content))
    {
    result = form.ShowDialog();
    }
    }
    else
    {
    result = DialogResult.Cancel;
    }
    }

    catch (Exception ex)
    {
    PluginDiagnostics.DisplayError("CreateContent", ex.Message);
    }
    return result;
    }

    public override string GeneratePublishHtml(ISmartContent content, IPublishingContext publishingContext)
    {
    string str = "”;
    try
    {
    str += “”;
    }
    catch (Exception ex)
    {
    PluginDiagnostics.DisplayError(“Generate Html”, ex.Message);
    }
    return str;

    }

    public override SmartContentEditor CreateEditor(ISmartContentEditorSite editorSite)
    {
    return new ContextEditor();
    }
    }
    }

    Its actually one picture upload plugin but when GeneratePublishHtml method return, it add default tag in between my generated html tag which i dont want and due to that it is not going to call MediaObjectInfo IMetaWeblog.NewMediaObject(…) from metaweblog api so that picture is not going to be save on my server….

    I have the same problem as described in this link which i got yesterday when i was finding a solution for above problem..
    http://social.msdn.microsoft.com/Forums/en/wlwdev/thread/f555955a-b96d-4e13-a078-f12df240393d

    Thanks for help….

Comment RSS · TrackBack URI

Leave a Comment

Name: (Required)

E-mail: (Required)

Website:

Comment: