home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-perl-addon-1.4.9-installer.exe / yaml2outline < prev    next >
Encoding:
Text File  |  2003-09-17  |  3.3 KB  |  144 lines

  1. #!perl -w
  2.  
  3. $VERSION = '0.35';
  4.  
  5.  
  6. =head1 NAME
  7.  
  8. yaml2outline - Convert a YAML outline into a standard one
  9.  
  10. =head1 USAGE
  11.  
  12.     yaml2outline outline.yaml > outline.txt
  13.  
  14. =head1 DESCRIPTION
  15.  
  16. This program converts a YAML file that conforms to a particular outline format, into a standard looking outline. It is really just a proof of concept, and not intended for any serious purpose.
  17.  
  18. Example:
  19.  
  20.     ---
  21.     - Intro
  22.     - Part 1:
  23.        - Life
  24.        - Love:
  25.           - True Love:
  26.              - Mexican Cuisine
  27.              - Perl
  28.           - Love at First Sight
  29.        - Pursuit of YAML
  30.     - Part 2:
  31.        - Scissors:
  32.           - Sewing
  33.           - Hair
  34.        - Paper
  35.        - Rock:
  36.           - Granite
  37.           - Pixies
  38.     - Part3:
  39.        - Perl
  40.        - Python
  41.        - Ruby
  42.     - Conclusion
  43.  
  44. yields:
  45.  
  46.     I) Intro
  47.  
  48.     II) Part 1
  49.        A) Life
  50.        B) Love
  51.            1) True Love
  52.                i) Mexican Cuisine
  53.                ii) Perl
  54.            2) Love at First Sight
  55.        C) Pursuit of YAML
  56.  
  57.     III) Part 2
  58.        A) Scissors
  59.            1) Sewing
  60.            2) Hair
  61.        B) Paper
  62.        C) Rock
  63.            1) Granite
  64.            2) Pixies
  65.  
  66.     IV) Part3
  67.        A) Perl
  68.        B) Python
  69.        C) Ruby
  70.  
  71.     V) Conclusion
  72.  
  73. =head1 AUTHOR
  74.  
  75. Brian Ingerson <ingy@cpan.org>
  76.  
  77. =head1 COPYRIGHT
  78.  
  79. Copyright 2002, Brian Ingerson - All rights reserved
  80.  
  81. You may use this hack under the same terms as Perl itself.
  82.  
  83. =cut
  84.  
  85. use strict;
  86. use YAML;
  87.  
  88. my ($yaml, $perl, $outline, $level, $symbol_level, $symbol_format, $lookup);
  89.  
  90. $yaml = join '', <>;
  91. eval { $perl = Load($yaml); };
  92. die "Error in YAML input:\n$@" if $@;
  93.  
  94. $level = -1;
  95. $symbol_format = ['ROMAN', 'ALPHA', 'NUMERIC'];
  96. $lookup = Load(join '', <DATA>);
  97.  
  98. walk_tree($perl);
  99. print $outline;
  100.  
  101. sub walk_tree {
  102.     my $node = shift;
  103.     $level++;
  104.     $symbol_level->[$level] = 0;
  105.     my $ref = ref($node);
  106.     die "Data structure is not valid for outline\n" 
  107.       unless $ref eq 'ARRAY';
  108.     for my $elem (@$node) {
  109.     $ref = ref($elem) || 'STRING';
  110.     if ($ref eq 'STRING') {
  111.         $outline .= (' ' x (4 * $level -1)) . 
  112.                     symbol($level) . ') ' . $elem . "\n";
  113.         }        
  114.     elsif ($ref eq 'HASH') {
  115.         die "Data structure is not valid for outline\n" 
  116.           unless keys(%$elem) == 1;
  117.         my ($key) = keys(%$elem);
  118.         $outline .= (' ' x (4 * $level -1)) . 
  119.                     symbol($level) . ') ' . $key . "\n";
  120.         walk_tree($elem->{$key});
  121.         } 
  122.     else {
  123.         die "Data structure is not valid for outline\n"; 
  124.     }
  125.     $outline .= "\n" unless $level;
  126.     }
  127.     $level--;
  128. }
  129.  
  130. sub symbol {
  131.     my $level = shift;
  132.     my $format = $symbol_format->[$level % @$symbol_format];
  133.     $format = lc($format) unless $level < @$symbol_format;
  134.     return $lookup->{$format}[$symbol_level->[$level]++];
  135. }
  136.  
  137. __DATA__
  138. ROMAN: [I, II, III, IV, V, VI, VII, VIII, IX, X, XI, XII, XIII, XIV, XV, XVI]
  139. ALPHA: [A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, w, x]
  140. NUMERIC: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
  141. roman: [i, ii, iii, iv, v, vi, vii, viii, ix, x, xi, xii, xiii, xiv, xv, xvi]
  142. alpha: [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x]
  143. numeric: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
  144.