HTML view source (with syntax highlighting)

Share your advanced PureBasic knowledge/code with the community.
firace
Addict
Addict
Posts: 899
Joined: Wed Nov 09, 2011 8:58 am

HTML view source (with syntax highlighting)

Post by firace »

I'm sharing a tiny code for viewing HTML source, with color highlighting (based on the prismJS library - no need to re-invent the wheel!). Will probably use it for implementing viewsource in my little browser.

screenshot:

Image

Code: Select all

Text$  = "<link  href=https://cdnjs.cloudflare.com/ajax/libs/prism/1.15.0/themes/prism.css rel=stylesheet />" + "<STYLE>body {font-size:18px;}</STYLE>"
Text$  + "<script src=https://cdnjs.cloudflare.com/ajax/libs/prism/1.15.0/prism.js></script><pre><code class='special language-html'></code></pre>"

HtmlFile$ = #PB_Compiler_Home + "\Examples\Sources - Advanced\Atomic Web Server\WWW\Index.html"
ReadFile(1, HtmlFile$) : Text2$ = ReadString(1, #PB_File_IgnoreEOL)

Text2$ = ReplaceString(text2$, "<", "&lt;")
Text2$ = ReplaceString(text2$, ">", "&gt;")
Text$  = ReplaceString(text$, "</code>", Text2$ + "</code>")

OpenWindow(0, 30, 30, 840+40, 450, "view-source:"+GetFilePart(HtmlFile$), #PB_Window_SystemMenu )
WebGadget (0, 0, 0, WindowWidth(0), WindowHeight(0), "")
SetGadgetItemText(0, #PB_Web_HtmlCode, "<meta http-equiv='X-UA-Compatible' content='IE=edge' /> <BODY> " + Text$ )

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow