Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
4.2 KiB

  1. # Pod::Text::Termcap -- Convert POD data to ASCII text with format escapes.
  2. # $Id: Termcap.pm,v 1.0 2000/12/25 12:52:48 eagle Exp $
  3. #
  4. # Copyright 1999 by Russ Allbery <[email protected]>
  5. #
  6. # This program is free software; you can redistribute it and/or modify it
  7. # under the same terms as Perl itself.
  8. #
  9. # This is a simple subclass of Pod::Text that overrides a few key methods to
  10. # output the right termcap escape sequences for formatted text on the
  11. # current terminal type.
  12. ############################################################################
  13. # Modules and declarations
  14. ############################################################################
  15. package Pod::Text::Termcap;
  16. require 5.004;
  17. use Pod::Text ();
  18. use POSIX ();
  19. use Term::Cap;
  20. use strict;
  21. use vars qw(@ISA $VERSION);
  22. @ISA = qw(Pod::Text);
  23. # Don't use the CVS revision as the version, since this module is also in
  24. # Perl core and too many things could munge CVS magic revision strings.
  25. # This number should ideally be the same as the CVS revision in podlators,
  26. # however.
  27. $VERSION = 1.00;
  28. ############################################################################
  29. # Overrides
  30. ############################################################################
  31. # In the initialization method, grab our terminal characteristics as well as
  32. # do all the stuff we normally do.
  33. sub initialize {
  34. my $self = shift;
  35. # The default Term::Cap path won't work on Solaris.
  36. $ENV{TERMPATH} = "$ENV{HOME}/.termcap:/etc/termcap"
  37. . ":/usr/share/misc/termcap:/usr/share/lib/termcap";
  38. my $termios = POSIX::Termios->new;
  39. $termios->getattr;
  40. my $ospeed = $termios->getospeed;
  41. my $term = Tgetent Term::Cap { TERM => undef, OSPEED => $ospeed };
  42. $$self{BOLD} = $$term{_md} or die 'BOLD';
  43. $$self{UNDL} = $$term{_us} or die 'UNDL';
  44. $$self{NORM} = $$term{_me} or die 'NORM';
  45. unless (defined $$self{width}) {
  46. $$self{width} = $ENV{COLUMNS} || $$term{_co} || 78;
  47. $$self{width} -= 2;
  48. }
  49. $self->SUPER::initialize;
  50. }
  51. # Make level one headings bold.
  52. sub cmd_head1 {
  53. my $self = shift;
  54. local $_ = shift;
  55. s/\s+$//;
  56. $self->SUPER::cmd_head1 ("$$self{BOLD}$_$$self{NORM}");
  57. }
  58. # Make level two headings bold.
  59. sub cmd_head2 {
  60. my $self = shift;
  61. local $_ = shift;
  62. s/\s+$//;
  63. $self->SUPER::cmd_head2 ("$$self{BOLD}$_$$self{NORM}");
  64. }
  65. # Fix up B<> and I<>. Note that we intentionally don't do F<>.
  66. sub seq_b { my $self = shift; return "$$self{BOLD}$_[0]$$self{NORM}" }
  67. sub seq_i { my $self = shift; return "$$self{UNDL}$_[0]$$self{NORM}" }
  68. # Override the wrapping code to igore the special sequences.
  69. sub wrap {
  70. my $self = shift;
  71. local $_ = shift;
  72. my $output = '';
  73. my $spaces = ' ' x $$self{MARGIN};
  74. my $width = $$self{width} - $$self{MARGIN};
  75. my $code = "(?:\Q$$self{BOLD}\E|\Q$$self{UNDL}\E|\Q$$self{NORM}\E)";
  76. while (length > $width) {
  77. if (s/^((?:$code?[^\n]){0,$width})\s+//
  78. || s/^((?:$code?[^\n]){$width})//) {
  79. $output .= $spaces . $1 . "\n";
  80. } else {
  81. last;
  82. }
  83. }
  84. $output .= $spaces . $_;
  85. $output =~ s/\s+$/\n\n/;
  86. $output;
  87. }
  88. ############################################################################
  89. # Module return value and documentation
  90. ############################################################################
  91. 1;
  92. __END__
  93. =head1 NAME
  94. Pod::Text::Color - Convert POD data to ASCII text with format escapes
  95. =head1 SYNOPSIS
  96. use Pod::Text::Termcap;
  97. my $parser = Pod::Text::Termcap->new (sentence => 0, width => 78);
  98. # Read POD from STDIN and write to STDOUT.
  99. $parser->parse_from_filehandle;
  100. # Read POD from file.pod and write to file.txt.
  101. $parser->parse_from_file ('file.pod', 'file.txt');
  102. =head1 DESCRIPTION
  103. Pod::Text::Termcap is a simple subclass of Pod::Text that highlights output
  104. text using the correct termcap escape sequences for the current terminal.
  105. Apart from the format codes, it in all ways functions like Pod::Text. See
  106. L<Pod::Text> for details and available options.
  107. =head1 SEE ALSO
  108. L<Pod::Text|Pod::Text>, L<Pod::Parser|Pod::Parser>
  109. =head1 AUTHOR
  110. Russ Allbery E<lt>rra@stanford.eduE<gt>.
  111. =cut