Re: UserScript • PB-SyntaxHighlighting für Codes im Forum
Verfasst: 17.04.2021 01:06
Mit dem neuen Forum-Update / Design möchte ich euch hiermit auch eine neue Version des User Scripts für das Forum-Syntax-Highlighting zur Verfügung stellen. Installieren oder einrichten könnt ihr User Scrips z.B. mit Tampermonkey.
Ja ich weiß, dass es wohl Probleme mit FireFox und GreaseMonkey geben kann, weil es dort kein "lookbehind" gibt. Allerdings komme ich da nicht drum herum. Ich bitte dies zu entschuldigen, vielleicht ändert sich das ja irgendwann mal.
Das eigentliche Java Script gibt es hier:
Damit jeder sein eigenes gewohntes Theme aus PureBasic nutzen kann, könnt ihr dieses Code in PureBasic ausführen und die Debuggerausgabe in den Bereich oben bei ==PureBasicPreference== einfügen. Es enthält die Editoreinstellungen wie Farben, Tab-Länge und CustomKeywords:
Sieht dann z.B. so aus:

Ja ich weiß, dass es wohl Probleme mit FireFox und GreaseMonkey geben kann, weil es dort kein "lookbehind" gibt. Allerdings komme ich da nicht drum herum. Ich bitte dies zu entschuldigen, vielleicht ändert sich das ja irgendwann mal.
Das eigentliche Java Script gibt es hier:
Code: Alles auswählen
// ==UserScript==
// @namespace STARGÅTE
// @name Pure Basic Syntax Highlighting
// @description Pure Basic Syntax Highlighting (V 2.0.2) (20. May 2021)
// @include https://purebasic.com/german/*
// @include https://purebasic.fr/german/*
// @include https://purebasic.fr/english/*
// @include https://purebasic.fr/french/*
// @include https://www.purebasic.com/german/*
// @include https://www.purebasic.fr/german/*
// @include https://www.purebasic.fr/english/*
// @include https://www.purebasic.fr/french/*
// @include https://forums.purebasic.com/german/*
// @include https://forums.purebasic.com/english/*
// @include https://forums.purebasic.fr/german/*
// @include https://forums.purebasic.fr/english/*
// @include https://forums.purebasic.fr/french/*
// ==/UserScript==
//--------------------------------------------
// Version 2.0.1 (17. Apr 2021)
//--------------------------------------------
// Farbdefinition für das Syntax Highlighting
// color definition for the syntax highlighting
//===================================================
var PBSH_Highlight = new Object();
// ==PureBasicPreference==
PBSH_Highlight.EnableKeywordBolding = 1;
PBSH_Highlight.TabLength = 2;
PBSH_Highlight.CustomKeywords = 'CompilerCase|CompilerDefault|CompilerElse|CompilerElseIf|CompilerEndIf|CompilerEndSelect|CompilerError|CompilerFor|CompilerIf|CompilerNext|CompilerSelect';
PBSH_Highlight.EditorFontName = 'Consolas';
PBSH_Highlight.EditorFontSize = '10';
PBSH_Highlight.EditorFontStyle = '';
PBSH_Highlight.BackgroundColor = '#101010';
PBSH_Highlight.NormalTextColor = '#A0A0A0';
PBSH_Highlight.BasicKeywordColor = '#5090D0';
PBSH_Highlight.CommentColor = '#C04040';
PBSH_Highlight.ConstantColor = '#FFD000';
PBSH_Highlight.StringColor = '#FFFFFF';
PBSH_Highlight.PureKeywordColor = '#80C040';
PBSH_Highlight.ASMKeywordColor = '#A0A040';
//PBSH_Highlight.PointerColor = '#ADE4B5';
PBSH_Highlight.NumberColor = '#C0C0C0';
//PBSH_Highlight.StructureColor = '#006090';
PBSH_Highlight.LabelColor = '#C080FF';
PBSH_Highlight.ModuleColor = '#208000';
PBSH_Highlight.OperatorColor = '#FFFFFF';
//PBSH_Highlight.SeparatorColor = '#FFFFFF';
PBSH_Highlight.CustomKeywordColor = '#3060E0';
// ==/PureBasicPreference==
//===================================================
// UserScript für die Syntaxhervorhebung
// user script for syntax highlighting
//===================================================
// Regulärer Ausdruck für das SyntaxHighlighting
// regular expressions for the syntax lighting
if (PBSH_Highlight.CustomKeywords == '') PBSH_Highlight.CustomKeywords = 'NoCustomKeywordsDefined';
var PBSH_Syntax = new RegExp(
/*Comment*/ '(;.*$)|' +
/*Constant*/ '(\\#\\w*\\$?|\'.*?(?:\'|$))|' +
/*String*/ '(~"(?:\\\\.|[^\\\\])*?(?:"|$)|"[^"]*?(?:"|$))|' +
/*CustomKeyword*/ '((?:^[ \\t]+|\\b)(?:'+PBSH_Highlight.CustomKeywords+')\\b)|' +
/*Keyword*/ '((?:^[ \\t]+|\\b)(?:And|Array|As|Break|CallDebugger|Case|CompilerCase|CompilerDefault|CompilerElse|CompilerElseIf|CompilerEndIf|CompilerEndSelect|CompilerError|CompilerIf|CompilerSelect|CompilerWarning|Continue|Data|DataSection|EndDataSection|Debug|DebugLevel|Declare|DeclareC|DeclareCDLL|DeclareDLL|DeclareModule|EndDeclareModule|Default|Define|Dim|DisableASM|DisableDebugger|DisableExplicit|Else|ElseIf|EnableASM|EnableDebugger|EnableExplicit|End|Enumeration|EnumerationBinary|EndEnumeration|FakeReturn|For|ForEach|ForEver|Global|Gosub|Goto|If|EndIf|Import|EndImport|ImportC|IncludeBinary|IncludeFile|IncludePath|Interface|EndInterface|List|Macro|EndMacro|MacroExpandedCount|Map|Module|EndModule|NewList|NewMap|Next|Not|Or|Procedure|EndProcedure|ProcedureC|ProcedureCDLL|ProcedureDLL|ProcedureReturn|Protected|Prototype|PrototypeC|Read|ReDim|Repeat|Restore|Return|Runtime|Select|EndSelect|Shared|Static|Step|Structure|EndStructure|StructureUnion|EndStructureUnion|Swap|Threaded|To|UndefineMacro|Until|UseModule|UnuseModule|Wend|While|With|EndWith|XIncludeFile|XOr)\\b|\\b[ \\t]+(?:Align|Extends)\\b)|' +
/*Structure*/ '((?<=\\.)[ \\t]*(?:[^\\Wabcdfilqsuw](?=\\b)|\\w\\w+)|(?<=\\\\)\\w+\\$?[ \\t]*(?![\\w\\( \\t]))|' +
/*Function*/ '(\\w+\\$?(?=(?:[ \\t]*\\.[ \\t]*\\w+[ \\t]*|[ \\t]*)\\())|' +
/*ASM*/ '(^[ \\t]*!.*?(?:$|(?=(?<!>|<|&);)))|' +
/*Pointer*/ '(\\@\\*?[\\w\\$]*|(?:(?<![ \\t\\w$)}\\]])|(?<=\\5))[ \\t]*\\*[\\w\\$]+|\\?\\w*)|' +
/*Number*/ '((?:\\b\\d+\\.?\\d*(?:e[+\\-]?\\d+)?|\\$[\\dabcdef]+|\\%[01]+))|' +
/*Module*/ '(\\w+(?=[ \\t]*::))|' +
/*Label*/ '(^[ \\t]*\\w+\\$?[ \\t]*:(?!:))|' +
/*Operator*/ '((?:[+*/\\-|!%=~]|::|>|<|&)+)|' +
/*Seperator*/ '([()\\[\\]\\\\,.:])',
'gim');
// Hervorheben einer Zeichenkette
// highlighting a string
function PBSH_Highlighting(str, key)
{
if (PBSH_Highlight[key])
return '<font color="'+PBSH_Highlight[key]+'">'+str+'</font>';
else
return str;
}
function PBSH_Highlighting_Keyword(str, key)
{
if (PBSH_Highlight[key])
{
if (PBSH_Highlight.EnableKeywordBolding)
return '<font color="'+PBSH_Highlight[key]+'"><b>'+str+'</b></font>';
else
return '<font color="'+PBSH_Highlight[key]+'">'+str+'</font>';
}
else
if (PBSH_Highlight.EnableKeywordBolding)
return '<b>'+str+'</b>';
else
return str;
}
// Ersetzen einer Zeichenkette
// replace a string
function PBSH_Replace(str, isComment, isConstant, isString, isCustomKeyword, isKeyword, isStructure, isFunction, isASM, isPointer, isNumber, isModule, isLabel, isOperator, isSeparator)
{
if (isCustomKeyword) return PBSH_Highlighting_Keyword(isCustomKeyword, 'CustomKeywordColor');
else if (isKeyword) return PBSH_Highlighting_Keyword(isKeyword, 'BasicKeywordColor');
else if (isComment) return PBSH_Highlighting(isComment, 'CommentColor');
else if (isConstant) return PBSH_Highlighting(isConstant, 'ConstantColor');
else if (isString) return PBSH_Highlighting(isString, 'StringColor');
else if (isFunction) return PBSH_Highlighting(isFunction, 'PureKeywordColor');
else if (isASM) return PBSH_Highlighting(isASM, 'ASMKeywordColor');
else if (isPointer) return PBSH_Highlighting(isPointer, 'PointerColor');
else if (isNumber) return PBSH_Highlighting(isNumber, 'NumberColor');
else if (isStructure) return PBSH_Highlighting(isStructure, 'StructureColor');
else if (isModule) return PBSH_Highlighting(isModule, 'ModuleColor');
else if (isLabel) return PBSH_Highlighting(isLabel, 'LabelColor');
else if (isOperator) return PBSH_Highlighting(isOperator, 'OperatorColor');
else if (isSeparator) return PBSH_Highlighting(isSeparator, 'SeparatorColor');
}
// Durchführen der Syntaxhervorhebung
// perform the syntax highlighting
function PureBasicSyntaxHighlighting()
{
// Anwenden der Syntaxhervorhebung auf alle <code>-Elemente
// apply the syntax highlighting on all the <code>-elements
var allMyDivs = document.getElementsByTagName('code');
for(var i = 0; i < allMyDivs.length; i++)
{
if (PBSH_Highlight.EditorFontName)
allMyDivs[i].style.font = PBSH_Highlight.EditorFontStyle + " " + PBSH_Highlight.EditorFontSize + "pt " + PBSH_Highlight.EditorFontName;
if (PBSH_Highlight.BackgroundColor)
allMyDivs[i].style.backgroundColor = PBSH_Highlight.BackgroundColor;
if (PBSH_Highlight.NormalTextColor)
allMyDivs[i].style.color = PBSH_Highlight.NormalTextColor;
allMyDivs[i].style.tabSize = PBSH_Highlight.TabLength;
var code = ''+allMyDivs[i].innerHTML+'';
code = code.replace(/<br>/gi, '\r\n');
//code = code.replace(/ /gi, '\t');
code = code.replace(/<\/?[^<>]*>/gi, '');
code = code.replace(PBSH_Syntax, PBSH_Replace);
//code = code.replace(/\t/gi, ' ');
//code = code.replace(/\r\n/gi, '<br>');
allMyDivs[i].innerHTML = code;
}
}
PureBasicSyntaxHighlighting();
//===================================================
Code: Alles auswählen
EnableExplicit
Procedure.s HTMLColor(Color)
ProcedureReturn "#"+RSet(Hex(Red(Color)),2,"0")+RSet(Hex(Green(Color)),2,"0")+RSet(Hex(Blue(Color)),2,"0")
EndProcedure
Procedure Export(PreferencesFileName.s)
Protected Keyword.s, String.s, CustomKeywords.s
If OpenPreferences(PreferencesFileName)
PreferenceGroup("Global")
Debug "PBSH_Highlight.EnableKeywordBolding = "+ReadPreferenceString("EnableKeywordBolding", "1")+";"
Debug "PBSH_Highlight.TabLength = "+ReadPreferenceString("TabLength", "2")+";"
PreferenceGroup("CustomKeywords")
If ExaminePreferenceKeys()
While NextPreferenceKey()
If Left(PreferenceKeyName(), 1) = "W"
If CustomKeywords : CustomKeywords + "|" : EndIf
CustomKeywords + PreferenceKeyValue()
EndIf
Wend
EndIf
Debug "PBSH_Highlight.CustomKeywords = '"+CustomKeywords+"';"
PreferenceGroup("Editor")
Restore PreferenceKeys
Repeat
Read.s Keyword
If Keyword
String = ReplaceString(ReadPreferenceString(Keyword, ""), ",", " ")
If Right(Keyword, 5) = "Color" : String = HTMLColor(Val(String)) : EndIf
If ReadPreferenceInteger(Keyword+"_Disabled", 0)
Debug "//PBSH_Highlight."+Keyword+" = '"+String+"';"
Else
Debug "PBSH_Highlight."+Keyword+" = '"+String+"';"
EndIf
EndIf
Until Keyword = ""
ProcedureReturn #True
EndIf
EndProcedure
Define PreferencesFileName.s
If Not Export(GetUserDirectory(#PB_Directory_ProgramData)+"PureBasic\PureBasic.prefs")
PreferencesFileName = OpenFileRequester("Preference File", GetHomeDirectory(), "PureBasic.prefs", 0)
Export(PreferencesFileName)
EndIf
DataSection
PreferenceKeys:
Data.s "EditorFontName", "EditorFontSize", "EditorFontStyle", "BackgroundColor", "NormalTextColor"
Data.s "BasicKeywordColor", "CommentColor", "ConstantColor", "StringColor", "PureKeywordColor"
Data.s "ASMKeywordColor", "PointerColor", "NumberColor", "StructureColor", "LabelColor", "ModuleColor"
Data.s "OperatorColor", "SeparatorColor", "CustomKeywordColor", ""
EndDataSection
