B<Text::Format> - Various subroutines to format text. |
Text::Format - Various subroutines to format text.
version 0.61
use Text::Format;
my $text = Text::Format->new ( { text => [], # all columns => 72, # format, paragraphs, center leftMargin => 0, # format, paragraphs, center rightMargin => 0, # format, paragraphs, center firstIndent => 4, # format, paragraphs bodyIndent => 0, # format, paragraphs rightFill => 0, # format, paragraphs rightAlign => 0, # format, paragraphs justify => 0, # format, paragraphs extraSpace => 0, # format, paragraphs abbrevs => {}, # format, paragraphs hangingIndent => 0, # format, paragraphs hangingText => [], # format, paragraphs noBreak => 0, # format, paragraphs noBreakRegex => {}, # format, paragraphs tabstop => 8, # expand, unexpand, center } ); # these are the default values
my %abbr = (foo => 1, bar => 1); $text->abbrevs(\%abbr); $text->abbrevs(); $text->abbrevs({foo => 1,bar => 1}); $text->abbrevs(qw/foo bar/); $text->text(\@text);
$text->columns(132); $text->tabstop(4); $text->extraSpace(1); $text->firstIndent(8); $text->bodyIndent(4); $text->config({tabstop => 4,firstIndent => 0}); $text->rightFill(0); $text->rightAlign(0);
The format routine will format under all circumstances even if the width isn't enough to contain the longest words. Text::Wrap will die under these circumstances, although I am told this is fixed. If columns is set to a small number and words are longer than that and the leading 'whitespace' than there will be a single word on each line. This will let you make a simple word list which could be indented or right aligned. There is a chance for croaking if you try to subvert the module. If you don't pass in text then the internal text is worked on, though not modified.
Text::Format is meant for more powerful text formatting than what Text::Wrap allows. I also have a module called Text::NWrap that is meant as a direct replacement for Text::Wrap. Text::NWrap requires Text::Format since it uses Text::Format->format to do the actual wrapping but gives you the interface of Text::Wrap.
General setup should be explained with the below graph.
columns <------------------------------------------------------------> <----------><------><---------------------------><-----------> leftMargin indent text is formatted into here rightMargin
indent is firstIndent or bodyIndent depending on where we are in the paragraph.
{'^Mrs?\.$' => '^\S+$','^\S+$' => '^(?:S|J)r\.$'}
don't break names such as Mr. Jones, Mrs. Jones, Jones Jr.
The breaking algorithm is simple. If there should not be a break at the current end of sentence, then a backtrack is done till there are two words on which breaking is allowed. If no two such words are found then the end of sentence is broken anyhow. If there is a single word on current line then no backtrack is done and the word is stuck on the end. This is so you can make a list of names for example.
Note: this feature requires noBreak to be set to true.
version 0.61
use Text::Format;
my $text = Text::Format->new;
$text->rightFill(1); $text->columns(65); $text->tabstop(4);
print $text->format("a line to format to an indented regular paragraph using 65 character wide display"); print $text->paragraphs("paragraph one","paragraph two"); print $text->center("hello world","nifty line 2"); print $text->expand("\t\thello world\n","hmm,\twell\n"); print $text->unexpand(" hello world\n"," hmm");
$text->config({columns => 132, tabstop => 4});
$text = Text::Format->new();
print $text->format(@text); print $text->paragraphs(@text); print $text->center(@text); print $text->format([<FILEHANDLE>]); print $text->format([$fh->getlines()]); print $text->paragraphs([<FILEHANDLE>]); print $text->expand(@text); print $text->unexpand(@text);
$text = Text::Format->new ({tabstop => 4,bodyIndent => 4,text => \@text});
print $text->format(); print $text->paragraphs(); print $text->center(); print $text->expand(); print $text->unexpand();
print Text::Format->new({columns => 95})->format(@text);
Line length can exceed the number of specified columns if columns is set to a small number and long words plus leading whitespace exceed the specified column length. Actually I see this as a feature since it can be used to make up a nice word list.
Copyright (c) 1998 Gabor Egressy. All rights reserved.
This program is free software; you can redistribute and/or modify it under the same terms as Perl itself.
Gabor Egressy gabor@vmunix.com
Copyright (c) 1998 Gabor Egressy. All rights reserved. All wrongs reversed. This program is free software; you can redistribute and/or modify it under the same terms as Perl itself.
Adopted and modified by Shlomi Fish, http://www.shlomifish.org/ - all rights disclaimed.
Tom Phoenix
Found a bug with code for two spaces at the end of the sentence and provided a code fragment for a better solution. Also some preliminary suggestions on the design.
Brad Appleton
Suggestion and explanation of hanging indents, suggestion for non-breaking whitespace, general suggestions with regard to interface design.
Byron Brummer
Suggestion for better interface design and object design, code for better implementation of getting abbreviations.
H. Merijn Brand
Suggestion for a justify feature and original code for doing the justification. I changed the code to take into account the extra space at end of sentence feature.
Anne Wainwright
Inspired a documentation clarification about noBreak required by noBreakRegex , thanks to a report with a problem.
Shlomi Fish <shlomif@cpan.org>
This software is copyright (c) 2018 by Gabor Egressy.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
Please report any bugs or feature requests on the bugtracker website https://github.com/shlomif/text-format/issues
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.
You can find documentation for this module with the perldoc command.
perldoc Text::Format
The following websites have more information about this module, and may be of help to you. As always, in addition to those websites please use your favorite search engine to discover more resources.
A modern, open-source CPAN search engine, useful to view POD in HTML format.
https://metacpan.org/release/Text-Format
Search CPANThe default CPAN search engine, useful to view POD in HTML format.
http://search.cpan.org/dist/Text-Format
RT: CPAN's Bug TrackerThe RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN.
https://rt.cpan.org/Public/Dist/Display.html?Name=Text-Format
AnnoCPANThe AnnoCPAN is a website that allows community annotations of Perl module documentation.
http://annocpan.org/dist/Text-Format
CPAN RatingsThe CPAN Ratings is a website that allows community ratings and reviews of Perl modules.
http://cpanratings.perl.org/d/Text-Format
CPANTSThe CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution.
http://cpants.cpanauthors.org/dist/Text-Format
CPAN TestersThe CPAN Testers is a network of smoke testers who run automated tests on uploaded CPAN distributions.
http://www.cpantesters.org/distro/T/Text-Format
CPAN Testers MatrixThe CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms.
http://matrix.cpantesters.org/
CPAN Testers DependenciesThe CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution.
Please report any bugs or feature requests by email to bug-text-format at rt.cpan.org
, or through
the web interface at https://rt.cpan.org/Public/Bug/Report.html?Queue=Text-Format. You will be automatically notified of any
progress on the request by the system.
The code is open to the world, and available for you to hack on. Please feel free to browse it and play with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull from your repository :)
https://github.com/shlomif/text-format
git clone https://github.com/shlomif/perl-Module-Format
B<Text::Format> - Various subroutines to format text. |