Almost-minimal filesystem based blog.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
2.5 KiB

17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
  1. package App::WRT::HTML;
  2. use strict;
  3. use warnings;
  4. no warnings 'uninitialized';
  5. use Exporter;
  6. our @ISA = qw(Exporter);
  7. our %EXPORT_TAGS = ( 'all' => [ qw(a div p em small strong table
  8. table_row table_cell entry_markup
  9. heading article nav section
  10. unordered_list ordered_list list_item) ],
  11. 'highlevel' => [ qw(a p em small strong table
  12. table_row table_cell
  13. entry_markup heading) ] );
  14. our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
  15. our @EXPORT = qw( );
  16. use HTML::Entities qw(encode_entities);
  17. # Generate subs for these:
  18. my %tags = (
  19. p => \&tag,
  20. em => \&tag,
  21. small => \&tag,
  22. strong => \&tag,
  23. table => \&tag,
  24. tr => \&tag,
  25. td => \&tag,
  26. a => \&tag,
  27. div => \&tag,
  28. article => \&tag,
  29. nav => \&tag,
  30. section => \&tag,
  31. ul => \&tag,
  32. ol => \&tag,
  33. li => \&tag,
  34. );
  35. # ...but map these tags to different sub names:
  36. my %tagmap = (
  37. tr => 'table_row',
  38. td => 'table_cell',
  39. ul => 'unordered_list',
  40. ol => 'ordered_list',
  41. li => 'list_item',
  42. );
  43. # Install appropriate subs in symbol table:
  44. { no strict 'refs';
  45. for my $key (keys %tags) {
  46. my $subname = $tagmap{$key};
  47. $subname = $key unless ($subname);
  48. *{ $subname } = sub { $tags{$key}->($key, @_); };
  49. }
  50. }
  51. # handle most HTML tags:
  52. sub tag {
  53. my ($tag) = shift;
  54. my ($attr_string, $text);
  55. for my $param (@_) {
  56. if (ref($param)) {
  57. # A hashref containing one or more attribute => value pairs. We sort
  58. # these by key because, if using each, order is random(ish), and this can
  59. # lead to different HTML for the same input.
  60. foreach my $attr (sort keys %{ $param }) {
  61. my $value = encode_entities( ${ $param }{$attr} );
  62. $attr_string .= ' ' . $attr . '="' . $value . '"';
  63. }
  64. }
  65. else {
  66. # Text that goes inside the content of the tag.
  67. $text .= "\n" if length($text) > 0;
  68. $text .= $param;
  69. }
  70. }
  71. # Voila, an X(HT)ML tag, pretty much:
  72. return '<' . $tag . $attr_string . '>' . $text . '</' . $tag . '>';
  73. }
  74. # Special cases and higher-level markup
  75. sub entry_markup {
  76. return qq{\n\n<article><div class="entry">}
  77. . $_[0]
  78. . "</div></article>\n\n";
  79. }
  80. sub heading {
  81. my ($text, $level) = @_;
  82. my $h = "h$level";
  83. return tag($h, $text);
  84. }
  85. 1;