#!/usr/bin/env bash ## This script is not ready for prime time yet... perl -ne ' BEGIN { my @SELF_CLOSING_TAGS = qw( area base br col command embed hr img input keygen link meta param source track wbr ); my %ALIASES = ( bu => "button", te => "textarea", in => "input", ## TODO: more (and better!) aliases ); my %ATTRS = ( a => [ ["href", "#"] ], img => ["src", "alt"], input => [ ["type", "text"] ], link => ["rel", "href"], option => ["value"], ## TODO: moar attributes ); sub is_self_closing_tag { my $tag = shift; for my $t (@SELF_CLOSING_TAGS) { return 1 if $tag eq $t; } return 0; } sub get_tag { my $input = shift; my ($tag) = $input =~ /^([a-z0-9-]+)/i; return "div" unless $tag; if (my $alias = $ALIASES{$tag}) { return $alias; } return $tag; } sub get_classes { my $input = shift; my ($classes_str) = $input =~ /^[a-z0-9-]*\.([a-z0-9-_.]*)/i; return split /\./, $classes_str; } sub get_content { my $input = shift; my ($content_type, $content) = $input =~ /([ >])(.*)/; if ($content_type =~ /^>/) { ## Nested elements $content = process_input($content); } return $content; } sub build_attributes { my $tag = shift; my $attr = ""; if (my $attrs = $ATTRS{$tag}) { for my $a (@$attrs) { if (ref $a eq "ARRAY") { my ($k, $v) = ($a->[0], $a->[1]); $attr .= qq( $k="$v"); } else { $attr .= qq( $a=""); } } } return $attr; } sub build_element { my ($tag, $content, @classes) = @_; my $cls = ""; if (@classes) { $cls = join " ", @classes; $cls = qq( class="$cls"); } my $attr = build_attributes $tag; my $tag_beg = "<$tag$cls$attr"; my $tag_end = (is_self_closing_tag $tag) ? "/>" # Acme does not like self-closing tags without "/" : ">$content"; return "$tag_beg$tag_end"; } sub build_html { return q( ); } sub process_input { my $input = shift; my $output; if ($input eq "!") { $output = build_html; } elsif ($input =~ /^[a-z0-9-_.]+\+/i) { my @siblings = split /\+/, $input; for my $s (@siblings) { $output .= process_input($s); } } else { my $tag = get_tag $input; my $content = get_content $input; my @classes = get_classes $input; $output = build_element $tag, $content, @classes; } return $output; } } my $newline = ""; $newline = "\n" if /\n/; chomp; my ($whitespace) = $_ =~ /^(\s*)/; s/^\s*//; my $output = process_input $_; print "$whitespace$output$newline"; ' "$@"