- 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
Handling the Indent Level
This code is not how you really want to indent the HTML; you want more deeply nested tags to be indented further. To accomplish this task, you need to keep track of the level of nesting of the tags, and you can do this by noticing whether the last < found was really < or </. The < character alone marks the beginning of an HTML tag pair, whereas the </ pattern ends an HTML tag pair. Every time you come across a < character alone, you are increasing the level of nesting, entering a new tag pair, and going deeper. Every time you encounter </, you are closing off a tag pair, popping out of one level of nesting of tags. If your code keeps track of this situation, it can insert the right number of spaces to produce the nested indenting you want (Figure 4.41).
Figure 4.41 Indenting the HTML code by inserting spaces before < characters.
To handle indent level:
Open the Indent method handler in the Code Editor.
Edit the method's code to look like this:
// Initialize some variables. 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 // 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 // Remove every return before a C in T. While Instr(S,ReturnC+C) <> 0 S = Replace(S,ReturnC+C,C) Wend // Indent before every tag. // Replace every C with OddChar. While Instr(S,C) <> 0 S = Replace(S,C,OddC) Wend // Replace every OddChar with return + → tab + C. While Instr(S,OddC) <> 0 If Mid(S,Instr(S,OddC)+1,1) = SlashC → Then TabLevel = TabLevel - 1 Else TabLevel = TabLevel + 1 End If IndentS = Mid(SpaceS,1,TabLevel*TabSize) S = Replace(S,OddC,ReturnC+IndentS+C) Wend // Add extra space before closing tabs. // Replace every C/ with OddChar. While Instr(S,C+SlashC) <> 0 S = Replace(S,C+SlashC,OddC) Wend // Replace every OddChar with tab + C/. While Instr(S,OddC) <> 0 IndentS = Mid(SpaceS,1,TabSize) S = Replace(S,OddC,IndentS+C+SlashC) Wend Return S
Save your work, and test it.