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");
}