HTML-Untidy

 view release on metacpan or  search on metacpan

lib/HTML/Untidy.pm  view on Meta::CPAN

package HTML::Untidy;
# ABSTRACT: yet another way to write HTML quickly and programmatically
$HTML::Untidy::VERSION = '0.02';

use strict;
use warnings;
use parent 'Exporter';
use HTML::Escape 'escape_html';

my @BASE = qw(element class attr prop text raw note);

# source: https://developer.mozilla.org/en-US/docs/Web/HTML/Element
my @TAGS = qw(
  a abbr address area article aside audio
  b base bdi bdo blockquote body br button
  canvas caption cite code col colgroup
  data datalist dd del details dfn dialog div dl dt
  em embed
  fieldset figcaption figure footer form
  h1 h2 h3 h4 h5 h6 head header hgroup hr html
  i iframe img input ins
  kbd
  label legend li link
  main map mark menu menuitem meta meter
  nav noframes noscript
  object ol optgroup option output
  p param picture pre progress
  q
  rp rt rtc ruby
  s samp script section select slot small source span strong style sub summary sup
  table tbody td template textarea tfoot th thead time title tr track
  u ul
  var video
  wbr
);

my @COMMON = qw(
  html head body title meta link script style
  h1 h2 h3 h4 h5 h6
  div p hr pre nav code img a b i u em strong sup sub small
  table tbody thead tr th td
  ul dl ol li dd dt
  form input textarea select option button label
  canvas
);

our @EXPORT_OK = (@BASE, @TAGS);

our %EXPORT_TAGS = (
  base   => [@BASE],
  common => [@BASE, @COMMON],
  all    => [@BASE, @TAGS],
);

our @CLASS;
our @ATTR;
our @PROP;
our @BODY;
our $INDENT = 0;

my $DEPTH = 0;

sub install_sub{
  no strict 'refs';
  my ($name, $code) = @_;
  *{"${name}"} = $code;
}

sub e ($){
  goto \&escape_html;
}

sub indent {
  return ' ' x ($DEPTH * $INDENT);
}

sub element ($&){
  my ($tag, $code) = @_;

  my $html = do {
    local @CLASS;
    local @ATTR;
    local @PROP;

lib/HTML/Untidy.pm  view on Meta::CPAN

=head3 attr

Takes key/value pairs as a hash and generates attributes. Again, no
deduplication, and the attributes are displayed in the order in which they are
passed.

=head3 prop

Adds a property which has no value (e.g. C<disabled> or C<hidden>).

=head3 text

Adds escaped text to the body of the tag.

=head3 raw

Adds unescaped text to the body of the tag.

=head3 note

Adds an HTML comment to the body of the tag.

=head3 indentation

If you want indentation, set C<$HTML::Untidy::INDENT> to the number of
spaces you want per level.

  local $HTML::Untidy::INDENT = 2;
  
  element 'div', sub{
    element 'button', sub{
      class 'btn btn-primary';
      attr id => 'some-button', 'data-toggle' => 'modal', 'data-target' => 'my-modal-id';

      if ($some_condition) {
        text 'Click me';
      }
      else {
        prop 'disabled' if $some_condition;
        text "Don't click me";
      }
    };
  };

=head2 :common

Exports the following curried aliases of L</element> for the most commonly used
HTML tags.

  html head body title meta link script style h1 h2 h3 h4 h5 h6 div p hr pre nav
  code img a b i u em strong sup sub small table tbody thead tr th td ul dl ol
  li dd dt form input textarea select option button label canvas

=head2 :all

Exports every HTML5 tag I could find.

  a abbr address area article aside audio b base bdi bdo blockquote body br
  button canvas caption cite code col colgroup data datalist dd del details dfn
  dialog div dl dt em embed fieldset figcaption figure footer form h1 h2 h3 h4
  h5 h6 head header hgroup hr html i iframe img input ins kbd label legend li
  link main map mark menu menuitem meta meter nav noframes noscript object ol
  optgroup option output p param picture pre progress q rp rt rtc ruby s samp
  script section select slot small source span strong style sub summary sup
  table tbody td template textarea tfoot th thead time title tr track u ul var
  video wbr

=head1 SEE ALSO

=over

=item L<HTML::Builder>

=back

=head1 AUTHOR

Jeff Ober <sysread@fastmail.fm>

=head1 LICENSE

Perl5

=head1 AUTHOR

Jeff Ober <sysread@fastmail.fm>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2017 by Jeff Ober.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut



( run in 2.317 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )