Page 1 of 1

PB2HTML, is this around somewhere?

Posted: Thu Mar 10, 2011 4:05 pm
by pdwyer
Saw it here viewtopic.php?f=18&t=5269 but it seems to be the old fourms and I don't know what the "resource site" is

Thannks

Re: PB2HTML, is this around somewhere?

Posted: Thu Mar 10, 2011 5:12 pm
by kenmo
Check your own PB installation, /PureBasic/SDK/Syntax Highlighting/example.pb ...

But note that
(A) it could use an update that uses CSS/classes for efficiency, since it currently applies a different font/style to every individual line of code
(B) it technically doesn't produce W3C-valid HTML, but HTML-Tidy or a similar tool could fix this right up

Re: PB2HTML, is this around somewhere?

Posted: Thu Mar 10, 2011 7:51 pm
by Nituvious
PB to HTML wouldn't be that useful. What would be super useful though, would be PB to Javascript/HTML5!

Re: PB2HTML, is this around somewhere?

Posted: Wed Mar 30, 2011 10:33 pm
by tinman
pdwyer wrote:Saw it here viewtopic.php?f=18&t=5269 but it seems to be the old fourms and I don't know what the "resource site" is

Thannks
I'll see if I can find it.

The resources site was at http://www.reelmediaproductions.com/pb IIRC, but that seems to not have the same extent of stuff. You used to be able to upload things to it.

Edit: I've uploaded it at http://www.david-mcminn.co.uk/files/pb2html.zip, but you'll probably find it out of date and doesn't recognise newer syntax so you might be as well using the syntax highlighting example as a basis to write your own.

Re: PB2HTML, is this around somewhere?

Posted: Thu Mar 31, 2011 10:36 am
by Trond
I made something based on the example a while ago. I wanted line numbering with a line continuation character where lines had to be broken. This basic feature turned out to be insanely difficult to accomplish in html and css. Currently it works only in Opera and Firefox (and Firefox doesn't draw the line continuation character nicely). It returns the highlighted output as a string of html code. You need to save it in the body tag of an html file manually.

Code: Select all

Import "SyntaxHilighting.lib"
  SyntaxHighlight(*Buffer, Length, Callback, InlineAsm)
EndImport

Enumeration
  #SYNTAX_Text
  #SYNTAX_Keyword
  #SYNTAX_Comment
  #SYNTAX_Constant
  #SYNTAX_String
  #SYNTAX_Function
  #SYNTAX_Asm
  #SYNTAX_Operator
  #SYNTAX_Structure
  #SYNTAX_Number
  #SYNTAX_Pointer
  #SYNTAX_Separator
  #SYNTAX_Label
EndEnumeration

Procedure FileToList(Filename.s, List Lines.s())
  F = ReadFile(#PB_Any, Filename)
  Fmt = ReadStringFormat(F)
  If F
    Repeat
      AddElement(Lines())
      Lines() = ReadString(F)
    Until Eof(F)
    CloseFile(F)
    ProcedureReturn 1
  EndIf
EndProcedure

Procedure Callback(*Start.Ascii, Length, Color)
  Shared    PB_SYNTAX_HIGHLIGHT.s
  Protected EndSpan = 1
  
  Select Color
    Case #SYNTAX_Keyword
      PB_SYNTAX_HIGHLIGHT + "<span class=pb_keyword>"
    Case #SYNTAX_Comment
      PB_SYNTAX_HIGHLIGHT + "<span class=pb_comment>"
    Case #SYNTAX_Constant
      PB_SYNTAX_HIGHLIGHT + "<span class=pb_constant>"
    Case #SYNTAX_String
      PB_SYNTAX_HIGHLIGHT + "<span class=pb_string>"
    Case #SYNTAX_Function
      PB_SYNTAX_HIGHLIGHT + "<span class=pb_function>"
    Case #SYNTAX_Asm
      PB_SYNTAX_HIGHLIGHT + "<span class=pb_asm>"
    Case #SYNTAX_Operator
      PB_SYNTAX_HIGHLIGHT + "<span class=pb_operator>"
    Case #SYNTAX_Structure
      PB_SYNTAX_HIGHLIGHT + "<span class=pb_structure>"
    Case #SYNTAX_Number
      PB_SYNTAX_HIGHLIGHT + "<span class=pb_number>"
    Case #SYNTAX_Pointer
      PB_SYNTAX_HIGHLIGHT + "<span class=pb_pointer>"
    Case #SYNTAX_Separator
      PB_SYNTAX_HIGHLIGHT + "<span class=pb_separator>"
    Case #SYNTAX_Label
      PB_SYNTAX_HIGHLIGHT + "<span class=pb_label>"
    Default
      EndSpan = 0
  EndSelect
  
  Tmp.s = PeekS(*Start, Length, #PB_UTF8)
  Tmp = ReplaceString(Tmp, "<", "<")
  Tmp = ReplaceString(Tmp, ">", ">")
  Tmp = ReplaceString(Tmp, "  ", "&nbsp;&nbsp;")
  
  PB_SYNTAX_HIGHLIGHT + Tmp
  
  If EndSpan
    PB_SYNTAX_HIGHLIGHT + "</span>"
  EndIf
  
EndProcedure

Procedure.s PBSyntaxHighlight(Text.s)
  Shared PB_SYNTAX_HIGHLIGHT.s
  PB_SYNTAX_HIGHLIGHT = ""
  *Buffer = AllocateMemory(StringByteLength(Text, #PB_UTF8)+2)
  Written = PokeS(*Buffer, Text, -1, #PB_UTF8)
  SyntaxHighlight(*Buffer, Written, @Callback(), 0)
  FreeMemory(*Buffer)
  ProcedureReturn PB_SYNTAX_HIGHLIGHT
EndProcedure

Procedure SkipBlank(List Lines.s())
  FirstElement(Lines())
  While Trim(Lines()) = ""
    DeleteElement(Lines(), 1)
  Wend
EndProcedure

Procedure SkipBlankFromEnd(List Lines.s())
  LastElement(Lines())
  While Trim(Lines()) = ""
    DeleteElement(Lines(), 1)
  Wend
EndProcedure

Procedure SkipIDEComments(List Lines.s())
  LastElement(Lines())
  Options.s = "; IDE Options = PureBasic "
  Options_Len = Len(Options)
  While Left(Lines(), Options_Len) <> Options And PreviousElement(Lines())
  Wend
  If Left(Lines(), Options_Len) = Options
    Repeat
      DeleteElement(Lines())
    Until NextElement(Lines()) = 0
  EndIf
EndProcedure

Procedure.s GetLineNumber(n)
  ProcedureReturn "<td class=pb_lnum>" + Str(n) + "<div class=ln_cont>&#8618;<br>&#8618;<br>&#8618;</div></td>"
EndProcedure

Procedure CropToPage(List Lines.s(), Page.s)
  Protected Line = 1
  Protected Found = 0
  ForEach Lines()
    If Left(Lines(), 2) = ";@"
      P.s = Trim(Mid(Lines(), 3))
      DeleteElement(Lines())
      ;Line - 1
      If Found
        ; Delete the rest of Lines()
        While NextElement(Lines())
          DeleteElement(Lines())
        Wend
        ProcedureReturn Line
      ElseIf P = Page
        Found = 1
      EndIf
    ElseIf Found = 0
      DeleteElement(Lines())
      Line + 1
    EndIf
  Next
  ProcedureReturn Line
EndProcedure

Procedure.s PBSyntaxHighlightFile(Fn.s, Page.s="")
  Protected NewList Lines.s()
  Protected Line = 1
  If FileSize(Fn) <= 0
    ProcedureReturn "File not found: " + Fn
  EndIf
  FileToList(Fn, Lines())
  ; SkipBlank(Lines())
  ; SkipIDEComments(Lines())
  If Page
    Line = CropToPage(Lines(), Page)
  EndIf
  ;SkipBlankFromEnd(Lines())
  
  T.s = "<table class=pb_code>"
  ForEach Lines()
    T + "<tr>" + GetLineNumber(Line)
    T + "<td class=pb_code_td>"
    T + PBSyntaxHighlight(Lines()) + "</td></tr>" + #CRLF$
    Line + 1
  Next
  T.s + "</table>"
  ProcedureReturn T
EndProcedure


; Debug PBSyntaxHighlight("global foreach")
Also you need this css file:

Code: Select all


/* PureBasic syntax highlight */

.pb_code {
    font-family: consolas;
    font-size: 10pt;
    line-height: 160%;
    background-color: white;
    color: black;
    /* table */
    border-spacing: 0px;
    width: 100%;
}

td {
    vertical-align: top;
}

.pb_code_td {
    padding: 0px;
    padding-left: 3px;
}

.pb_lnum {
    width: 1em;
    color: rgb(102, 102, 102);
    font-weight: normal;
    /*background-color: rgb(255, 198, 230);*/
    background-color: rgb(255, 212, 235);
    text-align: right;
    padding: 0px;
    padding-right: 0.3em;
    padding-left: 0.4em;
    overflow: hidden;
}

div.ln_cont {
    max-height: 0em;
}

.pb_code {
    font-weight: normal;
}

.pb_keyword {
    font-weight: bold;
    /*color: rgb(10, 108, 18);*/
    color: rgb(30,70,138);
}

.pb_comment {
    color: blue;
    font-style: italic;
}

.pb_constant {
    color: rgb(122, 24, 112);
    color: rgb(170, 0, 80);
}

.pb_number {
    color: rgb(170, 0, 80);
}

.pb_string {
    color: rgb(150, 94, 0);
}

.pb_function {
    color: rgb(0, 102, 0);
}

.pb_asm {
    color: rgb(132, 0, 72);
}

.pb_operator {
    color: rgb(0, 0, 0);
}

.pb_structure {
    
}

.pb_pointer {
    color: blue;
}

.pb_separator {
    /*color: red;*/
}

.pb_label {

}
The code supports the concept of named "pages". If you pass the name of the page as the second parameter to PBSyntaxHighlightFile() it will highlight that page only (better uncomment the lines starting with Skip also). Here is an example with two pages:

Code: Select all

;@ elephant
Structure MyPointType
  X.d
  Y.d
EndStructure
;@ burning_duck
Define MyPoint.MyPointType
MyPoint\X = 4
MyPoint\Y = 7
Debug MyPoint\X ; Shows 4.0
Debug MyPoint\Y ; Shows 7.0