Browse Source

Consolidated HTML crap.

pull/1/head
Brennen Bearnes 17 years ago
parent
commit
46d83b3167
2 changed files with 65 additions and 106 deletions
  1. +0
    -72
      Display/GenHTML.pm
  2. +65
    -34
      Display/HTML.pm

+ 0
- 72
Display/GenHTML.pm View File

@ -1,72 +0,0 @@
package Display::GenHTML;
use strict;
use warnings;
use Exporter;
our @ISA = qw(Exporter);
our %EXPORT_TAGS = ( 'all' => [ qw(a div p em small strong table table_row table_cell) ] );
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
our @EXPORT = qw( );
# Generate subs for these:
my %tags = (
p => \&tag,
em => \&tag,
small => \&tag,
strong => \&tag,
table => \&tag,
tr => \&tag,
td => \&tag,
a => \&tag,
div => \&tag,
);
# Map these tags to different sub names:
my %tagmap = (
tr => 'table_row',
td => 'table_cell',
);
# Install appropriate subs in symbol table:
{ no strict 'refs';
for my $key (keys %tags) {
my $subname = $tagmap{$key};
$subname = $key unless ($subname);
*{ $subname } = sub { $tags{$key}->($key, @_); };
}
}
########
# subs
# handle most HTML tags:
sub tag {
my ($tag) = shift;
my (@attribs) = @_;
my $attr_string = '';
my $text = '';
for my $attrib (@attribs) {
if ($attrib =~ m/^([a-z]+): ?(.*)$/) {
my ($name, $value) = ($1, $2);
$attr_string .= qq{ $name="$value"}
}
else {
if (length($text) > 0) {
$text .= "\n";
}
$text .= $attrib;
}
}
return "<${tag}${attr_string}>$text</$tag>";
}
1;

+ 65
- 34
Display/HTML.pm View File

@ -1,62 +1,93 @@
#!/usr/bin/perl
=pod
=head1 NAME
Display::HTML
=cut
package Display::HTML;
use strict;
use warnings;
no warnings 'uninitialized';
use Display::GenHTML qw(:all);
use Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(p a ornament heading em entry_markup table table_cell table_row small);
our @EXPORT;
=item entry_markup
our %EXPORT_TAGS = ( 'all' => [ qw(a div p em small strong table
table_row table_cell entry_markup
heading) ],
'highlevel' => [ qw(a p em small strong table
table_row table_cell
entry_markup heading) ] );
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
our @EXPORT = qw( );
# Generate subs for these:
my %tags = (
p => \&tag,
em => \&tag,
small => \&tag,
strong => \&tag,
table => \&tag,
tr => \&tag,
td => \&tag,
a => \&tag,
div => \&tag,
);
# ...but map these tags to different sub names:
my %tagmap = (
tr => 'table_row',
td => 'table_cell',
);
# Install appropriate subs in symbol table:
{ no strict 'refs';
for my $key (keys %tags) {
my $subname = $tagmap{$key};
$subname = $key unless ($subname);
*{ $subname } = sub { $tags{$key}->($key, @_); };
}
Return text wrapped in the appropriate markup for an entry. Just a wrapper
around div() at the moment.
=cut
sub entry_markup {
my ($text) = @_;
return div($text, 'class: entry') . "\n";
}
# handle most HTML tags:
sub tag {
my ($tag) = shift;
my (@params) = @_;
=item ornament
Returns a type ornament.
my ($attr_string, $text);
=cut
for my $param (@params) {
sub ornament {
return '<small>&sect;</small>';
}
if ($param =~ m/^([a-z]+): ?(.*)$/) {
my ($name, $value) = ($1, $2);
$attr_string .= qq{ $name="$value"}
}
else {
$text .= "\n" if length($text) > 0;
$text .= $param;
}
}
=item heading
# voila, an X(HT)ML tag:
return "<${tag}${attr_string}>$text</$tag>";
Returns a heading of the specified level.
}
For example, heading("p1k3", 1) == "<h1>p1k3</h1>";
########################################
# Special cases and higher-level markup
=cut
sub entry_markup {
my ($text) = @_;
return div($text, 'class: entry') . "\n";
}
sub heading {
my ($text, $level) = @_;
my $tag = "h$level";
return "<$tag>$text</$tag>";
my $h = "h$level";
return tag($h, $text);
}
1;

|||||||
x
 
000:0
Loading…
Cancel
Save