1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
#!/usr/bin/perl -w
use strict;
#
# Convert a perl script into an xml file
#
# usage:
# perlxmltok myfile.pl >myfile.xml
# perlxmltok <myfile.pl >myfile.xml
#
# The script is broken at the line and token level.
#
# This file is one of the examples distributed with perltidy and demonstrates
# using a callback object with Perl::Tidy to walk through a perl file and
# process its tokens. It may or may not have any actual usefulness. You can
# modify it to suit your own purposes; see sub get_line().
#
use Perl::Tidy;
use IO::File;
use Getopt::Std;
use vars qw($opt_h);
my $file;
my $usage = <<EOM;
usage: perlxmltok filename >outfile
EOM
getopts('h') or die "$usage";
if ($opt_h) {die $usage}
if ( @ARGV == 1 ) {
$file = $ARGV[0];
}
else { die $usage }
my $source;
my $fh;
if ($file) {
$fh = IO::File->new( $file, 'r' );
unless ($fh) { die "cannot open '$file': $!\n" }
$source = $fh;
}
else {
$source = '-';
}
my $formatter = Perl::Tidy::XmlWriter->new($file);
my $dest;
# start perltidy, which will start calling our write_line()
my $err = perltidy(
'formatter' => $formatter, # callback object
'source' => $source,
'destination' => \$dest, # not really needed
'argv' => "-npro -se", # dont need .perltidyrc
# errors to STDOUT
);
if ($err) {
die "Error calling perltidy\n";
}
$fh->close() if $fh;
#####################################################################
#
# The Perl::Tidy::XmlWriter class writes a copy of the input stream in xml
#
#####################################################################
package Perl::Tidy::XmlWriter;
# class variables
use vars qw{
%token_short_names
%short_to_long_names
$rOpts
$missing_html_entities
};
# replace unsafe characters with HTML entity representation if HTML::Entities
# is available
{ eval "use HTML::Entities"; $missing_html_entities = $@; }
sub new {
my ( $class, $input_file ) = @_;
my $self = bless { }, $class;
$self->print( <<"HEADER");
<?xml version = "1.0"?>
HEADER
unless ( !$input_file || $input_file eq '-' || ref($input_file) ) {
$self->print( <<"COMMENT");
<!-- created by perltidy from file: $input_file -->
COMMENT
}
$self->print("<file>\n");
return $self;
}
sub print {
my ( $self, $line ) = @_;
print $line;
}
sub write_line {
# This routine will be called once perl line by perltidy
my $self = shift;
my ($line_of_tokens) = @_;
my $line_type = $line_of_tokens->{_line_type};
my $input_line = $line_of_tokens->{_line_text};
my $line_number = $line_of_tokens->{_line_number};
chomp $input_line;
$self->print(" <line type='$line_type'>\n");
$self->print(" <text>\n");
$input_line = my_encode_entities($input_line);
$self->print("$input_line\n");
$self->print(" </text>\n");
# markup line of code..
if ( $line_type eq 'CODE' ) {
my $xml_line;
my $rtoken_type = $line_of_tokens->{_rtoken_type};
my $rtokens = $line_of_tokens->{_rtokens};
if ( $input_line =~ /(^\s*)/ ) {
$xml_line = $1;
}
else {
$xml_line = "";
}
my $rmarked_tokens = $self->markup_tokens( $rtokens, $rtoken_type );
$xml_line .= join '', @$rmarked_tokens;
$self->print(" <tokens>\n");
$self->print("$xml_line\n");
$self->print(" </tokens>\n");
}
$self->print(" </line>\n");
}
BEGIN {
# This is the official list of tokens which may be identified by the
# user. Long names are used as getopt keys. Short names are
# convenient short abbreviations for specifying input. Short names
# somewhat resemble token type characters, but are often different
# because they may only be alphanumeric, to allow command line
# input. Also, note that because of case insensitivity of xml,
# this table must be in a single case only (I've chosen to use all
# lower case).
# When adding NEW_TOKENS: update this hash table
# short names => long names
%short_to_long_names = (
'n' => 'numeric',
'p' => 'paren',
'q' => 'quote',
's' => 'structure',
'c' => 'comment',
'b' => 'blank',
'v' => 'v-string',
'cm' => 'comma',
'w' => 'bareword',
'co' => 'colon',
'pu' => 'punctuation',
'i' => 'identifier',
'j' => 'label',
'h' => 'here-doc-target',
'hh' => 'here-doc-text',
'k' => 'keyword',
'sc' => 'semicolon',
'm' => 'subroutine',
'pd' => 'pod-text',
);
# Now we have to map actual token types into one of the above short
# names; any token types not mapped will get 'punctuation'
# properties.
# The values of this hash table correspond to the keys of the
# previous hash table.
# The keys of this hash table are token types and can be seen
# by running with --dump-token-types (-dtt).
# When adding NEW_TOKENS: update this hash table
# $type => $short_name
%token_short_names = (
'#' => 'c',
'n' => 'n',
'v' => 'v',
'b' => 'b',
'k' => 'k',
'F' => 'k',
'Q' => 'q',
'q' => 'q',
'J' => 'j',
'j' => 'j',
'h' => 'h',
'H' => 'hh',
'w' => 'w',
',' => 'cm',
'=>' => 'cm',
';' => 'sc',
':' => 'co',
'f' => 'sc',
'(' => 'p',
')' => 'p',
'M' => 'm',
'P' => 'pd',
);
# These token types will all be called identifiers for now
# FIXME: need to separate user defined modules as separate type
my @identifier = qw" i t U C Y Z G :: ";
@token_short_names{@identifier} = ('i') x scalar(@identifier);
# These token types will be called 'structure'
my @structure = qw" { } ";
@token_short_names{@structure} = ('s') x scalar(@structure);
}
sub markup_tokens {
my $self = shift;
my ( $rtokens, $rtoken_type ) = @_;
my ( @marked_tokens, $j, $string, $type, $token );
for ( $j = 0 ; $j < @$rtoken_type ; $j++ ) {
$type = $$rtoken_type[$j];
$token = $$rtokens[$j];
#-------------------------------------------------------
# Patch : intercept a sub name here and split it
# into keyword 'sub' and sub name
if ( $type eq 'i' && $token =~ /^(sub\s+)(\w.*)$/ ) {
$token = $self->markup_xml_element( $1, 'k' );
push @marked_tokens, $token;
$token = $2;
$type = 'M';
}
# Patch : intercept a package name here and split it
# into keyword 'package' and name
if ( $type eq 'i' && $token =~ /^(package\s+)(\w.*)$/ ) {
$token = $self->markup_xml_element( $1, 'k' );
push @marked_tokens, $token;
$token = $2;
$type = 'i';
}
#-------------------------------------------------------
$token = $self->markup_xml_element( $token, $type );
push @marked_tokens, $token;
}
return \@marked_tokens;
}
sub my_encode_entities {
my ($token) = @_;
# escape any characters not allowed in XML content.
# ??s//'/;
if ($missing_html_entities) {
$token =~ s/\&/&/g;
$token =~ s/\</</g;
$token =~ s/\>/>/g;
$token =~ s/\"/"/g;
}
else {
HTML::Entities::encode_entities($token);
}
return $token;
}
sub markup_xml_element {
my $self = shift;
my ( $token, $type ) = @_;
if ($token) { $token = my_encode_entities($token) }
# get the short abbreviation for this token type
my $short_name = $token_short_names{$type};
if ( !defined($short_name) ) {
$short_name = "pu"; # punctuation is default
}
$token = qq(<$short_name>) . $token . qq(</$short_name>);
return $token;
}
sub finish_formatting {
# called after last line
my $self = shift;
$self->print("</file>\n");
return;
}
|