Le 17/06/2012 15:49, Adrien Prokopowicz a écrit :
> Le dimanche 17 juin 2012 15:38:22 Benoît Minisini a écrit :
>> Le 12/06/2012 23:52, Adrien Prokopowicz a écrit :
>>> Hi,
>>>
>>> I noticed that the Higlight.Analyze() method doesn't like when the Gambas 
>>> code
>>> hasn't spaces between operators, commas and a few other ones.
>>>
>>> There is a good example of that problem here :
>>> http://www.gambasforge.org/wiki/testHighlight
>>>
>>> Maybe this behaviour is intended, in order to keep Gambas code clean, but I
>>> haven't found any function to add these spaces, like in the IDE.
>>> Do you know where I can find this ?
>>>
>>> Regards,
>>> Adrien.
>>>
>>
>> Do you have some code to show? Maybe you don't use the Analyze() method
>> correctly...
>>
>>
>
> Here is the function that makes the conversion Gambas string ->  html :
>
> Public Sub GambasToHtml(sValue As String) As XmlElement
>
>    Dim sLignes, s, sCode As String
>    Dim i, nbLignes, lenligne As Integer
>    Dim sTypes As String[] = ["", "normal", "keyword", "function", "operator", 
> "symbol", "number", "string", "comment", "", "", "datatype", "", "", "", "", 
> "error", "help", "preprocessor"]
>    Dim Positions, Types, Lengths As New Integer[]
>    Dim lignes As String[]
>    Dim tableau As New HtmlTable
>    Dim elmt As XmlElement
>    Dim conteneur As New XmlElement("code")
>
>    If Not sValue Then Return
>    'sret = "<table class=\"code\"><tr><td class=\"numlignes\">"
>    tableau.ClassName = "code"
>    tableau[0, 0].ClassName = "numlignes"
>    tableau[0, 1].ClassName = "code"
>    tableau[0, 1].AppendChild(conteneur)
>    sValue = Replace$(sValue, "\r\n", "\n") 'Compatibilité win/mac/linux
>    sValue = Trim$(sValue) 'Espaces indésirables
>    lignes = Split(sValue, "\r\n", "", False)
>    For Each s In lignes
>
>      Highlight.Analyze(s)
>
>      If Highlight.Positions.Count = 0 Then
>        conteneur.AppendText(s)
>        conteneur.AppendFromText("<br />")
>        'sCode&= s&  "<br />"
>        Inc nbLignes
>        Continue
>      Endif
>      Positions = Highlight.Positions
>      Types = Highlight.Types
>      Lengths.Clear()
>
>      For i = 0 To Highlight.Symbols.Max
>        Lengths.Push(Len(Highlight.Symbols[i]))
>      Next
>
>      lenligne = Len(s)
>
>      For i = 1 To lenligne
>        If Positions.Count<= 0
>          'sCode&= String.Mid(s, i, 1)
>          conteneur.AppendText(Mid(s, i, 1))
>        Else If i = (Positions[0] + 1)
>          'sCode&= "<span class=\""&  sTypes[Types[0]]&  "\">"&  
> String.Mid$(s, Positions[0] + 1, Lengths[0])&  "</span>"
>          elmt = New XmlElement("span")
>          elmt.ClassName = sTypes[Types[0]]
>          elmt.TextContent = Mid$(s, Positions[0] + 1, Lengths[0])
>          conteneur.AppendChild(elmt)
>          i += (Lengths[0] - 1)
>          Positions.Extract(0)
>          Lengths.Extract(0)
>          Types.Extract(0)
>        ' Else If Mid$(s, i, 1) = " "
>        '   'sCode&= "&nbsp;"
>        '   tableau[0, 1].AppendFromText("&nbsp;")
>        Else
>          'sCode&= String.Mid$(s, i, 1)
>          conteneur.AppendText(String.Mid(s, i, 1))
>        Endif
>      Next
>      'sCode&= "<br />"
>      conteneur.AppendFromText("<br />")
>      Inc nbLignes
>
>    Next
>
>    For i = 1 To nbLignes
>      sLignes&= (i&  "<br />\n")
>    Next
>
>    tableau[0, 0].AppendFromText(sLignes)
>
>    ' sCode = Replace$(sCode, "&","&amp;")
>    ' sCode = Replace$(sCode, "<","&lt;")
>    ' sCode = Replace$(sCode, ">","&gt;")
>    ' sCode = RegexpReplace(sCode, "&lt;(/?(span|br).*)&gt;", "<&1>")
>    ' sCode = Replace$(sCode, "&amp;nbsp;", "&nbsp;")
>    ' sRet&= "</td><td class=\"code\">"
>    ' sret&= sCode
>    ' sRet&= "</td></tr></table>"
>    '   Return XmlElement.FromText(sRet)[0]
>    elmt = New XmlElement("div")
>    elmt.ClassName = "code"
>    elmt.AppendChild(tableau)
>    Return elmt
>
> End
>
>
> The full code is available  here : (svn checkout)
> https://gambasforge.svn.sourceforge.net/svnroot/gambasforge/trunk
>
>
> Regards,
> Adrien.

The Positions are relative to the string returned by 
Highlight.TextAfter. In other words, you just have to add a "s = 
Highlight.TextAfter" line just after the Highlight.Analyze call.

Beware that there was a bug: the value returned by Highlight.TextAfter 
was the original string when the Rewrite argument of Highlight.Analyze() 
was FALSE. Use revision #4844 that fixes that bug.

Otherwise, I suggest you set the Rewrite argument.

Regards,

-- 
Benoît Minisini

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Gambas-user mailing list
Gambas-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/gambas-user

Reply via email to