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");
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.