Syntax Highlighting in HTML (12th September 2007)

The syntax highlighting suggestions on public-html are quite varied. There’s lots of variety in what authors do, from the coding websites I sometimes visit. Microformats are gathering code examples.

Until that goes somewhere, here’s a blog post with some VB6 code I played with recently.

It is highlighted (highlit?) same as the IDE, which is conservative but makes a surprising difference to readability. Uses <i> for comments and <b> for keywords with a class on the <pre>. Consecutive words of the same type share the same element.

This approach gives lightweight markup, can be overridden by user CSS and degrades gracefully (such as when printed). This blog entry is probably the only place it is used, though.

What the Code Does

TextStudio is my homebrew text editor. It has an Edit > Delete Line feature (Ctrl+Y) just like the VB6 IDE.

But different newline characters are produced by some tools. Supporting LF and CRLF line feeds, including files which mix them both is the priority. I don’t come across files which use CR as a newline.

Previous Newline

Old

'Start of line:
lngCr = InStrRev(strText, vbCr, SelStart)
lngLf = InStrRev(strText, vbLf, SelStart)
If (Not lngCr = 0) And (lngCr < lngLf) Then
    lngStart = lngLf 'LINE FEED is nearest
ElseIf (Not lngLf = 0) And (lngLf < lngCr) Then
    lngStart = lngCr 'CARRIAGE RETURN is nearest
Else
    lngStart = 0 'use start of file
End If
'Ensure blank lines get selected:
If SelStart <> Len(strText) Then SelStart = SelStart + 1

New

'Start of line:
lngLf = InStrRev(strText, vbLf, SelStart)
If lngLf Then
    lngStart = lngLf
Else
    lngCr = InStrRev(strText, vbCr, SelStart)
    If lngCr Then
        lngStart = lngCr
    End If
End If

With CR Support

'Start of line:
lngLf = InStrRev(strText, vbLf, SelStart)
If lngLf Then
    lngCr = InStrRev(strText, vbCr, SelStart)
    If lngCr = lngLf - 1 Then
        lngStart = lngLf 'starts with CRLF
    Else
        If lngCr < lngLf Then
            lngStart = lngLf 'starts with LF
        Else
            lngStart = lngCr 'starts with CR
        End If
    End If
Else
    'No LF before current line:
    lngCr = InStrRev(strText, vbCr, SelStart)
    If lngCr Then
        lngStart = lngCr 'starts with CR
    End If
End If

Next Newline

Old

'End of line:
lngCr = InStr(SelStart, strText, vbCr)
lngLf = InStr(SelStart, strText, vbLf)
If (Not lngCr = 0) And (lngCr < lngLf) Then
    lngEnd = lngCr 'CARRIAGE RETURN is nearest
ElseIf (Not lngLf = 0) And (lngLf < lngCr) Then
    lngEnd = lngLf 'LINE FEED is nearest
Else
    lngEnd = Len(strText) 'use end of file
    
    'Blank line at end of file?
    If (lngEnd = lngStart) And (lngStart > 2) Then
        'Get previous line start:
        lngCr = InStrRev(strText, vbCr, lngStart - 2)
        lngLf = InStrRev(strText, vbLf, lngStart - 2)
        If (Not lngCr = 0) And (lngCr < lngLf) Then
            lngStart = lngCr 'CARRIAGE RETURN is nearest
        ElseIf (Not lngLf = 0) And (lngLf < lngCr) Then
            lngStart = lngLf 'LINE FEED is nearest
        Else
            lngStart = 0 'use start of file
        End If
    End If
End If

New

'End of line:
lngLf = InStr(SelStart + 1, strText, vbLf)
If lngLf Then
    lngEnd = lngLf
Else
    lngCr = InStr(SelStart + 1, strText, vbCr)
    If lngCr Then
        lngEnd = lngCr
    Else
        lngEnd = Len(strText) + 1
        If lngStart > 0 Then
            lngStart = lngStart - 1 'remove whole of last line
        End If
    End If
End If

With CR Support

'End of line:
SelStart = SelStart + 1
lngLf = InStr(SelStart, strText, vbLf)
If lngLf Then
    lngCr = InStr(SelStart, strText, vbCr)
    If lngCr = lngLf - 1 Then
        lngEnd = lngLf 'ends with CRLF
    Else
        If lngCr < lngLf Then
            lngEnd = lngCr 'ends with CR
        Else
            lngEnd = lngLf ' ends with LF
        End If
    End If
Else
    'No LF after current line:
    lngCr = InStr(SelStart, strText, vbCr)
    If lngCr Then
        lngEnd = lngCr 'ends with CR
    Else
        lngEnd = Len(strText) + 1
        If lngStart > 0 Then
            lngStart = lngStart - 1 'remove whole of last line
        End If
    End If
End If

This is much more reliable, uses simpler conditions and is almost self-documenting. If you know VB6, you know those functions return 0 on failure and 0 will evaluate to False for the conditionals. You also know which of these are 0-based and which are 1-based.

Update: Added CR Support

Some markup which used CR-only newlines came my way.