34
35:- module(swish_render_sudoku,
36 [ term_rendering//3 37 ]). 38:- use_module(library(apply)). 39:- use_module(library(http/html_write)). 40:- use_module(library(http/term_html)). 41:- use_module('../render'). 42
43:- register_renderer(sudoku, "Render a sudoku matrix").
68term_rendering(Term, _Vars, _Options) -->
69 { is_sudoku(Term)
70 }, !,
71 html(div([class(sudoku),
72 'data-render'('Sudoku matrix')
73 ],
74 [\rows(Term, 1), \sudoku_style])).
75
76sudoku_style -->
77 html({|html||
78 <style>
79div.sudoku { vertical-align: top;
80 display:inline-block;
81 border: 3px solid black;
82 width: 220px;
83 height: 220px;
84 font-size: 0;
85 }
86div.sudoku-row { height: 11.11%; }
87div.sudoku-row.fat { border-bottom: 2px solid black;}
88div.sudoku-cell { width: 11.11%; height: 100%;
89 font-size: 12px;
90 font-weight: bold;
91 display: inline-block;
92 box-sizing: border-box;
93 border: 1px solid #888;
94 margin: 0px;
95 text-align: center;
96 vertical-align: middle;
97 }
98div.sudoku-cell.fat { border-right: 2px solid black;}
99 </style>
100 |}).
101
102rows([], _) --> [].
103rows([H|T], I) -->
104 { I2 is I+1,
105 ( (I == 3 ; I == 6)
106 -> Extra = [fat]
107 ; Extra = []
108 )
109 },
110 html(div(class(['sudoku-row'|Extra]), \cells(H, 1))),
111 rows(T, I2).
112
113cells([], _) --> [].
114cells([H|T], I) -->
115 { I2 is I+1,
116 ( (I == 3 ; I == 6)
117 -> Extra = [fat]
118 ; Extra = []
119 )
120 },
121 html(div(class(['sudoku-cell'|Extra]), \value(H))), cells(T, I2).
122
123value(H) --> { var(H) }, !.
124value(H) --> term(H, []).
132is_sudoku(Term) :-
133 is_list(Term),
134 length(Term, 9),
135 maplist(is_row, Term).
136
137is_row(Row) :-
138 is_list(Row),
139 length(Row, 9),
140 maplist(is_cell, Row).
141
142is_cell(Var) :- var(Var).
143is_cell(I) :- integer(I), between(1, 9, I)
SWISH Sudoku renderer
Renders a term like below as a sudoku matrix
*/