One of the most common needs when documenting software is to write code blocks. The main element for this is programlisting
.
To add a programlisting
element:
-
Position the cursor at the start of the line where you want to add the code block.
-
Press Alt + Enter ⏎ (Windows) or Command ⌘ + Enter ⏎ (Mac) to display the Element Context Menu.
-
Search for the
programlisting
element and select it.When you add a
programlisting
element, a code snippet box appears in the topic. -
Add your code inside the code snippet box.
The programlisting
element is sometimes referred to as "verbatim", as the code you add inside the element will be rendered exactly how it appears in the editor. For example, it will keep spaces and line breaks to represent the code properly. Some other elements also work like this, such as screen
and literallayout
.
Example 36. Programlisting sample
Here, a javascript function has been added inside a programlisting
element. This is how the code looks in the output.
function myFunction() { 1 var x = "", i; for (i=0; i<5; i++) { x = x + "The number is " + i + "<br>"; } document.getElementById("demo").innerHTML = x; 2 }
In the sample shown , a code block is added inside the programlisting element. The javascript function contains (1) on the first line and (2) on the last line. These are callouts, which you can use to Annotate Code Blocks.
You can annotate code blocks by using the calloutlist
element and co
elements. In the published output, the callouts act as clickable "hotspots" that highlight parts of the code. When someone selects a "hotspot", they are taken to the callout text explanation for that "hotspot".
For example, in the image below, the code sample has two callout "hotspots" labelled 1 and 2. Below the code sample, there are numbers that match the hotspots, and next to each number is an annotation that explains the purpose of the corresponding code.
To annotate code using calloutlist
:
-
Select a position in the code block where you want a callout "hotspot" to appear.
-
Press Alt + Enter ⏎ (Windows) or Command ⌘ + Enter ⏎ (Mac) to display the Element Context Menu.
-
Select the
co
element and give it anxml:id value
in the Element attributes section. Eachco
element has to have anxml:id
value that is unique in that specific topic. For example, you could give the firstco
element thexml:id
id_1, the secondco
element thexml:id
id_2 and so on. -
Add a
calloutlist
element after theprogramlisting
element that contains your code block. -
Give each
callout
in thecalloutlist
anxml:id
as well, different from the ones in theco
elements, e.g "callout_1", "callout_2". Again, these ids need to be unique. -
Associate the
co
elements with thecallout
elements:-
For each
co
element, add alinkends
attribute. Set the value of thelinkends
attribute to thexml:id
value of thecallout
that you want to associate it with. -
For each
callout
element, add thearearefs
attribute. Set the value of the arearefs attribute to thexml:id
value of theco
that you want to associate it with.
-
Comments
0 comments
Article is closed for comments.