-
Notifications
You must be signed in to change notification settings - Fork 5
/
mdAndRTFLinkToClipboard.js
266 lines (227 loc) · 7.24 KB
/
mdAndRTFLinkToClipboard.js
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
(() => {
"use strict";
// Rob Trew @2021, @2022
// Value of KM mdLink variable placed in clipboard,
// both as plain text,
// and also as a labelled (and styled) RTF hyperlink.
// Ver 0.2 added RTF link style options.
// Ver 0.3 updated link style font, size, color.
// Ver 0.4 added a pasteboard item for Bike Outliner
ObjC.import("AppKit");
// main :: IO ()
const main = () => {
const linkStyle = {
"color": "#ACACAC",
"font-family": "Helvetica Neue, sans-serif",
"font-size": "13px"
};
const
md = Application("Keyboard Maestro Engine")
.getvariable("mdLink"),
labelLinks = mdLinkPartPairs(md);
return (
copyTypedString(true)(
"public.utf8-plain-text"
)(md),
copyTypedString(false)(
"com.hogbaysoftware.bike.xml"
)(
bikeLinksXML(labelLinks)
),
either(
alert("Copy as RTF Link")
)(
copyTypedString(false)("public.rtf")
)(
rtfFromHTML(
styledLinks(linkStyle)(
labelLinks
)
)
),
md
);
};
// ---------------------- LINKS ----------------------
const bikeLinksXML = labelLinks => {
const
ps = labelLinks.map(
([k, url]) =>
`<li><p><a href="${url}">${k}</a></p></li>`
).join("\n");
return `<html><body><ul>${ps}</ul></body></html>`;
};
// copyTypedString :: Bool -> String -> String -> IO ()
const copyTypedString = blnClear =>
// public.html, public.rtf, public.utf8-plain-text
pbType => s => {
const pb = $.NSPasteboard.generalPasteboard;
return (
blnClear && pb.clearContents,
pb.setStringForType(
$(s),
$(pbType)
)
);
};
// cssTag :: Dict {String :: String} -> String
const cssTag = settings => {
const
kvs = Object.entries(settings)
.map(([k, v]) => `${k}: ${v};`)
.join(" "),
css = `p { ${kvs} }`;
return `<style type="text/css">${css}</style>`;
};
// htmlEncoded :: String -> String
const htmlEncoded = s => {
const rgx = /[\w\s]/u;
return [...s].map(
c => rgx.test(c) ? (
c
) : `&#${c.codePointAt(0)};`
).join("");
};
// mdLinkPartPairs :: String -> [(String, String)]
const mdLinkPartPairs = s =>
lines(s).map(x => {
const ab = x.trim().split("](");
return 2 !== ab.length ? (
Tuple(s)("")
) : Tuple(ab[0].slice(1))(
ab[1].slice(0, -1)
);
});
// rtfFromHTML :: String -> Either String String
const rtfFromHTML = strHTML => {
const
as = $.NSAttributedString.alloc
.initWithHTMLDocumentAttributes($(strHTML)
.dataUsingEncoding($.NSUTF8StringEncoding),
0
);
return bindLR(
"function" !== typeof as
.dataFromRangeDocumentAttributesError ? (
Left("String could not be parsed as HTML")
) : Right(as)
)(
// Function bound if Right value obtained above:
htmlAS => {
const
error = $(),
rtfData = htmlAS
.dataFromRangeDocumentAttributesError({
"location": 0,
"length": htmlAS.length
}, {
DocumentType: "NSRTF"
},
error
);
return Boolean(
ObjC.unwrap(rtfData) && !error.code
) ? Right(
ObjC.unwrap($.NSString.alloc
.initWithDataEncoding(
rtfData,
$.NSUTF8StringEncoding
))
) : Left(ObjC.unwrap(
error.localizedDescription
));
}
);
};
// styledLinks :: Dict -> [(String, String)] -> String
const styledLinks = styleDict =>
// One or more <a href> lines, wrapped in <p>...</p>
// and preceded by a <style> tag based on styleDict.
kvs => {
const
css = cssTag(styleDict),
linkTags = kvs.map(kv => {
const [label, url] = biList(kv).map(
htmlEncoded
),
labelOrFullLink = Boolean(url) ? (
`<a href="${url}">${label}</a>`
) : label;
return `${labelOrFullLink}`;
})
.join("<br>");
return `${css}\n<p>${linkTags}</p>`;
};
// ----------------------- JXA -----------------------
// alert :: String => String -> IO String
const alert = title =>
s => {
const sa = Object.assign(
Application("System Events"), {
includeStandardAdditions: true
});
return (
sa.activate(),
sa.displayDialog(s, {
withTitle: title,
buttons: ["OK"],
defaultButton: "OK"
}),
s
);
};
// --------------------- GENERIC ---------------------
// Left :: a -> Either a b
const Left = x => ({
type: "Either",
Left: x
});
// Right :: b -> Either a b
const Right = x => ({
type: "Either",
Right: x
});
// Tuple (,) :: a -> b -> (a, b)
const Tuple = a =>
// A pair of values, possibly of
// different types.
b => ({
type: "Tuple",
"0": a,
"1": b,
length: 2,
*[Symbol.iterator]() {
for (const k in this) {
if (!isNaN(k)) {
yield this[k];
}
}
}
});
// biList :: (a, a) -> [a]
const biList = ab =>
// A list of two items derived from a tuple.
Array.from(ab);
// bindLR (>>=) :: Either a ->
// (a -> Either b) -> Either b
const bindLR = m =>
mf => m.Left ? (
m
) : mf(m.Right);
// either :: (a -> c) -> (b -> c) -> Either a b -> c
const either = fl =>
// Application of the function fl to the
// contents of any Left value in e, or
// the application of fr to its Right value.
fr => e => "Left" in e ? (
fl(e.Left)
) : fr(e.Right);
// lines :: String -> [String]
const lines = s =>
// A list of strings derived from a single
// string delimited by newline and or CR.
0 < s.length ? (
s.split(/[\r\n]+/u)
) : [];
return main();
})();