باختصار
كتاب ألغاز صيفي للأطفال من سن التاسعة إلى الحادية عشرة، فيه كلمات متقاطعة وبحث عن كلمات ومتاهة. يبيّن كيف تُبنى شبكات الألغاز من أسطر حروف بسيطة.
ما الذي ستنضده
كراسة صيفية للأطفال من سن التاسعة إلى الحادية عشرة: غلاف، وثلاثة ألغاز عن البحر، وصفحة حلول. تبدأ الصفحات من 2 إلى 5 تحت حقل لوني بحافة سفلية متموجة (أزرق البحر، فالنعناعي، فالمرجاني، ثم الأصفر للحلول) وعنوان بحجم 46 pt بخط Lilita One. تبني الشيفرة الكلمات المتقاطعة والبحث عن الكلمات كجدولين من أسطر حروف، وتخصّص لكلٍّ منهما نمط جدول خاصًا به: مربعات مسطَّرة للكلمات المتقاطعة، وبلاطات باهتة بفواصل بيضاء وزوايا مستديرة للبحث عن الكلمات. المربعات السوداء في الكلمات المتقاطعة والكلمة المكتشفة سلفًا في البحث عن الكلمات تعبئة خلايا، وأرقام التلميحات مرفوعة في الزوايا. المتاهة رسم SVG يُولَّد من بذرة. وتطبع الصفحة الأخيرة الحلول الثلاثة جنبًا إلى جنب من البيانات نفسها، فوق إطار عن تعشيش السلاحف ضخمة الرأس.
تجيب هذه الوصفة عن
- كيف أنسّق عدة جداول تنسيقات مختلفة (تعبئة، خلايا مخططة بالتناوب، أطر مستديرة) في مستند واحد؟
- كيف أضيف صورًا وجداول من الشيفرة (موارد) بدل ![]() في Markdown؟
- كيف أكتب الأرقام العلوية والسفلية والصيغ الكيميائية من دون محرّك الرياضيات الكامل؟
- كيف أصنع شارات مضمّنة: مفاتيح لوحة المفاتيح، ووسوم، وبنوك كلمات للتمارين؟
- كيف أضع عمودين داخل إطار (عمود نص بجانب عمود شكل)؟
الجواب المختصر
// Every grid shares tableStyle; a named style sets its size, padding, rules and fills.
// A row is as tall as its lines plus the padding and a column is its share of the
// table's width, so the padding sets how deep a square is.
const CELL = (15 * LEAD) / 9; // pt: nine squares are fifteen lines of text, 9.4 mm each
const PAD = 1; // pt: a clue number sits this close to the corner, clear of the 1 pt rule
const CLUE_FACE = (CELL - 2 * PAD) / (2 * LEAD / BODY); // 8.9 pt: two lines fill a square
const LETTER = 15; // pt: the word search's capitals
const pad = (cell, face) => pt((cell - face * LEAD / BODY) / 2); // one line fills the cell
const KEY = 54; // mm: the three answers are this deep, so their captions share a line
const key = (count) => { // count squares in KEY mm, letters of 4/3 pt per mm of square
const side = (KEY / count) * PT_PER_MM, face = (KEY / count) * 4 / 3;
return { id: `solucion-${count}`, bodyFontSize: pt(face), cellPadding: pad(side, face),
borderWidth: pt(0.5) };
};
// No grid has a head row; the grey head is off for the Cookbook's default-skin check.
const tableStyle = { headerBackgroundEnabled: false, bodyFontFamily: LABEL };
const tableStyles = [
{ id: 'crucigrama', bodyFontSize: pt(CLUE_FACE), cellPadding: pt(PAD), borderWidth: pt(1) },
{ id: 'sopa', bodyFontSize: pt(LETTER), cellPadding: pad(CELL, LETTER), // same squares
bodyBackgroundEnabled: true, bodyBackground: col('tint'), // tiles with white joints
borderColor: col('paper'), borderWidth: pt(2.4), borderRadius: mm(4) },
key(9), key(12), // 6 mm squares with 8 pt letters, 4.5 mm squares with 6 pt letters
];
// A table resource names its style; a cell's own background covers the style's fill.
const here = (width) => ({ position: 'here', width, align: 'center' }); // of the column
const grid = (id, typeId, model, styleId, width, extra) => ({ id, typeId, kind: 'table',
createdAt: 0, updatedAt: 0, table: { model, styleId }, placement: here(width), ...extra });
المكونات
- الميزات
- تعبئة الخلايانمط الجدولالنص المرتفع والمنخفضجداول من البياناتأنماط الجداول المسمّاةالشارات داخل السطرأعمدة داخل الإطار أو النصإطارات التنبيهأنماط العناوينألوان لكل جزءصفحات افتتاح مصمَّمةسمات العنوانالصور في تصاميم الصفحةالأغلفة وصفحات العنوان وبيانات النشرالترويسات وأرقام الصفحاتأنواع موارد مخصّصةأشكال في هذا الموضع بالضبطنمط التعليقالأشكال والجداول بوصفها مواردصفحات على اللوحة (Canvas)
- الخطوط
- Lexend, Lilita One, Chivo Mono (SIL OFL 1.1)
- الأصول
- لا شيء: كل صورة مرسومة بالشيفرة
طريقة التحضير
#1 · نمط جدول واحد، وثلاثة مظاهر
الشيفرة هي الجواب المختصر أعلاه. يضبط tableStyle خط Chivo Mono لكل الشبكات. كل نمط مسمّى في tableStyles يضبط حجم الحرف والحشوة والخطوط الخاصة به، ويختار الجدول أحدها بـtable.styleId. عرض العمود حصته من عرض الجدول، أما الصف فعمقه بقدر أسطره مع الحشوة، فالحشوة إذن تحدد عمق كل مربع. يحتفظ البحث عن الكلمات بمربع الكلمات المتقاطعة البالغ 9.41 مم حول سطر واحد من الحروف الكبيرة بحجم 15 pt، وتحسب pad() مسافة 2.9 pt التي يتركها ذلك السطر فوقه وتحته. خطوطه، بعرض 2.4 pt وبلون الورق، تقطّع التعبئة الباهتة إلى بلاطات، ويدوّر borderRadius زوايا الإطار إلى 4 مم ويقصّ التعبئة عليها. في صفحة الحلول تُدخل key() تسعة مربعات أو اثني عشر في 54 مم: مربعات 6 مم بحروف 8 pt للكلمات المتقاطعة، ومربعات 4.5 مم بحروف 6 pt للبحث عن الكلمات. يُرسم حل المتاهة مربعًا طول ضلعه 54 مم، فتتساوى الحلول الثلاثة في العمق وتشترك تعليقاتها في خط أساس على بعد 106.4 مم من الحافة العليا.
#2 · تسعة مربعات، خمسة عشر سطرًا
// Two lines of CLUE_FACE (a number, an empty line) and the padding make a square.
function crossword(source, solved = false) {
const lines = source.trim().split('\n');
const white = (r, c) => (lines[r]?.[c] ?? '#') !== '#';
let clue = 0;
const rows = lines.map((line, r) => [...line].map((letter, c) => {
if (!white(r, c)) return { content: '', background: col('ink') }; // a black square
if (solved) return { content: letter, align: 'center' };
// A square is numbered when a word starts in it, across or down, in reading order.
const starts = (!white(r, c - 1) && white(r, c + 1)) || (!white(r - 1, c) && white(r + 1, c));
const number = starts ? `**^${++clue}^**` : JOINER; // a bold superscript, or nothing
return { content: `${number}\n${JOINER}` }; // an empty second line would add no depth
}));
return { rows, columnWidths: rows[0].map(() => 1) };
}
يضم المربع الأبيض سطرين من Chivo Mono بحجم 8.9 pt (رقم التلميح وسطر فارغ) داخل حشوة 1 pt، أي 26.7 pt إجمالًا: 9.41 مم، تُسع عرض الجدول. عمق تسعة مربعات 84.7 مم، وهو عمق خمسة عشر سطرًا من نص المتن ارتفاع كل منها 16 pt، ويبدأ إطار التلميحات على شبكة خطوط الأساس بعد سطر واحد. يأخذ المربع رقمًا، بترتيب القراءة، حين تبدأ فيه كلمة أفقية أو عمودية. الرقم حرف علوي عريض بحجم 5.2 pt (58 % من 8.9 pt) يرتفع 3 pt (ثلث 8.9 pt)، فيقع في الزاوية العليا للمربع. السطر الفارغ رابط كلمات (U+2060). فالسطر المتروك فارغًا لا يضيف عمقًا إلى الخلية، ولخرجت الشبكة بعمق 45.5 مم. المربع الأسود خلية فارغة لها background خاصة بها، مرتبطة بـink.
#3 · الشيفرة تجد الكلمات في الشبكة
const STEPS = [[0, 1], [1, 0], [1, 1], [-1, 1], [0, -1], [-1, 0], [-1, -1], [1, -1]];
function locate(letters, word) { // the squares of a word, in any of eight directions
const hits = [];
letters.forEach((line, r) => [...line].forEach((_, c) => STEPS.forEach(([dr, dc]) => {
const cells = [...word].map((_, i) => ({ row: r + i * dr, col: c + i * dc }));
if (cells.every(({ row, col: k }, i) => letters[row]?.[k] === word[i])) hits.push(cells);
})));
if (hits.length !== 1) throw new Error(`${word} is in the grid ${hits.length} times`);
return hits[0];
}
function wordSearch(source, fills) { // fills: [word, palette id] pairs
const letters = source.trim().split('\n').slice(-12);
let model = { rows: letters.map((line) => [...line].map((content) => ({ content,
align: 'center' }))) };
for (const [word, fill] of fills) {
for (const at of locate(letters, word)) model = setCellBackground(model, at, col(fill));
}
return model;
}
الشبكة اثنا عشر سطرًا من الحروف الكبيرة في ملف محتوى خاص بها، بعد قائمة الكلمات المطلوب إيجادها. تبحث locate() عن كل كلمة في ثمانية اتجاهات وتطلق خطأ ما لم تجد تطابقًا واحدًا بالضبط، فخطأ مطبعي في الشبكة أو في القائمة يوقف البناء قبل أن يطبع كلمة لا يستطيع أحد إيجادها. تعيد setCellBackground نموذج جدول جديدًا فيه مربع معبأ إضافي. التعبئة مرتبطة بمدخل في لوحة الألوان وتغطي بلاطة النمط: مرجاني لـUMBRELLA في الصفحة 3، ونعناعي للكلمات العشر كلها في الصفحة 5. يخطّط التنويع الأول الصفوف المتناوبة من نمط الجدول (bodyAlternateBackground)، وتعبئة الخلية نفسها تغطي الخط. بنك الكلمات فوق الشبكة صفّان متوسَّطان من الشارات: شارة UMBRELLA معبأة بالمرجاني كمربعاتها، والتسع الأخرى بيضاء بحدّ كحلي.
#4 · صفحة افتتاح واحدة، بأربعة ألوان
const BAND = 40; // mm: the colour field, from the top edge
const pin = (to, edge, x, y, size) => ({ anchor: { to, edge },
offset: { x: mm(x), y: mm(y) }, ...(size && { size }) });
const text = (id, content, family, size, placement, extra) => ({ kind: 'text', id, content,
fontFamily: family, fontSize: pt(size), color: col('ink'), overflow: 'wrap', placement,
align: placement.anchor.edge.endsWith('right') ? 'right' : 'left', ...extra });
const CAPS = { fontWeight: 700, textTransform: 'uppercase', letterSpacing: em(1 / 6) };
// The field's box sets the opener's depth: the text starts on the next grid line below.
const opener = { enabled: true, slot: { elements: [
{ kind: 'box', id: 'field', style: { backgroundColor: col('band') },
placement: pin('page', 'top-left', 0, 0, { width: 'fill', height: mm(BAND) }) },
{ kind: 'image', id: 'surf', resourceId: 'olas', // the field's foot, cut in waves
placement: pin('page', 'top-left', 0, BAND - 3, { width: mm(PAGE), height: mm(3.2) }) },
text('kicker', '{attr.kicker}', LABEL, 8.5, pin('page', 'top-left', MARGIN, 11), CAPS),
text('title', '{titleText}', DISPLAY, 46, pin('#kicker', 'below', -0.6, 0.5),
{ lineHeight: 1 }), // set solid
text('theme', '{attr.theme}', TEXT, 10, pin('page', 'top-right', -MARGIN, 23.5), {
fontWeight: 700, box: { backgroundColor: col('paper'), borderRadius: mm(3.5),
padding: { top: mm(1.6), right: mm(3.2), bottom: mm(1.6), left: mm(3.2) } } }),
] } };
// parity 'any': each puzzle opens the next page. span 'page' even in one column: a design kept
// in its column is cut at the column's foot, which would trim the cover's picture.
const puzzleLevel = { level: 1, span: 'page', breakBefore: { enabled: true, parity: 'any' },
advancedDesign: opener };
// '# Sopa de letras {style="sopa"}': the style's palette turns 'band' mint on that page,
// in the field, the folio disc and anything else linked to it.
const BANDS = { crucigrama: 'band', sopa: 'mint', laberinto: 'coral', soluciones: 'sun' };
const puzzleStyles = Object.entries(BANDS)
.map(([id, band]) => ({ id, palette: { band: palette[band] } }));
تشترك الصفحات من 2 إلى 5 في تصميم واحد. يمتد حقل لوني 40 مم من الحافة العليا وينتهي بحافة سفلية متموجة مرسومة بلون الورق؛ وتأتي العبارة التمهيدية والعنوان والموضوع في كبسولته البيضاء من سطر العنوان وسماته. الحقل وقرص رقم الصفحة مرتبطان بمدخل لوحة الألوان band، ويحدّد نمط عنوان كل صفحة لـband قيمته الخاصة، فيجعل # Word Search {style="sopa"} كليهما نعناعيًا في الصفحة 3. يضبط المستوى 1 span: 'page' مع أن الكتاب بعمود واحد: التصميم الباقي في عموده يُقصّ عند أسفل العمود، فينتهي رسم الغلاف قبل الحافة السفلى بـ16 مم. صندوق الحقل يُحسب في العمق الذي تحجزه صفحة الافتتاح، فيبدأ النص على سطر الشبكة التالي، على بعد 44.2 مم من الحافة العليا و4 مم تحت الأمواج.
#5 · الحلول من البيانات نفسها
const resourceTypes = [ // a plain resource prints no caption; an answer is numbered
{ id: 'plain', name: 'Plain', shortLabel: '', captionPrefix: '' },
{ id: 'solucion', ...t({ en: { name: 'Solution', captionPrefix: 'Solution' },
es: { name: 'Solución', captionPrefix: 'Solución' } }), shortLabel: 'Sol.' },
].map((type) => ({ numberingTemplate: '{n}', resetOn: 'never', counterFormat: 'decimal',
...type }));
const ANSWER_GAP = 6; // mm between the three answers: gap=6mm on their :::columns fence
const ANSWER_WIDTH = KEY / ((MEASURE - 2 * ANSWER_GAP) / 3); // 54 mm of a 55.3 mm column
const answers = [
grid('sol-crucigrama', 'solucion', crossword(crosswordText, true), 'solucion-9', ANSWER_WIDTH,
{ caption: t({ en: 'Crossword', es: 'Crucigrama' }) }),
grid('sol-sopa', 'solucion', wordSearch(searchText, words.map((w) => [w, 'mint'])),
'solucion-12', ANSWER_WIDTH, { caption: t({ en: 'Word search', es: 'Sopa de letras' }) }),
picture('sol-laberinto', 'solucion', { placement: here(ANSWER_WIDTH), caption: t({
en: 'Maze', es: 'Laberinto' }), altText: t({ en: 'The maze and its way out to the sea',
es: 'El laberinto y su salida al mar' }) }),
];
تستدعي صفحة الحلول crossword() مع solved وwordSearch() مع الكلمات العشر كلها، على ملفات المحتوى نفسها التي تستعملها الألغاز، فأي تعديل في شبكة يغيّر حلها أيضًا. تستعمل الألغاز والرسوم النوع Plain، الذي لا تطبع captionPrefix الفارغة فيه أي تعليق لمورد بلا نص تعليق. وتستعمل الحلول النوع Solution، المرقّم من 1 إلى 3 بترتيب القراءة ليطابق العبارات التمهيدية من Puzzle 1 إلى Puzzle 3. تقع الحلول في مجموعة :::columns{count=3 breaks="2,3" gap=6mm} داخل النص، بلا إطار حولها (postext 1.25): يفتح breaks عمودًا جديدًا عند الحل الثاني والثالث، وتفصل gap بين الأعمدة 6 مم؛ عرض كل حل 54 مم في عمود عرضه 55.3 مم. ويضم إطار Did you know? مجموعة خاصة به، بـbreaks="2"، ليضع رسم العش بجانب فقرته.
الوصفة كاملة
// ═══ Postext Cookbook · Nº 069 · Puzzle book: crossword, word search and maze ═══════
// https://postext.dev/en/cookbook/puzzle-book
// Code: MIT · Text and puzzles: original (CC BY 4.0) · Drawings: generated in code (CC BY 4.0)
// Fonts: Lexend, Lilita One, Chivo Mono (SIL OFL 1.1) · Needs postext ≥ 1.25.0
import {
buildDocumentWithFonts, prepareFonts, renderPageToCanvas, registerResourceImage,
setCellBackground,
} from 'https://esm.sh/postext';
const LANG = 'es'; // @lang: the language of the sample document ('en' | 'es')
const RECIPE = 'puzzle-book';
// ─── 1 · Design ─────────────────────────────────────────────────────────────
const palette = {
ink: '#1f2a44', // text, grid rules and the black squares: a navy near-black
paper: '#ffffff',
band: '#2d9cdb', // a puzzle's colour field: sea blue until a puzzle's style replaces it
sun: '#ffb627', coral: '#ff6f59', mint: '#5cc8a8', // the other fields; coral marks a find
tint: '#fff1cc', // boxes and word-search tiles
muted: '#5b6477', // running titles and the colophon
};
// Every colour is linked to its palette entry by id: a puzzle's palette recolours 'band'.
const col = (id) => ({ hex: palette[id], model: 'hex', paletteId: id });
const colorPalette = Object.entries({ ...palette, 'main-color': palette.ink })
.map(([id, hex]) => ({ id, name: id, value: { hex, model: 'hex' } }));
const [TEXT, DISPLAY, LABEL] = ['Lexend', 'Lilita One', 'Chivo Mono'];
const PAGE = 210, MARGIN = 16; // mm: a square booklet, the same margin on every side
const MEASURE = PAGE - 2 * MARGIN; // 178 mm
const BODY = 11.5, LEAD = 16; // pt: large, open text for young readers
const PT_PER_MM = 72 / 25.4;
const JOINER = '\u2060'; // a word joiner: a line with nothing on it that still counts
// #region answer: a named table style for each grid, picked by the table's styleId
// Every grid shares tableStyle; a named style sets its size, padding, rules and fills.
// A row is as tall as its lines plus the padding and a column is its share of the
// table's width, so the padding sets how deep a square is.
const CELL = (15 * LEAD) / 9; // pt: nine squares are fifteen lines of text, 9.4 mm each
const PAD = 1; // pt: a clue number sits this close to the corner, clear of the 1 pt rule
const CLUE_FACE = (CELL - 2 * PAD) / (2 * LEAD / BODY); // 8.9 pt: two lines fill a square
const LETTER = 15; // pt: the word search's capitals
const pad = (cell, face) => pt((cell - face * LEAD / BODY) / 2); // one line fills the cell
const KEY = 54; // mm: the three answers are this deep, so their captions share a line
const key = (count) => { // count squares in KEY mm, letters of 4/3 pt per mm of square
const side = (KEY / count) * PT_PER_MM, face = (KEY / count) * 4 / 3;
return { id: `solucion-${count}`, bodyFontSize: pt(face), cellPadding: pad(side, face),
borderWidth: pt(0.5) };
};
// No grid has a head row; the grey head is off for the Cookbook's default-skin check.
يعمل ملف script.js المجمّع كما هو: الصقه في سكربت الوحدة (module) لأي صفحة، أو افتح الوصفة على CodePen. مجلد الوصفة على GitHub ↗ (يفتح في تبويب جديد)
تنويعات
#خطّط صفوف البحث عن الكلمات
يساعد أصفر أعمق في كل صف ثانٍ القارئ الصغير على تتبّع سطر من الحروف. وتعبئة الخلية نفسها تغطي الخط، فتبقى UMBRELLA مرجانية؛ أما شبكة الحل فلها نمطها الخاص ولا تأخذ خطوطًا.
bodyBackgroundEnabled: true, bodyBackground: col('tint'), // tiles with white joints
+ bodyAlternateBackgroundEnabled: true, // a shade deeper than the tiles on every other row
+ bodyAlternateBackground: { hex: '#ffdd80', model: 'hex' },
borderColor: col('paper'), borderWidth: pt(2.4), borderRadius: mm(4) },#ارسم متاهة أصعب
واحد وعشرون مربعًا في أحد عشر تتسع في العرض نفسه بـ8.4 مم لكل منها، ويُرسم الحل في الصفحة 5 من المتاهة الجديدة.
-const theMaze = maze(17, 9, 2027);
+const theMaze = maze(21, 11, 2027);أخطاء شائعة
- أرقام التلميحات في الإطار مكتوبة في ملف المحتوى، أما أرقام الشبكة فمحسوبة. بعد تعديل شبكة، راجع التلميحات عليها، فالشيفرة لا تقارن بينهما.
- تعيد لوحة ألوان نمط العنوان تلوين عناصر التصميم والنص وخطوط الجداول وتعبئتها، لكنها لا تبلغ داخل رسم SVG، لذا تُرسم الأمواج في أسفل كل حقل بلون الورق ويخدم رسم واحد الحقول الأربعة.
الحقوق
- الوصفة
- Ignacio Ferro
- النص
- نص أصلي, CC BY 4.0
- الخطوط
- Lexend (SIL OFL 1.1) · Lilita One (SIL OFL 1.1) · Chivo Mono (SIL OFL 1.1)
- الشيفرة
- MIT، مثل Postext
حرّر هذا الشرح ↗ (يفتح في تبويب جديد)مجلد الوصفة على GitHub ↗ (يفتح في تبويب جديد)


