-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathid-simple-syntax-highlighter.jsx
executable file
·108 lines (93 loc) · 3.43 KB
/
id-simple-syntax-highlighter.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
* Simple syntax highlighter for InDesign
*
* This script offers primitive syntax highlighting capabilities for Adobe InDesign. It has no language-specific syntax support but rather matches on
* generic tokens to provide an adequate but not perfect highlighting for most languages.
*
* The highlighting is done by a modified version of lolight by Lars Jung (https://larsjung.de/lolight/).
*/
#include "includes.jsx"
var styles = [
{
name: "nam",
fillColor: [33, 150, 243]
},
{
name: "num",
fillColor: [236, 64, 122]
},
{
name: "str",
fillColor: [67, 160, 71]
},
{
name: "rex",
fillColor: [239, 108, 0]
},
{
name: "pct",
fillColor: [102, 102, 102]
},
{
name: "key",
fillColor: [85, 85, 85],
fontStyle: "Bold"
},
{
name: "com",
fillColor: [170, 170, 170],
fontStyle: "Italic"
},
{
name: "spc"
},
{
name: "unk"
}
];
// The number of paragraphs is not static, so we need to manually count them instead of just saying `num_frames * num_paragraphs_per_frame`.
var steps = 0;
for(var i = 0; i < app.activeDocument.textFrames.length; i++) {
steps += app.activeDocument.textFrames[i].paragraphs.length;
}
var window = new Window("palette");
var progress_bar = progress_bar(window, steps, "Syntax highlighting. Please wait…");
window.show();
// Try to create a new `syntax` group for our character styles or if it already exists, use that.
var character_style_group;
try { character_style_group = document.characterStyleGroups.add({ name: 'syntax' }); }
catch(e) { character_style_group = document.characterStyleGroups.itemByName('syntax'); }
for(var i = 0; i < styles.length; i++) {
var style = styles[i];
if(style.fillColor) {
var color_name = style.fillColor.join(',');
try {document.colors.add({ name: color_name, model: ColorModel.PROCESS, space: ColorSpace.RGB, colorValue: style.fillColor });} catch(e){}
style.fillColor = color_name;
}
try {
var c_style = document.characterStyles.add(style);
try { c_style.move(LocationOptions.AT_END, character_style_group) }
// This means that the character style we are trying to create already exists in the `syntax` group, so we need to delete our newly-created
// root-level character style.
catch(e) { c_style.remove(); };
} catch(e) {}
}
for(var i = 0; i < app.activeDocument.textFrames.length; i++) {
var text_frames = app.activeDocument.textFrames[i];
for(var j = 0; j < text_frames.paragraphs.length; j++) {
var paragraph = text_frames.paragraphs[j];
if(paragraph.appliedParagraphStyle.name.startsWith('syntax')) {
var tokens = lolight.tok(paragraph.contents);
var start_index = 0;
for(var k = 0; k < tokens.length; k++) {
var token_type = tokens[k][0];
var token_text = tokens[k][1];
paragraph.characters.itemByRange(start_index, start_index + token_text.length - 1)
.appliedCharacterStyle = character_style_group.characterStyles.itemByName(token_type);
start_index = start_index + token_text.length;
}
}
progress_bar.value++;
}
};
window.close();