chiark / gitweb /
Javascript puzzles: switch to a CSS-based drop-down system.
[sgt-puzzles.git] / html / jspage.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 open my $footerfile, "<", shift @ARGV or die "footer: open: $!\n";
7 my $footer = "";
8 $footer .= $_ while <$footerfile>;
9 close $footerfile;
10
11 for my $arg (@ARGV) {
12     $arg =~ /(.*\/)?([^\/]+)\.html$/ or die;
13     my $filename = $2;
14     open my $gamefile, "<", $arg or die "$arg: open: $!\n";
15     my $unfinished = 0;
16     my $docname = $filename;
17     chomp(my $puzzlename = <$gamefile>);
18     while ($puzzlename =~ s/^([^:=]+)(=([^:]+))?://) {
19         if ($1 eq "unfinished") {
20             $unfinished = 1;
21         } elsif ($1 eq "docname") {
22             $docname = $3;
23         } else {
24             die "$arg: unknown keyword '$1'\n";
25         }
26     }
27     my $instructions = "";
28     $instructions .= $_ while <$gamefile>;
29     close $gamefile;
30
31     open my $outpage, ">", "${filename}.html";
32
33     my $unfinishedtitlefragment = $unfinished ? "an unfinished puzzle " : "";
34     my $unfinishedheading = $unfinished ? "<h2 align=center>an unfinished puzzle</h2>\n" : "";
35     my $unfinishedpara;
36     my $links;
37     if ($unfinished) {
38         $unfinishedpara = <<EOF;
39 <p>
40 You have found your way to a page containing an <em>unfinished</em>
41 puzzle in my collection, not linked from the <a href="../">main
42 puzzles page</a>. Don't be surprised if things are hard to understand
43 or don't work as you expect.
44 EOF
45         $links = <<EOF;
46 <p align="center">
47 <a href="../">Back to main puzzles page</a> (which does not link to this)
48 EOF
49     } else {
50         $unfinishedpara = "";
51         $links = <<EOF;
52 <p align="center">
53 <a href="../doc/${docname}.html#${docname}">Full instructions</a>
54 |
55 <a href="../">Back to main puzzles page</a>
56 EOF
57     }
58
59     print $outpage <<EOF;
60 <!DOCTYPE html>
61 <html>
62 <head>
63 <meta http-equiv="Content-Type" content="text/html; charset=ASCII" />
64 <title>${puzzlename}, ${unfinishedtitlefragment}from Simon Tatham's Portable Puzzle Collection</title>
65 <script type="text/javascript" src="${filename}.js"></script>
66 <style class="text/css">
67 /* Margins and centring on the top-level div for the game menu */
68 #gamemenu { margin-top: 0; margin-bottom: 0.5em; text-align: center }
69
70 /* Inside that div, the main menu bar and every submenu inside it is a <ul> */
71 #gamemenu ul {
72     list-style: none;  /* get rid of the normal unordered-list bullets */
73     display: inline;   /* make top-level menu bar items appear side by side */
74     position: relative; /* allow submenus to position themselves near parent */
75     margin: 0;
76     margin-bottom: 0.5em;
77     padding: 0;
78 }
79
80 /* Individual menu items are <li> elements within such a <ul> */
81 #gamemenu ul li {
82     /* Add a little mild text formatting */
83     font-weight: bold; font-size: 0.8em;
84     /* Line height and padding appropriate to top-level menu items */
85     padding-left: 0.75em; padding-right: 0.75em;
86     padding-top: 0.2em; padding-bottom: 0.2em;
87     margin: 0;
88     /* Make top-level menu items appear side by side, not vertically stacked */
89     display: inline;
90     /* Suppress the text-selection I-beam pointer */
91     cursor: default;
92     /* Surround each menu item with a border. The left border is removed
93      * because it will abut the right border of the previous item. (A rule
94      * below will reinstate the left border for the leftmost menu item.) */
95     border-left: 0;
96     border-right: 1px solid rgba(0,0,0,0.3);
97     border-top: 1px solid rgba(0,0,0,0.3);
98     border-bottom: 1px solid rgba(0,0,0,0.3);
99 }
100
101 #gamemenu ul li.disabled {
102     /* Grey out menu items with the "disabled" class */
103     color: rgba(0,0,0,0.5);
104 }
105
106 #gamemenu ul li:first-of-type {
107     /* Reinstate the left border for the leftmost top-level menu item */
108     border-left: 1px solid rgba(0,0,0,0.3);
109 }
110
111 #gamemenu ul li:hover {
112     /* When the mouse is over a menu item, highlight it */
113     background: rgba(0,0,0,0.3);
114     /* Set position:relative, so that if this item has a submenu it can
115      * position itself relative to the parent item. */
116     position: relative;
117 }
118
119 #gamemenu ul li.disabled:hover {
120     /* Disabled menu items don't get a highlight on mouse hover */
121     background: inherit;
122 }
123
124 #gamemenu ul ul {
125     /* Second-level menus and below are not displayed by default */
126     display: none;
127     /* When they are displayed, they are positioned immediately below
128      * their parent <li>, and with the left edge aligning */
129     position: absolute;
130     top: 100%;
131     left: 0;
132     /* We must specify an explicit background colour for submenus, because
133      * they must be opaque (don't want other page contents showing through
134      * them). */
135     background: white;
136     /* And make sure they appear in front. */
137     z-index: 1;
138 }
139
140 #gamemenu ul ul.left {
141     /* A second-level menu with class "left" aligns its right edge with
142      * its parent, rather than its left edge */
143     left: inherit; right: 0;
144 }
145
146 /* Menu items in second-level menus and below */
147 #gamemenu ul ul li {
148     /* Go back to vertical stacking, for drop-down submenus */
149     display: block;
150     /* Inhibit wrapping, so the submenu will expand its width as needed. */
151     white-space: nowrap;
152     /* Override the text-align:center from above */
153     text-align: left;
154     /* Don't make the text any smaller than the previous level of menu */
155     font-size: 100%;
156     /* This time it's the top border that we omit on all but the first
157      * element in the submenu, since now they're vertically stacked */
158     border-left: 1px solid rgba(0,0,0,0.3);
159     border-right: 1px solid rgba(0,0,0,0.3);
160     border-top: 0;
161     border-bottom: 1px solid rgba(0,0,0,0.3);
162 }
163
164 #gamemenu ul ul li:first-of-type {
165     /* Reinstate top border for first item in a submenu */
166     border-top: 1px solid rgba(0,0,0,0.3);
167 }
168
169 #gamemenu ul ul ul {
170     /* Third-level submenus are drawn to the side of their parent menu
171      * item, not below it */
172     top: 0; left: 100%;
173 }
174
175 #gamemenu ul ul ul.left {
176     /* A submenu with class "left" goes to the left of its parent,
177      * not the right */
178     left: inherit; right: 100%;
179 }
180
181 #gamemenu ul li:hover > ul {
182     /* Last but by no means least, the all-important line that makes
183      * submenus be displayed! Any <ul> whose parent <li> is being
184      * hovered over gets display:block overriding the display:none
185      * from above. */
186     display: block;
187 }
188 </style>
189 </head>
190 <body onLoad="initPuzzle();">
191 <h1 align=center>${puzzlename}</h1>
192 ${unfinishedheading}
193 <h2 align=center>from Simon Tatham's Portable Puzzle Collection</h2>
194
195 ${unfinishedpara}
196
197 <hr>
198 <div id="puzzle" style="display: none">
199 <div id="gamemenu"><ul><li id="new">New game</li
200 ><li id="restart">Restart game</li
201 ><li id="undo">Undo move</li
202 ><li id="redo">Redo move</li
203 ><li id="solve">Solve game</li
204 ><li id="specific">Enter game ID</li
205 ><li id="random">Enter random seed</li
206 ><li>Select game type<ul id="gametype" class="left"></ul></li
207 ></ul></div>
208 <div align=center>
209   <div id="resizable" style="position:relative; left:0; top:0">
210   <canvas style="display: block" id="puzzlecanvas" width="1px" height="1px" tabindex="1">
211   </canvas>
212   <div id="statusbarholder" style="display: block">
213   </div>
214   </div>
215   <p>
216     Link to this puzzle:
217     <a id="permalink-desc">by game ID</a>
218     <a id="permalink-seed">by random seed</a>
219   </p>
220 </div>
221 </div>
222 <div id="apology">
223 Sorry, this Javascript puzzle doesn't seem to work in your web
224 browser. Perhaps you have Javascript disabled, or perhaps your browser
225 doesn't provide a feature that the puzzle code requires (such as
226 <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Typed_arrays">typed arrays</a>).
227 These puzzles have been successfully run in Firefox 19, Chrome 26,
228 Internet Explorer 10 and Safari 6.
229 </div>
230 <hr>
231
232 ${instructions}
233
234 ${links}
235
236 ${footer}
237 </body>
238 </html>
239 EOF
240
241     close $outpage;
242 }