Converting to Snip-It Pro's ".snip" format

One of my goals in creating Snip-It Pro was to make it very easy to export your snippets to another format, so that using a proprietary format was not an excuse for not using Snip-It Pro. The latest release of Snip-It Pro introduces two additional export features: the ability to export folders to a single text file and the ability to define an export template and export all the snippets in a folder using that template. This feature effectively enables you to convert your snippets into any other text based format. I even included an example in the help file of defining a template for converting to the Visual Studio “.snippet” format.

But just as important as the ability to export, is the ability to import. Sure you can drag and drop text right into Snip-It Pro, but then you would still need to set a description and other properties. And there is also the ability to publish and retrieve favorite snippets from Snipplr, but then again you might not be using Snipplr, or you’re trying to import from a local format.

I thought about creating a similar import feature by allowing users to define regular expressions, but thought that would probably not be such a great feature, since as the old saying goes “if you have a problem that requires you to use regular expressions, then you have two problems.”

So until I crack this nut of a problem, I figured the best thing I could do, would be to write a post explaining the XML based “.snip” format that Snip-It Pro uses.  After all, most Snip-It Pro users are programmers, and writing scripts to convert between formats is just the sort of problem we like to solve.

Here is a guide to the nodes/fields within the format, with tips if you are going to populate these yourself:

Description – The title/description of the snippet. This is what is displayed as the name of the snippet in the toolbar and folder view.

Content – The actual contents of the snippet. In version 1, this field actually contained RTF. In version 2 and above, plain text is used since we began to support Syntax Highlighting.

FileVersion – If you are using RTF for your content field, set this to 1, otherwise set it to 2. If you do set it to 1, keep in mind the next time the snippets is edited, Snip-It Pro automatically converts it to 2, stripping any RTF from your content.

Order– Used to order the snippets in a folder. We don’t do any validation on this other than the fact that it has to be an integer. Once we load all the snippets in a folder, we use this to sort them before displaying in the UI

PreviewText– This is used to populate the tool tip that displays when you hover over the snippet. It was much more useful in version 1, as the tool tips couldn’t display RTF, but normally this is just set to the content.

Notes – This field can have any notes you want and can include RTF text.

Category– The Display Name of the category the snippet is in. Snip-It Pro does a lookup in the categories file located in the “AppData\Roaming\Snip-It Pro” folder. That file has info about how to comment it, what the internal Snipplr category is and which “.syn” file to load from the lib directory in the installation directory to get syntax highlight rules for that language.

ReferenceUrl– Here you should put any reference url as the UI gives you the ability to right click and navigate to the url, but you can put any string in here if you wanted to.

Tags – Put each Tag in a string node as this is a collection of strings.

ID– A guid that can serve as the ID of the snippet. If you don’t want to generate these, leave off the entire Node, as Snip-It Pro will complain if the node is there with no value. This is only used in the “auto commenting” feature, so leaving it off isn’t an issue. Snip-It Pro will actually generate a new Guid if you leave it off next time you save the snippet.

DisableAutoComments – Set to true or false. Controls whether to override the global application setting of inserting auto comments for this particular snippet.

LastUpdated – Put the last updated date time, or better yet, just leave off this node as well as it’s not required.

Here is a sample “.snip” file :

<?xml version="1.0" encoding="utf-8"?>
<Snippet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Description>C# Property</Description>
  <Order>2</Order>
  <PreviewText>private [[Data Type]] _[[Property Name]];
public [[Data Type]] [[Property Name]]
{
 get { return _[[Property Name]]; }
 set { _[[Property Name]] = value; }
}</PreviewText>
  <FileVersion>2</FileVersion>
  <Notes>{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Verdana;}}
\viewkind4\uc1\pard\f0\fs17 Use configure snippet to configure\par
}
</Notes>
  <Category>C#</Category>
  <ReferenceUrl>http://snipplr.com/view/14656/templated-property</ReferenceUrl>
  <Tags>
    <string>property</string>
    <string>getter</string>
    <string>setter</string>
  </Tags>
  <Content>private [[Data Type]] _[[Property Name]];
public [[Data Type]] [[Property Name]]
{
 get { return _[[Property Name]]; }
 set { _[[Property Name]] = value; }
}</Content>
  <ID>22033523-1561-4d9a-96ec-29d44b4b3a26</ID>
  <DisableAutoComments>false</DisableAutoComments>
  <LastUpdated>2009-05-16T11:52:40.824-04:00</LastUpdated>
</Snippet>

Something to keep in mind, is that Snip files are just .Net objects (Snip-it Pro is written in C#) that are serialized using the default XML serializer. They are deserialized using the same serializer. If you're a dot net developer, you actually can add references to the Service.File assembly and create your own instances of FileSnippet (Inherits from an abstract snippet class in the Infrastructure.Interface assembly). You can even call the SaveSnippet method of the FileSnippetService in the same assembly to save it once you've populated the fields instead of writing your own serialization code.

If you have any issues creating this format, send me an email or leave a comment.

Painless SharePoint Web Config Modifications with Custom Features

Minor Tweaks