#! /usr/bin/env perl # Author: Daniel Pfeiffer print "@ARGV"; $opt = '-newwindow'; $style = "$ENV{HOME}/.opera/html2html.css"; $style = -r( $style ) ? "" : <<'EOF'; EOF if( $ARGV[0] =~ /^-(?:[?h]|-help)$/ ) { print <<'EOF'; html2html[ -p|--newpage][ file] Transforms file or stdin to html code showing the highlighted html or xml source of file. The result is shown in opera. You can set this command as your external viewer in opera. That in fact makes it an internal viewer. Without the option it creates a new window. You can change the appearance of highlighting by copying all stylesheet lines out of this script into a file ~/.opera/html2html.css and modifying that. This is recommended even if you don't change it as opera doesn't need to reparse it. A few tags, such as headlines, text-attrs, lists and rough table row structure are also performed. This is supposed to ease recognition of the result, and to help in getting an overview of the structure. Entities have a hover display of the character. EOF exit; } elsif( $ARGV[0] =~ /^-(?:p|-?newpage)$/ ) { $opt = '-newpage'; shift; } sub mask($) { my $copy = $_[0]; for( $copy ) { s/&/&/g; s//>/g; } $copy; } sub tag($) { my( $copy, $insert, $after ) = mask $_[0]; for( $copy ) { $br = /^(?: (?:[bh]r) | \/?(?:[duo]l|dir|menu|blockquote|bq) | \/(?:h[1-6]|p|li|pre) )\b/xis; if( /^(\/?)(h[1-6]|p|li|su[bp]|d[td])\b/is ) { $insert = $1 . $2; $after = $1; } elsif( /^(\/?)([bh]r|pre|tt|[bius]|small|big|strike|em|strong|code|samp|kbd|var|dfn|cite|blockquote|bq|dl)\b/is ) { $insert = $1 . $2; $after = ! $1; $pre = lc( $2 ) eq 'pre' && $after; } elsif( /^(\/?)([uo]l|dir|menu)\b/is ) { $insert = $1 . 'ul'; $after = ! $1; } elsif( /^((\/?)table)\b/is ) { $insert = $2 ? $1 : "$1 border>$2" : "$3" . ($4 ? "$4$5" : '') }esg; s/\n/
\n/g unless $pre; } $copy = "<$copy>"; (!$insert) ? $copy : $after ? "$copy<$insert>" : "<$insert>$copy"; } sub masked_block($$$) { my $class = lc substr $_[2], 1; my $open = tag $_[0]; my $content = mask $_[1]; my $close = tag $_[2]; if( $class eq 'script' ) { $content =~ s,\b(abstract|boolean|break|byte|case|catch|char|class|const|continue|default|delete|do|double|else|export|extends|false|final|finally|float|for|function|goto|if|implements|in|instanceof|int|long|native|new|null|package|private|protected|public|return|short|static|super|switch|synchronized|this|throw|throws|transient|true|try|typeof|var|void|while|with)\b,$1,g; $content =~ s,(//.*|<!(?:--|\[CDATA\[)|\]\]>),$1,mg; } "$open
$content
$close"; } sub br() { my $tmp = $br; $br = 0; $tmp; } sub nontag($$;$$) { my $content = mask $_[0]; $content =~ s/\n/
\n/g unless $pre; "<$_[2]$content$_[3]>"; } undef $/; $_ = <>; ($title) = m!\s*(.*?)\s*!si and !$title and $title = '...'; s( (\n) | &(\#?\w+;?) | <(?: !--(.*?)-- | !([^>]+?(?:\[.+\])?) | \?(.+?)\? | ((script|style|textarea)\b[^>]*?)> \n? (.*?) \n? <(/\7) | (.+?) )> ) { $1 ? ((br || $pre) ? "\n" : "
\n") : $2 ? "&$2" : defined( $3 ) ? nontag $3, comment => '!--', '--' : $4 ? nontag $4, decl => '!' : $5 ? nontag $5, xmldecl => '?', '?' : $6 ? masked_block $6, $8, $9 : tag $10; }esigx; s!!!g; s!<(?=ml>)!!!g; $file = '/tmp/html2html-' . rand( $$ ) . '.html'; open F, "> $file"; print F "[$title] Source"; print F $style; print F; close F; exec 'opera', $opt, $file;