chiark / gitweb /
printable trading plans
authorIan Jackson <ian@liberator.relativity.greenend.org.uk>
Sun, 18 Oct 2009 16:49:02 +0000 (17:49 +0100)
committerIan Jackson <ian@liberator.relativity.greenend.org.uk>
Sun, 18 Oct 2009 16:49:02 +0000 (17:49 +0100)
yarrg/CommodsWeb.pm
yarrg/TODO
yarrg/example-plan.html [deleted file]
yarrg/web/autohandler
yarrg/web/lookup
yarrg/web/qtextstring
yarrg/web/routetrade
yarrg/web/script

index 0f3f43df8fd236975a57ef5310cccdbf4f14b25f..37bbfe7dcbcc2d878eb5a2d7016458b616358e35 100644 (file)
@@ -50,7 +50,7 @@ BEGIN {
     @ISA         = qw(Exporter);
     @EXPORT      = qw(&dbw_connect &dbw_filename &ocean_list &sourcebasedir
                      &to_json_shim &to_json_protecttags
-                     &set_ctype_utf8 &webdatadir
+                     &set_ctype_utf8 &webdatadir &printable
                      &expected_error &dbw_lookup_string
                      &prettyprint_age &meta_prettyprint_age);
     %EXPORT_TAGS = ( );
@@ -200,6 +200,15 @@ sub expected_error ($) {
     die $r;
 }
 
+sub printable ($) { # printable($m)  where $m is the Mason request object
+    my ($m) = @_;
+    my $a= scalar $m->caller_args(-1);
+    foreach my $t (qw(pdf ps html)) {
+       return $t if $a->{"printable_$t"};
+    }
+    return 0;
+}
+
 package CommodsWeb::ExpectedError;
 
 sub emsg ($) {
index 1486d1a6c44dbab8b50c420cc9c7658f92d84788..b202aad35c4cbc7b8a334035efd5df372001c0b6 100644 (file)
@@ -1,9 +1,3 @@
-
-printable trading plan
-  curl -s 'http://yarrg.chiark.net/lookup?routestring=eta%2C+jorvik&capacitystring=&lossperleague=&capitalstring=&submit=Go' >example-plan.html
-  <example-plan.html perl -ne 'print unless m/^\<script /..m/^\<\/script\>/' |   htmldoc >t.ps -t pdf --continuous --gray --size 210x279mm --nup 2 -
-
-
 UPLOADER
 --------
 
diff --git a/yarrg/example-plan.html b/yarrg/example-plan.html
deleted file mode 100644 (file)
index 7187c8c..0000000
+++ /dev/null
@@ -1,315 +0,0 @@
-<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
-<!--
-    This HTML is generated by the YARRG website, which is
-    Copyright 2009 Ian Jackson, Clare Boothby, Steve Early.
-
-    The YARRG website is Free Software and licenced according to the
-    GNU Affero General Public Licence (v3 or later).  See the link at
-    the bottom of this page for full licence and copyright
-    information.
-
-    Many YARRG web pages also display data from the YARRG database,
-    which contains mostly contributions from YARRG users and is not
-    covered by the AGPL.
--->
-
-
-
-
-<html lang="en"><head><title>Trades for route - YARRG</title>
-<style type="text/css">
-body {
-  color: #000000;
-  background: #ffffff;
-}
-tr.datarow0 { background: #e3e3e3; }
-tr.datarow1 { background: #ffffff; }
-</style>
-
-</head><body>
-
-QUERY HERE
-
-
-commit 8198bdb1afdf8ebedb130006e3af2af6b11045ab
-Author: Ian Jackson <ian@liberator.relativity.greenend.org.uk>
-Date:   Thu Sep 24 19:01:44 2009 +0100
-
-    Use full URLs for query sites
-
-commit 207dec6556d4642ce7223e243f08af3bbe8fd157
-Author: Ian Jackson <ian@liberator.relativity.greenend.org.uk>
-Date:   Thu Sep 24 19:00:12 2009 +0100
-
-    YARRG website is fine
-
-commit 19c49821438f668c75868ca696bd0b06c6a47d2a
-Author: Ian Jackson <ian@liberator.relativity.greenend.org.uk>
-Date:   Thu Sep 24 18:58:48 2009 +0100
-
-    Document that we actually upload to the main PCTB db
-
-commit 68843821b559b609abb26e50ed20171b108fb60c
-Author: Ian Jackson <ian@liberator.relativity.greenend.org.uk>
-Date:   Wed Sep 23 20:58:09 2009 +0100
-
-    Switch to main PCTB server now we are approved
-
-commit 016f2797ee2dfea8948bbb1247eaa9fc5234a35a
-Author: Ian Jackson <ian@liberator.relativity.greenend.org.uk>
-Date:   Thu Sep 24 00:09:43 2009 +0100
-
-    Dump bogus XML in _commodmap.xml if we can't parse it
-
-commit 938b4dd547bfc4d9538a5714b6f21ab3da50d8d1
-Author: Ian Jackson <ian@liberator.relativity.greenend.org.uk>
-Date:   Wed Sep 23 23:55:45 2009 +0100
-
-    Better HTTP User-Agent strings (rot13 the library, say who we are)
-
-commit c810347487961a59b3d8dfb2fed123f49dc22ed4
-Author: Ian Jackson <ijackson@chiark.greenend.org.uk>
-Date:   Wed Sep 23 15:12:46 2009 +0100
-
-    Do not allow caller to specify arbitrary content-types
-
-commit a9a88c14c5a5bd057932f76ad721b5f0fc845c20
-Author: Ian Jackson <ijackson@chiark.greenend.org.uk>
-Date:   Tue Sep 22 15:25:57 2009 +0100
-
-    Minor html style changes
-
-
-commit 8198bdb1afdf8ebedb130006e3af2af6b11045ab
-Author: Ian Jackson <ian@liberator.relativity.greenend.org.uk>
-Date:   Thu Sep 24 19:01:44 2009 +0100
-
-    Use full URLs for query sites
-
-commit 207dec6556d4642ce7223e243f08af3bbe8fd157
-Author: Ian Jackson <ian@liberator.relativity.greenend.org.uk>
-Date:   Thu Sep 24 19:00:12 2009 +0100
-
-    YARRG website is fine
-
-commit 19c49821438f668c75868ca696bd0b06c6a47d2a
-Author: Ian Jackson <ian@liberator.relativity.greenend.org.uk>
-Date:   Thu Sep 24 18:58:48 2009 +0100
-
-    Document that we actually upload to the main PCTB db
-
-commit 68843821b559b609abb26e50ed20171b108fb60c
-Author: Ian Jackson <ian@liberator.relativity.greenend.org.uk>
-Date:   Wed Sep 23 20:58:09 2009 +0100
-
-    Switch to main PCTB server now we are approved
-
-commit 016f2797ee2dfea8948bbb1247eaa9fc5234a35a
-Author: Ian Jackson <ian@liberator.relativity.greenend.org.uk>
-Date:   Thu Sep 24 00:09:43 2009 +0100
-
-    Dump bogus XML in _commodmap.xml if we can't parse it
-
-commit 938b4dd547bfc4d9538a5714b6f21ab3da50d8d1
-Author: Ian Jackson <ian@liberator.relativity.greenend.org.uk>
-Date:   Wed Sep 23 23:55:45 2009 +0100
-
-    Better HTTP User-Agent strings (rot13 the library, say who we are)
-
-commit c810347487961a59b3d8dfb2fed123f49dc22ed4
-Author: Ian Jackson <ijackson@chiark.greenend.org.uk>
-Date:   Wed Sep 23 15:12:46 2009 +0100
-
-    Do not allow caller to specify arbitrary content-types
-
-commit a9a88c14c5a5bd057932f76ad721b5f0fc845c20
-Author: Ian Jackson <ijackson@chiark.greenend.org.uk>
-Date:   Tue Sep 22 15:25:57 2009 +0100
-
-    Minor html style changes
-
-
-<h1>Voyage trading plan</h1>
-<table rules=groups>
-<tr bgcolor="#808080"><td colspan=6>
-<tr><td colspan=4>
-<strong>Start at Eta Island</strong>
-<td colspan=2>(Data age: <span id="da_0">2 days</span>)
-<tr bgcolor="#e0e0e0" class="datarow0">
-<td rowspan=2>Collect
-<td rowspan=2>Wood
-<td>Finagle&#39;s Distilling Stall
-<td rowspan=2 align=right>22 poe ea.
-<td rowspan=2 align=right>276 unit(s)
-<td rowspan=2 align=right>6072 total
-<tr bgcolor="#e0e0e0">
-<td>Samjones&#39;s Ironworking Stall
-<tr class="datarow1">
-<td rowspan=3>Collect
-<td rowspan=3>Wood
-<td>Acute Irony
-<td rowspan=3 align=right>23 poe ea.
-<td rowspan=3 align=right>647 unit(s)
-<td rowspan=3 align=right>14881 total
-<tr class="datarow1">
-<td>Fumundacheez&#39;s Ironworking Stall
-<tr class="datarow1">
-<td>Tesla Foils
-<tr>
-<td colspan=1>
-<td colspan=2>In hold 161525kg, 230750 l.
-
-<td colspan=2 align=right>Outlay
-<td align=right>20953 total
-<tr bgcolor="#808080"><td colspan=6>
-<tr><td colspan=4>
-<strong>Sail to Jorvik Island</strong>
-- 5 leagues,
- 22991poe at risk
- </td>
-<td colspan=2>(Data age: <span id="da_1">2 days</span>)
-<tr class="datarow0">
-<td rowspan=4>Deliver
-<td rowspan=4>Wood
-<td>Biker&#39;s Distilling Stall
-<td rowspan=4 align=right>25 poe ea.
-<td rowspan=4 align=right>839 unit(s)
-<td rowspan=4 align=right>20975 total
-<tr class="datarow0">
-<td>Canterbury Ales
-<tr class="datarow0">
-<td>Darkseid&#39;s Ironworking Stall
-<tr class="datarow0">
-<td>Spearhalk&#39;s Distilling Stall
-<tr class="datarow1">
-<td rowspan=2>Deliver
-<td rowspan=2>Wood
-<td>Affjordable Furnishing
-<td rowspan=2 align=right>24 poe ea.
-<td rowspan=2 align=right>84 unit(s)
-<td rowspan=2 align=right>2016 total
-<tr class="datarow1">
-<td>Helsingkor
-<tr>
-<td colspan=1>
-<td colspan=2>
-<td colspan=2 align=right>Proceeds
-<td align=right>22991 total
-<tr bgcolor="#808080"><td colspan=6>
-<tr class="datarow0">
-<td rowspan=1>Collect
-<td rowspan=1>Iron
-<td>Helsingkor
-<td rowspan=1 align=right>14 poe ea.
-<td rowspan=1 align=right>21 unit(s)
-<td rowspan=1 align=right>294 total
-<tr class="datarow1">
-<td rowspan=1>Collect
-<td rowspan=1>Rambutan
-<td>Yggdrasil
-<td rowspan=1 align=right>51 poe ea.
-<td rowspan=1 align=right>2 unit(s)
-<td rowspan=1 align=right>102 total
-<tr>
-<td colspan=1>
-<td colspan=2>
-<td colspan=2 align=right>(Arbitrage) outlay
-<td align=right>396 total
-<tr bgcolor="#808080"><td colspan=6>
-<tr class="datarow0">
-<td rowspan=1>Deliver
-<td rowspan=1>Iron
-<td>Jergs&#39;s Ironworking Stall
-<td rowspan=1 align=right>15 poe ea.
-<td rowspan=1 align=right>21 unit(s)
-<td rowspan=1 align=right>315 total
-<tr class="datarow1">
-<td rowspan=1>Deliver
-<td rowspan=1>Rambutan
-<td>Merchant of Vanir
-<td rowspan=1 align=right>59 poe ea.
-<td rowspan=1 align=right>2 unit(s)
-<td rowspan=1 align=right>118 total
-<tr>
-<td colspan=1>
-<td colspan=2>
-<td colspan=2 align=right>(Arbitrage) proceeds
-<td align=right>433 total
-<tr bgcolor="#808080"><td colspan=6><tr>
-<td colspan=3>Total distance: 5 leagues.
-<td colspan=2 align=right>Overall net cash flow
-<td align=right><strong>2075 gain</strong>
-</table>
-
-<script type="text/javascript">
-<!--
-
-  function da_Refresh() {
-    var now= Date.now();
-    debug('updating now='+now);
-    for (var ageid in da_ages) {
-      var oldage= da_ages[ageid];
-      var el= document.getElementById(ageid);
-      var age= oldage + (now - da_pageload) / 1000;
-      var newhtml=         age < 60 ?             'less than a minute'                    :
-        age < 60*2 ?           '1 minute'                              :
-        age < 3600*2 ?         Math.floor (age/60) +' minutes'        :
-        age < 86400*2 ?        Math.floor (age/3600) + ' hours'       :
-                                Math.floor (age/86400) + ' days';
-;
-      el.innerHTML= newhtml;
-    }
-  }
-  da_ages= {"da_1":173565,"da_0":173605};
-  window.setInterval(da_Refresh, 10000);
-  register_onload(da_Refresh);
-
-// -->
-</script>
-
-
-
-
-</form>
-
-
-<p>
-
-
-
-<script type="text/javascript">
-<!--
-
-function debug (m) {
-}
-
-// -->
-</script>
-
-
-<hr>
-<address>
-YARRG is Yet Another Revenue Research Gatherer, a project of the
-crew Special Circumstances on the Midnight Ocean
-and of the Sinister Greenend Organisation.
-<p>
-
-YARRG is Free Software.
-You may share and modify the code and the
-website, according to the terms of the GNU General Public Licence and
-the GNU Affero General Public Licence respectively (v3 or later).
-Note that there is <strong>NO WARRANTY</strong>.
-Please see the <a href="devel">YARRG Development webpage</a> for
-details of how to obtain the client and server code and full details
-of the licences.
-
-<p>
-YARRG is Copyright 2009 Ian Jackson, Clare Boothby, Steve Early.
-Yohoho and Puzzle Pirates are trademarks of Three Rings and are used
-without permission.  YARRG is not endorsed or sponsored by Three
-Rings.
-
-</address>
-
-
-
index 7344f076fb2c9d871d233a53b41d4669059285a5..55b9a93695552295b4899d5a52fd0fd5ca4f3651 100644 (file)
  copyright message.
 
 
-</%doc><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
+</%doc><%perl>
+
+use CommodsWeb;
+
+my $printable= printable($m);
+if ($printable eq 'pdf' || $printable eq 'ps') {
+       my $output;
+       my $got= $m->call_self(\$output);
+       if ($got) {
+               my $tmpfile= IO::File::new_tmpfile();
+               print $tmpfile $output or die $!;
+               $tmpfile->flush() or die $!;
+               seek $tmpfile,0,0 or die $!;
+               my $htmldoc= open HTMLDOC, "-|";
+               defined $htmldoc or die $!;
+               if (!$htmldoc) {
+                       eval {
+                               $ENV{'HTMLDOC_NOCGI'}=1;
+                               open STDIN, '<&', $tmpfile or die $!;
+                               exec qw(htmldoc -t),$printable,qw(
+                                   --continuous --gray --size 210x279mm -);
+                               die $!;
+                       };
+                       print STDERR "HTMLDOC FAILURE $@";
+                       _exit(1);
+               }
+               my ($data,$read);
+               $r->content_type($printable eq 'pdf' ? 'application/pdf' :
+                                               'application/postscript');
+               while ($read= read HTMLDOC,$data,32768) { print $data; }
+               defined $read or die $!;
+               $?=0; $!=0; close HTMLDOC or die "$! $? $output ";
+               return;
+       }
+}
+set_ctype_utf8();
+$r->content_type('text/html; charset=UTF-8');
+
+</%perl><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 <!--
     This HTML is generated by the YARRG website, which is
     <& copyrightdate &>.
     covered by the AGPL.
 -->
 
-% $m->call_next;
-
-<%init>
-use CommodsWeb;
-set_ctype_utf8();
-$r->content_type('text/html; charset=UTF-8');
-</%init>
+% $m->call_next();
index 2f67760c487d6f1281b37bcb819ca6a7b2dd3d01..a2ccc8de0a80df6b3659fafc95bdc41976888deb 100755 (executable)
@@ -146,6 +146,7 @@ tr.datarow1 { background: #ffffff; }
 </&script>
 </head><body>
 
+% if (!printable($m)) {
 <a href="<% $m->current_comp()->name() |u %>">YARRG</a> -
  Yet Another Revenue Research Gatherer
 |
@@ -155,6 +156,7 @@ tr.datarow1 { background: #ffffff; }
 |
 <a href="devel">development</a>
 <p>
+% }
 <%perl>
 
 foreach my $var (@vars) {
@@ -186,6 +188,7 @@ my $quri= sub {
 
 my $prselector_core= sub {
        my ($var)= @_;
+       return if printable($m);
        my $name= $var->{Name};
        my $lname= lc $var->{Name};
        my $delim= $var->{Before};
@@ -251,7 +254,9 @@ my $someresults= sub {
 $debug => 0
 </%args>
 
+% if (!printable($m)) {
 <hr>
+% }
 
 <& "query_$styles{Query}", %baseqf, %queryqf, %styles,
     quri => $quri, dbh => $dbh, queryqf => \%queryqf, allargs => \%ARGS,
index 93eb1249633499550bf763cb7c33ccd1c735115b..e046c56c3d4f90c7fe4b3034836015ca64e73879 100644 (file)
@@ -97,6 +97,7 @@ function <%$p%>Ready() {
 register_onload(<%$p%>Needed);
 </&script>
 
+% if (!printable($m)) {
 <input type="text" <% $m->content %>
  id="<% $thingstring %>" name="<% $thingstring %>"
  onchange="<%$p%>Needed();" onkeyup="<%$p%>Later();"
@@ -104,6 +105,11 @@ register_onload(<%$p%>Needed);
  ><% defined($helpref) ? "<a href=\"docs#$helpref\">[?]</a>" : '' %>
 <br>
 <div id="<%$p%>results">&nbsp;</div><br>
+% } else {
+<kbd><strong><% $stringval |h %></strong></kbd>
+<br>
+<br>
+% }
 
 <%perl>
 if ($significant_nonempty || length $thingstring) {
index c6ed408d80fe9d779e54de724856c6abf02ab673..1f8df2a5afdb6eafd5215a38f71cce74ad03dce5 100644 (file)
@@ -585,17 +585,23 @@ $addcols->({ Total => 0, DoReverse => 1, TotalSubflows => 1 }, qw(
 
 % } # ========== OPTIMISATION ==========
 
+% if (!printable($m)) {
 <h2>Contents</h2>
 <ul>
 % if ($optimise) {
  <li><a href="#plan">Voyage trading plan</a>
   <ul>
    <li><a href="#summary">Summary statistics</a>
+   <li>Printable:
+         <input type=submit name=printable_pdf value="PDF">
+         <input type=submit name=printable_html value="HTML">
+         <input type=submit name=printable_ps value="PostScript">
   </ul>
 % }
  <li><a href="#dataage">Data age summary</a>
  <li><a href="#trades">Relevant trades</a>
 </ul>
+% }
 
 % if ($optimise) { # ========== TRADING PLAN ==========
 %
@@ -781,7 +787,7 @@ Expected average profit:
 %
 % } # ========== TRADING PLAN ==========
 
-% {
+% if (!printable($m)) {
 <h2><a name="dataage">Data age summary</a></h2>
 <%perl>
        my $sth_i= $dbh->prepare(<<END);
@@ -825,10 +831,19 @@ END
 <&| query_age:agestable, now => $now, fetchrow => $fetchrow &>
 Islands shown in reverse order of visits.<br>
 </&>
+% } else {
+%      my @tl= gmtime $now or die $!;
+<p>
+Generated by YARRG at <strong><%
+       sprintf "%04d-%02d-%02d %02d:%02d:%02d UTC",
+               $tl[5]+1900, @tl[4,3,2,1,0]
+                       |h %></strong>.
+%#     
 % }
 
-% my %ts_sortkeys;
-% {
+% if (!printable($m)) {
+%   my %ts_sortkeys;
+%   {
 %      my $cdspan= $qa->{ShowStalls} ? ' colspan=2' : '';
 %      my $cdstall= $qa->{ShowStalls} ? '<th>Stall</th>' : '';
 <h2><a name="trades">Relevant trades</a></h2>
@@ -882,14 +897,14 @@ Islands shown in reverse order of visits.<br>
 <th>Capital
 <th>Profit
 %      }
-% }
+%   }
 
 <tr id="trades_sort">
-% foreach my $col (@cols) {
+%   foreach my $col (@cols) {
 <th>
-% }
+%   }
 
-% foreach my $flowix (0..$#flows) {
+%   foreach my $flowix (0..$#flows) {
 %      my $flow= $flows[$flowix];
 %      my $rowid= "id_row_$flow->{UidShort}";
 <tr id="<% $rowid %>" class="datarow<% $flowix & 1 %>">
@@ -923,17 +938,17 @@ Islands shown in reverse order of visits.<br>
  %>><% exists $spec->{String} ? $spec->{String} : $v |h %>
 %              $ci += $spec->{Span};
 %      }
-% }
+%   }
 <tr id="trades_total">
 <th>
 <th colspan=2>Total
-% foreach my $ci (3..$#cols) {
+%   foreach my $ci (3..$#cols) {
 %      my $col= $cols[$ci];
 <td align=right>
 %      if (defined $col->{Total}) {
 <% $col->{Total} |h %>
 %      }
-% }
+%   }
 </table>
 
 <&| tabsort, cols => \@cols, table => 'trades', rowclass => 'datarow',
@@ -943,6 +958,8 @@ Islands shown in reverse order of visits.<br>
 
 <input type=submit name=update value="Update">
 
+% } # !printable
+
 <%init>
 use CommodsWeb;
 use Commods;
index b8e7d03364827e39ed4f05e1d697f7a489dfef05..05ba792a96055f16f89f33e9f1486860f4665ec1 100644 (file)
     http://www.htmlhelp.com/tools/validator/problems.html#script
 
 </%doc>
+<%perl>
+my $ct= $m->content;
+
+die "bad script content $&"
+    if $ct =~ m,\<\/|--\>,;
+</%perl>
+% if (!printable($m)) {
 <script type="text/javascript">
 <!--
 <% $ct %>
 // -->
 </script>
-<%init>
-my $ct= $m->content;
-
-die "bad script content $&"
-    if $ct =~ m,\<\/|--\>,;
-</%init>
+% }