- Creating Behavior Without Programming
- Using the Code Editor to Write REALbasic Code
- Getting Help with the REALbasic Language
- Mastering Dim and Assignment Statements
- Making Tests and Comparisons
- Writing Code that Branches
- Writing Code that Repeats
- Writing Your Own Methods
- Extending the HTML Editor
- Creating the Indent Menu Item
- Removing Existing Indentation
- Inserting Indentation Before Tags
- Handling the Indent Level
- Extending the Project
Removing Existing Indentation
Before you can write the code that actually does the indenting, your code should remove existing spaces and return characters before tags. That way, you can be sure that existing spaces don't throw the indentation off (Figure 4.40).
Figure 4.40 Removing leading spaces before < characters so that the indentation will be consistent.
To remove spaces:
Open the Code Editor.
Click the Indent method in the browser pane to open it in the editor pane.
Enter this code in the editor pane:
// Remove any existing indentation. // Remove every space before a C in T. While Instr(S,SpaceC+C) <> 0 S = Replace(S,SpaceC+C,C) Wend
To remove returns:
Enter this code at the end of the Indent method:
// Remove every return before a C in T. While Instr(S,ReturnC+C) <> 0 S = Replace(S,ReturnC+C,C) Wend
Enter this code at the beginning of the method:
Dim S as String Dim SpaceC, ReturnC as String Dim SlashC as String Dim OddC, SpaceS as String Dim IndentS as String Dim TabLevel, TabSize as Integer S = T SpaceC = " " ReturnC = Chr(13) SlashC = "/" OddC = Chr(255) SpaceS = " " IndentS = "" TabLevel = 0 TabSize = 3
You don't need all of these variables yet, but enter them now; they'll come in handy shortly.
You should reflect on what you've learned and used in this little bit of code:
You know how to use REALbasic's While structure to repeat a block of code, and you've manipulated text with REALbasic's string methods instr and replace.
Your Indent handler removes spaces and returns from before HTML tags in the HTML text.
Now you're ready to write the code that inserts the needed spaces into the text before the tagsreally just a matter of inserting a few spaces and return characters into the text in the right places. Figuring out how many spaces in what places is the tricky part, but basically, the spaces go before the < characters that mark the start of HTML tags.