PDF

blockstblockst0.4.0

Render Scratch, Blockly, MakeCode and Open Roberta blocks for educational documents.

Scratch#

scratch() — Render Scratch Blocks#

Parses Scratch text and renders the blocks the way Scratch 3 draws them.

#scratch("when green flag clicked\nmove (10) steps")
#scratch(
  text,
  language: "en",
  theme: auto,
  scale: auto,
  font: auto,
  colors: auto,
  line-numbers: auto,
  line-number-start: auto,
  line-number-gutter: auto,
  inset-scale: auto,
)

text is the Scratch block text in the chosen language; the other parameters are the options every editor shares, auto meaning the document’s default. The parser knows the full Scratch 3 vocabulary in all 26 locales, including the pen extension.

raw-scratch — Code fences#

The raw-scratch() show rule renders scratch code fences as blocks:

#show: raw-scratch(language: "en")
when green flag clicked
repeat (4)
  move (30) steps
  turn cw (90) degrees
end

Languages#

Scratch blocks come in 26 languages, from Scratch’s official translations. Set the language on the call or with set-blockst(language: …); block text must match the chosen locale’s vocabulary.

CodeLanguageCodeLanguage
"en"English"de"German
"fr"French"es"Spanish
"it"Italian"nl"Dutch
"pt"Portuguese"pl"Polish
"ru"Russian"ja"Japanese
"ca"Catalan"cs"Czech
"cy"Welsh"el"Greek
"fa"Persian"gd"Scottish Gaelic
"he"Hebrew"hi"Hindi
"hr"Croatian"hu"Hungarian
"id"Indonesian"nb"Norwegian (Bokmål)
"ro"Romanian"sl"Slovenian
"tr"Turkish"ar"Arabic
#set-blockst(scale: 67.5%)
#scratch("
Wenn die grüne Flagge angeklickt
wiederhole (4) mal
  gehe (30) er Schritt
  drehe dich nach rechts um (90) Grad
end
", language: "de")

Right-to-left languages#

Arabic ("ar"), Hebrew ("he") and Persian ("fa") render right-to-left. Nothing has to be switched on: each locale declares its own direction, and the renderer mirrors the layout — the notch, the hat dome, the C-block mouth, the loop arrow, the pen badge, the define hat and the order of the labels all move to the reading edge.

#scratch("
عند نقر @greenFlag
تحرك (10) خطوة
كرِّر (4) مرة
استدر @turnRight (90) درجة
نهاية
", language: "ar")
What is mirrored, and what is not
Layout is mirrored; meaning is not. The loop arrow points back to the top of the loop, which is a claim about the drawing, so it flips with the drawing. @turnRight and @turnLeft are never mirrored — their direction is what the sprite is being told to do, and a mirrored @turnRight would tell the reader to turn the other way.

Arabic short vowels are optional and their order is not canonical, so blocks match whether or not you type the harakat: كرِّر and كرر both find the repeat block.

@category — Quick Color Defaults#

A @category prefix forces a block’s category colour, even without matching the full localized syntax.

@motion         // → default motion block
@motion free text  // → unrecognized block in motion color
TokenNormalizedDefault Block
@motionmotionmove (10) steps
@lookslookssay [Hello!]
@soundsoundplay sound [pop v]
@eventseventswhen green flag clicked
@controlcontrolrepeat (10)
@sensingsensingask [What's your name?] and wait
@operatorsoperators(() + ())
@variablevariablesset [var v] to (0)
@listlistsadd (thing) to [list v]
@penpenclear
@eventevents(alias for events)
@operatoroperators(alias for operators)

When @category is followed by text that matches a known block in that category, the actual block is rendered; otherwise the fallback is an unrecognized block in the forced colour.

@list add (12) to [my list v]    // → DATA_ADDTOLIST
@variable change [score v] by (1) // → DATA_CHANGEVARIABLEBY

scratch-parse() — Parse to AST#

Parses Scratch text to an abstract syntax tree for programmatic use — a nested structure of blocks, inputs and bodies.

#scratch-parse(text, language: "en")

The complete list of Scratch blocks, rendered live, is the Scratch block catalog at the end of this manual. Running Scratch programs as turtle graphics and importing .sb3 files have chapters of their own.