Many people recognize the benefit of managing 'snippets' as a part of routine coding practice. Many text editors and IDEs support such a feature. There are some problems, though, with snippet management. These problems can be handled very well with YAML and PERL.
"Snippet managers" often suffer from various hassles.

1) How to add 'metadata' to snippets without messing up the code of the snippet; 2) How to save snippets in plain text without characters in the snippet "colliding" with the delimiters used to delimit the snippet itself; 3) How to categorize snippets so they are easily searchable (eg, show me all snippets related to creating a 'for loop' in any language) and sortable (sort by name, author, date added, whatever);

All of these hassles go away when you use YAML to manage snippets. Included is a sample of how to manage snippets using YAML syntax (the perl code to extract the snippet is excluded for sake of brevity, I will include if anyone asks).

Benefits:
YAML uses indentation to distinguish blocks, so you can put anything in a snippet without 'delimiter collision' (unlike with XML or other text formats). Moreover, the 'internal indentation' of your snippet is preserved (and displayed flush left) as long as you declare it with a 'pipe' character (eg body : | ) (for more details see the YAML spec at yaml.org).
YAML supports throwaway comments, so you can add comments to your snippets without them appearing in the snippet itself.
YAML supports arbitrary data structures and multiple 'subdocuments' in a single file, this means you can include any level of detail you want with a snippet, as well as smush them all together in a single file, or spread them out across multiple files.
In the example below, each snippet gets its own 'workbook' the individual 'worksheets' are called 'main' 'keyword_groups' and 'text'. You can add your own 'worksheets' or do whatever you want. It is also easy to make "fill_in" snippets (aka snippets that get their values from variables)
--- ### detailed snippet for maximum manageability main : - caption: for loop array description: loop over an array language: perl prefix: loop tsid: tsid="fill_in_the_blank" keyword_groups : - desc: folders keywords: perl; array; loop; statement; - desc: search terms keywords: array; loop; statment; for; text : - body: | for my $item (@aryData){ print $item; print ("\n-----------\n"); } --- ### quick and dirty snippet for fastest input main : - caption: for loop arrayref description: loop over an array ref text : - body: | for my $item (@{$aryRef}){ print $item; print ("\n-----------\n"); }

Replies are listed 'Best First'.
Re: Advanced snippet management with YAML and Perl
by diotalevi (Canon) on Apr 25, 2004 at 13:04 UTC

    It also happens that YAML is a dirty, evil format to deal with on Win32 (and I suppose Mac as well). It seems to be hung up on the idea that LF is the One True Newline so while it may be convenient for all the people here where \n becomes \x0A after being written to a file, it is ghastly inconvenient for me (where \n is \x0D\x0A) and perhaps similarly inconvenient for Macs where I hear \n is \x0D.

    So no more YAML until YAML figures out how to handle newlines sanely. The alternative is that people on standard platforms have to carefully feed all of their YAML data through \r -> ε, which is to say, Yuck!

      Perhaps this is a request for a mod to YAML.pm?

      I've not had a problem using YAML on Win32... yet. Thanks for the advance warning.
      1. I'm a mac user. For the record, it's no trouble whatsoever. Unix and all.

      2.

      package YAML::WIN32NewlineHack; use YAML; use Exporter; our @ISA = qw(Exporter YAML); # only covers scalar string input, but i think this should work sub Load { shift; s/\x0D\x0A/\n/g; Load($_); }

        Thanks for the thought on filtering YAML output. You'd have to do the same thing to DumpFile, LoadFile, and Dump as well to get complete coverage. Then you'd have to teach the loader routines to guess about whether lines use \x0a\x0d endings, \x0d, or \x0a. Thinking further, you'd also have to patch all the YAMLs on *NIX hosts so that when they receive a YAML file which was written on a Win32 machine they can still read it.

        I forgot that Mac had turned into another UNIX recently. I meant that Mac, prior to being a UNIX, used the \x0a line ending. I suppose it uses \x0d now.

Re: Advanced snippet management with a wiki
by chb (Deacon) on Feb 18, 2005 at 08:58 UTC
    I actually stuff my snippets into a wiki. Supports fulltext-search, keywords, easy navigation, any metadata you want. And emacs-wiki.el is of course tighly integrated in The One True Editor (if I always had to fire up a browser to get at my snippets, I probably would use a different snippet repositiory).

    Update: changed node title