Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Updates to physics-unit script #15 #17

  • Loading branch information...
commit 32cb09b2e12f95e95d9ae29bb208ddc451ab36d1 1 parent a294cdf
@Klortho authored
View
6 bin/physics-unit
@@ -30,8 +30,8 @@ physics-unit - Helper script for the Physics::Unit Perl module
=head1 SYNOPSIS
- physics-unit meters # Request information about the unit 'meters'
- physics-unit -e # Export a list of units as HTML
+ physics-unit meters # Returns information about the unit 'meters'
+ physics-unit -e # Exports a list of units as HTML
physics-unit --help/-h # Shows a usage message
=head1 DESCRIPTION
@@ -46,7 +46,7 @@ L<Physics::Unit> is a Perl module for managing units and unit conversions. The C
-e
--export
- Export a complete list of known units and types as HTML documents.
+ Export a complete list of known units and types as HTML documents.
Prints the names of the created files to STDOUT, this allows:
firefox `physics-unit -e`
View
3  lib/Physics/Unit/Implementation.pod
@@ -7,7 +7,8 @@ module.
=head2 %reserved_word
-A list of reserved words in Unit expressions.
+A list of reserved words used in unit expressions. For example, "per",
+"square", "cubic".
=head2 %unit_by_name
View
42 lib/Physics/Unit/Script.pm
@@ -12,6 +12,7 @@ $VERSION = eval $VERSION;
use base 'Exporter';
our @EXPORT_OK = qw/run_script name_info/;
+
sub run_script {
my $opts = shift;
@@ -42,33 +43,24 @@ my %classes = (
);
sub name_info {
- my $name = shift;
-
- my $class = Physics::Unit::LookName($name);
- my $string = '';
- my $factor = '';
-
- if ($class == 1) {
- $string = 'NA';
- $factor = 'NA';
- } elsif ($class == 3) {
- $string = GetTypeUnit($name)->ToString;
- } else {
- my $unit = GetUnit($name);
- if ($class == 0 and defined $unit) {
- $class = -1;
+ my $name = shift;
+
+ my $class = Physics::Unit::LookName($name);
+ my $n = GetUnit($name);
+ if ($class == 0 && defined $n) {
+ $class = -1;
}
- my $type = GetTypeUnit($unit->type);
- $string = $unit->ToString;
- $factor = $unit->factor . " " . $type->ToString;
- }
- print <<INFO;
-Name: $name
-Class: $classes{$class}
-Definition: $string
-Conversion: $factor
-INFO
+ print "Name: $name\n" .
+ "Class: $classes{$class}\n";
+
+ if ($class == -1 || $class == 2 || $class == 3) {
+ print "Type: " . $n->type() . "\n" .
+ "Definition: " . $n->def() . "\n" .
+ "Expanded: " . $n->expanded() . "\n";
+ }
+
+ print "\n";
}
1;
View
58 lib/Physics/Unit/Script/GenPages.pm
@@ -15,28 +15,15 @@ our @EXPORT = qw/GenPages/;
sub GenPages
{
my @return;
-
- # Define some constants:
-
- my $trailer = <<END_TRAILER;
- </table>
- </body>
-</html>
-END_TRAILER
-
-
my $outFile;
-
# Generate Units by Name
$outFile = "UnitsByName.html";
push @return, $outFile;
-
open NAMES, ">$outFile" or die "Can't open $outFile for output";
- #print "Generating $outFile\n";
-
print NAMES header("Name");
+
print NAMES tableHeader(1);
for my $name (ListUnits()) {
@@ -44,7 +31,7 @@ END_TRAILER
printrow(1, $name, $n->type(), $n->def(), $n->expanded());
}
- print NAMES $trailer;
+ print NAMES trailer();
close NAMES;
@@ -52,15 +39,12 @@ END_TRAILER
$outFile = "UnitsByType.html";
push @return, $outFile;
-
open NAMES, ">$outFile" or die "Can't open $outFile for output";
- #print "Generating $outFile\n";
-
print NAMES header("Type");
# Print out the "Table of Contents"
- my @t = ('Unknown', 'prefix', ListTypes());
+ my @t = ('unknown', 'prefix', ListTypes());
my @links = map " <a href='#$_'>$_</a>", @t;
print NAMES join ",\n", @links;
@@ -82,7 +66,7 @@ END_TRAILER
printrow(0, $name, $t, $n->def, $n->expanded);
}
- print NAMES $trailer;
+ print NAMES trailer();
close NAMES;
return @return;
@@ -98,22 +82,26 @@ sub header
<html>
<head>
<title>$title</title>
- <style type='text/css'>
- <!--
- th {
- color: white;
- font-size: larger;
- }
- -->
- </style>
+ <link rel="stylesheet" href="http://st.pimg.net/tucs/style.css" type="text/css" />
</head>
- <body background="bg-paper.gif">
-
- <h1>$title</h1>
+ <body>
+ <div class='pod'>
+ <h1>$title</h1>
END_HEADER
}
#-----------------------------------------------------------
+sub trailer
+{
+ return <<END_TRAILER;
+ </table>
+ </div>
+ </body>
+</html>
+END_TRAILER
+}
+
+#-----------------------------------------------------------
sub tableHeader
{
my $printType = shift;
@@ -144,13 +132,13 @@ sub printrow
#-----------------------------------------------------------
# This is used in a couple of places. It returns a human-readable
-# string for a type. If $t is undef, this returns "Unknown";
+# string for a type. If $t is undef, this returns "unknown";
# otherwise, this returns $t.
sub typeStr
{
my $t = shift;
- return !defined $t ? 'Unknown' : $t;
+ return !defined $t || $t eq '' ? 'unknown' : $t;
}
#-----------------------------------------------------------
@@ -189,7 +177,9 @@ sub typeRow
return " <tr bgcolor='#B0D8FC'>\n" .
" <td colspan='4'>\n" .
- " <a name='$ts'>$ts</a> Types\n" .
+ " <a name='$ts'>" .
+ ($ts eq 'prefix' ? 'prefix (dimensionless)' : $ts) .
+ "</a>\n" .
" </td>\n" .
" </tr>\n";
}
Please sign in to comment.
Something went wrong with that request. Please try again.