Preserve colours on copy/Paste of code from IDE

Working on new editor enhancements?
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Preserve colours on copy/Paste of code from IDE

Post by Oso »

Hi all, is there a way to either export a source file that includes the IDE's colours, such as the four text colours below, or allow it to be copy/pasted into another tool? Sometimes I print a page of code to take out and peruse over a coffee. The solid colour makes it difficult to follow, in comparison with the screen. Thanks.

Image
BarryG
Addict
Addict
Posts: 3294
Joined: Thu Apr 18, 2019 8:17 am

Re: Preserve colours on copy/Paste of code from IDE

Post by BarryG »

Oso wrote: Tue Nov 22, 2022 8:40 pmSometimes I print a page of code to take out and peruse over a coffee
If it's just a page, you can always zoom out a bit in the IDE (to see it all), then take a screenshot and print that.
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Preserve colours on copy/Paste of code from IDE

Post by Oso »

BarryG wrote: Tue Nov 22, 2022 10:13 pmIf it's just a page, you can always zoom out a bit in the IDE (to see it all), then take a screenshot and print that.
Yes, I guess it's quite a workable option, Barry. Thanks, I'll go with that.

I was thinking afterwards, it would be nice to have an export to HTML and probably not difficult for someone who's familiar with it, given that the IDE already has knowledge of the colours.
AZJIO
Addict
Addict
Posts: 1319
Joined: Sun May 14, 2017 1:48 am

Re: Preserve colours on copy/Paste of code from IDE

Post by AZJIO »

So you can use Notepad++ or AkelPad. There is an export to HTML.

Code: Select all

;
; ------------------------------------------------------------
;
;   PureBasic - Syntax hilightning dll example
;
;    (c) 2010 - Fantaisie Software
;
; ------------------------------------------------------------
;
; The SyntaxHilighting.dll provides the syntax parser of the
; PureBasic IDE in form of a dll, so it can be easily reused
; to do other tasks as well.
;
; Some notes:
;
; - For speed reasons, the dll is not threadsave.
;
; - The hilighter does not handle unicode. It does however handle UTF8, so if
;   Unicode text should be parsed, convert it to UTF8 first.
;
; The dll exports only one function:
;
;    SyntaxHighlight(*Buffer, Length, @Callback(), EnableAsm)
;
; *Buffer and Length specify the text buffer to parse.
;
; Callback() must have the parameters as in the example below and will be called
; for each parsed token.
;
; If EnableAsm is set to nonzero, the parser will report asm keywords also outside of
; the special ! lines, just as the InlineAsm parser does.
;

; Color values returned in the Dll callback
;
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
	#SYNTAX_Module
EndEnumeration

#Dll    = 0
#Input  = 0
#Output = 1

; Callback that is called from the dll.
;
; NOTE: For performance reasons, whitespace characters (space, tab, newline)
; are returned together with the tokens they surround to reduce the number
; of required callback calls. If this is not desired, you must separate them
; here in the callback manually.
;
; The original buffer is not modified. The *Position parameter points to the
; current position in the original buffer.
;
;
Procedure Callback(*Position, Length, Color)
	
	; In this example, we simply write the data as it is to the output
	; buffer, and just apply bold to keywords, and colors to functions and comments.
	;
	Select Color
			
		Case #SYNTAX_Comment
			WriteString(#Output, "<span class='S2'>")
			WriteData(#Output, *Position, Length)
			WriteString(#Output, "</span>")
			
		Case #SYNTAX_Number
			WriteString(#Output, "<span class='S3'>")
			WriteData(#Output, *Position, Length)
			WriteString(#Output, "</span>")
			
		Case #SYNTAX_Function
			WriteString(#Output, "<span class='S4'>")
			WriteData(#Output, *Position, Length)
			WriteString(#Output, "</span>")
			
		Case #SYNTAX_Constant
			WriteString(#Output, "<span class='S5'>")
			WriteData(#Output, *Position, Length)
			WriteString(#Output, "</span>")
			
		Case #SYNTAX_Keyword
			WriteString(#Output, "<span class='S6'>")
			WriteData(#Output, *Position, Length)
			WriteString(#Output, "</span>")
			
		Case #SYNTAX_Operator
			WriteString(#Output, "<span class='S9'>")
			WriteData(#Output, *Position, Length)
			WriteString(#Output, "</span>")
			
		Case #SYNTAX_String
			WriteString(#Output, "<span class='S16'>")
			WriteData(#Output, *Position, Length)
			WriteString(#Output, "</span>")
			
		Case #SYNTAX_Asm
			WriteString(#Output, "<span class='S15'>")
			WriteData(#Output, *Position, Length)
			WriteString(#Output, "</span>")
			
		Case #SYNTAX_Text
			WriteString(#Output, "<span class='S7'>")
			WriteData(#Output, *Position, Length)
			WriteString(#Output, "</span>")
			
		Case #SYNTAX_Structure
			WriteString(#Output, "<span class='S8'>")
			WriteData(#Output, *Position, Length)
			WriteString(#Output, "</span>")
			
		Case #SYNTAX_Pointer
			WriteString(#Output, "<span class='S10'>")
			WriteData(#Output, *Position, Length)
			WriteString(#Output, "</span>")
			
		Case #SYNTAX_Separator
			WriteString(#Output, "<span class='S11'>")
			WriteData(#Output, *Position, Length)
			WriteString(#Output, "</span>")
			
		Case #SYNTAX_Label
			WriteString(#Output, "<span class='S14'>")
			WriteData(#Output, *Position, Length)
			WriteString(#Output, "</span>")
			
		Case #SYNTAX_Module
			WriteString(#Output, "<span class='S13'>")
			WriteData(#Output, *Position, Length)
			WriteString(#Output, "</span>")
			
		Default
			WriteString(#Output, "<span class='S0'>")
			WriteData(#Output, *Position, Length)
			WriteString(#Output, "</span>")
			
	EndSelect
	
EndProcedure

; Simple example code. It loads a PB file and outputs a HTML file withs some
; coloring for functions, keywords and comments
;
If OpenLibrary(#Dll, #PB_Compiler_Home + "SDK\Syntax Highlighting\SyntaxHighlighting.dll")
	
	InputFile$ = OpenFileRequester("Select PB File", "*.pb", "PB Files|*.pb|All Files|*.*", 0)
	If InputFile$
		OutputFile$ = SaveFileRequester("Select Target file", InputFile$+".html", "Html Files|*.html|All Files|*.*", 0)
		
		If ReadFile(#Input, InputFile$) And CreateFile(#Output, OutputFile$)
			Length = Lof(#Input)
			*Buffer = AllocateMemory(Length)
			
			If *Buffer
				ReadData(#Input, *Buffer, Length)
				
				WriteStringN(#Output, "<html><body><pre>")
				CallFunction(#Dll, "SyntaxHighlight", *Buffer, Length, @Callback(), 0)
				WriteStringN(#Output, "</pre></html></body>")
			EndIf
			
			CloseFile(#Input)
			CloseFile(#Output)
		EndIf
		
	EndIf
	
	CloseLibrary(#Dll)
EndIf

css

Code: Select all

/* Общий стиль */
body
{
	background-color:#F3F3F3;
	font-family: Verdana, Arial, 'Segoe UI', 'Lucida Grande', 'MS sans serif';
	font-size:x-small;
	font-weight:normal;
	color:#000000;
}

.codebox  /* общий для примеров кода */
{
	border: #aaa 1px solid;
	padding: 8px;
	font-size: x-small;
	font-family: Consolas, 'Courier New', Courier, mono;
	color: #000;
	background-color: #ffffef
}

.codebox1  /* общий для примеров кода */
{
	border: #aaa 1px solid;
	padding: 8px;
	font-size: x-small;
	font-family: Consolas, 'Courier New', Courier, mono;
	color: #000;
	white-space: normal;
	background-color: #ffffdf
}

/* =============================
Подсветка кода в примерах*/

.S0 /* ничего нет на S0 */
{
	color: #333333;
}
.S1  /* ничего нет на S1 */
{
	color: #00AAAA;
	font-style: italic;
}
.S2  /* Комментарий */
{
	color: #00AAAA;
	font-style: italic;
}
.S3  /* Число */
{
	color: #0061C1;
}
.S4  /* Функция */
{
	color: #006666;
}
.S5  /* #Константа */
{
	color: #924B72;
}
.S6  /* Ключевое слово */
{
	color: #005566;
	font-weight: bold;
}
.S7  /* по умолчанию */
{
	color: #000000;
}
.S8  /* Structure - Структура */
{
	color: #FF8040;
}
.S9  /* Переменные */
{
	color: #000000;
}
.S10  /* Pointer - Указтели */
{
	color: #400080;
}
.S11  /* Separator - Разделители  ;,[] S11 */
{
	color: #0000A0;
}
.S12   /* Оператор *+/- */
{
	color: #008080;
}
.S13  /* Module - Модули S13 */
{
	color: #924B72;
}
.S14  /* Label - Метка S12 */
{
	color: #FF0080;
}
.S15   /* Asm - Ассемблерные вставки 15 */
{
	color: #924B72;
}
.S16   /* Строка в кавычках */
{
	color: #800080;
}

span   /*  */
{
	font-family: Arial;
	color: #0000FF;
}
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Preserve colours on copy/Paste of code from IDE

Post by Oso »

AZJIO wrote: Wed Nov 23, 2022 2:12 am So you can use Notepad++ or AkelPad. There is an export to HTML.
Thanks very much AZJIO, that works well. It displayed in a very tiny proportional-spaced font at first, so I changed the fonts to Consolas/Courier New family and also removed the italics for comments.

For anyone else interested in this, you have to add the second line below, between <HTML> and <BODY>, to point to the separate css file, named here as mystyle.css. This could be added to the code of course. Maybe a job for another day :D Incidentally, it's very helpful to view code in a wide browser, especially since in my case I use a lot of long end-of-line comments.

Code: Select all

<html>
<link rel="stylesheet" href="mystyle.css">
<body><pre>
User avatar
Tenaja
Addict
Addict
Posts: 1948
Joined: Tue Nov 09, 2010 10:15 pm

Re: Preserve colours on copy/Paste of code from IDE

Post by Tenaja »

I haven't used it in probably ten years, but there used to be an export to html in the ide... Didn't there?
Post Reply