The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Tk::HyperText - Create and manipulate ROText widgets which render HTML code.

SYNOPSIS

  my $hypertext = $mw->Scrolled ("HyperText",
    -scrollbars   => 'e',
    -wrap         => 'word',
    -linkcommand  => \&onLink,  # what to do when <a> links are clicked
    -titlecommand => \&onTitle, # what to do when <title>s are found
  )->pack (-fill => 'both', -expand => 1);

  # insert some HTML code
  $hypertext->insert ("end","<body bgcolor=\"black\" text=\"yellow\">"
    . "Hello, <b>world!</b></body>");

WIDGET-SPECIFIC OPTIONS

-rerender

Boolean. When true (the default), the ENTIRE contents of your HyperText widget will be (re)rendered every time you modify it. In this way, if you insert, e.g. a "bold" tag and don't close it, then insert new text, the new text should logically still be in bold, and it would be when this flag is true.

When false, only the newly inserted text will be rendered independently of what else is already there. If re-rendering the page is too slow for you, try disabling this flag.

-titlecommand

This should be a CODEREF pointing to a subroutine that will handle changes in a page's title. While HTML code is being parsed, when a title tag is found, it will call this method.

The callback will received the following variables:

  $widget = a reference to the HyperText widget that wants to set a title.
  $title  = the text in the <title> tag.
-linkcommand

This should be a CODEREF pointing to a subroutine that will handle the clicking of hyperlinks.

The callback will received the following variables:

  $widget = a reference to the HyperText widget that invoked the link.
  $href   = the value of the link's "href" attribute.
  $target = the value of the link's "target" attribute.
-attributes

This option will allow you to define all of the default settings for the display of HTML pages. Here's an example:

  my $html = $mw->Scrolled ("HyperText",
    -attributes => {
      body => {
        bgcolor => 'white',
        text    => 'black',
        link    => 'blue',
        vlink   => 'purple',
        alink   => 'red',
      },
      font => {
        family => 'Arial',
        size   => 3,
        color  => '', # inherit from <body>
        back   => '', # inherit from <body>
      },
    },
  )->pack;

DESCRIPTION

Tk::HyperText is a derived Tk::ROText class which supports the automatic rendering of HTML code. It's designed to be easily useable as a drop-in replacement to any Tk::ROText widget. Rendering HTML code is as easy as inserting it as raw HTML, as shown in the synopsis.

WIDGET METHODS

In addition to all of the methods exported by Tk::ROText and Tk::Text, the following methods have special behaviors:

$text->insert (where, html-code)

Insert new HTML code, and render it automatically. Note that currently, only inserting to the "end" works. See "BUGS" below.

$text->delete (start, end)

Delete content from the textbox. Note that currently you can only delete EVERYTHING. See "BUGS" below.

$text->get (start, end)

Get the HTML code back out of the widget. Note that currently this gets ALL of the code. See "BUGS". This returns the actual HTML code, not just the text that's been rendered.

$text->clear

Clear the entire text widget display.

SUPPORTED HTML

The following HTML tags and attributes are fully supported by this module:

  <html>, <head>
  <title>      *calls -titlecommand when found
  <body>       (bgcolor, link, vlink, alink, text)
  <font>       (face, size, color, back)
  <a>          (href, target)
  <blockquote>
  <p>, <br>
  <pre>
  <code>, <tt>
  <center>, <right>, <left>
  <h1> - <h6>
  <sup>, <sub>
  <b>, <strong>
  <i>, <em>
  <u>, <ins>
  <s>, <del>

EXAMPLE

Run the `demo.pl` program included in the distribution for a demonstration. It's a kind of simple web browser that views HTML pages in the "demolib" directory, and supports hyperlinks that link from one page to another.

BUGS

As noted above, the insert method only inserts at the end, delete deletes everything, and get gets everything. I plan on coming up with a way to fix this in a later version.

There are some forms of HTML that might not render properly. For instance, if you set <font back="yellow">, then set <font color="red">, and then close the red font, it will also stop the yellow highlight color too. Situations like this aren't too serious, though. So, if you set one attribute, set them all. ;)

SEE ALSO

Tk::ROText and Tk::Text.

CHANGES

0.01 June 20, 2007

  - Initial release.

AUTHOR

Casey Kirsle, <casey at cuvou.net>