| 1 |
%% This is part of the OpTeX project, see http://petr.olsak.net/optex |
| 2 |
|
| 3 |
\_codedecl \_hisyntaxpython {Syntax highlighting for Python sources <2023-10-25>} |
| 4 |
|
| 5 |
\_newtoks \_hisyntaxpython \_newtoks \_hicolorspython |
| 6 |
|
| 7 |
\_global\_hicolorspython={% colors for Python language |
| 8 |
\_hicolor M \Orange % Multi line strings |
| 9 |
\_hicolor S {\_visiblesp \Grey} % Single line strings |
| 10 |
\_hicolor C \Green % Comments |
| 11 |
\_hicolor K \LightBlue % Keywords |
| 12 |
\_hicolor O \Blue % Operators |
| 13 |
\_hicolor N \Red % Numbers |
| 14 |
\_hicolor D \Magenta % def names |
| 15 |
\_hicolor L \Black % class names |
| 16 |
\_hicolor R \Magenta % Decorators |
| 17 |
} |
| 18 |
\_global\_hisyntaxpython={% |
| 19 |
\_the\_hicolorspython |
| 20 |
\_let\s=\_relax \_let\c=\_relax \_let\e=\_relax \_let\o=\_relax |
| 21 |
\_replthis{\_string\"}{{\_string\"}} % protect \", \' |
| 22 |
\_replthis{\_string\'}{{\_string\'}} |
| 23 |
% |
| 24 |
\_replfromto{\_string#}{^^J}{\z C{\##1}^^J} % comments |
| 25 |
% |
| 26 |
\_replthis{'''}{\_noexpand\_hipystr0{'''}M} % search string marks |
| 27 |
\_replthis{"""}{\_noexpand\_hipystr1{"""}M} |
| 28 |
\_replthis{"}{\_noexpand\_hipystr2{"}S} |
| 29 |
\_replthis{'}{\_noexpand\_hipystr3{'}S} |
| 30 |
\_edef\_tmpb{\_tmpb} % realize string marks |
| 31 |
% |
| 32 |
\_foreach {br}{Br}{bR}{BR}{rb}{rB}{Rb}{RB}uUrRbB % string prefixes |
| 33 |
\_do {\_replthis{#1\s}{\_noexpand\_hipystrpre#1}} |
| 34 |
\_def\s{} |
| 35 |
\_edef\_tmpb{\_tmpb} |
| 36 |
% |
| 37 |
\_edef \_tmpa {+-*./=<>()[]:,;!|\_pcent % operators |
| 38 |
\_string{\_string}@\_string&\_string~\_string^} |
| 39 |
\_ea\_foreach \_tmpa |
| 40 |
\_do {\_replthis{#1}{\n\o#1\n}} |
| 41 |
% |
| 42 |
\_foreach % keywords |
| 43 |
{and}{as}{assert}{async}{await}{break}{continue}{del}{elif}{else}{except}% |
| 44 |
{exec}{False}{finally}{for}{from}{global}{if}{import}{in}{is}{lambda}{None}% |
| 45 |
{nonlocal}{not}{or}{pass}{print}{raise}{return}{True}{try}{while}{with}{yield}% |
| 46 |
\_do {\_replthis{\n#1\n}{\z K{#1}}} |
| 47 |
\_replfromto{\n def\n}{\n\o(\n}{\z K{def}\x D{#1}\n\o(\n} |
| 48 |
\_replfromto{\n class\n}{\n\o(\n}{\z K{class}\x L{#1}\n\o(\n} |
| 49 |
% |
| 50 |
\_foreach 0123456789 % numbers |
| 51 |
\_do {\_replfromto{\n#1}{\n}{\c#1##1\e}} |
| 52 |
% |
| 53 |
\_replthis{\e\o.\c}{.} |
| 54 |
\_replthis{\e\o.\n}{.\e} |
| 55 |
\_replthis{\n\o.\c}{\c.} |
| 56 |
\_replthis{e\e\o+\c}{e+}\_replthis{e\e\o-\c}{e-} |
| 57 |
\_replthis{E\e\o+\c}{E+}\_replthis{E\e\o-\c}{E-} |
| 58 |
\_replthis{\e j}{j\e} |
| 59 |
% |
| 60 |
\_replfromto{\b\n\n\o@\n}{^^J}{\z K{@}\z R{#1}^^J} % decorators |
| 61 |
% |
| 62 |
\_def\o#1{\z O{#1}} |
| 63 |
\_def\c#1\e{\z N{#1}} |
| 64 |
} |
| 65 |
|
| 66 |
\_gdef\_hipystr#1#2#3#4\_hipystr#5#6#7{% |
| 67 |
\_ifx#1#5\s\x#3{#2#4#2}% |
| 68 |
\_else \_afterfi{\_hipystr#1{#2}#3#4#6}\_fi |
| 69 |
} |
| 70 |
\_gdef\_hipystrpre#1\x#2{\x#2{#1}\x#2} |
| 71 |
|
| 72 |
\_ifx\LightBlue\_undefined \_gdef\LightBlue {\_setcmykcolor{1 .43 0 0}}\_fi |
| 73 |
\_ifx\Orange\_undefined \_gdef\Orange {\_setcmykcolor{0 .64 1 0}}\_fi |
| 74 |
|
| 75 |
\_endcode %------------------------------------------------ |
| 76 |
|
| 77 |
This code was greatly inspired by the OPMac hisyntax macros for Python: |
| 78 |
|
| 79 |
https://gitlab.fit.cvut.cz/krajnpet/PySyntax |
| 80 |
|
| 81 |
created by Petr Krajnik. |
| 82 |
|
| 83 |
2023-10-25 decorators: hi-syntax corrected |
| 84 |
2020-04-04 released |