Formatter::HTML::Textile - Formatter to make HTML from Textile


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

Index


Code Index:

NAME

Top

Formatter::HTML::Textile - Formatter to make HTML from Textile

DESCRIPTION

Top

This module will format Textile input to HTML. It conforms with the Formatter API specification, version 1.0.

SYNOPSIS

Top

  my $textile = <<TEXTILE;
  h1. textile document

  this is a "textile":http://textism.com/tools/textile/ document
  TEXTILE

  my $formatter = Formatter::HTML::Textile->format( $textile );

  print "title is ".$formatter->title."\n";
  print $formatter->document;

  my @links = @{ $formatter->links };
  print "Links urls: ";
  print join ", " map { $_->{url} } @links;
  print "\n";

METHODS

Top

format($string)

This is a constructor method and initializes the formatter with the passed text.

This method returns a Formatter::HTML::Textile object.

document()

It returns a full HTML document, comprising the formatted textile source converted to HTML. 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.

fragment()

returns a minimal HTML chunk as textile.

Returns all the links found in the document, as a listref of hashrefs, with keys 'title', which is the title of the link, and 'url', which is the link.

title()

Returns the title of the document

SEE ALSO

Top

Formatter, Text::Textile

AUTHOR

Top

Originally written by Tom Insam, maintained by Kjetil Kjernsmo from 2005-11-19.

COPYRIGHT

Top


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


package Formatter::HTML::Textile;
use warnings;
use strict;
use Carp qw( croak );

our $VERSION = 1.02;

use base qw( Text::Textile );

sub format {
  my $class = shift;
  my $self = ref($class) ? $class : $class->new;
  $self->{_text} = shift || "";
  return $self;
}

sub document {
  my $self = shift;
  my $charset = shift;
  # TODO - holy cow this is a horrible hack. Make work, damnit. Needs docstrings,
  # etc, etc, etc.
  my $out = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">\n<html><head>";  
  if ($charset) {
    $out .= '<meta http-equiv="Content-Type" content="text/html; charset='.$charset.'">';
  }
  $out .= '<title>'
         .$self->title
         .'</title></head><body>'
         .$self->fragment
         .'</body></html>';
  return $out;
}


sub fragment {
  my $self = shift;
  return $self->process($self->{_text});
}



sub links {
  my $self = shift;
  my @arr;
  require HTML::TokeParser;
  my $p = HTML::TokeParser->new(\$self->fragment);

  while (my $token = $p->get_tag("a")) {
    my $url = $token->[1]{href} || "-";
    my $text = $p->get_trimmed_text("/a");
    push(@arr, {url => $url, title => $text});
  }
  return \@arr;
}


sub title {
  my $self = shift;
  if ( $self->{_text} =~ /^h1\.\s*(.*)$/im ) {
    return $1;
  }
  return undef;
}


1;