📜 ⬆️ ⬇️

Sublime Text 3 - custom syntax highlighting

Extensibility Sublime Text knows no bounds. For those who have few standard functions, on Habré they already talked about how to create a snippet , how to write a simple plugin , how to write a complex plugin, and many more things. About the manual setting of syntax highlighting, I couldn’t really find anything: for someone it’s too obvious, for someone it’s just not needed, but for some, it’s probably useful.

So, the task: having some abstract logs of access to wonder-net:

!->14/02 16:44:22 [134.249.51.251:39951>80] (t1 19) >HTTP in:504 out:34 Time:156 GET /516874233**21893/ HTTP/1.1 SCOD=00 !->14/02 16:44:24 [134.249.51.251:49507>80] (t1 20) GET /44058858**409377/ HTTP/1.1 SCOD=00 !->14/02 16:54:11 [195.18.13.107:1721>80] (t2 22) GET /41494377**562173/ HTTP/1.1 SCOD=00 !->14/02 16:54:11 [195.18.13.107:1721>80] (t2 23) >HTTP in:385 out:10138 Time:156 GET /5211537**1172048/ HTTP/1.1 SCOD=00 !->14/02 16:54:24 [195.18.13.107:1727>80] (t1 30) >HTTP in:423 out:1220 Time:187 GET /5211537**6447554/ HTTP/1.1 SCOD=23 !->14/02 18:07:24 [82.145.208.159:43634>80] (t2 52) GET /4149437**8265377/ HTTP/1.1 SCOD=00 !->14/02 18:09:41 [82.145.208.174:41708>80] (t2 61) GET /4149497**5750155 / HTTP/1.1 SCOD=00 !->14/02 18:51:39 [82.145.210.33:55210>80] (t1 66) >HTTP in:543 out:34 Time:0 GET /5168757**9478487/ HTTP/1.1 SCOD=00 !->14/02 18:51:40 [82.145.210.33:55332>80] (t1 68) >HTTP in:544 out:1243 Time:141 GET /4149497**0456701 / HTTP/1.1 SCOD=00 !->14/02 18:51:46 [82.145.210.33:57345>80] (t1 73) >HTTP in:544 out:1243 Time:125 GET /5168742**0521893/ HTTP/1.1 SCOD=00 

highlight:
The date-time is purple italic;
Card number - in yellow italics, if not in the black list or red, if not;
ip-address - dark green, if not in the black list or red, if otherwise;
scod = xx - dark green if 00 or red if otherwise.

In our color scheme you need to add styles that we are going to highlight the text. As I understand it, all the color schemes are neatly collected in the file c: \ Program Files \ Sublime Text 3 \ Packages \ Color Scheme - Default.sublime-package.
')
The file is a zip archive without compression. Unpack, select your favorite scheme (Monokai.tmTheme by default) and copy it to ... (run Sublime Text: Preferences - Browse Packages menu) User folder.

Open the newly edited Monokai.tmTheme for editing. At the very bottom before closing we add blocks of styles. A style can contain three parameters: background, foreground and fontStyle. fontStyle in turn bold , italic and underline .

purple italics will look like this:
 <dict> <key>scope</key> <string>violet</string> <!--        --> <key>settings</key> <dict> <key>foreground</key> <string>#EE82EE</string> <key>fontStyle</key> <string>italic</string> </dict> </dict> 

fat yellow (LJ):
 <dict> <key>scope</key> <string>yellow</string> <key>settings</key> <dict> <key>foreground</key> <string>#FFD700</string> <key>fontStyle</key> <string>bold</string> </dict> </dict> 


dark green:
 <dict> <key>scope</key> <string>green</string> <key>settings</key> <dict> <key>foreground</key> <string>#006400</string> </dict> </dict> 


red:
 <dict> <key>scope</key> <string>red</string> <key>settings</key> <dict> <key>foreground</key> <string>#FF0000</string> </dict> </dict> 

2. In the same User folder, create a syntax file: wonderLog.tmLanguage with the contents:
 <?xml version="1.0" encoding="UTF-8" ?> <plist version="1.0"> <dict> <key>patterns</key> <array> <dict> <key>name</key> <string>red</string> <!--   --> <key>match</key> <string>\b(?i)(?:5168742**0521893|414943**01562173|4149497**5750155)\b</string> <!--   -   (   ) --> </dict> <dict> <key>name</key> <string>red</string> <key>match</key> <string>\b(?i)(?:134\.249\.51\.251|82\.145\.208\.174)\b</string> <!-- ip    ( ) --> </dict> <dict> <key>name</key> <string>green</string> <key>match</key> <string>\b(?i)scod=00\b</string> <!-- scod=00 --> </dict> <dict> <key>name</key> <string>red</string> <key>match</key> <string>\b(?i)scod=\d+\b</string> <!-- scod!=00 --> </dict> <dict> <key>name</key> <string>green</string> <key>match</key> <string>\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b</string> <!--  ip (,   , ...) --> </dict> <dict> <key>name</key> <string>yellow</string> <key>match</key> <string>\b\d{16}\b</string> <!--   --> </dict> <dict> <key>name</key> <string>violet</string> <key>match</key> <string>\d{1,2}/\d{1,2}\s\d{2}:\d{2}:\d{2}</string> <!-- ,  --> </dict> </array> <key>name</key> <string>wonderLog</string> <key>scopeName</key> <string>wonderLog</string> <key>fileTypes</key> <array> <string>log</string> <!--          --> </array> </dict> </plist> 

The example shows that if several regular expressions pretend to color the color differently, then the one described above (first) will have priority.

Save everything.

Select a custom color scheme: Preferences - Color Scheme - User - Monokai.

Open our miracle log (if the file extension does not match the template one, do View - Syntax - wonderLog) and enjoy the result:

Source: https://habr.com/ru/post/258053/


All Articles