Formatter::HTML::Preformatted - Absolute minimal HTML formatting of pure text


Formatter-HTML-Preformatted documentation Contained in the Formatter-HTML-Preformatted distribution.

Index


Code Index:

NAME

Top

Formatter::HTML::Preformatted - Absolute minimal HTML formatting of pure text

SYNOPSIS

Top

  use Formatter::HTML::Preformatted;
  my $formatter = Formatter::HTML::Preformatted->format($data);
  print $formatter->fragment;
  my @links = $text->links;
  print ${$links}[0]->{url};

DESCRIPTION

Top

This module will simply take any text-string and put it in a HTML pre element. It will escape tags and entities. It will also look through it to see if there are any URIs, and they will be turned into hyperlinks.

METHODS

Top

This module conforms with the Formatter API specification, version 0.95:

format($string)

The format function that you call to initialise the formatter. It takes the plain text as a string argument and returns an object of this class.

fragment

To get only the text enclosed in the minimal pre element, you will call this method. It returns a string with the HTML fragment.

document([$charset])

Will add a document type declaration and some minimal markup, to return a full, valid, HTML document. You may specify an optional $charset parameter. This will include a HTML meta element with the chosen character set. It will still be your responsibility to ensure that the document served is encoded with this character set.

Will return all links found the input plain text string. They will be found in an arrayref where each element has a key url.

title

Since this formatter has no way of finding the title of the document this method will always return undef.

SEE ALSO

Top

Formatter, Formatter::HTML::Textile, URI::Find::Simple

AUTHOR

Top

Kjetil Kjernsmo, <kjetilk@cpan.org>

COPYRIGHT AND LICENSE

Top


Formatter-HTML-Preformatted documentation Contained in the Formatter-HTML-Preformatted distribution.
package Formatter::HTML::Preformatted;

use 5.006;
use strict;
use warnings;
use URI::Find::Simple qw( list_uris change_uris );


our $VERSION = '0.95';

sub format {
  my $that  = shift;
  my $class = ref($that) || $that;
  my $self = {
	      _text => shift,
	     };
  bless($self, $class);
  return $self;
}

sub fragment {
  my $self = shift;
  my $raw = $self->{_text};
  # Escaping the stuff that needs escaping.
  $raw =~ s/&/&amp;/g;
  $raw =~ s/\>/&gt;/g;
  $raw =~ s/\</&lt;/g;
  
  return "<pre>\n" . change_uris($raw, sub { "<a href=\"$_[0]\">$_[0]</a>" }) . "\n</pre>\n";
}

sub document {
  my $self = shift;
  my $result = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">' . "\n<html>\n";
  my $charset = shift;
  if ($charset) {
    $result .= "<head>\n" . '<meta http-equiv="Content-Type" content="text/html; charset=' . $charset . '">' . "\n</head>\n";
  }
  $result .= "<body>\n" . $self->fragment . "\n</body>\n</html>\n";
  return $result;
}


sub links {
  my $self = shift;
  my @arr;
  foreach (list_uris($self->{_text})) {
    push(@arr, {url => $_, title => ''});
  }
  return \@arr;
}


sub title {
  return undef;
}





1;
__END__