Code Samples
AsciiDoc supports code samples using the [source]
attribute before a new block. If you specify the coding language, the sample will be highlighted correctly in HTML output. Unlike other blocks in Asciidoc, line breaks and space indentations are preserved. Have a look at the example:
.Sample Code (1)
[source,ruby] (2)
==== (3)
puts "Hello, world." (4)
%w{a b c}.each do |x|
puts x
end
==== (5)
-
Title for your code sample (optional)
-
[source]
specifies that the block contains code.ruby
is the coding language. The block will highlight keywords depending on the coding language. -
Starting delimiter
-
Sample code
-
Ending delimiter
Practice time
The code for this exercise is available on GitHub.
The corresponding file is named exercise-2-3-6.adoc
There are some code samples in the document but they are not neatly packed into source
blocks, so they cause trouble.
-
Create a source block with the attribute
ruby
for the code in lines 20-35. -
Create a source block with the attribute
python
for the code in line 38. -
Create a source block with the attribute
java
for the code in lines 41-45.
Hint
Source code blocks look like this:
[source, language]
====
Code
====
We’re done with blocks - good job! The next lesson covers inline markup.