| 1 | # ex:ts=8 sw=4: | ||||||||||||||
| 2 | # $OpenBSD$ | ||||||||||||||
| 3 | # | ||||||||||||||
| 4 | # Copyright (c) 2026 Dick Olsson <hi@senzilla.io> | ||||||||||||||
| 5 | # | ||||||||||||||
| 6 | # Permission to use, copy, modify, and distribute this software for any | ||||||||||||||
| 7 | # purpose with or without fee is hereby granted, provided that the above | ||||||||||||||
| 8 | # copyright notice and this permission notice appear in all copies. | ||||||||||||||
| 9 | # | ||||||||||||||
| 10 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||||||||||||||
| 11 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||||||||||||||
| 12 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||||||||||||||
| 13 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||||||||||||||
| 14 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||||||||||||||
| 15 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||||||||||||||
| 16 | # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||||||||||||||
| 17 | |||||||||||||||
| 18 | package App::FuguSeed::Sheet; | ||||||||||||||
| 19 | 14 | our $VERSION = '0.2.0'; | |||||||||||||
| 20 | |||||||||||||||
| 21 | 14 | ▶ | use v5.34; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 22 | 14 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 23 | 14 | ▶ | use experimental 'signatures'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 24 | 14 | ▶ | no feature qw(indirect multidimensional bareword_filehandles); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 25 | |||||||||||||||
| 26 | # App::FuguSeed::Sheet - build the printed word sheet (WORDS-BUILD). | ||||||||||||||
| 27 | # | ||||||||||||||
| 28 | # The module builds the sheet text from the list, the template, the | ||||||||||||||
| 29 | # style sheet, and the date. It opens no file and it writes no file | ||||||||||||||
| 30 | # (WORDS-BUILD-8): App::FuguSeed::Words reads the two share files and | ||||||||||||||
| 31 | # prints the result. | ||||||||||||||
| 32 | # | ||||||||||||||
| 33 | # The sheet is the address space of the dice. The cell in block Y, | ||||||||||||||
| 34 | # row B, and column R holds the word of index | ||||||||||||||
| 35 | # (Y-1)*256 + (B-1)*16 + (R-1), so the person does no arithmetic | ||||||||||||||
| 36 | # (D-05, WORDS-BUILD-6). | ||||||||||||||
| 37 | # | ||||||||||||||
| 38 | # The sheet holds no place for a decoy: no class, no identifier, no | ||||||||||||||
| 39 | # style attribute, no script, no comment, and no entity | ||||||||||||||
| 40 | # (WORDS-BUILD-3). App::FuguSeed::Check proves each of those rules on | ||||||||||||||
| 41 | # a built sheet, and it holds its own copy of this grammar: a defect | ||||||||||||||
| 42 | # of this module must not hide in the checker (WORDS-CHECK-3). | ||||||||||||||
| 43 | |||||||||||||||
| 44 | # STYLE_TOKEN and SIDES_TOKEN: | ||||||||||||||
| 45 | # The two places of share/fuguseed/sheet.html that the build | ||||||||||||||
| 46 | # fills. The style sheet arrives byte for byte, so the check can | ||||||||||||||
| 47 | # compare the inlined text with the shipped file (D-09). | ||||||||||||||
| 48 | 14 | ▶ | use constant STYLE_TOKEN => '{{STYLE}}'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 49 | 14 | ▶ | use constant SIDES_TOKEN => '{{SIDES}}'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 50 | |||||||||||||||
| 51 | # SIDES, BLOCKS, ROWS and COLUMNS: | ||||||||||||||
| 52 | # The shape of the sheet. Two sides of four blocks hold the | ||||||||||||||
| 53 | # eight blocks of the YELLOW d8, and each block holds the 16 | ||||||||||||||
| 54 | # rows of the BLUE d16 and the 16 columns of the RED d16 | ||||||||||||||
| 55 | # (WORDS-BUILD-4, WORDS-BUILD-5). | ||||||||||||||
| 56 | 14 | ▶ | use constant SIDES => 2; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 57 | 14 | ▶ | use constant BLOCKS => 4; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 58 | 14 | ▶ | use constant ROWS => 16; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 59 | 14 | ▶ | use constant COLUMNS => 16; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 60 | |||||||||||||||
| 61 | # $class->build(%argument): | ||||||||||||||
| 62 | # The whole sheet as text. | ||||||||||||||
| 63 | # | ||||||||||||||
| 64 | # %argument: | ||||||||||||||
| 65 | # list => $list the list of App::FuguSeed::ListFile | ||||||||||||||
| 66 | # template => $text share/fuguseed/sheet.html | ||||||||||||||
| 67 | # style => $text share/fuguseed/sheet.css | ||||||||||||||
| 68 | # date => $date the build date, as YYYY-MM-DD | ||||||||||||||
| 69 | # | ||||||||||||||
| 70 | # Two builds of one list on one date give one text | ||||||||||||||
| 71 | # (WORDS-BUILD-7). | ||||||||||||||
| 72 | 4 | ▶ | sub build ( $, %argument ) | ||||||||||||
|
Subroutine
build: called CC=2 SCAR=9.2
Pod
documented
| |||||||||||||||
| 73 | 0 | ▶ | { | ||||||||||||
|
Subroutine
build: not called CC=2 SCAR=9.2
Pod
documented
| |||||||||||||||
| 74 | my ( $list, $template, $style, $date ) = | ||||||||||||||
| 75 | 0 | @argument{qw(list template style date)}; | |||||||||||||
| 76 | |||||||||||||||
| 77 | 0 | my $sides = q{}; | |||||||||||||
| 78 | 0 | $sides .= _side( $_, $list, $date ) for 1 .. SIDES; | |||||||||||||
| 79 | |||||||||||||||
| 80 | 0 | my $sheet = _fill( $template, STYLE_TOKEN, $style ); | |||||||||||||
| 81 | |||||||||||||||
| 82 | 0 | return _fill( $sheet, SIDES_TOKEN, $sides ); | |||||||||||||
| 83 | } | ||||||||||||||
| 84 | |||||||||||||||
| 85 | # _side($side, $list, $date): | ||||||||||||||
| 86 | # One side of the sheet: the heading, the legend, the four | ||||||||||||||
| 87 | # blocks, and the footer (WORDS-BUILD-4, WORDS-BUILD-7). | ||||||||||||||
| 88 | 8 | ▶ | sub _side ( $side, $list, $date ) | ||||||||||||
|
Subroutine
_side: called CC=2 SCAR=9.2
| |||||||||||||||
| 89 | 0 | ▶ | { | ||||||||||||
|
Subroutine
_side: not called CC=2 SCAR=9.2
| |||||||||||||||
| 90 | 0 | my $text = | |||||||||||||
| 91 | "<section>\n" | ||||||||||||||
| 92 | . "<h1>FuguSeed word sheet, side $side of " | ||||||||||||||
| 93 | . SIDES | ||||||||||||||
| 94 | . "</h1>\n" | ||||||||||||||
| 95 | . "<ul>\n" | ||||||||||||||
| 96 | . "<li>YELLOW d8 gives the block number, 1 to 8.</li>\n" | ||||||||||||||
| 97 | . "<li>BLUE d16 gives the row number, 1 to 16.</li>\n" | ||||||||||||||
| 98 | . "<li>RED d16 gives the column number, 1 to 16.</li>\n" | ||||||||||||||
| 99 | . "</ul>\n"; | ||||||||||||||
| 100 | |||||||||||||||
| 101 | 0 | my $first = ( $side - 1 ) * BLOCKS + 1; | |||||||||||||
| 102 | 0 | $text .= _block( $_, $list->{words} ) for $first .. $first + BLOCKS - 1; | |||||||||||||
| 103 | |||||||||||||||
| 104 | $text .= | ||||||||||||||
| 105 | "<footer>\n<p>Side $side of " | ||||||||||||||
| 106 | . SIDES | ||||||||||||||
| 107 | . '. The word list is the English list of BIP39.' | ||||||||||||||
| 108 | . ' Its SHA-256 is ' | ||||||||||||||
| 109 | . $list->{digest} | ||||||||||||||
| 110 | 0 | . ". The build date is $date.</p>\n" | |||||||||||||
| 111 | . "</footer>\n" | ||||||||||||||
| 112 | . "</section>\n"; | ||||||||||||||
| 113 | |||||||||||||||
| 114 | 0 | return $text; | |||||||||||||
| 115 | } | ||||||||||||||
| 116 | |||||||||||||||
| 117 | # _block($block, $words): | ||||||||||||||
| 118 | # One block as one table: the caption, the header row of the | ||||||||||||||
| 119 | # RED columns, and the 16 rows of the BLUE die | ||||||||||||||
| 120 | # (WORDS-BUILD-5, WORDS-BUILD-6). | ||||||||||||||
| 121 | 32 | ▶ | sub _block ( $block, $words ) | ||||||||||||
|
Subroutine
_block: called CC=4 SCAR=17.9
| |||||||||||||||
| 122 | 0 | ▶ | { | ||||||||||||
|
Subroutine
_block: not called CC=4 SCAR=17.9
| |||||||||||||||
| 123 | 0 | my $text = "<table>\n<caption>YELLOW $block</caption>\n<tr><td></td>"; | |||||||||||||
| 124 | 0 | $text .= "<th>RED $_</th>" for 1 .. COLUMNS; | |||||||||||||
| 125 | 0 | $text .= "</tr>\n"; | |||||||||||||
| 126 | |||||||||||||||
| 127 | 0 | for my $row ( 1 .. ROWS ) { | |||||||||||||
| 128 | 0 | my $base = | |||||||||||||
| 129 | ( $block - 1 ) * ROWS * COLUMNS + ( $row - 1 ) * COLUMNS; | ||||||||||||||
| 130 | 0 | $text .= "<tr><th>BLUE $row</th>"; | |||||||||||||
| 131 | $text .= '<td>' . $words->[ $base + $_ - 1 ] . '</td>' | ||||||||||||||
| 132 | 0 | for 1 .. COLUMNS; | |||||||||||||
| 133 | 0 | $text .= "</tr>\n"; | |||||||||||||
| 134 | } | ||||||||||||||
| 135 | |||||||||||||||
| 136 | 0 | return $text . "</table>\n"; | |||||||||||||
| 137 | } | ||||||||||||||
| 138 | |||||||||||||||
| 139 | # _fill($text, $token, $value): | ||||||||||||||
| 140 | # $text with the one $token replaced by $value. A template | ||||||||||||||
| 141 | # without the token is a defect of the share file, and the | ||||||||||||||
| 142 | # build dies on it. | ||||||||||||||
| 143 | 8 | ▶ | sub _fill ( $text, $token, $value ) | ||||||||||||
|
Subroutine
_fill: called CC=2 SCAR=10.3
| |||||||||||||||
| 144 | 0 | ▶ | { | ||||||||||||
|
Subroutine
_fill: not called CC=2 SCAR=10.3
| |||||||||||||||
| 145 | 0 | my $at = index $text, $token; | |||||||||||||
| 146 | 0 | ▶ | die "App::FuguSeed::Sheet: the template holds no $token\n" if $at < 0; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 147 | 0 | substr $text, $at, length $token, $value; | |||||||||||||
| 148 | |||||||||||||||
| 149 | 0 | return $text; | |||||||||||||
| 150 | } | ||||||||||||||
| 151 | |||||||||||||||
| 152 | 14 | 1; | |||||||||||||