+private class NitunitDecorator
+ super HTMLDecorator
+
+ var executor: NitUnitExecutor
+
+ redef fun add_code(v, block) do
+ var code = block.raw_content
+ var meta = "nit"
+ if block isa BlockFence and block.meta != null then
+ meta = block.meta.to_s
+ end
+ # Do not try to test non-nit code.
+ if meta != "nit" then return
+ # Try to parse code blocks
+ var ast = executor.toolcontext.parse_something(code)
+
+ # Skip pure comments
+ if ast isa TComment then return
+
+ # We want executable code
+ if not (ast isa AModule or ast isa ABlockExpr or ast isa AExpr) then
+ var message = ""
+ if ast isa AError then message = " At {ast.location}: {ast.message}."
+ executor.toolcontext.warning(executor.mdoc.location, "invalid-block", "Error: there is a block of invalid Nit code, thus not considered a nitunit. To suppress this warning, enclose the block with a fence tagged `nitish` or `raw` (see `man nitdoc`).{message}")
+ executor.failures.add("{executor.mdoc.location}: Invalid block of code.{message}")
+ return
+ end
+
+ # Create a first block
+ # Or create a new block for modules that are more than a main part
+ if executor.blocks.is_empty or ast isa AModule then
+ executor.blocks.add(new Buffer)
+ end
+
+ # Add it to the file
+ executor.blocks.last.append code
+ end
+end
+