class MarkdownBlocks
package texter.general.markdown
The MarkdownBlocks
class is a collection of static methods that can be used to get
syntax highlighting for Markdown code-blocks.
You might notice this class is a bit different from the other Markdown
classes, as its
methods are fully editable. This is because i cant just make syntax highlighters for every single language,
but you can help with some that i dont knw and you are :)
How to implement a syntax parser.
For this example, Ill use Haxe:
Lets say you wanted to give haxe syntax highlighting, but you discover its not supported and the text remains in a single color. how do you fix that?
first, make sure the language exists on the class. you can look at the file/try autocompletion for that
second, you make a syntax parser of your own. i recommand working with this template:
using texter.general.TextTools;
function parseLanguage(text) {
var interp:Array<{color:Int, start:Int, end:Int}> = [];
var indexOfKeyColor1 = text.indexesFromEReg(~/(?: |\n|^)(keywords|that|need|to|be|colored|blue)/m),
indexOfKeyColor2 = text.indexesFromArray([ "You", "can", "also", "add", "words", "with", "an", "array"]),
indexOfFunctionName = text.indexesFromEReg(~/([a-zA-Z_]+)\(/m), //detects function syntax, camal/snake/Title case
indexOfClassName = text.indexesFromEReg(~/(?:\(| |\n|^)[A-Z]+[a-z]+/m), // detects Title Case
indexOfString = text.indexesFromEReg(~/"[^"]*"|'[^']*'/), // detects "" and ''
indexOfNumbers = text.indexesFromArray(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]),
indexOfComment = text.indexesFromEReg(~/\/\/[^\n]*|\/\*[^]*?\*\//m); // detects //
//add the indexes to the interp array
for (i in indexOfFunctionName) interp.push({color: customColor1, start: i.startIndex, end: i.endIndex - 1}); //dont coint the last (
for (i in indexOfKeyColor1) interp.push({color: customColor2, start: i.startIndex + 1, end: i.endIndex}); //dont count the first char, its not related.
for (i in indexOfClassName) interp.push({color: customColor3, start: i.startIndex, end: i.endIndex});
for (i in indexOfKeyColor2) interp.push({color: customColor4, start: i.startIndex + 1, end: i.endIndex}); //dont count the first char, its not related.
for (i in indexOfString) interp.push({color: customColor5, start: i.startIndex, end: i.endIndex});
for (i in indexOfNumbers) interp.push({color: customColor6, start: i.startIndex, end: i.endIndex});
for (i in indexOfComments) interp.push({color: customColor7, start: i.startIndex, end: i.endIndex});
for (i in indexOfConditionals) interp.push({color: customColor8, start: i.startIndex, end: i.endIndex});
return interp;
}
What do i do here:
- Im getting all of the special syntax - functions, classes, keywords, strings, numbers, comments, etc.
- I differentiate between the different types of syntax, so i can color them differently.
- I add the indexes to the
interp
array, each one with a unique start & end index. - I return the
interp
array.
After that, i just assign this function to the correct language, in our case its haxe:
MarkdownBlocks.parseHaxe = parseLanguage;