 |
Controlling the Range of Lines Displayed for a Posting
The best code example from the point of view of a person who wants to learn a programming language or
how an API works, is short and to-the-point. Sometimes, however, the code example needs
a setup, and this means that the code example might not be as short as the reader would like.
One technique to address this problem is to create some scaffolding. Another technique is to only
display the minimum number of lines necessary when viewing the code example within a narrative document, and
to display the complete code example in the Zamplizer.
The following Zamples REST interface invocations accept range specifications:
The REST invocation to create HTML buttons does yet not support range specifications.
Zamples has two facilities to support showing portions of a complete code example. The first, used
when the code example is retrieved from an HTML button, is to edit the displayed text using a tool like
DreamWeaver. When the code is stored in the Zamples knowledge base, or when it is provided by the Zamples
REST interface, ranges can be specified. Ranges allow portions of the text to be displayed, and
optionally numbered. Each range is set off from the others by a separation delimeter,
which can be customized.
Given some program text to display, following are some range specifications along with
the text that is displayed. First, here is the full program text:
One fine day
in the middle of the night,
two dead men got up to fight.
Back to back, they faced each other;
drew their swords
and shot each other.
The following displayed text uses the default range separator (<hr class=zrangeRule>).
| Range Specification |
Displayed Text |
A null or empty string causes the entire program text to be displayed. |
One fine day
in the middle of the night,
two dead men got up to fight.
Back to back, they faced each other;
drew their swords
and shot each other. |
numbered
Specifying numbered as a range specification causes the entire range listing to be numbered.
The column containing the numbers is exactly wide enough to contain the line number of the last line,
the line numbers are right-justified, and there are two spaces after the line numbers. |
1 One fine day
2 in the middle of the night,
3 two dead men got up to fight.
4 Back to back, they faced each other;
5 drew their swords
6 and shot each other. |
numbered
"middle", 3
Multiple range specifications and range commands can be specified, each of which must be
delimited by newline characters.
Starting from the first line containing the string middle, display down to
and including line 3 (the first line is line #1; otherwise known as origin 1.) |
2 in the middle of the night,
3 two dead men got up to fight. |
numbered
"middle"+1, 3
Starting from the line following the line that contains the string middle, display down to
and including line 3. |
3 two dead men got up to fight. |
1,3
Lines one through three are displayed.
Spaces are ignored before or after a range specification, as are any spaces around the comma. |
One fine day
in the middle of the night,
two dead men got up to fight. |
"middle"+1,"swords"
Display starting from the line after the line in which middle is first found,
down to and including the first following line that contains the string swords. |
two dead men got up to fight.
Back to back, they faced each other;
drew their swords |
"swords"
Simply specifying a string begins the listing at the first line containing that string,
and the remainder of the text is listed. |
drew their swords
and shot each other. |
,"swords"
Specifying a string after a leading comma begins the listing at line one, and continues
down to and including the first line containing that string. |
One fine day
in the middle of the night,
two dead men got up to fight.
Back to back, they faced each other;
drew their swords |
,"swords"-1
As before, range specifications can be modified with +n or -n. |
One fine day
in the middle of the night,
two dead men got up to fight.
Back to back, they faced each other; |
numbered
"swords",+0
Positive numbers after the comma without a preceeding string are relative
to the line number indicated by the line specification before the comma.
For example, +0 means 'same line' (so only that single line will be displayed), +1 means 'next line'
(so only two lines will be displayed), etc. |
5 drew their swords |
numbered
"swords",-1
Negative numbers without a preceeding string refer to lines counted up from the bottom.
For example, -0 means 'last line', -1 means 'second last line', -2 means 'third last line', etc. |
5 drew their swords |
numbered
"swords",-0
-0 is shorthand for 'last line' |
5 drew their swords
6 and shot each other. |
"middle","back"
5,6
numbered
This example shows two ranges and a range command.
As mentioned above, the ranges are delimited by the default range delimiter
(a dashed horizontal rule.) |
2 in the middle of the night,
3 two dead men got up to fight.
4 Back to back, they faced each other;
5 drew their swords
6 and shot each other. |
numbered
5,6
"middle","back"
Just for fun, this example shows the ranges shown immediately above
specified in the reverse order. |
5 drew their swords
6 and shot each other.
2 in the middle of the night,
3 two dead men got up to fight.
4 Back to back, they faced each other; |
bad spec,3
The starting range specification component (bad spec) is not a valid search
string because it is not enclosed within double quotes. If a range
specification component is invalid, a default value is assumed.
The default value for the start range component is 1; the default for the end
range component is the last line of the program text. |
One fine day
in the middle of the night,
two dead men got up to fight. |
Managing Block Indentation
Zamples can display code blocks in any of four modes:
- shiftleft max
- shiftleft byblock [tabs n]
- shiftleft uniform [tabs n]
- shiftleft none [tabs n]
In max mode, all whitespace surrounding each line of text is removed.
The other modes need to know how many spaces any tab characters should expand to;
The default is 4.
The code listing for this section is:
private static int countLeadingSpaces(String string) {
int i;
for (i = 0; i<string.length(); i++) {
char c = string.charAt(i);
switch (c) {
case ' ':
break;
case '\t':
i += 3;
break;
default:
return i;
}
}
return i;
}
We'll use only one code block definition for every example in this section to show how the four
shiftleft modes affect the code display:
"for","default"
"switch","\t"
| shiftleft Mode |
Displayed code |
max
This mode trims all leading and trailing blanks from each line. |
for (i = 0; i<string.length(); i++) {
char c = string.charAt(i);
switch (c) {
case ' ':
break;
case '\t':
i += 3;
break;
default:
switch (c) {
case ' ':
break;
case '\t': |
byblock [tabs n]
Code blocks are shifted left as possible while keeping their shapes.
The left-most line of each code block will all align. |
for (i = 0; i<string.length(); i++) {
char c = string.charAt(i);
switch (c) {
case ' ':
break;
case '\t':
i += 3;
break;
default:
switch (c) {
case ' ':
break;
case '\t': |
uniform [tabs n]
The set of code blocks is shifted left as one unit.
This is the default shiftleft mode. |
for (i = 0; i<string.length(); i++) {
char c = string.charAt(i);
switch (c) {
case ' ':
break;
case '\t':
i += 3;
break;
default:
switch (c) {
case ' ':
break;
case '\t': |
off [tabs n]
Code blocks are not shifted left. |
for (i = 0; i<string.length(); i++) {
char c = string.charAt(i);
switch (c) {
case ' ':
break;
case '\t':
i += 3;
break;
default:
switch (c) {
case ' ':
break;
case '\t': |
|
|  |